这个想法是创建一个计时器,它将返回执行某个功能所需的时间。我坐下来编写了一个矩阵类和一个Strass
函数,这个函数应该乘以我输入的值。
timer函数工作正常,因为它返回执行Strass
函数所需的时间但是,Strass
函数不返回已被乘法的矩阵。它是一个全零的矩阵。就好像Strass
函数没有给矩阵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
函数,据我所知它工作正常。加减函数也是如此我尽了最大的努力去检查并确认我在所有正确的位置都有正确的尺寸和号码,我非常肯定我有所以,在这一切的某个地方,有点不对劲。为了参考和简洁,我粘贴了所有相关代码here。
最佳答案
C = A.times(B);
不正确这将为C
分配一个新矩阵,它不会修改传入的矩阵对象。