[R] Fortran Code to R Code
Duncan Murdoch
murdoch at stats.uwo.ca
Wed Nov 28 18:19:26 CET 2007
On 11/28/2007 12:54 PM, Dave Roberts wrote:
> Bryan,
>
> The previous responses will point you in the right direction. I
> have found, however, that it takes a while to get used to the
> requirements, and there are many possible sources of error.
This is true, and the advice below is all good. One addition: the
inline package can help when you're starting. Ask it to write a simple
Fortran wrapper function for you, and you'll see what it should look like.
Duncan Murdoch
>
> 1) You have to convert the main program to a subroutine. Any arrays
> declared in the main program or any nested subroutine have to created in
> R before the subroutine is called, and passed to the subroutine. On the
> other hand, this provides the advantage of allocatable array size in
> FORTRAN 77.
>
> 2) You have to make sure that you are very careful about storage. I
> have reluctantly decided that attempting minimize storage by using
> integer*2 and real*4 is not worth it, and I now use just integer and
> double precision to minimize problems with R.
>
> 3) Consider casting every variable passed in the .Fortran() call. Many
> of them would be correct by default possibly, but it is easier to simply
> cast them all, e.g. as.integer() for all integers, as.double() for real
> numbers. These get the storage right, and strip of any attributes
> besides he actual values. Fortunately, R and FORTRAN agree about
> storage order in arrays and you don't have to micro-manage that.
>
> 4) When you get it wrong, the probability of a seg fault and hard crash
> is very high. So, every time you modify the R function that calls the
> .Fortran you have written, do a save.image() before you try the code.
> Otherwise you lose all your changes. If you have the luxury of working
> in linux (and presumably other *nixes), you can use "write(6,*)
> whatever" debug statements in your code and the output goes to your R
> session. In Windows, you have to use the specific debug routines
> described in the R manuals.
>
> 5) Managing the returned values has to handled in the .Fortran() call by
> giving the argument a name. Then, that name can be used as the
> component name of the returned object. Just below is an example from
> labdsv that converts any dissimilarity or distance matrix to the nearest
> euclidean distance object. The PACKAGE= argument is used when the
> routine is pat of a package; it's not necessary for ad hoc functions.
>
> euclidify <- function (x,upper=FALSE,diag=FALSE)
> {
> x <- as.dist(x)
> tmp <- .Fortran("euclid",
> x=as.matrix(x),
> as.integer(attr(x,"Size")),
> PACKAGE='labdsv')
> tmp2 <- as.dist(tmp$x)
> attr(tmp2, "Labels") <- dimnames(x)[[1]]
> attr(tmp2, "Diag") <- diag
> attr(tmp2, "Upper") <- upper
> attr(tmp2, "method") <- paste("euclidify", attr(x, "method"))
> attr(tmp2, "call") <- match.call()
> tmp2
> }
>
> Dave Roberts
>
>
> Bryan Klingaman wrote:
>> How do you convert Fortran Code to R Code to use and execute in R? Or how do you take Fortran code and make it run in R? So what I'm getting at is, I have some code in fortran and I want to be able to run that same code for R. Please email me back and let me know how to do that, thanks.
>>
>> Bryan
>> Email: BKling9er at yahoo.com
>>
>>
>> ---------------------------------
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list