This forum has been archived. All content is frozen. Please use KDE Discuss instead.

[SOLVED] Can't compute right eigenvectors/values

Tags: None
(comma "," separated)
User avatar
Xupito
Registered Member
Posts
4
Karma
0
OS
Hi! For sure this is a dumb question, cause I'm a newbie using Eigen, but I can't fix the following:

I try to compute the eigenvalues/vectors of a real symmetric matrix:

Code: Select all
#include <Eigen/Core>
#include <Eigen/Array>
#include <Eigen/QR>

using namespace std;
using namespace Eigen;

int main(int argc, char **argv)
{
   int rows = 4;
   int cols = 4;

   MatrixXf MCV(rows, cols);   
    MCV(0, 0) = 1   ; MCV(1, 0) = 2   ; MCV(2, 0) = 3   ; MCV(3, 0) = 4;
   MCV(0, 1) = 2   ; MCV(1, 2) = 1   ; MCV(2, 1) = 5   ; MCV(3, 1) = 6;
   MCV(0, 2) = 3   ; MCV(1, 2) = 5   ; MCV(2, 2) = 1   ; MCV(3, 2) = 7;   
   MCV(0, 3) = 4   ; MCV(1, 3) = 6   ; MCV(2, 3) = 7   ; MCV(3, 3) = 1;

   SelfAdjointEigenSolver<MatrixXf> MEV(MCV);
   
   cout << "MCV = " << MCV << endl;
   cout << "MEVAL = " << MEV.eigenvalues() << endl;
   cout << "MEVEC = " << MEV.eigenvectors() << endl;
   
   return 0;
}


I compile in WinXp 32 with mingw:
g++ -O2 -DSSE2 -I./eigen2 -o test_eigen.exe test_eigen.cpp

The output:
Code: Select all
MCV =
1 2 3 4
2 0 5 6
3 5 1 7
4 6 7 1
MEVAL =
14.78 0 0 0
0 -0.8161 0 0
0 0 -6.386 0
0 0 0 -4.582
MEVEC =
0.3589 -0.9116 0.152 -0.1305
0.473 0.3479 0.3384 -0.7353
0.5486 0.1993 0.4688 0.663
0.5886 0.09051 -0.8016 0.05253


But according to Mr Matlab the correct values are:
Code: Select all
MEVAL =

   -6.3011         0         0         0
         0   -4.0412         0         0
         0         0   -0.6747         0
         0         0         0   15.0170
MEVEC =

   -0.1496   -0.2104    0.8995   -0.3525
   -0.2498   -0.7284   -0.4051   -0.4929
   -0.5307    0.6335   -0.1525   -0.5420
    0.7960    0.1542   -0.0597   -0.5823




Thanks in advance.

And great job you Eigen developers, keep up the good work.

Cheers!

Last edited by Xupito on Fri Aug 07, 2009 1:53 am, edited 1 time in total.
User avatar
bjacob
Registered Member
Posts
658
Karma
3
I checked with both 2.0 and the development branch: the values found by Eigen are correct.

My best guess is that you made a typo when checking in Matlab.

Here's my test program:
Code: Select all
#include <Eigen/Core>
#include <Eigen/Array>
#include <Eigen/QR>

using namespace std;
using namespace Eigen;

int main(int argc, char **argv)
{
   int rows = 4;
   int cols = 4;

   MatrixXf MCV(rows, cols);
   MCV(0, 0) = 1   ; MCV(1, 0) = 2   ; MCV(2, 0) = 3   ; MCV(3, 0) = 4;
   MCV(0, 1) = 2   ; MCV(1, 2) = 1   ; MCV(2, 1) = 5   ; MCV(3, 1) = 6;
   MCV(0, 2) = 3   ; MCV(1, 2) = 5   ; MCV(2, 2) = 1   ; MCV(3, 2) = 7;
   MCV(0, 3) = 4   ; MCV(1, 3) = 6   ; MCV(2, 3) = 7   ; MCV(3, 3) = 1;

   SelfAdjointEigenSolver<MatrixXf> MEV(MCV);

   cout << "MCV = \n" << MCV << endl;
   cout << "result = \n" << MEV.eigenvectors() * MEV.eigenvalues().asDiagonal() * MEV.eigenvectors().adjoint() << endl;
   cout << "check unitary:\n" << MEV.eigenvectors() * MEV.eigenvectors().adjoint() << endl;
   cout << "eigenvalues:\n" << MEV.eigenvalues() << endl;
   return 0;
}


Here's the output:
Code: Select all
=== 00:00:15 ~$ g++ a.cpp -o a -I eigen2 && ./a
MCV =
1 2 3 4
2 0 5 6
3 5 1 7
4 6 7 1
result =
1 2 3 4
2 7.153e-07 5 6
3 5 1 7
4 6 7 1
check unitary:
1 -5.96e-08 -4.47e-08 -5.96e-08
-5.96e-08 1 1.49e-07 1.49e-07
-4.47e-08 1.49e-07 1 2.98e-08
-5.96e-08 1.49e-07 2.98e-08 1
eigenvalues:
-6.386
-4.582
-0.8161
14.78


As you can see, the eigenvectors really form a unitary matrix and the product UDU* is really the same as the original matrix, so there's no doubt that the eigenvalues are correct.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
phr
Registered Member
Posts
7
Karma
1
There's a typo in the initialization, you're setting MCV(1,2) twice, which you probably didn't do in MATLAB. :)
User avatar
bjacob
Registered Member
Posts
658
Karma
3
good catch.

To the OP: next time better use the comma-initializer...

Code: Select all
matrix << 1, 2, 3,
          4, 5, 6;


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
User avatar
Xupito
Registered Member
Posts
4
Karma
0
OS
Thanks a lot bjacob and phr, I should have tried to do the check as you did. I initialized bad the matrix MCV, so as I was afraid, I feel dumber than ever :(( .
User avatar
silicontoad
Registered Member
Posts
4
Karma
0
Also it is worth remembering that eigenvectors are not unique, check the eigen condition to verify correctness of a pair of values, eigenvalue & eigen vector respectively.

A * X = lambda * X

A is your matrix, X is the eigenvector and lambda is the eigenvalue


Bookmarks



Who is online

Registered users: Bing [Bot], daret, Google [Bot], sandyvee, Sogou [Bot]