gene
gene

Reputation: 143

NumPy linalg.eigh is returning incorrect eigenvectors

I'm using NumPy's linear algebra package to calculate the eigenvectors corresponding to the minimum eigenvalue of large Hermitian matrices. The linalg.eigh function claims to return the eigenvalues of a Hermitian matrix in ascending order, as well as the corresponding eigenvectors. This is precisely what I need. However, it seems that this function is failing even in the simple case of an already diagonal matrix. For example:

import numpy as np

H = np.diag([-0.4,-0.5, 0.4, 2.3, -0.5, -0.6, 0.3, 2.2, 0.4, 0.3, 1.2, 3.1, 2.3, 2.2, 3.1, 5.])
np.linalg.eigh(H)

The output is

(array([-0.6, -0.5, -0.5, -0.4,  0.3,  0.3,  0.4,  0.4,  1.2,  2.2,  2.2, 2.3,  2.3,  3.1,  3.1,  5. ]),
array([[ 0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.]]))

i.e. the function outputs [0,0,0,1,...] as the eigenvector corresponding to -0.6, which is clearly untrue. Can anybody tell me why this function is failing?

Upvotes: 1

Views: 2500

Answers (1)

Alex
Alex

Reputation: 158

See the NumPy documentation: https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.linalg.eigh.html .

The eigenvectors are returned as columns of the output array, not the rows. You'll see that v[:,0] is the eigenvector corresponding to -0.6. If you transpose the result you can convert the eigenvectors to be in a row format.

Upvotes: 3

Related Questions