Page 1 of 1

zgeev eigenvalue results don't match matlab results

PostPosted: Fri Aug 10, 2012 3:36 pm
by eigenguy
The first half of the eigenvalues and eigenvectors match, but the second half is totally different.

Here is the test matrix I'm using:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16


Matlab's Solution:
Eigenvalues:
36.20937 0 0 0
0 -2.209372 0 0
0 0 -3.188632e-15 0
0 0 0 -1.348400e-16


Eigenvectors:
-0.1511543 0.7270499 0.5037001 -0.06456090
-0.3492373 0.2832087 -0.8319576 -0.3193211
-0.5473203 -0.1606324 0.1528148 0.8323249
-0.7454033 -0.6044736 0.1754426 -0.4484429



lapack's (in c++) solution:

Image


^^Note: second half of both eigenvalue and eigenvector matrices between the two sets of answers are different!


If anyone wanted to take a look, here's my y c++ eigenvalue/eigenvector function:

[quote=codes_that_should_work]#if T == double
#define lpcomplex lapack_complex_double
#define lpcomplex_r lapack_complex_double_real
#define lpcomplex_i lapack_complex_double_imag
#elif T == float
#define lpcomplex lapack_complex_float
#define lpcomplex_r lapack_complex_float_real
#define lpcomplex_i lapack_complex_float_imag
#endif

template <class T>
int eig(matrix<std::complex<T> >& eval, matrix<std::complex<T> >& evec, const matrix<T>& x) {

int dim;

if(x.row != x.col) {

return 0;
}

else {

dim = x.row;
}

lapack_int info,n,ldx;

ldx = x.col;

n = dim;

lpcomplex d[dim];

const T* xtemp = vectoarr<T>(x.vec);

lpcomplex xarr[x.vec.size()];

int i;
for (i=0; i<x.vec.size(); i++) {

xarr[i] = xtemp[i];
}

int ldvl = x.col;
int ldvr = x.col;
lpcomplex vl[x.vec.size()],vr[x.vec.size()];

info = LAPACKE_zgeev(LAPACK_ROW_MAJOR,'N','V',n,xarr,ldx,d,vl,ldvl,vr,ldvr);

std::vector<std::complex<T> > result1;

for(i=0; i<dim; i++) {

result1.push_back(std::complex<T>(creal(d[i]),cimag(d[i])));
}

eval = diag(result1);

std::vector<std::complex<T> > result2;

for(i=0; i<x.vec.size(); i++) {

result2.push_back(std::complex<T>(creal(vr[i]),cimag(vr[i])));
}

evec.vec = result2;
evec.row = x.row;
evec.col = x.col;

return 1;
}[/quote]



Note: I tried the same with dgeev. Here again the first half matches, but the second half does not match either matlab or the zgeev values!

I tried all sorts of little tweaks to make it work, to no avail.

Sounds familiar, anyone? Any advice, suggestions?

thanks!

Re: zgeev eigenvalue results don't match with matlab results

PostPosted: Mon Aug 13, 2012 12:39 pm
by Julien Langou
This matrix has two identical eigenvalues zero. This is a problem. This means that the eigenspace associated with the eigenvalue zero (so therefore this is also the nullspace) is of dimension 2. What LAPACK is returning you is a basis of this eigenspace, what matlab is returning you is another basis of this eigenspace. Both answers are correct (up to machine precision). If you want to check that an answer is correct, check that
Code: Select all
norm( A *v - lambda * v ) / norm(A) / norm( v )

is close from machine precision. (You can also check that the two vectors returned are linearly independent.)

Another answer for a basis for this subspace (this one is exact) is for example:
Code: Select all
     1     2
    -2    -3
     1     0
     0     1

But you see that you can generate as many good answers as you wish.

Julien.