/* Produit d'une matrice par une matrice */ public class ProduitMatriceMatrice { /* 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 de double de taille n.m */ /* par une matrice de double de taille m.p */ /* Le retour est une matrice de double */ /* de taille n.p */ /* n, m et p quelconques */ /* m1 : La première matrice de double */ /* m2 : La seconde matrice de double */ static double [][] produitMatriceMatrice(double [][] m1, double [][] m2) { int n = m1.length; int m = m2.length; int p = m2[0].length; double [][] r = new double[n][p]; for ( int i = 0 ; i < n ; i++ ) { for ( int j = 0 ; j < p ; j++ ) { r[i][j] = 0.0; for ( int k = 0 ; k < m ; k++ ) { 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.5,-1.0, 2.0,-3.0 }, { 0.0, 0.4, 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); } }