Java implementation of arbitrary matrix Strassen algorithm _java

Source: Internet
Author: User

This example is entered as a matrix M * N, N * m of two arbitrary dimensions, and the output is the product of two matrices. The Strassen algorithm is used to compute the multiplication of arbitrary dimension matrices. Program for the self, after testing, please rest assured that use. The basic algorithm is:
1. For matrices (square matrices), find the largest l, making L = 2 ^ k, k for integers and L < M. The square matrix with the edges L uses the Strassen algorithm, the remainder and the missing part of the square matrix by brute force method.
2. For the non phalanx, add 0 according to the row and column to make it a phalanx.
Strassenmethodtest.java

Package matrixalgorithm;
 
Import Java.util.Scanner;
   
   public class Strassenmethodtest {private Strassenmethod strassenmultiply;
  Strassenmethodtest () {strassenmultiply = new Strassenmethod ();
    }//end cons public static void main (string[] args) {Scanner input = new Scanner (system.in);
    System.out.println ("Input Row size of the" the "the");
    int arow = Input.nextint ();
    System.out.println ("Input column size of the" the "the");
    int acol = Input.nextint ();
    System.out.println ("Input Row size of the second matrix:");
    int brow = Input.nextint ();
    System.out.println ("Input column size of the second matrix:");
 
    int bcol = Input.nextint ();
    double[][] A = new Double[arow][acol];
    double[][] B = new Double[brow][bcol];
    double[][] C = new Double[arow][bcol];
     
    System.out.println ("Input Data for Matrix A:");
    /*in all of the codes later in this project, r means row while C means column. */For (int R = 0; R < Arow;
        r++) {for (int c = 0; c < Acol; C + +) {System.out.printf ("Data of a[%d][%d]:", R, c);
      A[R][C] = input.nextdouble ();
    }//end Inner loop}//end loop System.out.println ("Input Data for Matrix B:"); for (int r = 0; r < Brow. r++) {for (int c = 0; c < bcol C + +) {System.out.printf ("Data of a[%d][%d]
        : ", R, c);
      B[R][C] = input.nextdouble ();
    }//end Inner loop}//end loop strassenmethodtest algorithm = new Strassenmethodtest ();
 
    C = Algorithm.multiplyrectmatrix (A, B, Arow, Acol, Brow, bcol);
    Display The calculation result:System.out.println ("Result from matrix C:"); for (int r = 0; r < Arow. r++) {for (int c = 0; c < bcol C + +) {System.out.printf ("Data of c[%d][%d]
      :%f\n ", R, C, C[r][c]); }//end Inner loop}//end outter loop}//end main//deal with matrices then are not square:public ] Multiplyrectmatrix (DoubLe[][] A, double[][] B, int arow, int acol, int brow, int bcol) {if (Arow!= bcol)//invalid Multiplicatio
    
    return new Double[][]{{0}};
 
    double[][] C = new Double[arow][bcol];
      if (Arow < Acol) {double[][] Newa = new Double[acol][acol];
 
      double[][] newb = new Double[brow][brow];
       
      int n = acol;
 
      for (int r = 0; r < Acol. r++) for (int c = 0; c < Acol; C + +) newa[r][c] = 0.0;
 
      for (int r = 0; r < Brow. r++) for (int c = 0; c < brow; C + +) newb[r][c] = 0.0;
 
      for (int r = 0; r < Arow. r++) for (int c = 0; c < Acol; C + +) Newa[r][c] = A[r][c];
       
      for (int r = 0; r < Brow. r++) for (int c = 0; c < Bcol; C + +) Newb[r][c] = B[r][c];
      double[][] C2 = Multiplysquarematrix (Newa, NEWB, N); for (int r = 0; r < Arow. r++) for (int c = 0; c < Bcol; C + +) C[r][c] = C2[r][c];
    }//end if Else if (Arow = = Acol) C = Multiplysquarematrix (A, B, Arow);
      else {int n = arow;
      double[][] Newa = new Double[arow][arow];
 
      double[][] newb = new Double[bcol][bcol];
 
      for (int r = 0; r < Arow. r++) for (int c = 0; c < Arow; C + +) newa[r][c] = 0.0;
 
 
      for (int r = 0; r < Bcol. r++) for (int c = 0; c < Bcol; C + +) newb[r][c] = 0.0;
 
      for (int r = 0; r < Arow. r++) for (int c = 0; c < Acol; C + +) Newa[r][c] = A[r][c];
 
      for (int r = 0; r < Brow. r++) for (int c = 0; c < Bcol; C + +) Newb[r][c] = B[r][c];
      double[][] C2 = Multiplysquarematrix (Newa, NEWB, N);
    for (int r = 0; r < Arow. r++) for (int c = 0; c < Bcol; C + +) C[r][c] = C2[r][c];
   }//end else return C; 
   }//end method//deal and matrices that are square matrices. Public double[][] MultiplYsquarematrix (double[][] A2, double[][] B2, int n) {double[][] C2 = new Double[n][n];
     
     for (int r = 0; r < N; r++) for (int c = 0; c < n; c + +) C2[r][c] = 0;
      if (n = = 1) {c2[0][0] = a2[0][0] * B2[0][0];
     return C2;
     
     }//end if int exp2k = 2;
     while (exp2k <= (N/2)) {exp2k *= 2;
       }//end loop if (exp2k = n) {C2 = Strassenmultiply.strassenmultiplymatrix (A2, B2, N);
     return C2;
     }//end else//the "biggest" Strassen matrix:double[][][] A = new DOUBLE[6][EXP2K][EXP2K];
     double[][][] B = new DOUBLE[6][EXP2K][EXP2K];
     
     double[][][] C = new DOUBLE[6][EXP2K][EXP2K];
         for (int r = 0; r < exp2k. r++) {for (int c = 0; c < exp2k; C + +) {A[0][r][c] = A2[r][c];
       B[0][R][C] = B2[r][c];
 }//end Inner loop}//end outter loop c[0] = Strassenmultiply.strassenmultiplymatrix (a[0), b[0], exp2k);    
    for (int r = 0; r < exp2k. r++) for (int c = 0; c < exp2k; C + +) C2[r][c] = C[0][r][c];
     
    int middle = EXP2K/2;
        for (int r = 0; r < Middle. r++) {for (int c = exp2k; c < n; C + +) {a[1][r][c-exp2k] = A2[r][c];
      B[3][R][C-EXP2K] = B2[r][c]; }//end Inner Loop}//end outter loop for (int r = exp2k; r < N; r++) {for (int c = 0; C < mid dle;
        C + +) {A[3][r-exp2k][c] = A2[r][c];
      B[1][R-EXP2K][C] = B2[r][c];  }//end Inner Loop}//end outter loop for (int r = middle; r < exp2k; r++) {for (int c = exp2k; c < n;
        C + +) {a[2][r-middle][c-exp2k] = A2[r][c];
      B[4][R-MIDDLE][C-EXP2K] = B2[r][c]; }//end Inner Loop}//end outter loop for (int r = exp2k; r < N; r++) {for (int c = middle; c ; N-EXP2K + 1;
        C + +) {A[4][r-exp2k][c-middle] = A2[r][c]; B[2][r-exp2k][c-middLe] = b2[r][c]; }//end Inner Loop}//end outter loop for (int i = 1; I <= 4; i++) C[i] = Multiplyrectmatrix (a[i)
     
    , B[i], middle, a[i].length, a[i].length, middle); /* Calculate The final results of grids in the biggest 2^k square, according to the rules of Matrice Multiplicati
    On. * for (int row = 0; row < exp2k row++) {for (int col = 0; col < exp2k; col++) {for (int k = exp2k; K < n;
         k++) {C2[row][col] + = a2[row][k] * B2[k][col]; }//end Loop}//end Inner loop}//end outter loop//use brute force to solve the rest, would be improv  Ed later:for (int col = exp2k; col < n; col++) {for (int row = 0; row < n; row++) {for (int k = 0; k < n;
      k++) C2[row][col] + + = a2[row][k] * B2[k][row]; }//end Inner Loop}//end outter loop for (int row = exp2k, row < n; row++) {for (int col = 0; ; exp2k; col++) {
        for (int k = 0; k < n; k++) C2[row][col] + = a2[row][k] * B2[k][row];
   }//end Inner Loop}//end outter loop return C2; }//end method}//end Class

Strassenmethod.java

Package matrixalgorithm;
 
Import Java.util.Scanner;
  public class Strassenmethod {private double[][][][] A = new double[2][2][][];
  Private double[][][][] B = new double[2][2][][];
 
  Private double[][][][] C = new double[2][2][][]; 
    /*//codes for testing this class:public static void main (string[] args) {Scanner input = new Scanner (system.in);
    System.out.println ("Input size of The Matrix:");
 
    int n = input.nextint ();
    double[][] A = new Double[n][n];
    double[][] B = new Double[n][n];
    double[][] C = new Double[n][n];
    System.out.println ("Input Data for Matrix A:"); for (int r = 0; r < N; r++) {for (int c = 0; c < n; C + +) {System.out.printf ("Data of a[%d][%d]:", R
        , c);
      A[R][C] = input.nextdouble ();
    }//end Inner loop}//end loop System.out.println ("Input Data for Matrix B:"); for (int r = 0; r < N; r++) {for (int c = 0; c < n; C + +) {System.out.printf ("Data of a[%d][%D]: ", R, c);
      B[R][C] = input.nextdouble ();
    }//end Inner loop}//end loop Strassenmethod algorithm = new Strassenmethod ();
 
    C = Algorithm.strassenmultiplymatrix (A, B, N);
    SYSTEM.OUT.PRINTLN ("Result from matrix C:"); for (int r = 0; r < N; r++) {for (int c = 0; c < n; C + +) {System.out.printf ("Data of c[%d][%d]:%f\n
      ", R, C, C[r][c]); }//end Inner loop}//end outter loop}//end main*/public double[][] Strassenmultiplymatrix (double[][) A2,
    Double b2[][], int n) {double[][] C2 = new Double[n][n]; Initialize the matrix:for (int rowIndex = 0; rowIndex < n; rowindex++) for (int colindex = 0; Colindex < N
 
    colindex++) C2[rowindex][colindex] = 0.0;
    if (n = = 1) c2[0][0] = a2[0][0] * B2[0][0];
      "Slice matrices into 2 * 2 parts:else{double[][][][] A = new DOUBLE[2][2][N/2][N/2];
      double[][][][] B = new DOUBLE[2][2][N/2][N/2]; double[][][] [] C = new DOUBLE[2][2][N/2][N/2]; for (int r = 0; r < N/2. r++) {for (int c = 0; c < N/2; C + +) {A[0][0][r][c] = A2[r][c]
          ;
          A[0][1][R][C] = A2[R][N/2 + c];
          A[1][0][R][C] = A2[N/2 + r][c];
           
          A[1][1][R][C] = A2[N/2 + R][N/2 + c];
          B[0][0][R][C] = B2[r][c];
          B[0][1][R][C] = B2[R][N/2 + c];
          B[1][0][R][C] = B2[N/2 + r][c];
        B[1][1][R][C] = B2[N/2 + R][N/2 + c];
       
      }//end loop}//end loop n = n/2;
      double[][][] S = new Double[10][n][n];
      S[0] = Minusmatrix (b[0][1], b[1][1], n);
      S[1] = Addmatrix (a[0][0], a[0][1], n);
      S[2] = Addmatrix (a[1][0], a[1][1], n);
      S[3] = Minusmatrix (b[1][0], b[0][0], n);
      S[4] = Addmatrix (a[0][0], a[1][1], n);
      S[5] = Addmatrix (b[0][0], b[1][1], n);
      S[6] = Minusmatrix (a[0][1], a[1][1], n);
      S[7] = Addmatrix (b[1][0], b[1][1], n); S[8] = Minusmatrix (A[0][0], a[1][0], n);
       
      S[9] = Addmatrix (b[0][0], b[0][1], n);
      double[][][] P = new Double[7][n][n];
      P[0] = Strassenmultiplymatrix (a[0][0], s[0], n);
      P[1] = Strassenmultiplymatrix (s[1], b[1][1], n);
      P[2] = Strassenmultiplymatrix (s[2], b[0][0], n);
      P[3] = Strassenmultiplymatrix (a[1][1], s[3], n);
      P[4] = Strassenmultiplymatrix (S[4], s[5], n);
      P[5] = Strassenmultiplymatrix (S[6], s[7], n);
       
      P[6] = Strassenmultiplymatrix (S[8], s[9], n);
      C[0][0] = Addmatrix (Minusmatrix (Addmatrix (p[4), p[3], N), p[1], N), p[5], n);
      C[0][1] = Addmatrix (p[0], p[1], n);
      C[1][0] = Addmatrix (p[2], p[3], n);
       
      C[1][1] = Minusmatrix (Minusmatrix (Addmatrix (p[4), p[0], N), p[2], N), p[6], n);
       
       n *= 2;
          for (int r = 0; r < N/2. r++) {for (int c = 0; c < N/2; C + +) {C2[r][c] = C[0][0][r][c];
          C2[R][N/2 + c] = C[0][1][r][c]; C2[N/2 + r][c] = c[1][0][R][C];
        C2[N/2 + R][N/2 + c] = C[1][1][r][c];
  }//end Inner loop}//end outter loop}//end else return C2;
   }//end method//add two matrices according to matrix addition.
     
    Private double[][] Addmatrix (double[][] A, double[][] B, int n) {double c[][] = new Double[n][n];
     
    for (int r = 0; r < N; r++) for (int c = 0; c < n; c + +) C[r][c] = A[r][c] + b[r][c];
  return C;
   }//end method//substract two matrices according to matrix addition.
     
    Private double[][] Minusmatrix (double[][] A, double[][] B, int n) {double c[][] = new Double[n][n];
     
    for (int r = 0; r < N; r++) for (int c = 0; c < n; c + +) C[r][c] = A[r][c]-b[r][c];
  return C; }//end method}//end Class

Hopefully this article will help you learn about Java programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.