LAPACK dgeev: parameter had illegal value

AI Thread Summary
The discussion revolves around using LAPACK's DGEEV function in C for diagonalizing a matrix. The user encountered an error indicating that parameter number 9 had an illegal value, which relates to the leading dimension of an array. The code was modified to avoid issues with the browser interpreting the letter 'i' as a BBCode tag, leading to confusion in the posted code. Clarifications were provided regarding the use of parameters in LAPACK, particularly the distinction between passing parameters by reference in Fortran versus by value in C. It was emphasized that parameters marked as [in] do not require addresses, while those marked [in,out] or [out] do. The user was advised to replace array index variables to avoid rendering issues in the forum. The conversation concluded with the user confirming that the code appeared to work after making the suggested adjustments, although only trivial cases were tested.
Angelos K
Messages
43
Reaction score
0
Mod note: I revised the code below slightly, changing the loop control variable i to either j or k. The reason for this is that the browser mistakes the letter i in brackets for the BBCode italics tag, which causes some array expressions to partially disappear.

Hello,

I am trying for the first time to use LAPACK from C to diagonalize a matrix and I am stuck.

I try to modify this example http://rcabreral.blogspot.co.uk/2010/05/eigenvalues-clapack.html from zgeev to degeev. I have looked at the DGEEV input parameters, but it seems I don't understand the well enough.

Below is my code. Running it produces:
** On entry to DGEEV parameter number 9 had an illegal value

EDIT: The error occurs in the call of degeev spanning lines 48 to (including) 53.
#include<stdio.h>
#include<math.h>

#include<complex.h>
#include <stdlib.h>

//...................
void dgeTranspose( double *Transposed, double *M ,int n)
{

int i,j;
for(i=0;i<n;i++)

for(j=0;j<n;j++) Transposed[i+n*j] = M[i*n+j];
}

//...................
// MatrixComplexEigensystem: computes the eigenvectors and eigenValues of input matrix A
// The eigenvectors are stored in columns
//..................
void MatrixComplexEigensystem( double *eigenvectorsVR, double *eigenvaluesW, double *A, int N)
{

int i;

double *AT = (double *) malloc( N*N*sizeof(double ) );

dgeTranspose( AT, A , N);

char JOBVL ='N'; // Compute Right eigenvectors

char JOBVR ='V'; // Do not compute Left eigenvectors

double VL[1];

int LDVL = 1;
int LDVR = N;

int LWORK = 4*N;

double *WORK = (double *)malloc( LWORK*sizeof(double));

double *RWORK = (double *)malloc( 2*N*sizeof(double));

int INFO;

dgeev_( &JOBVL, &JOBVR, &N, AT , &N , eigenvaluesW ,

VL, &LDVL,
eigenvectorsVR, &LDVR,
WORK,
&LWORK, RWORK, &INFO );

dgeTranspose( AT, eigenvectorsVR , N);

for(j=0;j<N*N;j++) eigenvectorsVR[j]=AT[j];

free(WORK);
free(RWORK);
free(AT);
}int main()
{
int i,j;
const int N = 3; double A[] = { 1.+I , 2. , 3 , 4. , 5.+I , 6. , 7., 8., 9. + I};

double eigenVectors[N*N];
double eigenValues[N]; MatrixComplexEigensystem( eigenVectors, eigenValues, A, N);

printf("\nEigenvectors\n");

for(k=0;k<N;k++){
for(j=0;j<N;j++) printf("%e", eigenVectors[k*N + j]);

printf("\n");
}

printf("\nEigenvalues \n");
for(j=0;i<N;j++) printf("%e", eigenValues[j] );printf("\n------------------------------------------------------------\n");
return 0;}
Any help much appreciated!
 
Last edited by a moderator:
Technology news on Phys.org
Thanks. This code appears to work.

#include<stdio.h>
#include<math.h>

#include <stdlib.h>

//...................
void dgeTranspose( double *Transposed, double *M ,int n)
{

int i,j;
for(i=0;i<n;i++)

for(j=0;j<n;j++) Transposed[i+n*j] = M[i*n+j];
}

//...................
// MatrixComplexEigensystem: computes the eigenvectors and eigenValues of input matrix A
// The eigenvectors are stored in columns
//..................
void MatrixComplexEigensystem( double *eigenvectorsVR, double *eigenvaluesW, double *A, int N)
{

int i;

double *AT = (double *) malloc( N*N*sizeof(double ) );

dgeTranspose( AT, A , N);

char JOBVL ='N'; // Compute Right eigenvectors

char JOBVR ='V'; // Do not compute Left eigenvectors

double VL[1];

int LDVL = 1;
int LDVR = N;

int LWORK = 4*N;

double *WORK = (double *)malloc( LWORK*sizeof(double));

double *RWORK = (double *)malloc( 2*N*sizeof(double));

int INFO;
printf("\nping2\n");
double *eigenvaluesWR =eigenvaluesW;
double *eigenvaluesWI = eigenvaluesW + N;
dgeev_( &JOBVL, &JOBVR, &N, AT , &N,
eigenvaluesWR, eigenvaluesWI,
VL, &LDVL,
eigenvectorsVR, &LDVR,
WORK,
&LWORK, &INFO );
printf("\nping1\n");
dgeTranspose( AT, eigenvectorsVR , N);

for(j=0;i<N*N;j++) eigenvectorsVR[j]=AT[j];

free(WORK);
free(RWORK);
free(AT);
}int main()
{
int i,j;
const int N = 3; double A[] = { 1. , 0. , 0. , 0. , 1., 0. , 0., 0., 1.};

double eigenVectors[N*N];
double eigenValues[2*N]; MatrixComplexEigensystem( eigenVectors, eigenValues, A, N);

printf("\nEigenvectors\n");

for(i=0;i<N;i++){
for(j=0;j<N;j++) printf("%e ", eigenVectors[i*N + j]);

printf("\n");
}

printf("\nEigenvalues \n");
for(j=0;i<N;j++) printf("%e ", eigenValues[j] );printf("\n------------------------------------------------------------\n");
return 0;}​
 
Last edited by a moderator:
Did this work? Note that your code as it appear here is not the same as what you pasted in. Anywhere you have [ i ] (without spaces), the browser strips out, thinking that you want the text that follows it to be in italics. That's why I modified your code in post #1.

As far as I know, there's not a workaround, other than to not use i as an index in arrays.
 
  • Like
Likes Angelos K
Mark44 said:
Did this work? Note that your code as it appear here is not the same as what you pasted in. Anywhere you have [ i ] (without spaces), the browser strips out, thinking that you want the text that follows it to be in italics. That's why I modified your code in post #1.

As far as I know, there's not a workaround, other than to not use i as an index in arrays.

Thank you. It appears to work, but I have only tested trivial cases.

I saw your warning earlier, but understood it just now (blush). It seems I am not able to modify my post anymore. So I should replace [ i ] by [k] evereywhere and then make the corresponding logical adjustments, right?

What trubled me most - and I am a newbie - is that I understand the documentation here
http://www.netlib.org/lapack/explore-html/d9/d28/dgeev_8f.html
as asking for an integer N. while the code (the prototype and my version) uses &N.

It turns out that this is needed for translating the Fortran routines to C.
http://www.physics.orst.edu/~rubin/nacphy/lapack/cprogp.html
 
Last edited by a moderator:
Angelos K said:
Thank you. It appears to work, but I have only tested trivial cases.

I saw your warning earlier, but understood it just now (blush). It seems I am not able to modify my post anymore. So I should replace [ i ] by [k] evereywhere and then make the corresponding logical adjustments, right?
Or you could replace [ i ] by [ii] - that should work. Keep in mind that is only for code that you post here, and it is an artifact of the browser thinking that you are entering BBCode markup, which changes how text is rendered.

There is a limited-time window for members to change their posts, but we mentors don't have that limit, so I edited your post.
Angelos K said:
What trubled me most - and I am a newbie - is that I understand the documentation here
http://www.netlib.org/lapack/explore-html/d9/d28/dgeev_8f.html
as asking for an integer N. while the code (the prototype and my version) uses &N.

It turns out that this is needed for translating the Fortran routines to C.
You shouldn't have to do that, at least not for any parameters that are marked [in] in the documentation. A major difference between Fortran and C is how parameters are passed. In Fortran, they are passed by reference, but in C, they are passed by value. A Fortran subroutine or function is free to modify any of its parameters. In C, this can only be simulated by passing the address of (a pointer to) the thing whose value is to be changed.

In the DGEEV subroutine, the N parameter is marked as [in], which says that DGEEV is going to treat it as read-only. The parameters you need to watch out for (and for which addresses are needed) are the ones marked in the documentation with [in,out] or [out], such as A, WR, WI, VL, VR, and a couple of others. The error you showed in post #1 pertained to VL.

Any parameter that is marked [in] you shouldn't need to supply an address.
Angelos K said:
http://www.physics.orst.edu/~rubin/nacphy/lapack/cprogp.html
 
  • Like
Likes Angelos K
Thank you very much for the clarification about [in] parameters! As you resolved all problems: Should I mark this thread as solved, and if so, how?
 
Last edited:
Typically we don't do anything special, such as marking a thread "solved."
 
Back
Top