Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 739
user8270077 Avatar asked Mar 07 '23 03:03

user8270077


1 Answers

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.

like image 85
JJR4 Avatar answered Mar 11 '23 02:03

JJR4