/* Produit d'une matrice par un vecteur */ public class ProduitMatriceVecteur { /* Fonction d'affichage des valeurs contenues */ /* dans un tableau de double a 1 indice */ static void affichageVecteur(double [] t) { for ( int i = 0 ; i < t.length ; i++ ) { Ecran.formater("%8.3f\n",t[i]); } } /* Fonction d'affichage des valeurs contenues */ /* dans un tableau de double a 2 indices */ static void affichageMatrice(double [][] t) { for ( int i = 0 ; i < t.length ; i++ ) { for ( int j = 0 ; j < t[i].length ; j++ ) { Ecran.formater("%8.3f",t[i][j]); } Ecran.sautDeLigne(); } } ////////////////////////////////////////////////// /* Fonction de calcul et retour du produit */ /* d'une matrice carree de reel de taille n.n */ /* par un vecteur (tableau) de reel de taille n */ /* n quelconque */ /* Le retour est un tableau de reel de taille n */ /* m : La matrice de reel */ /* v : Le vecteur de reel */ static double [] produitMatriceVecteur(double [][] m, double [] v) { int n = v.length; double [] w = new double[n]; for ( int i = 0 ; i < n ; i++ ) { w[i] = 0.0; for ( int j = 0 ; j < n ; j++ ) { w[i] = w[i] + m[i][j]*v[j]; } } return w; } ////////////////////////////////////////////////// /* Programme principal */ public static void main(String [] args) { double [][] m = { { 2.0, 3.0, 5.0, 1.0 }, { -1.0, 4.0, 3.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0 }, { 1.0, 2.0, 0.0, 0.0 } }; double [] v = { 0.0, 4.0, -2.0, 0.5 }; double [] w; Ecran.afficherln("Matrice M"); affichageMatrice(m); Ecran.sautDeLigne(); Ecran.afficherln("Vecteur V"); affichageVecteur(v); Ecran.sautDeLigne(); w = produitMatriceVecteur(m,v); Ecran.afficherln("Produit de M par V"); affichageVecteur(w); } }