Numpy: Difference Between Linalg.eig() And Linalg.eigh()
In a Python 3 application I'm using NumPy to calculate eigenvalues and eigenvectors of a symmetric real matrix. Here's my demo code: import numpy as np a = np.random.rand(3,3) # g
Solution 1:
eigh
guarantees you that the eigenvalues are sorted and uses a faster algorithm that takes advantage of the fact that the matrix is symmetric. If you know that your matrix is symmetric, use this function.Attention, eigh
doesn't check if your matrix is indeed symmetric, it by default just takes the lower triangular part of the matrix and assumes that the upper triangular part is defined by the symmetry of the matrix.
eig
works for general matrices and therefore uses a slower algorithm, you can check that for example with IPythons magic command %timeit
. If you test with larger matrices, you will also see that in general the eigenvalues are not sorted here.
Post a Comment for "Numpy: Difference Between Linalg.eig() And Linalg.eigh()"