简体   繁体   中英

Numpy eigenvalues/eigenvectors seem wrong for complex valued matrix?

This may be something really stupid, but I am getting a rather weird output with Numpy, version 1.12.1. I am trying to diagonalise a random symmetric matrix, then check the quality by transforming back the diagonal eigenvalue matrix, but it seems to fail for complex values. Basically:

A = np.random.random((3, 3))
A += A.T.conj()
evals, evecs = np.linalg.eig(A)
print np.isclose(np.dot(evecs, np.dot(np.diag(evals), evecs.T)), A).all()

prints True whereas

A = np.random.random((3, 3))+1.0j*np.random.random((3, 3))
A += A.T.conj()
evals, evecs = np.linalg.eig(A)
print np.isclose(np.dot(evecs, np.dot(np.diag(evals), evecs.T)), A).all()

prints False . I checked the values and it doesn't seem just some numerical inaccuracy, it seems dead wrong. Am I doing something fundamentally wrong? I know it works for Hermitian matrices when I use np.linalg.eigh as that's something I use very often, but why does eig fail for complex values along the diagonal too?

The answer to your question is that you failed to do the diagonalisation/matrix reconstruction properly.

A = np.random.random((3, 3))+1.0j*np.random.random((3, 3))
A += A.T.conj()
evals, evecs = np.linalg.eig(A)
from scipy.linalg import inv
print(np.isclose(np.dot(evecs, np.dot(np.diag(evals), inv(evecs))), A).all())

will tell you a neat little True , as it is the proper formula.

Now, what happens if you call

print np.isclose(np.dot(evecs, np.dot(np.diag(evals), evecs.T)), A).all() #False

is that you multiply by the transpose of the eigenvector matrix, which is valid in the case of a real-valued normalised eigenvector matrix. The normalised part is luckily still true, so all you have to do to mimic the inverse is to take the complex conjugate of the matrix.

print(np.isclose(np.dot(evecs, np.dot(np.diag(evals), evecs.T.conj())), A).all()) #True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM