简体   繁体   English

numpy 的“linalg.eig()”和“linalg.eigh()”用于同一个厄密矩阵

[英]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. numpy.linalg 方法 eig() 和 eigh() 似乎为同一厄米矩阵返回不同的特征向量。 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])

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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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