### zgeev eigenvalue results don't match matlab results

Posted:

**Fri Aug 10, 2012 3:36 pm**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:

^^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!

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:

^^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!