我正在尝试以下列方式实现Strassen的矩阵乘法(一些变量在类的顶部声明,但你知道它们是什么):
public double[][] multiplyStrassen(double[][] m, double[][] m2) {
int n = m.length;
double [][] result = new double[n][n];
System.out.println("n = " + n);
if(n == 1) {
result[0][0] = m[0][0] * m2[0][0];
} else {
A11 = new double[n/2][n/2];
A12 = new double[n/2][n/2];
A21 = new double[n/2][n/2];
A22 = new double[n/2][n/2];
B11 = new double[n/2][n/2];
B12 = new double[n/2][n/2];
B21 = new double[n/2][n/2];
B22 = new double[n/2][n/2];
partitionMatrices(m, A11, 0 , 0);
partitionMatrices(m, A12, 0 , n/2);
partitionMatrices(m, A21, n/2, 0);
partitionMatrices(m, A22, n/2, n/2);
partitionMatrices(m2, B11, 0 , 0);
partitionMatrices(m2, B12, 0 , n/2);
partitionMatrices(m2, B21, n/2, 0);
partitionMatrices(m2, B22, n/2, n/2);
P1 = multiplyStrassen(addMatrices(A11, A22), addMatrices(B11, B22));
P2 = multiplyStrassen(addMatrices(A21, A22), B11);
P3 = multiplyStrassen(A11, subMatrices(B12, B22));
P4 = multiplyStrassen(A22, subMatrices(B21, B11));
P5 = multiplyStrassen(addMatrices(A11, A12), B22);
P6 = multiplyStrassen(subMatrices(A21, A11), addMatrices(B11, B12));
P7 = multiplyStrassen(subMatrices(A12, A22), addMatrices(B21, B22));
C11 = addMatrices(subMatrices(addMatrices(P1, P4), P5), P7);
C12 = addMatrices(P3, P5);
C21 = addMatrices(P2, P4);
C22 = addMatrices(subMatrices(addMatrices(P1, P3), P2), P6);
copyMatrix(C11, result, 0 , 0);
copyMatrix(C12, result, 0 , n/2);
copyMatrix(C21, result, n/2, 0);
copyMatrix(C22, result, n/2, n/2);
}
return result;
}
public void partitionMatrices(double[][] firstM, double[][] secondM, int A, int B) {
for(int i1 = 0, i2 = A; i1 < secondM.length; i1++, i2++){
for(int j1 = 0, j2 = B; j1 < secondM.length; j1++, j2++){
secondM[i1][j1] = firstM[i2][j2];
}
}
}
public double[][] addMatrices(double[][] m1, double[][] m2) {
int n = m1.length;
double [][] result = new double[n][n];
System.out.println("Printing m1: ");
printGrid(m1.length, m1.length);
System.out.println("Printing m2: ");
printGrid(m2.length, m2.length);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
System.out.println("m1[i][j] = " + m1[i][j]);
System.out.println("m2[i][j] = " + m2[i][j]);
result[i][j] = m1[i][j] + m2[i][j];
}
}
return result;
}
public double [][] subMatrices(double [][] m1, double [][] m2) {
int n = m1.length;
double [][] result = new double[n][n];
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
result[i][j] = m1[i][j] - m2[i][j];
return result;
}
public static void copyMatrix(double[][] firstM, double[][] secondM, int A, int B) {
for(int i1 = 0, i2 = A; i1 < firstM.length; i1++, i2++){
for(int j1 = 0, j2 = B; j1 < firstM.length; j1++, j2++){
secondM[i2][j2] = firstM[i1][j1];
}
}
}
编译器抛出:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Matrix.addMatrices(Matrix.java:142)
at Matrix.multiplyStrassen(Matrix.java:114)
at MatrixMultiplicationTest.main(MatrixMultiplicationTest.java:20)
我知道这些行号不会为你加起来,因为我在该课程中有其他代码,但我不知道自己哪里出错了,因为我只是在实施算法。代码几乎全部运行,但最终似乎只是破解了。我很感激我能得到的任何帮助。
答案 0 :(得分:0)
由于错误显示ArrayIndexOutOfBoundsException:1,因此应该在某个地方尝试从零大小的数组中获取值。它可能来自表达式:
int n = m.length;
double [][] result = new double[n][n];
如果n变为零,则导致初始化零大小的数组。