Eigendecomposition makes me wonder in numpy
Asked Answered
L

1

5

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?

Lakendra answered 23/5, 2018 at 11:22 Comment(3)
shouldn't the last matrix be inversed not transposed according to theorem?Nathalia
Yes you are right. The transpose would be equal to the inverse when the matrix has orthogonal columns which is not the case always. Thank you for reminding me that.Lakendra
Yes, that's right. You are welcome.Nathalia
C
10

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.

Cabot answered 23/5, 2018 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.