/* Produit de convolution d'une matrice */ /* de double par une matrice de double */ public class ProduitDeConvolution { /* Methode de creation d'une matrice de double */ /* initialise avec des nombres aleatoires */ /* compris dans l'intervalle [0.0,max] */ /* et quatre chiffres apres la virgule */ static double [][] initRandMatrice(int lignes, int colonnes, double max) { int i; int j; double [][] tab = new double[lignes][colonnes]; for ( i = 0 ; i < lignes ; i = i+1 ) { for ( j = 0 ; j < colonnes ; j = j+1 ) { tab[i][j] = ((int) (Math.random()*max*10000.0))/10000.0; } } return tab; } /* Methode d'affichage des valeurs contenues */ /* dans une matrice de double */ /* Affichage avec 3 chiffres apres la virgule */ static void affichageMatrice(double [][] t) { int i; int j; for ( i = 0 ; i < t.length ; i = i+1 ) { for ( j = 0 ; j < t[0].length ; j = j+1 ) { Ecran.afficher(String.format("%7.4f",t[i][j])," "); } Ecran.sautDeLigne(); } } ////////////////////////////////////////////////// /* Methode de calcul du produit de convolution */ /* d'une matrice de double par une matrice */ /* de convolution */ /* Stockage du resultat dans la 3eme matrice */ /* passee en entete supposee de taille */ /* compatible avec les deux premieres */ static void produitDeConvolution(double [][] m, double [][] mc, double [][] mr) { int i; int j; int x; int y; int l = mr.length; int c = mr[0].length; int xf; int yf; double v; for ( i = 0 ; i < l ; i = i+1 ) { for ( j = 0 ; j < c ; j = j+1 ) { mr[i][j] = 0.0; yf = i+mc.length; xf = j+mc[0].length; for ( y = i ; y < yf ; y = y+1 ) { for ( x = j ; x < xf ; x = x+1 ) { mr[i][j] = mr[i][j] + mc[y-i][x-j]*m[y][x]; } } } } } /* Methode de calcul du produit de convolution */ /* d'une matrice de double par une matrice */ /* de convolution */ /* Stockage du resultat dans une matrice */ /* allouee et retournee en resultat de fonction */ static double [][] produitDeConvolution(double [][] m, double [][] mc) { int l = m.length-mc.length+1; int c = m[0].length-mc[0].length+1; double [][] mr = new double[l][c]; produitDeConvolution(m,mc,mr); return mr; } ////////////////////////////////////////////////// /* Programme principal */ public static void main(String [] args) { int l; int c; Ecran.afficher("Nombre de lignes ? "); l = Clavier.saisirInt(); Ecran.afficher("Nombre de colonnes ? "); c = Clavier.saisirInt(); double [][] m = initRandMatrice(l,c,90.0); Ecran.sautDeLigne(); Ecran.afficherln("Matrice a convoluer [", m.length,",",m[0].length,"]"); affichageMatrice(m); double fact = 12.0; double [][] mc = { { 1.0/fact,1.0/fact,1.0/fact }, { 1.0/fact,4.0/fact,1.0/fact }, { 1.0/fact,1.0/fact,1.0/fact } }; Ecran.sautDeLigne(); Ecran.afficherln("Matrice de convolution [", mc.length,",",mc[0].length,"]"); affichageMatrice(mc); double [][] res = produitDeConvolution(m,mc); Ecran.sautDeLigne(); Ecran.afficherln("Matrice apres convolution [", res.length,",",res[0].length,"]"); affichageMatrice(res); } }