0

Say I have the following matrix Pi in Matlab where I need to perform the following code

Pi = [0.2 0.2 0.15; 0.1 0.15 0.48; 0.9 0.96 0.9]
Pia = .5*(Pi-Pi')*1i;
[W,D] = eig(Pia,'vector');

eigenvalues D are [-0.4480 -0.0000 0.4480] and the eigenvector matrix W is

   0.0425 + 0.5956i   0.5357 + 0.0000i   0.0425 - 0.5956i
  -0.0665 + 0.3812i  -0.8370 + 0.0000i  -0.0665 - 0.3812i
   0.7027 + 0.0000i  -0.1116 + 0.0000i   0.7027 + 0.0000i

I translate this into python like following:

Pi = np.array([[0.2, 0.2, 0.15], [0.1, 0.15, 0.48],[0.9, 0.96, 0.9]])
Piap = 0.5*(Pi-Pi.T)*(0+1j)
Dp,Wp   = np.linalg.eigh(Piap)

While the eigenvalues are exactly the same, the eigenvector matrix Wp result

array([[-0.59709305+0.00000000e+00j,-0.535686260.00000000e+00j,-0.59709305+0.00000000e+00j],
       [-0.37546463-9.34538627e-02j,0.83700978+5.55111512e-17j,-0.37546463+9.34538627e-2j],
       [-0.05006195+7.00903970e-01j,0.1116013 -1.18169661e-16j,-0.05006195-7.00903970e-1j]])

I know eigenvectors are the same up to a sign and scaling constant, but here the two results do not coincide at all. In particular, while the second column is the same in both cases, the first (which is the same as the third) is very different. I tried to see if there was a sort of normalization comparing the first column of W with the one of Wp, but the ratios across the two cases for the same row are not constant. And providede there is a normlization in python, this must be row-wise as the thirs column gives the same results. What to do here?

user9875321__
  • 195
  • 12

2 Answers2

1

From Matlab's eig documentation (emphasis mine):

Different machines and releases of MATLAB can produce different eigenvectors that are still numerically accurate. [...] For complex eigenvectors, the eigenvectors can be multiplied by any complex number of magnitude 1.

This is acceptable since any multiple of an eigenvector is also an eigenvector, associated to the same eigenvalue. And this is precisely what is happening between Matlab and Python here. Note first that you have some typo in your Wp in Python. The actual result is

[[-0.59709305+0.00000000e+00j  0.53568626+0.00000000e+00j -0.59709305+0.00000000e+00j]
 [-0.37546463-9.34538627e-02j -0.83700978-1.24900090e-16j -0.37546463+9.34538627e-02j]
 [-0.05006195+7.00903970e-01j -0.1116013 +0.00000000e+00j -0.05006195-7.00903970e-01j]]

With this correction, element-wise division of Matlab's W by Python's Wp gives

>> Pi = [0.2 0.2 0.15; 0.1 0.15 0.48; 0.9 0.96 0.9];
Pia = .5*(Pi-Pi')*1i;
[W,D] = eig(Pia,'vector');
>> Wp = [-0.59709305+0.00000000e+00j  0.53568626+0.00000000e+00j -0.59709305+0.00000000e+00j;
         -0.37546463-9.34538627e-02j -0.83700978-1.24900090e-16j -0.37546463+9.34538627e-02j;
         -0.05006195+7.00903970e-01j -0.1116013+0.00000000e+00j  -0.05006195-7.00903970e-01j];
>> W./Wp
ans =
  -0.0712 - 0.9975i   1.0000 - 0.0000i  -0.0712 + 0.9975i
  -0.0712 - 0.9975i   1.0000 - 0.0000i  -0.0712 + 0.9975i
  -0.0712 - 0.9975i   1.0000 + 0.0000i  -0.0712 + 0.9975i

You can see that each column in W is a multiple of the corresponding column in Wp, up to numerical precision inacccuracies.

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • I could not find what typo you are referring to in the code, but I see your point. Then I guess it is just matlab vs python and there is no way to say which one is less arbitrary than the other as they are both correct. Thanks for clarifying this โ€“ user9875321__ Mar 01 '23 at 09:12
  • The typo is in first row, second column: `-0.535686260.00000000e+00j` should be `0.53568626+0.00000000e+00j`. (There might be others, I haven't checked) โ€“ Luis Mendo Mar 01 '23 at 10:31
0

eigenvector magnitudes match

In [186]: np.abs(Wp)
Out[186]: 
array([[0.59709305, 0.53568626, 0.59709305],
       [0.38692029, 0.83700978, 0.38692029],
       [0.70268953, 0.1116013 , 0.70268953]])

and from Octave

โน›>> abs(W)
ans =

   0.5971   0.5357   0.5971
   0.3869   0.8370   0.3869
   0.7027   0.1116   0.7027

The 2nd column match you noted is for a column that has negligible imaginary values.

hpaulj
  • 221,503
  • 14
  • 230
  • 353