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) # generate a random array shaped (3,3) a = (a + a.T)/2 # a becomes a random simmetric matrix evalues1, evectors1 = np.linalg.eig(a) evalues2, evectors2 = np.linalg.eigh(a)
Except for the signs, I got the same eigenvectors and eigenvalues using np.linalg.eig
and np.linalg.eigh
. So, what's the difference between the two methods?
Thanks
EDIT: I've read the docs here https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.eig.html and here https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.eigh.html but still I can not understand why I should use eigh()
when I have a symmetric array.
"Eig" returns the right eigenvectors. "Eigh" returns the left eigenvectors. It means the order of the values is different. The other difference is the speed of computation.
In NumPy we can compute the eigenvalues and right eigenvectors of a given square array with the help of numpy. linalg. eig(). It will take a square array as a parameter and it will return two values first one is eigenvalues of the array and second is the right eigenvectors of a given square array.
Advertisements. SciPy is built using the optimized ATLAS LAPACK and BLAS libraries. It has very fast linear algebra capabilities. All of these linear algebra routines expect an object that can be converted into a two-dimensional array.
numpy.linalg.eigh ¶ linalg.eigh(a, UPLO='L') [source] ¶ Return the eigenvalues and eigenvectors of a complex Hermitian (conjugate symmetric) or a real symmetric matrix. Returns two objects, a 1-D array containing the eigenvalues of a, and a 2-D square array or matrix (depending on the input type) of the corresponding eigenvectors (in columns).
For non-Hermitian normal matrices the SciPy function scipy.linalg.schur is preferred because the matrix v is guaranteed to be unitary, which is not the case when using eig.
As I mentioned elsewhere, the eigenvalue and eigenvector are not unique. The only thing that is true is that for each eigenvalue $A v = lambda v$, the two matrices returned by eig and eigh describe those solutions, it is natural that eig inexact but approximate results.
The only thing that is true is that for each eigenvalue $A v = lambda v$, the two matrices returned by eig and eigh describe those solutions, it is natural that eig inexact but approximate results. You can see that both the solutions will triangularize your matrix in different ways
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With