线性方程系统求解器结果(MATLAB vs Math.NET vs Python)

Linear equation system solver results (MATLAB vs Math.NET vs Python)

[1] C#:使用 Math.NET 库求解方程组

// test solver in Math.NET
var A = Matrix<double>.Build.DenseOfArray(new double[,] {
                                {1, 1, 3},
                                {2, 0, 4},
                                {-1, 6, -1}
                            });
Console.WriteLine(A);
var b = Vector<double>.Build.Dense(new double[] { 2, 19, 8 });
Console.WriteLine(b);
var x = A.Solve(b);//Math.NET

Console.WriteLine("Test Solver in Math.NET: " + x);
>> Test Solver in Math.NET: DenseVector 3-Double
 34.5
    5
-12.5

Press any key to continue . . .

[2] MATLAB 中相同输入的结果:

A = [1 1 3; 2 0 4; -1 6 -1]
B = [2 19 8]
x = B/A
A =

     1     1     3
     2     0     4
    -1     6    -1


B =

     2    19     8


x =

   1.0000e+00   2.0000e+00   3.0000e+00

[3] 在 Python 中用于相同的输入并借助 numpy.linalg:

In[10]: 
import numpy as np

# matrix A
A = np.matrix ([[1, 1, 3],[2, 0, 4],[-1, 6, -1]])

# vector b
b = np.array([2, 19, 8])
b.shape = (3,1)
# attempt to solve Ax=b
z = np.linalg.solve(A,b)
z
Out[10]: 
array([[ 34.5],
       [  5. ],
       [-12.5]])

[4] C#(Math.NET) 和 Python 的结果似乎相同,而 MATLAB 则有很大不同,为什么会这样?

C# 和 Python 示例求解方程 A*x=b,而 MATLAB 示例求解 x*A=b.

可以更改 MATLAB 示例以通过转置 B 并使用 \ 代替 / 来求解 A*x=b

可以更改 Math.NET(和 Python)示例以通过转置 A 来求解 x*A=b,即 A.Transpose().Solve(b) 而不是 A.Solve(b)