Search code examples
pythonnumpymatheigenvalue

Not getting expected output when calculating eigenvectors


I was trying out numpy.linalg.eig and the output wasn't as predicted

Ax = λx where

  • A is a matrix
  • λ is eigenvalue
  • x is eigenvector

Here's the code

import numpy as np
from numpy import linalg

A = np.array([[2,3],[-1,3]])
eigenvalues,eigenvectors = linalg.eig(A)

leftside = A[0]*eigenvectors[:,0]
rightside = eigenvalues[0]*eigenvectors[:,0]
print(leftside)
print(rightside)

I expected the leftside to be equal to the right side but the outputs are different?

[1.73205081+0.j         0.4330127 +1.43614066j]
[ 2.16506351+1.43614066j -0.4330127 +1.43614066j]

Why does this occur? How do I make the equation work? ( as in the left side will be equal to the right side )


Solution

  • To do matrix-vector multiplication use A @ ... not A[0] * ... which multiplies the vector A[0]. Code:

    import numpy as np
    from numpy import linalg
    
    A = np.array([[2, 3], [-1, 3]])
    eigenvalues, eigenvectors = linalg.eig(A)
    
    leftside = A @ eigenvectors[:, 0]
    rightside = eigenvalues[0] * eigenvectors[:, 0]
    print(leftside)
    print(rightside)
    

    Prints:

    [ 2.16506351+1.43614066j -0.4330127 +1.43614066j]
    [ 2.16506351+1.43614066j -0.4330127 +1.43614066j]