Numpy giving wrong Eigenvectors
Question:
from numpy.linalg import eig
import numpy as np
A = np.array([[1,2],[3,4]])
eval, evec = eig(A)
print("Eigenvectors:", evec)
gives:
Eigenvectors: [[-0.82456484 -0.41597356]
[ 0.56576746 -0.90937671]]
while in mathematica:
A = {{1, 2}, {3, 4}};
{eval, evec} = Eigensystem[A];
N[evec]
gives:
{{0.457427, 1.}, {-1.45743, 1.}}
And I know the mathematica one is correct because it gives the correct graph of the wavefuntion calculated using eigenvectors.
And this is not just a matter of normalization because the numpy result cannot be scaled by a factor to get the correct result.(there are two 1s in mathematica result but there are no two same numbers in our numpy code)
There is a workaround using sympy,but What is the problem and is there a way to get correct eigen vectors in numpy itself?
Answers:
Both computations are correct. Numpy gives normalized eigenvectors of length 1. Divide each column of the numpy eigenvector matrix by the second number in the column to get the vectors computed by Mathematica.
import numpy as np
A = np.array([[1,2],[3,4]])
vals, vects = np.linalg.eig(A)
print(vects)
It gives:
[[-0.82456484 -0.41597356]
[ 0.56576746 -0.90937671]]
Divide by the last row:
print(vects/vects[-1])
Result:
[[-1.45742711 0.45742711]
[ 1. 1. ]]
from numpy.linalg import eig
import numpy as np
A = np.array([[1,2],[3,4]])
eval, evec = eig(A)
print("Eigenvectors:", evec)
gives:
Eigenvectors: [[-0.82456484 -0.41597356]
[ 0.56576746 -0.90937671]]
while in mathematica:
A = {{1, 2}, {3, 4}};
{eval, evec} = Eigensystem[A];
N[evec]
gives:
{{0.457427, 1.}, {-1.45743, 1.}}
And I know the mathematica one is correct because it gives the correct graph of the wavefuntion calculated using eigenvectors.
And this is not just a matter of normalization because the numpy result cannot be scaled by a factor to get the correct result.(there are two 1s in mathematica result but there are no two same numbers in our numpy code)
There is a workaround using sympy,but What is the problem and is there a way to get correct eigen vectors in numpy itself?
Both computations are correct. Numpy gives normalized eigenvectors of length 1. Divide each column of the numpy eigenvector matrix by the second number in the column to get the vectors computed by Mathematica.
import numpy as np
A = np.array([[1,2],[3,4]])
vals, vects = np.linalg.eig(A)
print(vects)
It gives:
[[-0.82456484 -0.41597356]
[ 0.56576746 -0.90937671]]
Divide by the last row:
print(vects/vects[-1])
Result:
[[-1.45742711 0.45742711]
[ 1. 1. ]]