scipy.linalg.eig是否给出正确的左特征向量? [英] Is scipy.linalg.eig giving the correct left eigenvectors?
问题描述
我对scipy.linalg.eig如何计算左右特征向量有疑问.也许我误解了一切,但事情似乎不适合我...
I have a question regarding the way how scipy.linalg.eig computes left and right eigenvectors. Maybe I misunderstood everything, but things seem not to be right to me...
从一开始.为了获得特征值和两个特征向量,我使用以下代码:
From the beginning. To get eigenvalues and both eigenvectors I used the following:
ev, left_v, right_v = scipy.linalg.eig(A, left=True)
根据手册,在调用函数时设置left=True
,我应该期望将左特征向量作为left_v
的列,其中第i列引用第i个特征值.但是,结果不是我预期的,所以我做了一个简单的检查.
According to the manual, after setting left=True
while calling the function I should expect to get left eigenvectors as columns of left_v
where the ith column refers to the ith eigenvalue. However, the results were not what i anticipated so I did a simple check.
我计算了两次调用该函数的左右特征向量(有关详细信息,请参见此处):
I computed right and left eigenvectors invoking the function twice (look here for details):
right_ev, right_v_2 = scipy.linalg.eig(A)
left_ev, left_v_2 = scipy.linalg.eig(A.T)
其中,left_v_2
的列是与left_ev
中的对应值关联的特征向量.
值得强调的是,right_ev_2
和left_ev_2
都给出相同的特征值,但是它们的顺序不同,需要加以考虑.
where columns ofleft_v_2
are eigenvectors associated with corresponding values in left_ev
.
Worth stressing that both right_ev_2
and left_ev_2
give the same eigenvalues, however they are in different order, which needs to be accounted for.
比较left_ev
和left_ev_2
(在对特征值进行重新排序之后),可以迅速发现前者是后者的共轭,因此从scipy.linalg.eig
与left=True
得出的left_ev
无效.左特征向量.
Comparing left_ev
and left_ev_2
(after reordering with respect to eigenvalues) one can quickly spot that the former is the conjugate of the latter and therefore left_ev
obtained from scipy.linalg.eig
with left=True
is not a valid left eigenvector.
可以基于以下事实对特征向量的有效性进行另一次检查:对于任意实方矩阵,左右特征向量都是正交的,即:
Another check on the validity of the eigenvectors can be done based on the fact that for an arbitrary real square matrix left and right eigenvectors are biorthogonal, i.e.:
left_v.T.dot(right_v)
应该给出一个对角矩阵,但是没有,
直到我将其更改为:left_v.T.conj().dot(right_v)
,
left_v.T.dot(right_v)
should give a diagonal matrix, but it doesn't,
until i change it to: left_v.T.conj().dot(right_v)
,
同时:
left_v_2.T.dot(right_v_2)
给出了预期的对角矩阵.
left_v_2.T.dot(right_v_2)
gives an anticipated diagonal matrix.
有人以前遇到过类似的问题吗?我说的对吗?在描述eig
时,科学手册是否有点不准确?你能给点建议吗?
Did anyone encounter similar problem before? Am I right with what I say? Is the sciPy manual a bit imprecise while describing eig
? Can you give any advice?
非常感谢!
推荐答案
关于vl
,eig
文档字符串说:
a.H vl[:,i] = w[i].conj() b.H vl[:,i]
或者,采用两侧的共轭转置(即埃尔米特转置)(即.H的意思),并假设b
是同一性,
Or, taking the conjugate transpose (i.e. Hermitian transpose) of both sides (which is what .H means), and assuming b
is the identity,
vl[:,i].H a = w[i] vl[:,i].H
所以vl
的共轭转置的行是a
的实际左特征向量.
So the rows of the conjugate transpose of vl
are the actual left eigenvectors of a
.
Numpy数组实际上没有.H属性,因此必须使用.conj().T.
Numpy arrays don't actually have the .H attribute, so you must use .conj().T.
这是一个验证计算的脚本:
Here's a script to verify the calculation:
import numpy as np
from scipy.linalg import eig
# This only affects the printed output.
np.set_printoptions(precision=4)
a = np.array([[6, 2],
[-1, 4]])
w, vl, vr = eig(a, left=True)
print "eigenvalues:", w
print
# check the left eigenvectors one-by-one:
for k in range(a.shape[0]):
val = w[k]
# Use a slice to maintain shape; vec is a 2x1 array.
# That allows a meaningful transpose using .T.
vec = vl[:, k:k+1]
# rowvec is 1x2; it is the conjugate transpose of vec.
# This should be the left eigenvector.
rowvec = vec.conj().T
# Verify that rowvec is a left eigenvector
lhs = rowvec.dot(a)
rhs = val * rowvec
print "Compare", lhs, "to", rhs
print rowvec, "is",
if not np.allclose(lhs, rhs):
print "*NOT*",
print "a left eigenvector for eigenvalue", val
print
print "Matrix version:"
print "This"
print vl.conj().T.dot(a)
print "should equal this"
print np.diag(w).dot(vl.conj().T)
输出:
eigenvalues: [ 5.+1.j 5.-1.j]
Compare [[ 1.6330+2.4495j 4.0825+0.8165j]] to [[ 1.6330+2.4495j 4.0825+0.8165j]]
[[ 0.4082+0.4082j 0.8165-0.j ]] is a left eigenvector for eigenvalue (5+1j)
Compare [[ 1.6330-2.4495j 4.0825-0.8165j]] to [[ 1.6330-2.4495j 4.0825-0.8165j]]
[[ 0.4082-0.4082j 0.8165+0.j ]] is a left eigenvector for eigenvalue (5-1j)
Matrix version:
This
[[ 1.6330+2.4495j 4.0825+0.8165j]
[ 1.6330-2.4495j 4.0825-0.8165j]]
should equal this
[[ 1.6330+2.4495j 4.0825+0.8165j]
[ 1.6330-2.4495j 4.0825-0.8165j]]
现在,eig
文档字符串在返回值的描述中也说:
Now, the eig
docstring also says in the description of the return values:
vl : double or complex ndarray
The normalized left eigenvector corresponding to the eigenvalue
``w[i]`` is the column v[:,i]. Only returned if ``left=True``.
Of shape ``(M, M)``.
,这可能会引起误解,因为左特征向量的常规定义(例如 http://mathworld. wolfram.com/LeftEigenvector.html 或 http://en.wikipedia.org/wiki /Eigenvalues_and_eigenvectors#Left_and_right_eigenvectors )是一个行向量,因此vl
列的共轭转置实际上是左特征向量.
and that is potentially misleading, since the conventional definition of a left eigenvector (e.g. http://mathworld.wolfram.com/LeftEigenvector.html or http://en.wikipedia.org/wiki/Eigenvalues_and_eigenvectors#Left_and_right_eigenvectors) is a row vector, so it is the conjugate transpose of the column of vl
that is actually the left eigenvector.
这篇关于scipy.linalg.eig是否给出正确的左特征向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!