/* Produit d'une matrice par un vecteur */ public class ProduitMatriceVecteur { /* Methode d'affichage des valeurs contenues */ /* dans un tableau de double a 1 indice */ static void affichageVecteur(double [] t) { int i; for ( i = 0 ; i < t.length ; i = i+1 ) { Ecran.afficherln(t[i]); } } /* Methode d'affichage des valeurs contenues */ /* dans un tableau de double a 2 indices */ static void affichageMatrice(double [][] t) { int i; int j; for ( i = 0 ; i < t.length ; i = i+1 ) { for ( j = 0 ; j < t[i].length ; j = j+1 ) { Ecran.afficher(t[i][j]," "); } Ecran.sautDeLigne(); } } ///////////////////////////////////////////////// /* Methode de calcul du produit d'un tableau */ /* carre de taille n.n par un tableau */ /* a 1 indice de taille n */ /* Resultat: Un tableau a 1 indice de taille n */ static double [] produitMatriceVecteur(double [][] m, double [] v) { int i; int j; int n; n = v.length; double [] w = new double[n]; for ( i = 0 ; i < n ; i = i+1 ) { w[i] = 0.0; for ( j = 0 ; j < n ; j = j+1 ) { 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); } }