|
|
|
|
Posté par
guymenime, le jeudi 14 février 2008 à 22:26:53Configuration: Windows XP Internet Explorer 6.0
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
|
bjr.
je cherche une fonction qui fait la multiplication d'une matrice avec un vecteur . merci de me rependre. bonne continuation. |
Un vecteur c'est un juste un cas particulier de matrice... D'ailleurs dans l'exemple que j'ai donné col est un vecteur donné en colonne et row un vecteur en ligne.
Bonne chance |
| 20/06 19h58 | La vectorialisation sous Matlab | MatLab |
| 03/03 09h01 | [WiFi] Cours d'introduction | WiFi |
| 25/08 17h31 | [Infographie] Optimiser la taille des images PNG. | Infographie |
| 24/05 06h49 | L'indexation linéaire et le reshape sous Matlab | MatLab |
| 27/05 09h47 | Les templates | Langage C++ |
| 25/08 11h22 | Produit matrice vecteur | 4 |
| 09/11 12h21 | Produit matriciel en c | 10 |
| 10/11 00h16 | Produit matriciel en C++ | 6 |
| 24/07 11h59 | Produit matriciel en vba | 1 |
![]() | Inkscape - Inkscape est un logiciel libre d'édition de graphismes vectoriels, doté de capacités similaires à Illustrator, Freehand,... | Catégorie: Dessin Licence: Open Source |
![]() | Sodipodi - Sodipodi est une application de dessin vectoriel pour Linux/Unix et Windows utilisant le format W3C SVG comme format natif.... | Catégorie: Dessin Licence: Open Source |
![]() | Shareaza - Shareaza est un client d'échange de fichiers P2P pour Windows qui vous permet de télécharger tout type de fichier que vous... | Catégorie: Téléchargement Licence: Open Source |
![]() | Everest - Everest (ex Aida32) est un outil de diagnostif permettant de dresser un inventaire très précis des composants matériels et... | Catégorie: Diagnostic Licence: Freeware/gratuit |
![]() | Daemon Vector | Catégorie: Jeu vidéo PC | |
![]() | Minolta Vectis S1 | Catégorie: Appareil Photo Compact | |
![]() | PLATOON : Jeu complet | Catégorie: Jeu vidéo PC | |
![]() | Magnat Vector Needle 25A | Catégorie: Enceintes |