这个想法是创建一个计时器,该计时器将返回执行某个功能所需的时间。我坐下来编写了一个矩阵类和一个应该相乘的Strass
函数,我将其输入其中。
计时器函数正常工作,因为它返回执行该Strass
函数所需的时间。但是,该Strass
函数不返回已相乘的矩阵。它是一个全零矩阵。就好像该Strass
函数没有为 Matrix C 分配任何东西。
例如,乘以 2x2 矩阵会得到以下结果:
0.00 // P1
0.00 0.00 // the matrix after multiplication
0.00 0.00
7102000 // the time it took to do this
该Strass
函数如下所示:
public static void Strass(Matrix A, Matrix B, Matrix C) {
// It has been suggested that P1-P7 should be of size
// A.size()/2. Changing this does not fix the problem.
Matrix P1 = new Matrix(A.size());
Matrix P2 = new Matrix(A.size());
Matrix P3 = new Matrix(A.size());
Matrix P4 = new Matrix(A.size());
Matrix P5 = new Matrix(A.size());
Matrix P6 = new Matrix(A.size());
Matrix P7 = new Matrix(A.size());
// if n = 1 then
if (A.size() == 1) {
C = A.times(B);
} else {
if (A.size() != B.size()) throw new RuntimeException("Somehow, the sizes of the matrices aren't equal.");
int sizeOf = A.size();
// The ungodly recursive calls.
Strass(A.partition(1, sizeOf/2, 1, sizeOf/2).plus(A.partition(sizeOf/2+1, sizeOf, sizeOf/2+1, sizeOf)), B.partition(1, sizeOf/2, 1, sizeOf/2).plus(B.partition(sizeOf/2+1, sizeOf, sizeOf/2+1, sizeOf)), P1);
Strass(A.partition(sizeOf/2+1, sizeOf, 1, sizeOf/2).plus(A.partition(sizeOf/2+1, sizeOf, sizeOf/2+1, sizeOf)), B.partition(1, sizeOf/2, 1, sizeOf/2), P2);
Strass(A.partition(1, sizeOf/2, 1, sizeOf/2), B.partition(1, sizeOf/2, sizeOf/2+1, sizeOf).minus(B.partition(sizeOf/2+1, sizeOf, sizeOf/2+1, sizeOf)), P3);
Strass(A.partition(sizeOf/2+1, sizeOf, sizeOf/2+1, sizeOf), B.partition(sizeOf/2+1, sizeOf, 1, sizeOf/2).minus(B.partition(1, sizeOf/2, 1, sizeOf/2)), P4);
Strass(A.partition(1, sizeOf/2, 1, sizeOf/2).plus(A.partition(1, sizeOf/2, sizeOf/2+1, sizeOf)), B.partition(sizeOf/2+1, sizeOf, sizeOf/2+1, sizeOf), P5);
Strass(A.partition(sizeOf/2+1, sizeOf, 1, sizeOf/2).minus(A.partition(1, sizeOf/2, 1, sizeOf/2)), B.partition(1, sizeOf/2, 1, sizeOf/2).plus(B.partition(1, sizeOf/2, sizeOf/2+1, sizeOf)), P6);
Strass(A.partition(1, sizeOf/2, sizeOf/2+1, 1).minus(A.partition(sizeOf/2+1, sizeOf, sizeOf/2+1, sizeOf)), B.partition(sizeOf/2+1, sizeOf, 1, sizeOf/2).plus(B.partition(sizeOf/2+1, sizeOf, sizeOf/2+1, sizeOf)), P7);
C.addPart(1, sizeOf/2, 1, sizeOf/2, (P1.plus(P4)).minus(P5.plus(P7)));
C.addPart(sizeOf/2+1, sizeOf, 1, sizeOf/2, (P2.plus(P4)));
C.addPart(1, sizeOf/2, sizeOf/2+1, sizeOf, (P3.plus(P5)));
C.addPart(sizeOf/2+1, sizeOf, sizeOf/2+1, sizeOf, (P1.plus(P3)).minus(P2.plus(P3)));
}
}
我已经测试了这个addPart
功能,据我所知它工作正常。加号和减号功能也是如此。我尽我最大的努力去验证我在所有正确的位置都有正确的尺寸和数字,我非常确定我做到了。所以,在这一切的某个地方,有些地方不对劲。
为了参考和简洁,我在这里粘贴了所有相关代码。