Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

finding the real eigenvectors of a real symmetric matrix in numpy or scipy

I have a real symmetric matrix with a lot of degenerate eigenvalues, and I would like to find the real valued eigenvectors of this matrix. I am struggling to find a method in numpy or scipy that does this for me, the ones I have tried give complex valued eigenvectors. Does anyone know if such a function exists?

like image 790
Joel Klassen Avatar asked Mar 15 '23 08:03

Joel Klassen


1 Answers

Use numpy.linalg.eigh or scipy.linalg.eigh. These functions are designed for symmetric (or Hermitian) matrices, and with a real symmetric matrix, they should always return real eigenvalues and eigenvectors.

For example,

In [62]: from numpy.linalg import eigh

In [63]: a
Out[63]: 
array([[ 2.,  1.,  0.,  0.],
       [ 1.,  2.,  0.,  0.],
       [ 0.,  0.,  2.,  1.],
       [ 0.,  0.,  1.,  2.]])

In [64]: vals, vecs = eigh(a)

The eigenvalues are in vals, and the corresponding eigenvectors are in the columns of vecs:

In [65]: vals
Out[65]: array([ 1.,  1.,  3.,  3.])

In [66]: vecs
Out[66]: 
array([[-0.70710678,  0.        ,  0.        ,  0.70710678],
       [ 0.70710678,  0.        ,  0.        ,  0.70710678],
       [ 0.        , -0.70710678,  0.70710678,  0.        ],
       [ 0.        ,  0.70710678,  0.70710678,  0.        ]])
like image 195
Warren Weckesser Avatar answered Apr 08 '23 08:04

Warren Weckesser