Eigendecomposition makes me wonder in numpy

牧云@^-^@ 提交于 2020-05-14 07:13:08

问题


I test the theorem that A = Q * Lambda * Q_inverse where Q the Matrix with the Eigenvectors and Lambda the Diagonal matrix having the Eigenvalues in the Diagonal.

My code is the following:

import numpy as np
from numpy import linalg as lg

Eigenvalues, Eigenvectors = lg.eigh(np.array([

    [1, 3],

    [2, 5]


]))

Lambda = np.diag(Eigenvalues)


Eigenvectors @ Lambda @ lg.inv(Eigenvectors)

Which returns :

array([[ 1.,  2.],
       [ 2.,  5.]])

Shouldn't the returned Matrix be the same as the Original one that was decomposed?


回答1:


You are using the function linalg.eigh which is for symmetric/Hermitian matricies, your matrix is not symmetric.

https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.linalg.eigh.html

You need to use linalg.eig and you will get the correct result:

https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.eig.html

import numpy as np
from numpy import linalg as lg

Eigenvalues, Eigenvectors = lg.eig(np.array([

[1, 3],

[2, 5]


]))

Lambda = np.diag(Eigenvalues)


Eigenvectors @ Lambda @ lg.inv(Eigenvectors)

returns

[[ 1.  3.]
 [ 2.  5.]]

As expected.



来源:https://stackoverflow.com/questions/50487118/eigendecomposition-makes-me-wonder-in-numpy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!