numpy - 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?
解决方案
带矩阵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]
是错误的。
推荐阅读
- vba - IE VBA打开网页等于点击第一个网页链接
- php - 在具有 polylang 支持的 wordpress 中为登录用户和来宾用户设置不同的页面
- dart - DARTFMT 搞乱了@Component 装饰器格式
- java - 如何在 HTML 页面中部署 JavaFX 应用程序?
- python-2.7 - 在 matplotlib 3D scatter 中将一点放在前面
- r - stringr 是否使用一组不同的字符类作为标点符号?
- c - 通过 ref 从 struct 中的数组传回值
- cassandra - 在 Cassandra 中使用 UDT 会导致性能问题吗?
- json - 更新后如何保存我的 JSON 文件(重启程序后重置)
- sql - SQL Server 更新子查询的公式不太正确