Consider A a real symmetric matrix and
import scipy
(s,u)=scipy.linalg.eigh(A)
If A has repeated eigenvalues then the columns of u are not necessarily orthonormal. What is the most efficient way to obtain a basis of orthonormal eigenvectors in python?
Use eigh()
instead of eig()
, since eigh()
is specially designed to deal with complex hermitian and real symmetric matrices.
In [1]: import numpy as np
...: sigma = np.array([[ 7, -3, 2],
...: [ -3, -1, 6],
...: [ 2, 6, 4]])
In [2]: val, vec = np.linalg.eig(sigma)
In [3]: val
Out[3]: array([ 8., -6., 8.])
In [4]: vec
Out[4]:
array([[ 0.96362411, -0.26726124, -0.06680865],
[-0.22237479, -0.80178373, 0.56878282],
[ 0.14824986, 0.53452248, 0.81976991]])
Observe that vec.T @ vec
is not an identity matrix:
In [5]: vec.T @ vec
Out[5]:
array([[ 1.00000000e+00, 0.00000000e+00, -6.93306162e-02],
[ 0.00000000e+00, 1.00000000e+00, 1.11022302e-16],
[-6.93306162e-02, 1.11022302e-16, 1.00000000e+00]])
Now let's use eigh()
:
In [6]: val, vec = np.linalg.eigh(sigma)
In [7]: val
Out[7]: array([-6., 8., 8.])
In [8]: vec
Out[8]:
array([[-0.26726124, 0.87287156, -0.40824829],
[-0.80178373, 0.03357198, 0.59667058],
[ 0.53452248, 0.48679376, 0.69088172]])
Observe that vec.T @ vec
is an identity matrix:
In [9]: vec.T @ vec
Out[9]:
array([[1.00000000e+00, 5.78786501e-17, 5.56940743e-17],
[5.78786501e-17, 1.00000000e+00, 4.61864879e-17],
[5.56940743e-17, 4.61864879e-17, 1.00000000e+00]])
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.