From sgiannerini at gmail.com  Fri Aug 14 11:19:41 2009
From: sgiannerini at gmail.com (Simone Giannerini)
Date: Fri, 14 Aug 2009 11:19:41 +0200
Subject: [Rd] Problem with function in fortran 95
In-Reply-To: <795354.83301.qm@web59705.mail.ac4.yahoo.com>
References: <795354.83301.qm@web59705.mail.ac4.yahoo.com>
Message-ID: <3c12769c0908140219y2c0f176eqca21d774082078bb@mail.gmail.com>

Fabio,

I see two problems with your code:

1. R type numeric corresponds to FORTRAN Real*8 (or double precision)
so that line 4 of your mat.f95 becomes:

REAL*8 :: x, y, res

2. your R code won't ever succeed because you pass integer matrices
(x,y,res) to a subroutine that expects REAL*8 data.
you need to coerce all your matrices to "double" as follows:

storage.mode(x) <- "double"
storage.mode(y) <- "double"
storage.mode(res) <- "double"

Finally, you can call the corrected function:

.Fortran("mymult", x,y,res,as.integer(l),as.integer(c))
[[1]]
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20

[[2]]
     [,1] [,2] [,3] [,4] [,5]
[1,]   20   16   12    8    4
[2,]   19   15   11    7    3
[3,]   18   14   10    6    2
[4,]   17   13    9    5    1

[[3]]
     [,1] [,2] [,3] [,4] [,5]
[1,]  604  468  332  196   60
[2,]  678  526  374  222   70
[3,]  752  584  416  248   80
[4,]  826  642  458  274   90
[5,]  900  700  500  300  100

[[4]]
[1] 5

[[5]]
[1] 4


All these issues are discussed in the "Writing R Extensions" manual,
please read it carefully.

Ciao

Simone



On Wed, Aug 12, 2009 at 1:32 PM, Fabio Mathias
Corr?a<fabio.ufla at yahoo.com.br> wrote:
> I am writing a function in fortran 95, but the intrinsic function MATMUL is not working properly. Here's an example.
>
> ? ? ? ?SUBROUTINE mymult(x,y,res,m,n)
> ? ? ? ?IMPLICIT NONE
> ? ? ? ?INTEGER :: m,n
> ? ? ? ?REAL :: x, y, res
> ? ? ? ?DIMENSION :: x(m,n), y(n,m), res(m,m)
> ? ? ? ?res = MATMUL(x,y)
> ? ? ? ?END SUBROUTINE mymult
>
> R CMD SHLIB mat.f95
>
> In R:
>
> dyn.load("mat.so")
> x ? <- matrix(1:20,5)
> l ? <- nrow(x)
> c ? <- ncol(x)
> y ? <- matrix(20:1,c)
> res <- matrix(0,l,l)
> dim(x)
> dim(y)
> dim(res)
> l
> c
> .Fortran("mymult", x,y,res,l,c)
>
> [[1]]
> ? ? [,1] [,2] [,3] [,4]
> [1,] ? ?1 ? ?6 ? 11 ? 16
> [2,] ? ?2 ? ?7 ? 12 ? 17
> [3,] ? ?3 ? ?8 ? 13 ? 18
> [4,] ? ?4 ? ?9 ? 14 ? 19
> [5,] ? ?5 ? 10 ? 15 ? 20
>
> [[2]]
> ? ? [,1] [,2] [,3] [,4] [,5]
> [1,] ? 20 ? 16 ? 12 ? ?8 ? ?4
> [2,] ? 19 ? 15 ? 11 ? ?7 ? ?3
> [3,] ? 18 ? 14 ? 10 ? ?6 ? ?2
> [4,] ? 17 ? 13 ? ?9 ? ?5 ? ?1
>
> [[3]]
> ? ? [,1] [,2] [,3] [,4] [,5]
> [1,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0
> [2,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0
> [3,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0
> [4,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0
> [5,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0
>
> [[4]]
> [1] 5
>
> [[5]]
> [1] 4
>
>
> Linux Ubuntu 8.04 and use compiler gfortran 4.2.
> The problem is the compiler?
>
> Thanks!
>
> ? ???????????? F?bio Mathias Corr?a
> Estat?stica e Experimenta??o Agropecu?ria/UFLA
> ? ? ? ? ? ? ? ? ? ? ?Brazil
>
>
> ? ? ?____________________________________________________________________________________
> Veja quais s?o os assuntos do momento no Yahoo! +Buscados
> http://br.maisbuscados.yahoo.com
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



-- 
______________________________________________________

Simone Giannerini
Dipartimento di Scienze Statistiche "Paolo Fortunati"
Universita' di Bologna
Via delle belle arti 41 - 40126  Bologna,  ITALY
Tel: +39 051 2098262  Fax: +39 051 232153
http://www2.stat.unibo.it/giannerini/



[Rd] Problem with function in fortran 95
Simone Giannerini