Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab vs Mathematica, eigenvectors?

function H = calcHyperlinkMatrix(M)
    [r c] = size(M);
    H = zeros(r,c);
    for i=1:r,
        for j=1:c,
            if (M(j,i) == 1)
                colsum = sum(M,2);
                H(i,j) = 1 / colsum(j);
            end;
        end;
    end;
    H     


function V = pageRank(M)
    [V D] = eigs(M,1);
    V

function R = google(links)
    R = pageRank(calcHyperlinkMatrix(links));
    R

M=[[0 1 1 0 0 0 0 0];[0 0 0 1 0 0 0 0];[0 1 0 0 1 0 0 0];[0 1 0 0 1 1 0 0];
    [0 0 0 0 0 1 1 1];[0 0 0 0 0 0 0 1];[1 0 0 0 1 0 0 1];[0 0 0 0 0 1 1 0];]
google(M)

ans =

   -0.1400
   -0.1576
   -0.0700
   -0.1576
   -0.2276
   -0.4727
   -0.4201
   -0.6886

Mathematica:

calculateHyperlinkMatrix[linkMatrix_] := {
  {r, c} = Dimensions[linkMatrix];
  H = Table[0, {a, 1, r}, {b, 1, c}];
  For[i = 1, i < r + 1, i++,
   For[j = 1, j < c + 1, j++,
    If[linkMatrix[[j, i]] == 1, H[[i, j]] = 1/Total[linkMatrix[[j]]], 
     0]
    ]
   ];
  H
  }


H = {{0, 0, 0, 0, 0, 0, 1/3, 0}, {1/2, 0, 1/2, 1/3, 0, 0, 0, 0}, {1/2,
     0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0}, {0, 0, 1/2, 1/3, 
    0, 0, 1/3, 0}, {0, 0, 0, 1/3, 1/3, 0, 0, 1/2}, {0, 0, 0, 0, 1/3, 
    0, 0, 1/2}, {0, 0, 0, 0, 1/3, 1, 1/3, 0}};
R = Eigensystem[H];
VR = {R[[1, 1]], R[[2, 1]]}
PageRank = VR[[2]]


{1, {12/59, 27/118, 6/59, 27/118, 39/118, 81/118, 36/59, 1}}

Matlab and Mathematica doesn't give the same eigenvector with the eigenvalue 1. Both works though...which one is correct and why are they different? How do I gte all eigenvectors with the eigenvalue 1?

like image 439
jon Avatar asked Mar 24 '26 00:03

jon


2 Answers

The Definition of an Eigenvector X is some vector X that satisfies

AX = kX

where A is a matrix and k is a constant. It is pretty clear from the definition that cX is also an Eigenvector for any c not equal to 0. So there is some constant c such that X_matlab = cX_mathematica.

It looks like the first is normal (has Euclidean length 1, i.e. add the sums of the squares of the coordinates then take the square root and you will get 1) and the second is normalised so that the final coordinate is 1 (any Eigenvector was found and then all coordinates were divided by the final coordinate).

You can use whichever one you want, if all you need is an Eigenvector.

like image 190
satnhak Avatar answered Mar 26 '26 17:03

satnhak


This is because if a vector x is an eigenvector of matrix H, so is any multiple of the x. Vector you quote as an answer for matlab does not quite check:

In[41]:= H.matlab - matlab

Out[41]= {-0.0000333333, 0.0000666667, 0., 0., 0.0000333333, 0., \
-0.0000666667, 0.}

But assuming it is close enough, you see that

In[43]:= {12/59, 27/118, 6/59, 27/118, 39/118, 81/118, 36/59, 
  1}/{-0.1400, -0.1576, -0.0700, -0.1576,
  -0.2276, -0.4727, -0.4201, -0.6886}

Out[43]= {-1.45278, -1.45186, -1.45278, -1.45186, -1.45215, -1.45217, \
-1.45244, -1.45222}

consists of almost the same elements. Thus matlab's vector is -1.45 multiple of Mathematica's.

like image 26
Sasha Avatar answered Mar 26 '26 15:03

Sasha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!