Search code examples
numpyeigenvector

Strange behaviour of numpy eigenvector: bug or no bug


NumPy's eigenvector solution differs from Wolfram Alpha and my personal calculation by hand.

>>> import numpy.linalg
>>> import numpy as np
>>> numpy.linalg.eig(np.array([[-2, 1], [2, -1]]))
(array([-3.,  0.]), array([[-0.70710678, -0.4472136 ],
       [ 0.70710678, -0.89442719]]))

Wolfram Alpha https://www.wolframalpha.com/input/?i=eigenvectors+%7B%7B-2,1%7D,%7B%2B2,-1%7D%7D and my personal calculation give the eigenvectors (-1, 1) and (2, 1). The NumPy solution however differs.

NumPy's calculated eigenvalues however are confirmed by Wolfram Alpha and my personal calculation.

So, is this a bug in NumPy or is my understanding of math to simple? A similar thread Numpy seems to produce incorrect eigenvectors sees the main difference in rounding/scaling of the eigenvectors but the deviation between the solutions would be massive.

Regards


Solution

  • numpy.linalg.eig normalizes the eigen vectors with the results being the column vectors

    eig_vectors = np.linalg.eig(np.array([[-2, 1], [2, -1]]))[1]
    vec_1 = eig_vectors[:,0]
    vec_2 = eig_vectors[:,1]
    

    now these 2 vectors are just normalized versions of the vectors you calculated ie

    print(vec_1 * np.sqrt(2)) # where root 2 is the magnitude of [-1, 1]
    print(vec_1 * np.sqrt(5)) # where root 5 is the magnitude of [2, 1]
    

    So bottom line the both sets of calculations are equivalent just Numpy likes to normalze the results.