我为矩阵的运算符重载编写了如下代码。但是带负号的乘法结果效果不佳。
public static MyMatrix operator -(MyMatrix A, MyMatrix B)
{
MyMath math = new MyMath();
return math.matSubtract(A, B);
}
public static MyMatrix operator -(MyMatrix A, float B)
{
MyMathmath = new MyMath();
return math.matSubtract(A, B);
}
public static MyMatrix operator -(MyMatrix A)
{
MyMathmath = new MyMath();
return math.matMul(A, -1);
}
public static MyMatrix operator *(MyMatrix , MyMatrix B)
{
MyMath = new MyMath();
return math.matMul(A, B);
}
我检查了math.matmul并减去工作正常的结果。
然后,我像这样运行测试:
var A = new MyMatrix(0,1,2,3,4,5,6,7,8);
print(A);
var B = new MyMatrix(0,1,2,3,4,5,6,7,8);
print(B) ;
A = [0 1 2 3 4 5 6 7 8]B = [0 1 2 3 4 5 6 7 8]
ans = [-15 -18 -21 -42 -54 -66 -69 -90 -111] ans = [15 18 21 42 54 66 69 90 111] ans= [-180 -234 -288 -558 -720 -882 -936 -1206 -1476] ans= [-180 -234 -288 -558 -720 -882 -936 -1206 -1476] ans= [180 234 288 558 720 882 936 1206 1476]print(A*-B) print(-B*A) print(A*-B*-B); print(A*(-B)*(-B)); var C = -B; print(A*C*C)
是优先问题还是其他问题?
谢谢。
答案 0 :(得分:0)
没关系,这是别名:我正在更新对象值。