/* Produit d'une matrice par une matrice */ public class ProduitMatriceMatrice { /* 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 */ /* de dimension 2 de taille n.m */ /* par un tableau de dimension 2 de taille m.p */ /* Resultat: Un tableau de dimension 2 */ /* de taille n.p */ static double [][] produitMatriceMatrice(double [][] m1, double [][] m2) { int i; int j; int k; int n; int m; int p; n = m1.length; m = m2.length; p = m2[0].length; double [][] r = new double[n][p]; for ( i = 0 ; i < n ; i = i+1 ) { for ( j = 0 ; j < p ; j = j+1 ) { r[i][j] = 0.0; for ( k = 0 ; k < m ; k = k+1 ) { r[i][j] = r[i][j] + m1[i][k]*m2[k][j]; } } } return r; } ///////////////////////////////////////////////// /* Programme principal */ public static void main(String [] args) { double [][] m1 = { { 1.0,-1.0, 2.0,-3.0 }, { 0.0, 0.0, 1.0, 2.0 } }; double [][] m2 = { { 5.0, 0.0, 1.0 }, { 4.0, 0.0, 0.0 }, { 3.0, 1.0, 2.0 }, { -2.0, 3.0,-1.0 } }; double [][] m; Ecran.afficherln("Matrice M1"); affichageMatrice(m1); Ecran.sautDeLigne(); Ecran.afficherln("Matrice M2"); affichageMatrice(m2); Ecran.sautDeLigne(); m = produitMatriceMatrice(m1,m2); Ecran.afficherln("Produit de M1 par M2"); affichageMatrice(m); } }