numpyeigenvalueeigenvectormatrix-decomposition

Eigendecomposition makes me wonder in numpy


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?


Solution

  • 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.