numpy-模态矩阵和对角特征值 [英] Numpy - Modal matrix and diagonal Eigenvalues

查看:179
本文介绍了numpy-模态矩阵和对角特征值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python Numpy中编写了一个简单的线性代数代码,通过计算$ M ^ {-1} .A.M $(M是模态矩阵)来计算特征值的对角线.

I wrote a simple Linear Algebra code in Python Numpy to calculate the Diagonal of EigenValues by calculating $M^{-1}.A.M$ (M is the Modal Matrix) and it's working strange.

这是代码:

import numpy as np

array = np.arange(16)
array = array.reshape(4, -1)
print(array)

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

eigenvalues, eigenvectors = np.linalg.eig(array)

print eigenvalues
[  3.24642492e+01  -2.46424920e+00   1.92979794e-15  -4.09576009e-16]

print eigenvectors
[[-0.11417645 -0.7327781   0.54500164  0.00135151]
 [-0.3300046  -0.28974835 -0.68602671  0.40644504]
 [-0.54583275  0.15328139 -0.2629515  -0.8169446 ]
 [-0.76166089  0.59631113  0.40397657  0.40914805]]

inverseEigenVectors = np.linalg.inv(eigenvectors) #M^(-1)
diagonal= inverseEigenVectors.dot(array).dot(eigenvectors) #M^(-1).A.M

print(diagonal)
[[  3.24642492e+01  -1.06581410e-14   5.32907052e-15   0.00000000e+00]
 [  7.54951657e-15  -2.46424920e+00  -1.72084569e-15  -2.22044605e-16]
 [ -2.80737213e-15   1.46768503e-15   2.33547852e-16   7.25592561e-16]
 [ -6.22319863e-15  -9.69656080e-16  -1.38050658e-30   1.97215226e-31]]

最终的对角线"矩阵应该是对角线矩阵,其主对角线上为EigenValues,其他位置为零.但这不是...第一个主对角线值是特征值,而第二个不是对角线值(尽管就像两个第二个对角线值一样,它们几乎都为零).

the final 'diagonal' matrix should be a diagonal matrix with EigenValues on the main diagonal and zeros elsewhere. but it's not... the two first main diagonal values ARE eigenvalues but the two second aren't (although just like the two second eigenvalues, they are nearly zero).

顺便说一下,像$ -1.06581410e-14 $这样的数字实际上是零,那么如何使numpy将它们显示为零?

and by the way a number like $-1.06581410e-14$ is literally zero so how can I make numpy show them as zero?

我在做什么错了?

谢谢...

推荐答案

只需将最终结果四舍五入到所需数字:

Just round the final result to the desired digits :

print(diagonal.round(5))

array([[ 32.46425,   0.     ,   0.     ,   0.     ],
       [  0.     ,  -2.46425,   0.     ,   0.     ],
       [  0.     ,   0.     ,   0.     ,   0.     ],
       [  0.     ,   0.     ,   0.     ,   0.     ]])

不要混淆计算和打印策略的精度.

Don't confuse precision of computation and printing policies.

这篇关于numpy-模态矩阵和对角特征值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆