矩阵的C#运算符重载

时间:2018-06-20 17:31:49

标签: c# operator-overloading

我为矩阵的运算符重载编写了如下代码。但是带负号的乘法结果效果不佳。

 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]

print(A*-B)
print(-B*A)
print(A*-B*-B);
print(A*(-B)*(-B));
var C = -B;
print(A*C*C)
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]

是优先问题还是其他问题?

谢谢。

1 个答案:

答案 0 :(得分:0)

没关系,这是别名:我正在更新对象值。