SymPy等级与NumPy矩阵等级不同

时间:2018-12-15 13:12:57

标签: python matlab numpy linear-algebra sympy

给出一些SymPy矩阵M

M = Matrix([
 [0.000111334436666596, 0.00114870370895408, -0.000328330524152990, 5.61388353859808e-6, -0.000464532588930332, -0.000969955779635878, 1.70579589853818e-5, -5.77891177019884e-6, -0.000186812539472235, -2.37115911398055e-5],
 [-0.00105346453420510, 0.000165063406707273, -0.00184449574409890, 0.000658080565333929, 0.00197652092300241, 0.000516180213512589, 9.53823860082390e-5, 0.000189858427211978, -3.80494288487685e-5, 0.000188984043643408],
 [-0.00102465075104153, -0.000402915220398109, 0.00123785300884241, -0.00125808154543978, 0.000126618511490838, 0.00185985865307693, 0.000123626008509804, 0.000211557638637554, 0.000407232404255796, 1.89851719447102e-5],
 [0.230813497584639, -0.209574389008468, 0.742275067362657, -0.202368828927654, -0.236683258718819, 0.183258819107153, 0.180335891933511, -0.530606389541138, -0.379368598768419, 0.334800403899511],
 [-0.00102465075104153, -0.000402915220398109, 0.00123785300884241, -0.00125808154543978, 0.000126618511490838, 0.00185985865307693, 0.000123626008509804, 0.000211557638637554, 0.000407232404255796, 1.89851719447102e-5],
 [0.00105346453420510, -0.000165063406707273, 0.00184449574409890, -0.000658080565333929, -0.00197652092300241, -0.000516180213512589, -9.53823860082390e-5, -0.000189858427211978, 3.80494288487685e-5, -0.000188984043643408],
 [0.945967255845168, -0.0468645728473480, 0.165423896937049, -0.893045423193559, -0.519428986944650, -0.0463256408085840, -0.0257001217930424, 0.0757328764368606, 0.0541336731317414, -0.0477734271777646],
 [-0.0273371493900004, -0.954100482348723, -0.0879282784854250, 0.100704543595514, -0.243312734473589, -0.0217088779350294, 0.900584332231093, 0.616061129532614, 0.0651163853434486, -0.0396603397583054],
 [0.0967584768347089, -0.0877680087304911, -0.667679934757176, -0.0848411039101494, -0.0224646387789634, -0.194501966574153, 0.0755161040544943, 0.699388977592066, 0.394125039254254, -0.342798611994521],
 [-0.000222668873333193, -0.00229740741790816, 0.000656661048305981, -1.12277670771962e-5, 0.000929065177860663, 0.00193991155927176, -3.41159179707635e-5, 1.15578235403977e-5, 0.000373625078944470, 4.74231822796110e-5]
 ])

我已经计算出矩阵的SymPy rank()rref()。排名是7rref()结果是:

Matrix([
[1, 0, 0, 0, 0, 0, 0, -5.14556976678473, -3.72094268951566,  3.48581267477014],
[0, 1, 0, 0, 0, 0, 0, -5.52930150663022, -4.02230308325653,  3.79193678096199],
[0, 0, 1, 0, 0, 0, 0,  2.44893308665325,  1.83777402439421, -1.87489784909824],
[0, 0, 0, 1, 0, 0, 0, -7.33732284392352, -5.25036238623229,  4.97256759287563],
[0, 0, 0, 0, 1, 0, 0,  5.48049237370489,  3.90091366576548, -3.83642187384021],
[0, 0, 0, 0, 0, 1, 0, -10.6826798792866, -7.56560803870182,  7.45974067056387],
[0, 0, 0, 0, 0, 0, 1, -3.04726210012149, -2.66388837034592,  2.48327234504403],
[0, 0, 0, 0, 0, 0, 0,                 0,                 0,                 0],
[0, 0, 0, 0, 0, 0, 0,                 0,                 0,                 0],
[0, 0, 0, 0, 0, 0, 0,                 0,                 0,                 0]])

奇怪的是,如果我使用NumPyMATLAB来计算排名,我会得到值6并使用rref来计算MATLAB会得到预期的结果-最后4行全为零(而不是仅最后3行)。

有人知道这种差异来自何处,为什么我无法用SymPy获得正确的结果?我知道等级6是正确的,因为它是方程组的系统,其中存在一些线性相关性。

1 个答案:

答案 0 :(得分:3)

看看矩阵的特征值,排名确实是6:

array([ 1.14550481e+00+0.00000000e+00j, -1.82137718e-01+6.83443168e-01j,
   -1.82137718e-01-6.83443168e-01j,  2.76223053e-03+0.00000000e+00j,
   -3.51138883e-04+8.61508469e-04j, -3.51138883e-04-8.61508469e-04j,
    5.21160131e-17+0.00000000e+00j, -2.65160469e-16+0.00000000e+00j,
   -2.67753616e-18+9.70937977e-18j, -2.67753616e-18-9.70937977e-18j])

有了sympy版本,我得到的排名甚至是8,而numpy返回的排名是6。

但是实际上,由于矩阵的大小(可能与SymPy could not compute the eigenvalues of this matrix有关),Sympy无法求解该矩阵的特征值。

因此,其中一个Sympy试图象征性地求解方程并找到秩(based on imperfect floating point numbers),而另一个numpy使用近似值({{1 }} IIRC)来找到特征值。通过设置适当的阈值,lapack可以找到适当的排名,但是对于不同的阈值来说,它可能会有所不同。 numpy尝试根据完美6等级系统的近似系统找到等级,并发现它的等级为7或8。这并不奇怪,因为浮点差(Sympy移至整数例如尝试找到特征值,而不是停留在浮点域中。