Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class Methodes{
- static long TempsS, TempsC;
- /////////////////// Methode Addition ///////////////////////////////
- public static int[][] Add(int[][]A,int [][]B){
- int[][]C = new int[A.length][B.length];
- for(int i = 0; i <A.length;i++){
- for(int j = 0; j< B.length;j++){
- C[i][j] = A[i][j] + B[i][j];
- }
- }
- return C;
- }
- /////////////////// Methode Soustraction ////////////////////////
- public static int[][] Sub(int[][]A,int [][]B){
- int[][]C = new int[A.length][B.length];
- for(int i = 0; i <A.length;i++){
- for(int j = 0; j < B.length;j++){
- C[i][j] = A[i][j] - B[i][j];
- }
- }
- return C;
- }
- /////////////////// Methode Muliplication Classic //////////////////////
- public static int[][] Mul(int[][]A,int [][]B){
- int[][]C = new int[A.length][A.length];
- for(int i = 0; i < C.length; i++){
- for(int j = 0; j <C.length;j++){
- for(int k = 0; k< C.length; k++){
- C[i][j] += A[i][k] * B[k][j];
- }
- }
- }
- return C;
- }
- //////////////////// Methode Muliplication Strassen ////////////
- public static int[][] StrassenMul(int[][]A, int[][]B){
- int N = A.length/2;
- if(A.length == 1)
- {
- int[][] C = new int[1][1];
- C[0][0] = A[0][0] * B[0][0];
- return C;
- }
- int[][]A11 = new int[N][N];
- int[][]A12 = new int[N][N];
- int[][]A21 = new int[N][N];
- int[][]A22 = new int[N][N];
- int[][]B11 = new int[N][N];
- int[][]B12 = new int[N][N];
- int[][]B21 = new int[N][N];
- int[][]B22 = new int[N][N];
- for(int i = 0; i < N; i++){
- for(int j = 0; j< N; j++){
- A11[i][j] = A[i][j];
- B11[i][j] = B[i][j];
- A12[i][j] = A[i][N+j];
- B12[i][j] = B[i][N+j];
- A21[i][j] = A[N+i][j];
- B21[i][j] = B[N+i][j];
- A22[i][j] = A[N+i][N+j];
- B22[i][j] = B[N+i][N+j];
- }
- }
- int[][]M1 = Mul((Add(A11,A22)),(Add(B11,B22)));
- int[][]M2 = Mul((Add(A21,A22)),(B11));
- int[][]M3 = Mul((A11),(Sub(B12,B22)));
- int[][]M4 = Mul((A22),(Sub(B21,B11)));
- int[][]M5 = Mul((Add(A11,A12)),(B22));
- int[][]M6 = Mul((Sub(A21,A11)),(Add(B11,B12)));
- int[][]M7 = Mul((Sub(A12,A22)),(Add(B21,B22)));
- int[][]C11 = Add((Add(M1,Sub(M4,M5))),M7);
- int[][]C12 = Add(M3,M5);
- int[][]C21 = Add(M2,M4);
- int[][]C22 = Add((Add(Sub(M1,M2),M3)),M6);
- int[][]C = new int[A.length][A.length];
- for(int i = 0; i<N; i++){
- for(int j= 0; j<N;j++){
- C[i][j] = C11[i][j];
- C[i][N+j] = C12[i][j];
- C[N+i][j] = C21[i][j];
- C[N+i][N+j] = C22[i][j];
- }
- }
- return C;
- }
- //////////////////// Methode Affichage //////////////////////////
- public static void Affi(int[][]array){
- for(int i = 0; i < array.length; i++){
- for(int j= 0; j < array.length; j++){
- System.out.print(array[i][j] + " ");
- }
- System.out.println();
- }}
- ///////////// MAIN ///////////////
- public static void main(String[]args){
- System.out.println("Entrez la taille de matrices");
- Scanner sc =new Scanner(System.in);
- int n=sc.nextInt();
- int [][]mat1 = new int [n][n];
- int [][]mat2 = new int [n][n];
- final Random random=new Random();
- for(int i = 0; i < n; i++){
- for(int j = 0; j< n; j++){
- mat1[i][j] = random.nextInt(5);
- mat2[i][j] = random.nextInt(5);
- }
- }
- System.out.println("Entez le nombre de repetitions");
- int k=sc.nextInt();
- int l=k;
- while (k>0){
- System.out.println("Itteration Strassen "+k);
- final long A1 = System.currentTimeMillis();
- StrassenMul(mat1,mat2);
- final long A2 = System.currentTimeMillis();
- k=k-1;
- TempsS=A2-A1;
- }
- System.out.println("Strassen: " + (TempsS/l) + "ms");
- k=l;
- while (k>0){
- System.out.println("Itteration Classique "+k);
- final long A3 = System.currentTimeMillis();
- Mul(mat1,mat2);
- final long A4 = System.currentTimeMillis();
- k=k-1;
- TempsC=A4-A3;
- }
- System.out.println("Classique: " + (TempsC/l) + "ms");
- }
- ////////////////////--------------------------------/////////////////
Advertisement
Add Comment
Please, Sign In to add comment