From chuck at sharpsteen.net  Sat Jun 19 18:47:50 2010
From: chuck at sharpsteen.net (Sharpie)
Date: Sat, 19 Jun 2010 09:47:50 -0700 (PDT)
Subject: [Rd] [R] Use of .Fortran
In-Reply-To: <alpine.LFD.2.00.1006181551410.17082@gannet.stats.ox.ac.uk>
References: <alpine.LFD.2.00.1006181551410.17082@gannet.stats.ox.ac.uk>
Message-ID: <1276966070866-2261266.post@n4.nabble.com>



Prof Brian Ripley wrote:
> 
> Well, it is not Fortran 77 but Fortran 95, and so needs to be given a 
> .f95 extension to be sure to work.
> 

I think most compilers only distinguish two fortran file extensions: .f or
.f90.  .f denotes fixed-form source code while .f90 denotes free-form.  Some
compilers also take the capitalized versions, .F and .F90, to indicate
source code that must be run through a preprocessor.

It is a little weird, but Fortran file extensions have nothing to do with
the year of the language standard the code is to be compiled against.



David Scott wrote:
> 
> file SSFcoef.f
>      subroutine SSFcoef(nmax,nu,A,nrowA,ncolA)
>      implicit double precision(a-h,o-z)
>      implicit integer (i-n)
>      integer l,i,nmax
>      double precision nu,A(0:nmax,0:nmax)
>      A(0,0) = 1D0
>      do l=1,nmax
>       do i=1,l-1
>         A(l,i) = (-nu+i+l-1D0)*A(l-1,i)+A(l-1,i-1)
>       end do
>       A(l,0) = (-nu+l-1D0)*A(l-1,0)
>       A(l,l) = 1D0
>      end do
>      return
>      end
> I created a dll (this is windows) using R CMD SHLIB SSFcoef.f
> Then my R code is:
> 
>  ### Load the compiled shared library in.
>  dyn.load("SSFcoef.dll")
> 
>  ### Write a function that calls the Fortran subroutine
>  SSFcoef <- function(nmax, nu){
>   .Fortran("SSFcoef",
>            as.integer(nmax),
>            as.integer(nu)
>            )$A
>  }
> 
>  SSFcoef(10,2)
> 
> which when run gives
> 
>> SSFcoef(10,2)
>  NULL
> 
> I am pretty sure the problem is that I am not dealing with the matrix A
> properly. I also tried this on linux and got a segfault.
> 
> Can anyone supply the appropriate modification to my call (and possibly to
> the subroutine) to make this work?
> 

When calling a Fortran function for R, for each argument that appears in the
subroutine declaration:

  subroutine subName(...arg list...)

You *must* provide a matching input to the .Fortran() call:

  .Fortran("subName", ...arg list...)

In the case of arrays that are filled by the Fortran subroutine, just pass
an empty vector of the appropriate length- perhaps created using the
double() function.

-Charlie

-----
Charlie Sharpsteen
Undergraduate-- Environmental Resources Engineering
Humboldt State University
-- 
View this message in context: http://r.789695.n4.nabble.com/Re-R-Use-of-Fortran-tp2260362p2261266.html
Sent from the R devel mailing list archive at Nabble.com.



[Rd] [R] Use of .Fortran
Sharpie