From Berwin.Turlach at gmail.com  Tue Mar  6 12:56:08 2012
From: Berwin.Turlach at gmail.com (Berwin A Turlach)
Date: Tue, 6 Mar 2012 19:56:08 +0800
Subject: [Rd] Calling FORTRAN function from R issue?
In-Reply-To: <B1B70BE3-2D8A-495B-8893-CC67026E1E9F@xs4all.nl>
References: <CADUbQ5ghmCd-3K4hx=FrzhA8YwfohhUp0-g6rJHHU0s-uEfN+A@mail.gmail.com>
	<B1B70BE3-2D8A-495B-8893-CC67026E1E9F@xs4all.nl>
Message-ID: <20120306195608.7ec858c0@bossiaea>

G'day Berend,

On Tue, 6 Mar 2012 11:19:07 +0100
Berend Hasselman <bhh at xs4all.nl> wrote:

> On 06-03-2012, at 01:21, Dominick Samperi wrote:
[...]
> >    zx[0].r = 1.0; zx[0].i = 0.0;
> >    zx[1].r = 2.0; zx[0].i = 0.0;
> >    zx[2].r = 3.0; zx[0].i = 0.0;

Just noticing that it is always zx[0].i, same with the imaginary part
of zy.  But this is probably not of importance. :)

> I tried calling zdotc  through an intermediate Fortran routine hoping
> it would solve your problem.
[...] 
> Above C routine changed to
[...]
> The fortran subroutine is
> 
> <code>
>       subroutine callzdotc(retval,n, zx, incx, zy, incy)
>       integer n, incx, incy
>       double complex retval, zx(*), zy(*)
>       external double complex zdotc
> 
>       retval = zdotc(n, zx, incx, zy, incy)
> 
>       return
>       end
> </code>
> 
> Made a shared object with
> 
> R CMD SHLIB --output=dozdot.so callzdotc.f czdot.c 
> 
> and ran
> 
> dyn.load("dozdot.so")
> .C("testzdotc")
> 
> with the result 0.0, 0.0

Same here.

Once I change the line

	external double complex zdotc

in your fortran subroutine to

	double complex zdotc

everything works fine and I get as result 14.0, 0.0.

It is long time ago that I was taught (and studied) the FORTRAN 77
standard.  But flipping through some books from that time I thing I
have some inkling on what is going on.  The "external" statement is not
needed here (seems to be used in the sense that C is using the
"external" statement).

Cheers,

	Berwin



[Rd] Calling FORTRAN function from R issue?
Berwin A Turlach