0

I perform the following in R:

> m = matrix(c(0.563291, -0.478813,  0.574175,
+ 0.160779,  -0.03407,  0.381922,
+ 0.0677914,  0.870361,  -0.88602), 3, 3)
> mt = t(m)
> mt

[1,] 0.5632910 -0.478813  0.574175
[2,] 0.1607790 -0.034070  0.381922
[3,] 0.0677914  0.870361 -0.886020
> e<-eigen(mt)
> e
$values
[1] -1.1583554  0.5215205  0.2800359

$vectors

[1,] -0.3684057 0.8245987 -0.1255897
[2,] -0.2513624 0.4625355 -0.7915182
[3,]  0.8950387 0.3257267 -0.5981021

In eigen3 with the following c++ code:

std::cout << "=========" << std::endl;
std::cout << A << std::endl;

EigenSolver<MatrixXd> es(A);

std::cout << "evals: " << std::endl;
std::cout << es.eigenvalues();
std::cout << std::endl << "evecs: " << std::endl;
std::cout << es.eigenvectors() << std::endl;
std::cout << "=========" << std::endl;

I get the following values:

=========
0.563291 -0.478813  0.574175
0.160779  -0.03407  0.381922
0.0677914  0.870361  -0.88602

evals: 
(0.521521,0)
(0.280036,0)
(-1.15836,0)
evecs: 
(-0.824599,0)  (0.125591,0) (-0.368406,0)
(-0.462535,0)  (0.791518,0) (-0.251362,0)
(-0.325726,0)  (0.598102,0)  (0.895039,0)
=========

Why is the order different in eigen3 than in R? I am looking for the eigen version to store and print the information in "highest eigenvalue and corresponding eigenvector" format, which it appears to do, but why the discrepancy with R in the eigenvectors in that it appears to print the vectors as colum-vectors instead of row-vectors, with values off by multiplication by -1?

If I take the evs output of R and the evs output of Eigen and multiply them by each other if they are equivalent I should get the Identity Matrix I, no?

> v = matrix(c(-0.3684057, 0.8245987, -0.1255897,
+       -0.2513624, 0.4625355, -0.7915182,
+       0.8950387, 0.3257267, -0.5981021), nrow = 3, ncol = 3)
> v
           [,1]       [,2]       [,3]
[1,] -0.3684057 -0.2513624  0.8950387
[2,]  0.8245987  0.4625355  0.3257267
[3,] -0.1255897 -0.7915182 -0.5981021

> u = matrix(c(-0.824599, 0.125591, -0.368406,
+            -0.462535, 0.791518, -0.251362,
+            -0.325726,  0.598102, 0.895039), nrow = 3, ncol = 3)
> u
          [,1]      [,2]      [,3]
[1,] -0.824599 -0.462535 -0.325726
[2,]  0.125591  0.791518  0.598102
[3,] -0.368406 -0.251362  0.895039

> c = u*v
> c
          [,1]      [,2]       [,3]
[1,] 0.3037870 0.1162639 -0.2915374
[2,] 0.1035622 0.3661052  0.1948178
[3,] 0.0462680 0.1989576 -0.5353247

> u = t(u)
> u
          [,1]     [,2]      [,3]
[1,] -0.824599 0.125591 -0.368406
[2,] -0.462535 0.791518 -0.251362
[3,] -0.325726 0.598102  0.895039
> c = u*v
> c
            [,1]        [,2]        [,3]
[1,]  0.30378697 -0.03156886 -0.32973763
[2,] -0.38140576  0.36610517 -0.08187531
[3,]  0.04090783 -0.47340862 -0.53532471
> 
Ivan
  • 7,448
  • 14
  • 69
  • 134
  • Column vectors? Because that is the way R is designed. Furthermore, eigen values are only defined up to an arbitrary sign. Think about it. Most vector spaces features are preserved under "mirror imaging." – IRTFM Dec 31 '14 at 19:36
  • @BondedDust Do you mean that "eigen **vectors** are only defined up to an arbitrary sign"? – Matthew Lundberg Dec 31 '14 at 20:25
  • Maybe. I thought it could be either one. If I'm wrong about this aspect of matrix algebra, it will another in a long trail of mistakes. – IRTFM Dec 31 '14 at 20:40
  • The problem is when doing automated tests when comparing eigen to R output. – Ivan Dec 31 '14 at 22:13
  • See the modified post. I multiplied the Eigen output by the R output and I don't get the Identity Matrix "I". – Ivan Dec 31 '14 at 22:30
  • @Ivan You need to sort the results so that the eigenvalues are in the same order for each method. – Matthew Lundberg Jan 01 '15 at 15:54
  • @Ivan The sign can differ here because the eigenvectors are scaled to be length 1 and the results are real. Note the eigenvector is representative. Any scalar multiple will work. So you can check that the eigenvectors are scalar multiples of each other which covers the case that there are no repeated eigenvalues. – Matthew Lundberg Jan 01 '15 at 15:59

1 Answers1

1

You can just sort them.

For R, the documentation says

Value:

     The spectral decomposition of ‘x’ is returned as components of a
     list with components

  values: a vector containing the p eigenvalues of ‘x’, sorted in
          _decreasing_ order, according to ‘Mod(values)’ in the
          asymmetric case when they might be complex (even for real
          matrices).  For real asymmetric matrices the vector will be
          complex only if complex conjugate pairs of eigenvalues are
          detected.

Nothing requires the values to be sorted, doing so is just a convention.

(And the related question of the sign of the vectors is also a quasi-FAQ.)

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Thank you. Do you know how I can make the eigen version look like the R version by a simple function call? I know I can do it "by hand" – Ivan Dec 31 '14 at 19:21
  • What is interesting also is that my R sorts in __increasing__ order which seems to contradict the documentation you posted. – Ivan Dec 31 '14 at 19:27
  • @Ivan - I think you're just misunderstanding it? The output you've provided is listed in decreasing order (note that it's ordered based on Mod(values) as indicated in the documentation - when the eigenvalues are real this is equivalent to the absolute value). – Dason Dec 31 '14 at 19:35
  • Ok thanks, so how do I get the eigen3 output to look _exactly_ like the R version? – Ivan Dec 31 '14 at 20:37
  • 1
    By writing code that formats the output to your specification. – Dirk Eddelbuettel Dec 31 '14 at 20:40
  • Including figuring out the sign of the eigenvectors? – Ivan Dec 31 '14 at 20:41