这篇文章主要介绍了java实现任意矩阵Strassen算法的相关资料,需要的朋友可以参考下
本例输入为两个任意尺寸的矩阵m * n, n * m,输出为两个矩阵的乘积。计算任意尺寸矩阵相乘时,使用了Strassen算法。程序为自编,经过测试,请放心使用。基本算法是:
1.对于方阵(正方形矩阵),找到最大的l, 使得l = 2 ^ k, k为整数并且l <m。边长为l的方形矩阵则采用Strassen算法,其余部分以及方形矩阵中遗漏的部分用蛮力法。
2.对于非方阵,依照行列相应添加0使其成为方阵。
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 first matrix: "); int arow = input.nextInt(); System.out.println("Input column size of the first matrix: "); 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
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]来源gaodai$ma#com搞$代*码*网[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
希望本文所述对大家学习java程序设计有所帮助。
以上就是java实现任意矩阵Strassen算法的详细内容,更多请关注gaodaima搞代码网其它相关文章!