使用Power方法从Python 3x3矩阵中获取特征值 [英] Getting eigenvalues from 3x3 matrix in Python using Power method
问题描述
我正在尝试使用Python中的Power方法从3x3矩阵中获取所有特征值.但是由于某种原因,我的方法从正确的特征值中返回了不同的特征值.
I'm trying to get all eigenvalues from a 3x3 matrix by using Power Method in Python. However my method returns diffrent eigenvalues from the correct ones for some reason.
我的矩阵:A = [[1、2、3],[2、4、5],[3、5,-1]]
My matrix: A = [[1, 2, 3], [2, 4, 5], [3, 5,-1]]
正确的特征值:[8.54851285,-4.57408723,0.02557437]
Correct eigenvalues: [ 8.54851285, -4.57408723, 0.02557437 ]
我的方法返回的特征值:[8.5485128481521926,4.5740872291939381,9.148174458392436]
Eigenvalues returned by my method: [ 8.5485128481521926, 4.5740872291939381, 9.148174458392436 ]
因此,第一个是正确的,第二个是错误的符号,而第三个都是错误的.我不知道自己做错了什么,也看不出哪里出错了.
So the first one is correct, second one has wrong sign and the third one is all wrong. I don't know what I'm doing wrong and I can't see where have I made mistake.
这是我的代码:
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.有没有一种简单的方法可以从幂方法中获得第二和第三特征值,而不是矩阵偏转?
PS. Is there a simpler way to get the second and third eigenvalue from power method instead of matrix deflaction?
推荐答案
使用
lamb = x_norm
您只能计算特征值的绝对值.更好地将它们计算为
you ever only compute the absolute value of the eigenvalues. Better compute them as
lamb = dot(x,x_1)
其中 x
被认为是标准化的.
where x
is assumed to be normalized.
由于您没有删除负特征值-4.57408723,而是有效地添加了负特征值,因此第三阶段的最大特征值是 2 * -4.574 .. = -9.148 ..
,在此您再次进行了计算绝对值.
As you do not remove the negative eigenvalue -4.57408723, but effectively add it instead, the largest eigenvalue in the third stage is 2*-4.574.. = -9.148..
where you again computed the absolute value.
这篇关于使用Power方法从Python 3x3矩阵中获取特征值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!