Conflicting eigen vector outputs between Matlab and Numpy
Asked Answered
G

1

8

I am calculating eigenvectors in Matlab and Numpy, but getting different results. I was under the impression there was only one set of eigenvectors for a given matrix, however both of these outputs seem valid.

Here is my matlab code:

m = [  1.4675 + 0.0000i   0.1669 + 1.2654i;
       0.1669 - 1.2654i   1.3085 + 0.0000i]
[eig_vec,eig_val] = eig(m)

eig_val contains:

eig_val =
     0.1092         0
          0    2.6668

eig_vec contains:

eig_vec =
      0.0896 + 0.6789i   0.0953 + 0.7225i
     -0.7288 + 0.0000i   0.6848 + 0.0000i

Here is my python code:

m = np.array([[1.46753694+0.j,         0.16692111+1.26535838j],
              [0.16692111-1.26535838j, 1.30851770+0.j]])
eig_val,eig_vec = linalg.eigh(m)

eig_val contains:

array([ 0.10923247,  2.66682217])

eig_vec contains:

array([[-0.68477170+0.j        , -0.72875765+0.j        ],
       [ 0.09530915-0.72249836j, -0.08955653+0.67889021j]])

Can anyone explain why these outputs are different, it seems like each the two different sets of eigenvectors are rotated versions of each other. Is one set more correct that the other?

Gastrula answered 18/9, 2013 at 23:51 Comment(2)
eigenvectors are not unique: https://mcmap.net/q/671996/-matlab-eig-returns-inverted-signs-sometimes, but I think both MATLAB and NumPy rely on the same LAPACK routines to compute them, so so you will likely get similar results.Eshelman
See this older question for more reading on the non-uniqueness of eigenvectors (it relates to matlab versus mathematica, but is otherwise essentially a duplicate question)...Unlisted
P
15

It's not immediately obvious, but the eigenvectors you are being returned are actually the same in both cases. Try the following:

>>> matlab_eigvec = np.array([[0.0896+0.6789j, 0.0953+0.7225j],
...                           [-0.7288+0.j, 0.6848+0.j]])
>>> 
>>> f1, f2 = matlab_eigvec.T # matlab eigenvectors
>>> e1, e2 = eig_vec.T # numpy eigenvectors
>>> f1/e1
array([-0.13084653-0.99142531j, -0.13079065-0.99146862j])
>>> f2/e2
array([-0.13077050-0.99141326j, -0.13078845-0.99145198j])

So you can get the matlab eigenvectors by multiplying the numpy ones by -0.13-0.99j, i.e. they are colinear and therefore the same as far as eigenvectors are concerned.

Polymyxin answered 19/9, 2013 at 0:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.