[c++] algorithme produit matrice vecteur
Résolu
Bonjour,
j'ai besoin du principe de l'algorithme qui permet de faire le produit matriciel entre une latrice et vecteur.
j'ai besoin du principe de l'algorithme qui permet de faire le produit matriciel entre une latrice et vecteur.
Configuration: Windows XP Internet Explorer 6.0
3 réponses
-
En C++ ca ressemble à ça :
#include <vector> #include <ostream> #include <iostream> // Définition d'une matrice template <typename T> class matrix_t{ protected: std::size_t nb_ligne; std::size_t nb_colonne; std::vector<std::vector<T> > data; public: matrix_t( const std::size_t & nb_ligne0 = 1, const std::size_t & nb_colonne0 = 1 ): nb_ligne(nb_ligne0), nb_colonne(nb_colonne0) { data.resize(nb_ligne); for(std::size_t i=0;i<nb_ligne;++i){ data[i].resize(nb_colonne); } } inline void set( const std::size_t & i, const std::size_t & j, const T & x ){ if(i >= get_nb_ligne()) throw; if(j >= get_nb_colonne()) throw; data[i][j] = x; } inline std::vector<T> & operator[]( const std::size_t & i ){ return data[i]; } inline const std::vector<T> & operator[]( const std::size_t & i ) const { return data[i]; } inline std::size_t get_nb_ligne() const{ return nb_ligne; } inline std::size_t get_nb_colonne() const{ return nb_colonne; } }; // Opérateur d'affichage d'une matrice template <typename T> std::ostream & operator<<(std::ostream & out,const matrix_t<T> & m){ const std::size_t & nb_lig = m.get_nb_ligne(), nb_col = m.get_nb_colonne(); for(std::size_t i=0;i<nb_lig;++i){ for(std::size_t j=0;j<nb_col;++j){ out << m[i][j] << '\t'; } out << std::endl; } return out; } // L'addition des matrices template <typename T1,typename T2> matrix_t<T1> operator+( const matrix_t<T1> & m1, const matrix_t<T2> & m2 ){ // vérifier que les deux matrices peuvent être sommées const std::size_t & nb_lig1 = m1.get_nb_ligne(), nb_col1 = m1.get_nb_colonne(), nb_lig2 = m2.get_nb_ligne(), nb_col2 = m2.get_nb_colonne(); if(nb_lig1 != nb_lig2 || nb_col1 != nb_col2) throw; matrix_t<T1> m(nb_lig1,nb_col1); // Aij = Bij + Cij for(std::size_t i=0;i<nb_lig1;++i){ for(std::size_t j=0;j<nb_col1;++j){ m[i][j] = m1[i][j] + m2[i][j]; } } return m; } // Le produit des matrices template <typename T1,typename T2> matrix_t<T1> operator*( const matrix_t<T1> & m1, const matrix_t<T2> & m2 ){ // vérifier que les deux matrices peuvent être multipliées const std::size_t & nb_lig1 = m1.get_nb_ligne(), nb_col1 = m1.get_nb_colonne(), nb_lig2 = m2.get_nb_ligne(), nb_col2 = m2.get_nb_colonne(); if(nb_col1 != nb_lig2) throw; matrix_t<T1> m(nb_lig1,nb_col2); // Aik = sum_j(Bij * Cjk) for(std::size_t i=0;i<nb_lig1;++i){ for(std::size_t k=0;k<nb_col2;++k){ for(std::size_t j=0;j<nb_col1;++j){ m[i][k] += m1[i][j] * m2[j][k]; } } } return m; } // Le programme principal int main(){ // en italique les zones que tu peux modifier pour générer d'autres exemples const std::size_t nb_lig = 3, nb_col = 5; matrix_t<int> m(nb_lig,nb_col),m1(nb_lig,nb_col),m2(nb_col,nb_lig); // initialiser les trois matrices for(std::size_t i=0;i<nb_lig;++i){ for(std::size_t j=0;j<nb_col;++j){ m[i][j] = 10*(i+1) + (j+1); m1[i][j] = 1000*(i+1) + 100*(j+1); m2[j][i] = i + j; } } // afficher les matrices et le calcul... std::cout << "m = " << std::endl << m << std::endl << "m1 = " << std::endl << m1 << std::endl << "m2 = " << std::endl << m2 << std::endl; std::cout << "m + m2 = " << std::endl << (m + m1) << std::endl; std::cout << "m * m2 = " << std::endl << (m * m2) << std::endl; // inialiser un vecteur ligne et un veteur colonne matrix_t<int> row(1,nb_lig),col(nb_col,1); for(std::size_t i=0;i<nb_lig;++i) row[0][i] = 1; for(std::size_t i=0;i<nb_col;++i) col[i][0] = 1; std::cout << "row = " << std::endl << row << std::endl << "col = " << std::endl << col << std::endl << "row * m = " << std::endl << (row * m) << std::endl << "m * col = " << std::endl << (m * col) << std::endl; return 0; }
Ce qui donne :(mando@aldur) (~) $ g++ -W -Wall plop.cpp && ./a.out m = 11 12 13 14 15 21 22 23 24 25 31 32 33 34 35 m1 = 1100 1200 1300 1400 1500 2100 2200 2300 2400 2500 3100 3200 3300 3400 3500 m2 = 0 1 2 1 2 3 2 3 4 3 4 5 4 5 6 m + m2 = 1111 1212 1313 1414 1515 2121 2222 2323 2424 2525 3131 3232 3333 3434 3535 m * m2 = 140 205 270 240 355 470 340 505 670 row = 1 1 1 col = 1 1 1 1 1 row * m = 63 66 69 72 75 m * col = 65 115 165
Bonne chance