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

floating point exception when computing eigen vectors..

Tags: None
(comma "," separated)
pendyala
Registered Member
Posts
2
Karma
0
hi,

given this example code, I am getting a floating point exception when I run this simple program. I am a newbie and would love some help

thank you

#include <iostream>
#include <Eigen/Dense>

using namespace Eigen;
using namespace std;

int main()
{
Matrix<int, 6, 6> A;
A << 2 , -1, -1, 0 , 0 , 0,
-1, 3, 0, -1, 0 , -1,
-1, 0, 2, -1, 0, 0,
0, -1, -1, 3, -1, 0,
0, 0, 0, -1, 2, -1,
0, -1, 0, 0, -1, 2;

cout << "A:" << endl << A << endl << endl;
EigenSolver< Matrix<int, 6, 6> > es(A);
cout << "The eigenvalues of A are:" << endl << es.eigenvalues() << endl;
cout << "The matrix of eigenvectors, V, is:" << endl <<
es.eigenvectors() << endl << endl;
return 0;
}

but I get a floating point exception when I run the program..

A:
2 -1 -1 0 0 0
-1 3 0 -1 0 -1
-1 0 2 -1 0 0
0 -1 -1 3 -1 0
0 0 0 -1 2 -1
0 -1 0 0 -1 2

Floating point exception: 8

I am using eigen package 3.2.4 and my g++ version is
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
jitseniesen
Registered Member
Posts
204
Karma
2
As you saw, EigenSolver does not work with a matrix of integers. You need to use a matrix of floating-point numbers.

The Eigen library probably should guard against such mistakes; this is logged as bug 750 at http://eigen.tuxfamily.org/bz/show_bug.cgi?id=750 .
pendyala
Registered Member
Posts
2
Karma
0
Thank you so much. you are a life saver !!
replacing with MatrixXf seems to give correct eigen values but the eigen vector seem to be approximate for one eigen value and incorrect for second eigen value.

is this a limitation of eigen or is there some initialization to do so that I get correct eigen vectors ? I am working mostly with laplacian matrixes. is there something additional
that I should do ?

int main()
{
MatrixXf A(2,2);
A << 2.0, 1.0,
1.0, 2.0;
EigenSolver< MatrixXf > es(A);

cout << "A:" << endl << A << endl << endl;
cout << "The eigenvalues of A are:" << endl << es.eigenvalues() << endl;
cout << "The matrix of eigenvectors, V, is:" << endl <<
es.eigenvectors() << endl << endl;

cout << "first lamba" << es.eigenvalues()[0] << endl;
cout << "first eigen vector: " << endl << es.eigenvectors().col(0) << endl;

cout << "second lamba" << es.eigenvalues()[1] << endl;
cout << "second eigen vector: " << endl << es.eigenvectors().col(1) << endl;

return 0;
}

output
A:
2 1
1 2

The eigenvalues of A are:
(3,0)
(1,0)
The matrix of eigenvectors, V, is:
(0.707107,0) (-0.707107,0)
(0.707107,0) (0.707107,0)

first lamba(3,0)
first eigen vector:
(0.707107,0)
(0.707107,0)
second lamba(1,0)
second eigen vector:
(-0.707107,0)
(0.707107,0)

but the actual eigen vector for 3 is (1,1) and
eigen vector for 1 is (1, -1)
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
I do not see anything wrong, eigenvectors are supposed to be unit vectors.

BTW, if your matrices are selfadjoint (or real symmetric), then better use the SelfAdjointEigenSolver class. You might also consider using double precision (MatrixXd).


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]