【发布时间】:2018-01-22 15:59:52
【问题描述】:
我正在尝试通过在 Python 中使用幂法从 3x3 矩阵中获取所有特征值。但是由于某种原因,我的方法从正确的特征值返回不同的特征值。
我的矩阵:A = [[1, 2, 3], [2, 4, 5], [3, 5,-1]]
正确的特征值:[ 8.54851285, -4.57408723, 0.02557437 ]
我的方法返回的特征值:[ 8.5485128481521926, 4.5740872291939381, 9.148174458392436 ]
所以第一个是正确的,第二个有错误的符号,第三个都是错误的。我不知道我做错了什么,我看不出我在哪里做错了。
这是我的代码:
import numpy as np
import numpy.linalg as la
eps = 1e-8 # Precision of eigenvalue
def trans(v): # translates vector (v^T)
v_1 = np.copy(v)
return v_1.reshape((-1, 1))
def power(A):
eig = []
Ac = np.copy(A)
lamb = 0
for i in range(3):
x = np.array([1, 1, 1])
while True:
x_1 = Ac.dot(x) # y_n = A*x_(n-1)
x_norm = la.norm(x_1)
x_1 = x_1/x_norm # x_n = y_n/||y_n||
if(abs(lamb - x_norm) <= eps): # If precision is reached, it returns eigenvalue
break
else:
lamb = x_norm
x = x_1
eig.append(lamb)
# Matrix Deflaction: A - Lambda * norm[V]*norm[V]^T
v = x_1/la.norm(x_1)
R = v * trans(v)
R = eig[i]*R
Ac = Ac - R
return eig
def main():
A = np.array([1, 2, 3, 2, 4, 5, 3, 5, -1]).reshape((3, 3))
print(power(A))
if __name__ == '__main__':
main()
PS。有没有更简单的方法可以从幂法而不是矩阵变形中获得第二个和第三个特征值?
【问题讨论】:
-
除非你这样做是为了学习,否则
numpy中已经有一个existing eigenvalue method -
@Petar 我知道,但我这样做是为了学习。所以这种特征值方法对我来说不是一个选择。
标签: python numpy linear-algebra numerical-methods