python - 复厄米矩阵 : different phase angles for EIG and EIGH 的特征分析

标签 python numpy matrix eigenvector

我知道特征向量仅定义为乘法常数。据我所知numpy算法(例如 linalg.eiglinalg.eighlinalg.svd )为 产生相同的特征向量实矩阵 ,所以显然他们使用相同的归一化。然而,在复杂矩阵的情况下,算法会产生不同的结果。

也就是说,特征向量在(复数)常数 z 之前是相同的。 .经过一些试验 eigeigh我意识到 eigh对于每个特征向量的第一个分量,始终将相位角(定义为 arctan(复数部分/实数部分))设置为 0,而 eig似乎从一些(任意?)非零相位角开始。

问:有没有办法对来自 eigh 的特征向量进行归一化?顺便eig是在做吗(即不是强制相位角 = 0)?

例子

我有一个复杂的厄密矩阵 G我想使用以下两种算法计算特征向量:

  • numpy.linalg.eig真正的/复方阵
  • numpy.linalg.eigh对于实对称/复厄米矩阵 (1 的特殊情况。)

  • 检查 G 是否为厄密
    # check if a matrix is hermitian
    def isHermitian(a, rtol=1e-05, atol=1e-08):
        return np.allclose(a, a.conjugate().T, rtol=rtol, atol=atol)
    
    print('G is hermitian:', isHermitian(G))
    

    出去:
    G is hermitian: True
    

    进行特征分析
    # eigenvectors from EIG()
    l1,u1 = np.linalg.eig(G)
    idx = np.argsort(l1)[::-1]
    l1,u1 = l1[idx].real,u1[:,idx]
    
    # eigenvectors from EIGH()
    l2,u2 = np.linalg.eigh(G)
    idx = np.argsort(l2)[::-1]
    l2,u2 = l2[idx],u2[:,idx]
    

    检查特征值
    print('Eigenvalues')
    print('eig\t:',l1[:3])
    print('eigh\t:',l2[:3])
    

    出去:
    Eigenvalues
    eig     : [2.55621629e+03 3.48520440e+00 3.16452447e-02]
    eigh    : [2.55621629e+03 3.48520440e+00 3.16452447e-02]
    

    两种方法产生相同的特征向量。

    检查特征向量

    现在看看特征向量(例如 3. eigenvector),它们相差一个常数因子 z .
    multFactors = u1[:,2]/u2[:,2]
    if np.count_nonzero(multFactors[0] == multFactors):
        print("All multiplication factors are same:", multFactors[0])
    else:
        print("Multiplication factors are different.")
    
    

    出去:
    All multiplication factors are same: (-0.8916113627685007+0.45280147727156245j)
    

    检查相位角

    现在检查 3. 特征向量的第一个分量的相位角:
    print('Phase angel (in PI) for first point:')
    print('Eig\t:',np.arctan2(u1[0,2].imag,u1[0,2].real)/np.pi)
    print('Eigh\t:',np.arctan2(u2[0,2].imag,u2[0,2].real)/np.pi)
    

    出去:
    Phase angel (in PI) for first point:
    Eig     : 0.8504246311627189
    Eigh    : 0.0
    

    I still cannot embed pictures so here's the link to the output figure.

    重现图的代码
    num = 2
    fig = plt.figure()
    gs = gridspec.GridSpec(2, 3) 
    ax0 = plt.subplot(gs[0,0])
    ax1 = plt.subplot(gs[1,0])
    ax2 = plt.subplot(gs[0,1:])
    ax3 = plt.subplot(gs[1,1:])
    ax2r= ax2.twinx()
    ax3r= ax3.twinx()
    ax0.imshow(G.real,vmin=-30,vmax=30,cmap='RdGy')
    ax1.imshow(G.imag,vmin=-30,vmax=30,cmap='RdGy')
    ax2.plot(u1[:,num].real,label='eig')
    ax2.plot((u2[:,num]).real,label='eigh')
    ax3.plot(u1[:,num].imag,label='eig')
    ax3.plot((u2[:,num]).imag,label='eigh')
    for a in [ax0,ax1,ax2,ax3]:
        a.set_xticks([])
        a.set_yticks([])
    ax0.set_title('Re(G)')
    ax1.set_title('Im(G)')
    ax2.set_title('Re('+str(num+1)+'. Eigenvector)')
    ax3.set_title('Im('+str(num+1)+'. Eigenvector)')
    ax2.legend(loc=0)
    ax3.legend(loc=0)
    fig.subplots_adjust(wspace=0, hspace=.2,top=.9) 
    fig.suptitle('Eigenanalysis of Hermitian Matrix G',size=16)
    plt.show()
    

    最佳答案

    根据我的经验(这里有很多问题可以支持这一点),您 从不 想用eigeigh是一个选项 - eig非常缓慢且非常不稳定。与此相关的是,我相信您的问题是倒退的 - 您想对 eig 的特征向量进行归一化。和 eigh 的一样,这你知道怎么做。

    关于python - 复厄米矩阵 : different phase angles for EIG and EIGH 的特征分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61914105/

    相关文章:

    c# - 如何使用矩阵中的相邻字母查找所有可能的单词

    python - 在多个 QTreeWidget 之间拖放数据

    Python Pandas : boolean function in Data Frame

    numpy - 根据行号更改 2d numpy 数组中行的每个第 n 个元素

    python - 如何从 Numpy 矩阵中的每一列获取值

    python - 点积过滤数据框 pandas

    algorithm - 调整二维矩阵大小的基本算法

    javascript - 使用react显示多维数组/矩阵

    python - 如何在启用多进程的 Flask 应用程序中使用全局变量

    python - 删除函数更改不相关的 for 循环的元素 (Python)