如何获得特征值和特征向量问题的更多有效数字?

huangapple go评论65阅读模式
英文:

How can I get more significant figures for the eigenvalues and eigenvectors problem?

问题

我尝试在Python中计算矩阵的特征值和特征向量。我使用了numpy,并且以矩阵M为例进行了以下操作:

w, v = eig(M)
idx = w.argsort()[::1]   
eigVal = w[idx]
eigVec = v[:, idx]
print(eigVal)
print("特征向量是:")
print(eigVec[0])
print()
nnn = M.dot(eigVec[0])  # 矩阵乘以假定的特征向量
for i in range(lenght):
  nnn[i] = nnn[i] / eigVal[0]
print("M*向量/特征值的结果是:")
print(nnn)

得到的结果如下:

[452.78324098 461.88198554 468.47201706 474.43054819]
特征向量是:
[ 0.92852341  0.37084248 -0.01780576  0.00175573]

M*向量/特征值的结果是:
[ 9.28755114e-01  3.72671398e-01 -2.29673727e-02 -9.27549232e-05]

如您所见,尽管相似,但乘法后得到的特征向量与numpy最初计算的特征向量并不非常接近。如何提高精度呢?

英文:

I'm trying to calculate the eigenvalues and eigenvector of a matrix in Python. I used numpy and, as an example, did this with a matrix M:

w,v=eig(M)
idx = w.argsort()[::1]   
eigVal= w[idx]
eigVec = v[:,idx]
print(eigVal)
print("an eigen vector is:")
print(eigVec[0])
print()
nnn=M.dot(eigVec[0]) #matrix times supposed eigen vector
for i in range(lenght):
  nnn[i]=nnn[i]/eigVal[0]
print("The result of M*vector/eigenvalue is:")
print(nnn)

And got as a result:

[452.78324098 461.88198554 468.47201706 474.43054819]
an eigen vector is:
[ 0.92852341  0.37084248 -0.01780576  0.00175573]

The result of M*vector/eigenvalue is:
[ 9.28755114e-01  3.72671398e-01 -2.29673727e-02 -9.27549232e-05]

As you can see, although similar, the resulting eigenvector after the multiplication is not that close to what numpy originally computed. How can the precision be improved?

答案1

得分: 1

你需要沿着正确的轴取特征向量;

w, v = eig(M)
idx = w.argsort()[::1]
eigVal = w[idx]
eigVec = np.transpose(v[:, idx]) # <-- transpose here
print(eigVal)
print("特征向量是:")
print(eigVec[0])
print()
nnn = M.dot(eigVec[0]) #矩阵乘以假定的特征向量
nnn /= eigVal[0]
print("M*向量/特征值的结果是:")
print(nnn)
英文:

You need to take the eigenvectors along the right axis;

w,v=eig(M)
idx = w.argsort()[::1]   
eigVal= w[idx]
eigVec = np.transpose(v[:,idx]) # <-- transpose here
print(eigVal)
print("an eigen vector is:")
print(eigVec[0])
print()
nnn=M.dot(eigVec[0]) #matrix times supposed eigen vector
nnn /= eigVal[0]
print("The result of M*vector/eigenvalue is:")
print(nnn)

huangapple
  • 本文由 发表于 2023年2月10日 07:07:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定