Re: Reading complex multidimensionalarrays from C
- From: e p chandler <epc8@xxxxxxxx>
- Date: 28 Apr 2007 11:11:01 -0700
On Apr 28, 1:58 pm, e p chandler <e...@xxxxxxxx> wrote:
On Apr 28, 12:55 pm, t...@xxxxxxxxxxxxxxxxxx wrote:
Hi,
I defined a multidimensional Array (with complex values) in Fortran:
PROGRAM Hello
complex, dimension(2,2) :: A
A(1,1) = cmplx(3, 2)
A(1,2) = cmplx(3, 7)
A(2,1) = cmplx(9, 9)
A(2,2) = cmplx(2, 7)
CALL test(A,2,2)
You have an argument mismatch here. You are passing a pointer to the
start of A, then two integers. Your routine expects a pointer to an
array of two doubles. The type mismatch is corrected below.
The proper syntax is:
CALL test(A(2,2))
END
I tried to read values with the folowing C function:
#include <stdio.h>
typedef struct {
double dr;
double di;
The components of a Fortran COMPLEX are two variables of type default
REAL. In Fortran this is single precision - which is FLOAT in C. You
are not guaranteed that a particular compiler supports what might be
declared as (non-standard) DOUBLE COMPLEX or COMPLEX*16 or a type of
complex using kinds).
float dr;
float di;
} fcomplex;
int test_ (fcomplex *fc, int *n, int *m) {
printf("YAY: %f + %fi\n", fc->dr, fc->di);
return(0);
}
Excuse replying to self, but I messed up your declaration on the C
side too! :-(. If you really want to pass an array and two subscripts
then you have to account for two factors in your C code.
1. Fortran subscripts (unless declared otherwise) start at 1. In C
arrays are 0 based.
2. Fortran is column major but C is row major. So with 2 subscripts
you need to interchange them on the C side.
I won't try to suggest what you might write in C to handle the 3
argument case.
int test_ (fcomplex *fc) {
And I made the same mistake as you did in my original reply.
I compiled it like this:
gfortran -c -g testF2.f
You have posted free form source but gfortran expects fixed format
with a file extension of .f:
gfortran -c -g -ffree-form testF2.f
gcc -c -g testC.c
On my system I need to add a path to the include files. You may not
need this:
gcc -c -g -I\gfortran\include testC.c
gfortran -g -o test testC.o testF2.o
OK
It compiles well, but when I execute ./test I get:
YAY: 2.000000 + 262144.063538i
YAY: 2.000000 + 7.000000i
So could anyone please give me some hints how to do it right?
See comments interspersed with your code and output.
Regards,
Timo
You have fallen into several traps common in mixed language
programming. You have two different sources of argument mismatch here.
First, your incorrect syntax gives you a mismatch in the NUMBER of
arguments. Second, the TYPES of your arguments do not correspond. In
Fortran you are using single precision, but in C you are using double.
Worse, your compilers will not catch this type of error. If you had
included an explicit interface (somewhat analagous to a function
prototype in C) for your C routine in Fortram, you might have caught
the syntax error, but not the type misatch error.
-- e-mail: epc8 at juno dot com
-- elliot
.
- Follow-Ups:
- References:
- Reading complex multidimensionalarrays from C
- From: timos
- Re: Reading complex multidimensionalarrays from C
- From: e p chandler
- Reading complex multidimensionalarrays from C
- Prev by Date: Re: Reading complex multidimensionalarrays from C
- Next by Date: Re: error with "if" !!!
- Previous by thread: Re: Reading complex multidimensionalarrays from C
- Next by thread: Re: Reading complex multidimensionalarrays from C
- Index(es):
Relevant Pages
|
|