data type incompatible btw C and FORTRAN

Open discussion regarding features, bugs, issues, vendors, etc.

data type incompatible btw C and FORTRAN

Postby nosarthur » Thu Aug 11, 2005 3:18 pm

hi, there
i am using zheev_() to calculate eigen value of hermitian matrices and i am under linux. it seems that double complex in C is not equivalent to complex*16 in FORTRAN. is there any way to fix this?

my test code is like this:
=====================================================================
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <complex.h>
#include <f2c.h>
#include <clapack.h>


int main(){
char v='v';
char u='u';

integer n=2;
integer info;
double complex A[4] = {2,-I,I,1};
double w[2];
integer lwork=66;
complex double work[66];
double rwork[66];
int i,j;

zheev_(&v,&u,&n,A,&n,w,work,&lwork,rwork,&info);
if (info != 0)
printf("error!");
//printf("%f\n", creal(work[0]));
for (i=0;i<2;++i){
printf("the %d-th one:", i+1);
for (j=0;j<2;++j)
printf("\t%f + %f i",creal(A[i*2+j]),cimag(A[i*2+j]));
printf("\n");
}
printf("eigen values:\n");
for (i=0;i<2;++i)
printf("%f\t",w[i]);
printf("\n");


return 0;
}
========================================================================

my gcc compiler give me two warnings:

gcc eigen.c -L/usr/local/lib -llapack -lblas -lF77 -lI77 -lm
eigen.c: In function `main':
eigen.c:21: warning: passing arg 4 of `zheev_' from incompatible pointer type
eigen.c:21: warning: passing arg 7 of `zheev_' from incompatible pointer type



Another issue is that if i use 'const char' for those control variables, I wil get warning for zheev_() , but there is no warning for zgemm_(), which is used to multiply matrices.
nosarthur
 
Posts: 2
Joined: Thu Aug 11, 2005 2:52 pm

Postby Julie » Thu Aug 11, 2005 4:33 pm

Hi,

If you want to use the lapack library with your C code, just modify your code as follow (I have corrected some small mistakes and removed some include):

Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <complex.h>

int main(){
        char v='v';
        char u='u';

        int n=2;
        int info;
        double complex A[4] = {2,-I,I,1};
        double w[2];
        int lwork=66;
        complex double work[66];
        double rwork[66];
        int i,j;

        zheev_(&v,&u,&n,A,&n,w,work,&lwork,rwork,&info);
        if (info != 0)
                printf("error!");
        //printf("%f\n", creal(work[0]));
        for (i=0;i<2;++i){
                printf("the %d-th one:", i+1);
                for (j=0;j<2;++j)
                        printf("\t%f + %f i",creal(A[i*2+j]),cimag(A[i*2+j]));
                printf("\n");
        }
        printf("eigen values:\n");
        for (i=0;i<2;++i)
                printf("%f\t",w[i]);
        printf("\n");

        return 0;
}


and compile it with adding the g2c library
Code: Select all
gcc -o exe eigen.c -L/usr/local/lib -llapack -lblas  /usr/local/lib/libg2c.a -lm


Here is the output:
Code: Select all
the 1-th one:   -0.000000 + 0.525731 i  -0.850651 + 0.000000 i
the 2-th one:   0.000000 + 0.850651 i   0.525731 + 0.000000 i
eigen values:
0.381966        2.618034


Julie Langou[/quote]
Julie
 
Posts: 299
Joined: Wed Feb 23, 2005 12:32 am
Location: ICL, Denver. Colorado

Postby nosarthur » Thu Aug 11, 2005 10:09 pm

thanks Julie, it works. I have a further related question: actually I don't have libg2c.a, but I have libg2c.o.0 and libg2c.o.0.0.0

I tried to link either of them and both works. Is there any difference btw these two?

Thanks.
nosarthur
 
Posts: 2
Joined: Thu Aug 11, 2005 2:52 pm

Postby Julien Langou » Sun Aug 14, 2005 10:30 pm

Hello,
the best (in most of the cases) is that you do not specify which libg2c you want to use but let gcc choose. Just add -lg2c. (Not as Julie did :) !).
Julien
Julien Langou
 
Posts: 735
Joined: Thu Dec 09, 2004 12:32 pm
Location: Denver, CO, USA


Return to User Discussion

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests