2
votes

First, don't be fooled by the long post, there is not a lot of code just an observation of results so there are few example matrices.

This is a bit related to this question: Matlab Codegen Eig Function - Is this a Bug?

I know that mex/C/C++ translated eig() function may not return the same eigenvectors when using the same function in MATLAB and that's fine, but i am puzzled with results I'm getting.

First this simple example:

Output
% c = diagonal matrix of eigenvalues
% b = matrix whose columns are the corresponding right eigenvectors

function [ b, c ] = eig_test(a)
    [b, c] = eig(a);
end

Running as is for [b,c] = eig_test(magic(5)) this returns:

b =
-0.4472    0.0976   -0.6330    0.6780   -0.2619
-0.4472    0.3525    0.5895    0.3223   -0.1732
-0.4472    0.5501   -0.3915   -0.5501    0.3915
-0.4472   -0.3223    0.1732   -0.3525   -0.5895
-0.4472   -0.6780    0.2619   -0.0976    0.6330

c =
65.0000         0         0         0         0
      0  -21.2768         0         0         0
      0         0  -13.1263         0         0
      0         0         0   21.2768         0
      0         0         0         0   13.1263

Translating that to mex function and running eig_test_mex(magic(5)) returns:

b =
0.4472 + 0.0000i   0.0976 + 0.0000i  -0.6330 + 0.0000i   0.6780 + 0.0000i  -0.2619 + 0.0000i
0.4472 + 0.0000i   0.3525 + 0.0000i   0.5895 + 0.0000i   0.3223 + 0.0000i  -0.1732 + 0.0000i
0.4472 + 0.0000i   0.5501 + 0.0000i  -0.3915 + 0.0000i  -0.5501 + 0.0000i   0.3915 + 0.0000i
0.4472 + 0.0000i  -0.3223 + 0.0000i   0.1732 + 0.0000i  -0.3525 + 0.0000i  -0.5895 + 0.0000i
0.4472 + 0.0000i  -0.6780 + 0.0000i   0.2619 + 0.0000i  -0.0976 + 0.0000i   0.6330 + 0.0000i


c =
65.0000 + 0.0000i   0.0000 + 0.0000i   0.0000 + 0.0000i   0.0000 + 0.0000i   0.0000 + 0.0000i
0.0000 + 0.0000i -21.2768 + 0.0000i   0.0000 + 0.0000i   0.0000 + 0.0000i   0.0000 + 0.0000i
0.0000 + 0.0000i   0.0000 + 0.0000i -13.1263 + 0.0000i   0.0000 + 0.0000i   0.0000 + 0.0000i
0.0000 + 0.0000i   0.0000 + 0.0000i   0.0000 + 0.0000i  21.2768 + 0.0000i   0.0000 + 0.0000i
0.0000 + 0.0000i   0.0000 + 0.0000i   0.0000 + 0.0000i   0.0000 + 0.0000i  13.1263 + 0.0000i

Now here the values are actually the same (with exception of first vector that differs only in - sign) but from where are these complex imaginary parts coming from?

Second example:

Using the same function as above, the input matrix is now this one below instead of magic(5):

input_matrix =
 0.0440   -0.0000    0.0486    0.0752    0.0848    0.0881    0.0883    0.0874    0.0856    0.0832    0.0805    0.0775    0.0742    0.0709    0.0676    0.0644    0.0612    0.0580    0.0548    0.0518    0.0487
-0.0000         0   -0.0000   -0.0000         0   -0.0000   -0.0000         0         0   -0.0000   -0.0000   -0.0000         0         0   -0.0000   -0.0000         0         0         0   -0.0000         0
 0.0486   -0.0000    0.1253    0.1231    0.1128    0.1028    0.0940    0.0867    0.0803    0.0747    0.0697    0.0651    0.0609    0.0570    0.0535    0.0503    0.0473    0.0446    0.0421    0.0397    0.0375
 0.0752   -0.0000    0.1231    0.3049    0.2850    0.2641    0.2454    0.2297    0.2157    0.2034    0.1924    0.1820    0.1723    0.1636    0.1556    0.1482    0.1414    0.1351    0.1292    0.1237    0.1186
 0.0848         0    0.1128    0.2850    0.3941    0.3685    0.3451    0.3253    0.3077    0.2921    0.2780    0.2646    0.2521    0.2407    0.2303    0.2207    0.2119    0.2036    0.1959    0.1887    0.1819
 0.0881   -0.0000    0.1028    0.2641    0.3685    0.4420    0.4161    0.3943    0.3746    0.3571    0.3413    0.3262    0.3120    0.2990    0.2871    0.2762    0.2661    0.2566    0.2478    0.2396    0.2318
 0.0883   -0.0000    0.0940    0.2454    0.3451    0.4161    0.4705    0.4474    0.4266    0.4079    0.3910    0.3748    0.3595    0.3455    0.3326    0.3208    0.3098    0.2996    0.2900    0.2811    0.2726
 0.0874         0    0.0867    0.2297    0.3253    0.3943    0.4474    0.4918    0.4701    0.4507    0.4330    0.4160    0.3999    0.3851    0.3716    0.3591    0.3474    0.3366    0.3265    0.3170    0.3080
 0.0856         0    0.0803    0.2157    0.3077    0.3746    0.4266    0.4701    0.5067    0.4868    0.4686    0.4511    0.4343    0.4190    0.4049    0.3919    0.3798    0.3686    0.3580    0.3481    0.3387
 0.0832   -0.0000    0.0747    0.2034    0.2921    0.3571    0.4079    0.4507    0.4868    0.5182    0.4997    0.4817    0.4645    0.4487    0.4343    0.4209    0.4084    0.3968    0.3859    0.3757    0.3660
 0.0805   -0.0000    0.0697    0.1924    0.2780    0.3413    0.3910    0.4330    0.4686    0.4997    0.5269    0.5087    0.4911    0.4751    0.4603    0.4466    0.4339    0.4220    0.4108    0.4003    0.3905
 0.0775   -0.0000    0.0651    0.1820    0.2646    0.3262    0.3748    0.4160    0.4511    0.4817    0.5087    0.5317    0.5140    0.4977    0.4827    0.4688    0.4559    0.4438    0.4325    0.4218    0.4117
 0.0742         0    0.0609    0.1723    0.2521    0.3120    0.3595    0.3999    0.4343    0.4645    0.4911    0.5140    0.5337    0.5173    0.5021    0.4881    0.4750    0.4628    0.4514    0.4406    0.4304
 0.0709         0    0.0570    0.1636    0.2407    0.2990    0.3455    0.3851    0.4190    0.4487    0.4751    0.4977    0.5173    0.5351    0.5199    0.5057    0.4926    0.4803    0.4687    0.4578    0.4475
 0.0676   -0.0000    0.0535    0.1556    0.2303    0.2871    0.3326    0.3716    0.4049    0.4343    0.4603    0.4827    0.5021    0.5199    0.5362    0.5220    0.5087    0.4963    0.4847    0.4737    0.4634
 0.0644   -0.0000    0.0503    0.1482    0.2207    0.2762    0.3208    0.3591    0.3919    0.4209    0.4466    0.4688    0.4881    0.5057    0.5220    0.5370    0.5237    0.5112    0.4995    0.4885    0.4781
 0.0612         0    0.0473    0.1414    0.2119    0.2661    0.3098    0.3474    0.3798    0.4084    0.4339    0.4559    0.4750    0.4926    0.5087    0.5237    0.5376    0.5251    0.5134    0.5023    0.4918
 0.0580         0    0.0446    0.1351    0.2036    0.2566    0.2996    0.3366    0.3686    0.3968    0.4220    0.4438    0.4628    0.4803    0.4963    0.5112    0.5251    0.5381    0.5263    0.5152    0.5047
 0.0548         0    0.0421    0.1292    0.1959    0.2478    0.2900    0.3265    0.3580    0.3859    0.4108    0.4325    0.4514    0.4687    0.4847    0.4995    0.5134    0.5263    0.5384    0.5273    0.5167
 0.0518   -0.0000    0.0397    0.1237    0.1887    0.2396    0.2811    0.3170    0.3481    0.3757    0.4003    0.4218    0.4406    0.4578    0.4737    0.4885    0.5023    0.5152    0.5273    0.5387    0.5281
 0.0487         0    0.0375    0.1186    0.1819    0.2318    0.2726    0.3080    0.3387    0.3660    0.3905    0.4117    0.4304    0.4475    0.4634    0.4781    0.4918    0.5047    0.5167    0.5281    0.5389

Running for [b,c] = eig_test(input_matrix) returns:

b =
 0.0000   -0.0085   -0.0117   -0.0166   -0.0251   -0.0442    0.1334    0.9260   -0.1493    0.0548   -0.0283    0.0382    0.0170    0.0977    0.1285   -0.1697    0.1100   -0.1149    0.0635    0.0881    0.0424
 1.0000   -0.0000    0.0000   -0.0000    0.0000   -0.0000   -0.0000   -0.0000    0.0000   -0.0000   -0.0000    0.0000    0.0000    0.0000   -0.0000    0.0000   -0.0000    0.0000   -0.0000   -0.0000   -0.0000
-0.0000    0.0020    0.0029    0.0042    0.0067    0.0125   -0.0404   -0.2919    0.0495   -0.0178    0.0219    0.0179    0.1086    0.2330    0.4679   -0.5139    0.4286   -0.3220    0.2241    0.1454    0.0392
 0.0000    0.0001    0.0001    0.0002    0.0003    0.0005   -0.0010   -0.0102   -0.0017   -0.0143   -0.0484   -0.1305   -0.2829   -0.4495   -0.4237    0.0949    0.2592   -0.3986    0.4161    0.3145    0.1072
-0.0000    0.0001    0.0002    0.0003    0.0006    0.0005   -0.0076   -0.0267    0.0241    0.0641    0.1763    0.3391    0.4702    0.3538   -0.0858    0.3847   -0.2103   -0.1113    0.3607    0.3698    0.1531
 0.0000    0.0001    0.0001    0.0003   -0.0001    0.0044    0.0173   -0.0280   -0.0740   -0.2070   -0.3832   -0.4693   -0.2586    0.1790    0.3224    0.0909   -0.3636    0.1799    0.2003    0.3654    0.1849
 0.0000    0.0001    0.0001   -0.0000    0.0029   -0.0168   -0.0758    0.0155    0.2178    0.4038    0.4549    0.1676   -0.2686   -0.2836    0.1914   -0.2398   -0.2029    0.3295    0.0210    0.3288    0.2079
-0.0000    0.0001    0.0000    0.0015   -0.0114    0.0623    0.1958   -0.0857   -0.3987   -0.4488   -0.1154    0.3136    0.2223   -0.2549   -0.1457   -0.2976    0.0628    0.3254   -0.1323    0.2755    0.2255
-0.0000    0.0001    0.0005   -0.0058    0.0419   -0.1658   -0.3782    0.0966    0.4538    0.1163   -0.3266   -0.1826    0.2893    0.0922   -0.2891   -0.1140    0.2566    0.2114   -0.2394    0.2128    0.2385
 0.0000    0.0001   -0.0020    0.0224   -0.1169    0.3338    0.4695   -0.1012   -0.1655    0.3142    0.1810   -0.3027   -0.0583    0.2923   -0.1581    0.1266    0.3013    0.0487   -0.2951    0.1468    0.2482
-0.0000   -0.0003    0.0085   -0.0675    0.2572   -0.4725   -0.2826   -0.0124   -0.2658   -0.2251    0.2927    0.0580   -0.2952    0.1782    0.0838    0.2657    0.2098   -0.1089   -0.3025    0.0811    0.2552
 0.0000    0.0016   -0.0282    0.1667   -0.4258    0.4018   -0.1483    0.0743    0.2952   -0.2511   -0.1046    0.3011   -0.1748   -0.0861    0.2409    0.2463    0.0445   -0.2242   -0.2699    0.0183    0.2593
-0.0000   -0.0060    0.0796   -0.3248    0.4873   -0.0371    0.3582   -0.0471    0.1556    0.1904   -0.3026    0.1397    0.1135   -0.2564    0.2258    0.1072   -0.1212   -0.2793   -0.2087   -0.0393    0.2613
-0.0000    0.0202   -0.1839    0.4791   -0.2780   -0.3330   -0.0369   -0.0733   -0.2823    0.2701   -0.0569   -0.1694    0.2707   -0.2224    0.0779   -0.0674   -0.2333   -0.2741   -0.1307   -0.0908    0.2618
 0.0000   -0.0569    0.3438   -0.4801   -0.1422    0.2530   -0.3217    0.0344   -0.1598   -0.0783    0.2433   -0.2745    0.1829   -0.0400   -0.1011   -0.1994   -0.2669   -0.2193   -0.0462   -0.1354    0.2611
 0.0000    0.1366   -0.4999    0.2085    0.3815    0.1931    0.0665    0.0563    0.2298   -0.2886    0.2299   -0.1025   -0.0438    0.1536   -0.2162   -0.2443   -0.2244   -0.1314    0.0362   -0.1730    0.2594
-0.0000   -0.2750    0.5187    0.2125   -0.1234   -0.2906    0.3002   -0.0330    0.2275   -0.0976   -0.0389    0.1510   -0.2225    0.2429   -0.2221   -0.1981   -0.1272   -0.0288    0.1102   -0.2036    0.2569
 0.0000    0.4544   -0.2791   -0.3911   -0.2949   -0.1393   -0.0141   -0.0736   -0.1126    0.2031   -0.2494    0.2586   -0.2356    0.1924   -0.1303   -0.0881   -0.0057    0.0716    0.1710   -0.2273    0.2536
-0.0000   -0.5901   -0.1434    0.0927    0.2187    0.2750   -0.2874   -0.0090   -0.2727    0.2455   -0.2023    0.1516   -0.0947    0.0451    0.0094    0.0433    0.1096    0.1561    0.2158   -0.2445    0.2496
 0.0000    0.5431    0.4134    0.3143    0.2317    0.1604   -0.0958    0.0563   -0.0617    0.0119    0.0294   -0.0645    0.0962   -0.1161    0.1372    0.1548    0.1950    0.2157    0.2434   -0.2554    0.2451
-0.0000   -0.2286   -0.2287   -0.2288   -0.2283   -0.2265    0.2263    0.0338    0.2163   -0.2220    0.2231   -0.2228    0.2210   -0.2154    0.2075    0.2171    0.2365    0.2458    0.2538   -0.2607    0.2401


c =
-0.0000         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0
      0    0.0063         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0
      0         0    0.0076         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0
      0         0         0    0.0089         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0
      0         0         0         0    0.0105         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0
      0         0         0         0         0    0.0123         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0
      0         0         0         0         0         0    0.0144         0         0         0         0         0         0         0         0         0         0         0         0         0         0
      0         0         0         0         0         0         0    0.0157         0         0         0         0         0         0         0         0         0         0         0         0         0
      0         0         0         0         0         0         0         0    0.0171         0         0         0         0         0         0         0         0         0         0         0         0
      0         0         0         0         0         0         0         0         0    0.0203         0         0         0         0         0         0         0         0         0         0         0
      0         0         0         0         0         0         0         0         0         0    0.0244         0         0         0         0         0         0         0         0         0         0
      0         0         0         0         0         0         0         0         0         0         0    0.0298         0         0         0         0         0         0         0         0         0
      0         0         0         0         0         0         0         0         0         0         0         0    0.0368         0         0         0         0         0         0         0         0
      0         0         0         0         0         0         0         0         0         0         0         0         0    0.0464         0         0         0         0         0         0         0
      0         0         0         0         0         0         0         0         0         0         0         0         0         0    0.0581         0         0         0         0         0         0
      0         0         0         0         0         0         0         0         0         0         0         0         0         0         0    0.0762         0         0         0         0         0
      0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0    0.1063         0         0         0         0
      0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0    0.1746         0         0         0
      0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0    0.3324         0         0
      0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0    1.0781         0
      0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0         0    7.1136

And running mex version for [b,c] = eig_test_mex(input_matrix) returns:

b=
0.0424+0.0000i  0.0881+0.0000i  0.0635+0.0000i  -0.1149+0.0000i 0.11+0.0000i    -0.1697+0.0000i 0.1285+0.0000i  0.0977+0.0000i  0.017+0.0000i   0.0382+0.0000i  -0.0283+0.0000i 0.0548+0.0000i  0.1493+0.0000i  -0.926+0.0000i  -0.1334+0.0000i 0.0085+0.0000i  -0.0442+0.0000i -0.0117+0.0000i -0.0251+0.0000i -0.0166+0.0000i 0+0.0000i
+0.0000i        0+0.0000i       0+0.0000i       0+0.0000i       0+0.0000i       0+0.0000i       0+0.0000i       0+0.0000i       0+0.0000i       0+0.0000i       0+0.0000i       0+0.0000i       0+0.0000i       0+0.0000i       0+0.0000i       0+0.0000i       0+0.0000i       0+0.0000i       0+0.0000i       0+0.0000i       -1+0.0000i
0.0392+0.0000i  0.1454+0.0000i  0.2241+0.0000i  -0.322+0.0000i  0.4286+0.0000i  -0.5139+0.0000i 0.4679+0.0000i  0.233+0.0000i   0.1086+0.0000i  0.0179+0.0000i  0.0219+0.0000i  -0.0178+0.0000i -0.0495+0.0000i 0.2919+0.0000i  0.0404+0.0000i  -0.002+0.0000i  0.0125+0.0000i  0.0029+0.0000i  0.0067+0.0000i  0.0042+0.0000i  0+0.0000i
0.1072+0.0000i  0.3145+0.0000i  0.4161+0.0000i  -0.3986+0.0000i 0.2592+0.0000i  0.0949+0.0000i  -0.4237+0.0000i -0.4495+0.0000i -0.2829+0.0000i -0.1305+0.0000i -0.0484+0.0000i -0.0143+0.0000i 0.0017+0.0000i  0.0102+0.0000i  0.001+0.0000i   -0.0001+0.0000i 0.0005+0.0000i  0.0001+0.0000i  0.0003+0.0000i  0.0002+0.0000i  0+0.0000i
0.1531+0.0000i  0.3698+0.0000i  0.3607+0.0000i  -0.1113+0.0000i -0.2103+0.0000i 0.3847+0.0000i  -0.0858+0.0000i 0.3538+0.0000i  0.4702+0.0000i  0.3391+0.0000i  0.1763+0.0000i  0.0641+0.0000i  -0.0241+0.0000i 0.0267+0.0000i  0.0076+0.0000i  -0.0001+0.0000i 0.0005+0.0000i  0.0002+0.0000i  0.0006+0.0000i  0.0003+0.0000i  0+0.0000i
0.1849+0.0000i  0.3654+0.0000i  0.2003+0.0000i  0.1799+0.0000i  -0.3636+0.0000i 0.0909+0.0000i  0.3224+0.0000i  0.179+0.0000i   -0.2586+0.0000i -0.4693+0.0000i -0.3832+0.0000i -0.207+0.0000i  0.074+0.0000i   0.028+0.0000i   -0.0173+0.0000i -0.0001+0.0000i 0.0044+0.0000i  0.0001+0.0000i  -0.0001+0.0000i 0.0003+0.0000i  0+0.0000i
0.2079+0.0000i  0.3288+0.0000i  0.021+0.0000i   0.3295+0.0000i  -0.2029+0.0000i -0.2398+0.0000i 0.1914+0.0000i  -0.2836+0.0000i -0.2686+0.0000i 0.1676+0.0000i  0.4549+0.0000i  0.4038+0.0000i  -0.2178+0.0000i -0.0155+0.0000i 0.0758+0.0000i  -0.0001+0.0000i -0.0168+0.0000i 0.0001+0.0000i  0.0029+0.0000i  0+0.0000i       0+0.0000i
0.2255+0.0000i  0.2755+0.0000i  -0.1323+0.0000i 0.3254+0.0000i  0.0628+0.0000i  -0.2976+0.0000i -0.1457+0.0000i -0.2549+0.0000i 0.2223+0.0000i  0.3136+0.0000i  -0.1154+0.0000i -0.4488+0.0000i 0.3987+0.0000i  0.0857+0.0000i  -0.1958+0.0000i -0.0001+0.0000i 0.0623+0.0000i  0+0.0000i       -0.0114+0.0000i 0.0015+0.0000i  0+0.0000i
0.2385+0.0000i  0.2128+0.0000i  -0.2394+0.0000i 0.2114+0.0000i  0.2566+0.0000i  -0.114+0.0000i  -0.2891+0.0000i 0.0922+0.0000i  0.2893+0.0000i  -0.1826+0.0000i -0.3266+0.0000i 0.1163+0.0000i  -0.4538+0.0000i -0.0966+0.0000i 0.3782+0.0000i  -0.0001+0.0000i -0.1658+0.0000i 0.0005+0.0000i  0.0419+0.0000i  -0.0058+0.0000i 0+0.0000i
0.2482+0.0000i  0.1468+0.0000i  -0.2951+0.0000i 0.0487+0.0000i  0.3013+0.0000i  0.1266+0.0000i  -0.1581+0.0000i 0.2923+0.0000i  -0.0583+0.0000i -0.3027+0.0000i 0.181+0.0000i   0.3142+0.0000i  0.1655+0.0000i  0.1012+0.0000i  -0.4695+0.0000i -0.0001+0.0000i 0.3338+0.0000i  -0.002+0.0000i  -0.1169+0.0000i 0.0224+0.0000i  0+0.0000i
0.2552+0.0000i  0.0811+0.0000i  -0.3025+0.0000i -0.1089+0.0000i 0.2098+0.0000i  0.2657+0.0000i  0.0838+0.0000i  0.1782+0.0000i  -0.2952+0.0000i 0.058+0.0000i   0.2927+0.0000i  -0.2251+0.0000i 0.2658+0.0000i  0.0124+0.0000i  0.2826+0.0000i  0.0003+0.0000i  -0.4725+0.0000i 0.0085+0.0000i  0.2572+0.0000i  -0.0675+0.0000i 0+0.0000i
0.2593+0.0000i  0.0183+0.0000i  -0.2699+0.0000i -0.2242+0.0000i 0.0445+0.0000i  0.2463+0.0000i  0.2409+0.0000i  -0.0861+0.0000i -0.1748+0.0000i 0.3011+0.0000i  -0.1046+0.0000i -0.2511+0.0000i -0.2952+0.0000i -0.0743+0.0000i 0.1483+0.0000i  -0.0016+0.0000i 0.4018+0.0000i  -0.0282+0.0000i -0.4258+0.0000i 0.1667+0.0000i  0+0.0000i
0.2613+0.0000i  -0.0393+0.0000i -0.2087+0.0000i -0.2793+0.0000i -0.1212+0.0000i 0.1072+0.0000i  0.2258+0.0000i  -0.2564+0.0000i 0.1135+0.0000i  0.1397+0.0000i  -0.3026+0.0000i 0.1904+0.0000i  -0.1556+0.0000i 0.0471+0.0000i  -0.3582+0.0000i 0.006+0.0000i   -0.0371+0.0000i 0.0796+0.0000i  0.4873+0.0000i  -0.3248+0.0000i 0+0.0000i
0.2618+0.0000i  -0.0908+0.0000i -0.1307+0.0000i -0.2741+0.0000i -0.2333+0.0000i -0.0674+0.0000i 0.0779+0.0000i  -0.2224+0.0000i 0.2707+0.0000i  -0.1694+0.0000i -0.0569+0.0000i 0.2701+0.0000i  0.2823+0.0000i  0.0733+0.0000i  0.0369+0.0000i  -0.0202+0.0000i -0.333+0.0000i  -0.1839+0.0000i -0.278+0.0000i  0.4791+0.0000i  0+0.0000i
0.2611+0.0000i  -0.1354+0.0000i -0.0462+0.0000i -0.2193+0.0000i -0.2669+0.0000i -0.1994+0.0000i -0.1011+0.0000i -0.04+0.0000i   0.1829+0.0000i  -0.2745+0.0000i 0.2433+0.0000i  -0.0783+0.0000i 0.1598+0.0000i  -0.0344+0.0000i 0.3217+0.0000i  0.0569+0.0000i  0.253+0.0000i   0.3438+0.0000i  -0.1422+0.0000i -0.4801+0.0000i 0+0.0000i
0.2594+0.0000i  -0.173+0.0000i  0.0362+0.0000i  -0.1314+0.0000i -0.2244+0.0000i -0.2443+0.0000i -0.2162+0.0000i 0.1536+0.0000i  -0.0438+0.0000i -0.1025+0.0000i 0.2299+0.0000i  -0.2886+0.0000i -0.2298+0.0000i -0.0563+0.0000i -0.0665+0.0000i -0.1366+0.0000i 0.1931+0.0000i  -0.4999+0.0000i 0.3815+0.0000i  0.2085+0.0000i  0+0.0000i
0.2569+0.0000i  -0.2036+0.0000i 0.1102+0.0000i  -0.0288+0.0000i -0.1272+0.0000i -0.1981+0.0000i -0.2221+0.0000i 0.2429+0.0000i  -0.2225+0.0000i 0.151+0.0000i   -0.0389+0.0000i -0.0976+0.0000i -0.2275+0.0000i 0.033+0.0000i   -0.3002+0.0000i 0.275+0.0000i   -0.2906+0.0000i 0.5187+0.0000i  -0.1234+0.0000i 0.2125+0.0000i  0+0.0000i
0.2536+0.0000i  -0.2273+0.0000i 0.171+0.0000i   0.0716+0.0000i  -0.0057+0.0000i -0.0881+0.0000i -0.1303+0.0000i 0.1924+0.0000i  -0.2356+0.0000i 0.2586+0.0000i  -0.2494+0.0000i 0.2031+0.0000i  0.1126+0.0000i  0.0736+0.0000i  0.0141+0.0000i  -0.4544+0.0000i -0.1393+0.0000i -0.2791+0.0000i -0.2949+0.0000i -0.3911+0.0000i 0+0.0000i
0.2496+0.0000i  -0.2445+0.0000i 0.2158+0.0000i  0.1561+0.0000i  0.1096+0.0000i  0.0433+0.0000i  0.0094+0.0000i  0.0451+0.0000i  -0.0947+0.0000i 0.1516+0.0000i  -0.2023+0.0000i 0.2455+0.0000i  0.2727+0.0000i  0.009+0.0000i   0.2874+0.0000i  0.5901+0.0000i  0.275+0.0000i   -0.1434+0.0000i 0.2187+0.0000i  0.0927+0.0000i  0+0.0000i
0.2451+0.0000i  -0.2554+0.0000i 0.2434+0.0000i  0.2157+0.0000i  0.195+0.0000i   0.1548+0.0000i  0.1372+0.0000i  -0.1161+0.0000i 0.0962+0.0000i  -0.0645+0.0000i 0.0294+0.0000i  0.0119+0.0000i  0.0617+0.0000i  -0.0563+0.0000i 0.0958+0.0000i  -0.5431+0.0000i 0.1604+0.0000i  0.4134+0.0000i  0.2317+0.0000i  0.3143+0.0000i  0+0.0000i
0.2401+0.0000i  -0.2607+0.0000i 0.2538+0.0000i  0.2458+0.0000i  0.2365+0.0000i  0.2171+0.0000i  0.2075+0.0000i  -0.2154+0.0000i 0.221+0.0000i   -0.2228+0.0000i 0.2231+0.0000i  -0.222+0.0000i  -0.2163+0.0000i -0.0338+0.0000i -0.2263+0.0000i 0.2286+0.0000i  -0.2265+0.0000i -0.2287+0.0000i -0.2283+0.0000i -0.2288+0.0000i 0+0.0000i

c=
7.1136   0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0
0        1.0781   0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0
0        0        0.3324   0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0
0        0        0        0.1746   0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0
0        0        0        0        0.1063   0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0
0        0        0        0        0        0.0762   0        0        0        0        0        0        0        0        0        0        0        0        0        0        0
0        0        0        0        0        0        0.0581   0        0        0        0        0        0        0        0        0        0        0        0        0        0
0        0        0        0        0        0        0        0.0464   0        0        0        0        0        0        0        0        0        0        0        0        0
0        0        0        0        0        0        0        0        0.0368   0        0        0        0        0        0        0        0        0        0        0        0
0        0        0        0        0        0        0        0        0        0.0298   0        0        0        0        0        0        0        0        0        0        0
0        0        0        0        0        0        0        0        0        0        0.0244   0        0        0        0        0        0        0        0        0        0
0        0        0        0        0        0        0        0        0        0        0        0.0203   0        0        0        0        0        0        0        0        0
0        0        0        0        0        0        0        0        0        0        0        0        0.0171   0        0        0        0        0        0        0        0
0        0        0        0        0        0        0        0        0        0        0        0        0        0.0157   0        0        0        0        0        0        0
0        0        0        0        0        0        0        0        0        0        0        0        0        0        0.0144   0        0        0        0        0        0
0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0.0063   0        0        0        0        0
0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0.0123   0        0        0        0
0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0.0076   0        0        0
0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0.0105   0        0
0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0.0089   0
0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0

Ok, so the complex imaginary part is here again (i removed it from matrix c for clarity) but notice something. In eigenvalue matrix c there are the same values again but in reverse order. Why is that? At first glance is seems that values in b matrices are also the same just columns are reversed, but there are some differences. But as i understand this is ok.

Finally

I'm confused manly with two things. One is this transformation to complex numbers and the other one is this reverse ordering.

Does anyone have some insight on whats going on. Could first phenomena be resolved with updated MATLAB version or tool-chain? (I'm using MATLAB 2013, GCC 4.6 on Ubuntu 12.04). Second phenomena can probably be explained with math and implemented algorithms to compute eigenvectors/values. Any possible explanation?

1
I think the complex matrix is the eigen-vector, not the eigen-value, but you can take the magnitude (i.e. abs(eigen vector)) to obtain the eigen-value - GameOfThrows
If you expect the same answer, you have to implement some normalization process on top of the result. You can not rely on two different libraries returning the same answer, you can only rely on getting a correct answer. - Daniel
For the complex part problem yes, this can be solved either with what @GameOfThrows suggested or with real(matrix). I am just curious why this happens. - micco
@micco - You are not guaranteed that eig will return the eigenvalues and eigenvectors in the right order. To ensure this, you will need to sort them. You'd have to sort the eigenvalues first then rearrange the eigenvectors in the same ordering as the eigenvalues. - rayryeng
@micco - I was just about to give you that link :) Very nice. - rayryeng

1 Answers

2
votes

There is not only one valid answer for a eigenvalue problem. Whenever dealing with such cases, best practice is to use or define a canonical form to convert equivalent answers to identical answers. To convert any answer into such a canonical form, I would apply the following steps:

  1. Normalize all eigenvectors to length 1 (example: [-0.4472,-0.4472,-0.4472,0.4472,0.4472]' instead of [-1,-1,-1,1,1]'). Could be achieved using b=bsxfun(@rdivide,b,sqrt(sum(b.^2,1)))
  2. For each eigenvectors with a negative value in the first component, take the negative value. (example: [0.4472,0.4472,0.4472,-0.4472,-0.4472]' instead of [-0.4472,-0.4472,-0.4472,0.4472,0.4472]'). Could be achieved using b=bsxfun(@times,sign(b(1,:)),b)
  3. Sort eigenvectors and eigenvalues in ascending order of the eigenvalues. Could be achieved using this code

Please note that this is just one canonical form, not the canonical form. There might be canonical forms established but I have not found any reference so I basically put together what we discussed above to define a canonical form. Mathematically, applying such a canonical form results in a unique solution. In practice you will observe minor differences because of floating point precision errors.