pythonmatrixeigenvector

numpy's "linalg.eig()" and "linalg.eigh()" for the same hermitian matrix


This question was due to a misunderstanding. See the answer below.

numpy.linalg methods eig() and eigh() appear to return different eigenvectors for the same hermitian matrix. Here the code:

import numpy as np

H = [[0.6 , -1j, 0], [1j, 0.4, 0], [0, 0, -1]]

evals, evects = np.linalg.eig(H)
print('\nOutput of the eig function')
for i in range(0,3):
    print('evect for eval=',evals[i],'\n',evects[i,0],'\n',evects[i][1],'\n',evects[i][2])
    
evals, evects = np.linalg.eigh(H)
print('\nOutput of the eigh function')
for i in range(0,3):
    print('evect for eval=',evals[i],'\n',evects[i,0],'\n',evects[i][1],'\n',evects[i][2])

Solution

  • Posting this to help anyone who might have had the same kind of misunderstanding I had:

    The eigenvectors are the columns of the resulting matrix for both functions. The fault was in the original code, which extracted rows from the eigenvector matrix instead of columns. The correct code is the following one.

    H = [[0.6 , -1j, 0], [1j, 0.4, 0], [0, 0, -1]]
    
    evals, evects = np.linalg.eig(H)
    print('\nOutput of the eig function')
    for i in range(0,3):
        print('evect for eval=',evals[i],'\n',evects.T[i,0],'\n',evects.T[i][1],'\n',evects.T[i][2])
        
    evals, evects = np.linalg.eigh(H)
    print('\nOutput of the eigh function')
    for i in range(0,3):
        print('evect for eval=',evals[i],'\n',evects.T[i,0],'\n',evects.T[i][1],'\n',evects.T[i][2])