【问题标题】:C# operator overloading for matrix矩阵的 C# 运算符重载
【发布时间】:2018-06-20 17:31:48
【问题描述】:

我已经为矩阵的运算符重载编写了代码,如下所示。但是与负号相乘的结果效果不佳。

 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)
答案 = [-15 -18 -21 -42 -54 -66 -69 -90 -111] 答案 = [15 18 21 42 54 66 69 90 111] 答案= [-180 -234 -288 -558 -720 -882 -936 -1206 -1476] 答案= [-180 -234 -288 -558 -720 -882 -936 -1206 -1476] 答案= [180 234 288 558 720 882 936 1206 1476]

可能是优先级问题还是其他问题?

提前致谢。

【问题讨论】:

  • 有趣!如果你只将它乘以 -B 会发生同样的事情吗?

标签: c# operator-overloading


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    相关资源
    最近更新 更多