首页 > 解决方案 > numpy eigen decomposition produce unexpected output?

问题描述

According to documentation of numpy.linalg.eig and what I understand about eigen decomposition, the following code :

a = [[1,1],[-1,-1]]
w, v = np.linalg.eig(a)
c = a@v
print(c)
print(w)

should produce :

[[√2,0],[-√2,0]]
[4,0]

but instead it produced :

[[ 1.11022302e-16+1.11022302e-16j  1.11022302e-16-1.11022302e-16j]
 [-1.11022302e-16-1.11022302e-16j -1.11022302e-16+1.11022302e-16j]]
[-3.25176795e-17+1.57009246e-16j -3.25176795e-17-1.57009246e-16j]

so where was I wrong?

标签: numpyalgebraeigenvector

解决方案


带矩阵a

a = np.array([[ 1,  1],\
              [-1, -1]])

你的两个特征值理论上应该是w_th=[0,0],所以:

w
>>> array([-3.25176795e-17+1.57009246e-16j, -3.25176795e-17-1.57009246e-16j])

只是复杂形式的一些零 +/- 舍入误差。关于特征向量,这些v_th=[[1,1],[-1,-1]]只是numpy.linalg.eig将它们归一化为单位长度(例如对于第一个np.linalg.norm(v[:,0],2) = 0.99...),这意味着它只是给了你一个近似值[[1/sqrt(2),1/sqrt(2)],[-1/sqrt(2),-1/sqrt(2)]]

v
>>> array([[ 0.70710678+0.00000000e+00j,  0.70710678-0.00000000e+00j],
            [-0.70710678+1.11022302e-16j, -0.70710678-1.11022302e-16j]])

了解了以上所有内容,您现在可以通过比较等式的两边来验证它公式

np.allclose(a@v,w*v)
>>> True

或具有理论值,“没有”舍入误差:

a@np.asarray(v_th)
>>> array([[0, 0],
           [0, 0]])

np.asarray(w_th)*np.asarray(v_th)
>>> array([[0, 0],
           [0, 0]])

所以这里的 numpy 输出没有什么意外,似乎只是你的分析特征值[4,0]是错误的。


推荐阅读