Incorrect EigenValues/Vectors with Numpy

后端 未结 1 679
遥遥无期
遥遥无期 2021-01-06 02:44

I am trying to find the eigenvalues/vectors for the following matrix:

A = np.array([[1, 0, 0],
              [0, 1, 0],
              [1, 1, 0]])


        
相关标签:
1条回答
  • 2021-01-06 03:05

    The eigenvalues returned by linalg.eig are columns vectors, so you need to iterate over the transpose of e_vecs (since iteration over a 2D array returns row vectors by default):

    import numpy as np
    import numpy.linalg as LA
    A = np.array([[1, 0, 0], [0, 1, 0], [1, 1, 0]])
    e_vals, e_vecs = LA.eig(A)
    
    print(e_vals)
    # [ 0.  1.  1.]
    print(e_vecs)
    # [[ 0.          0.          1.        ]
    #  [ 0.70710678  0.          0.70710678]
    #  [ 0.          0.70710678  0.70710678]]
    
    for val, vec in zip(e_vals, e_vecs.T):
        assert np.allclose(np.dot(A, vec), val * vec)
    
    0 讨论(0)
提交回复
热议问题