Matrix with random number

View: New views
5 Messages — Rating Filter:   Alert me  

Matrix with random number

by Fabio Mathias Corrêa :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello!

 I have a program in Fortran and would like to build a matrix with random numbers, I have a function in C.
 However, I have problems with the use of function in R.

 Code to compile: R CMD SHLIB mat.f myrbeta.c -o func.so


Code in C.

#include <R.h>
#include <Rmath.h>

void F77_SUB(fseedi)(void){
  GetRNGstate();
}

void F77_SUB(fseedo)(void){
  PutRNGstate();
}

void F77_SUB(myrbeta)(double *px, double *pa, double *pb){

  *px = rbeta(*pa,*pb);
}



Code in Fortran

    subroutine mat(x,l,c)
    integer l,c
    double precision x(l,c)
    integer i,j
     do j = 1, c
       do i = 1, l
        call fseedi()

         x(i,j) = call myrbeta(1,4,5) ! It's correct?
        call fseedo()
           enddo
          enddo
    end


The code of the error in R is:
dyn.load("func.so")
Error in dyn.load("func.so") :
  unable to load shared library '/home/julio/Orientados/Fabio/Fortran/mat-fortran/func.so':
  /home/julio/Orientados/Fabio/Fortran/mat-fortran/func.so: undefined symbol: callmyrbeta_



Thanks very much!

             Fábio Mathias Corrêa                       UFLA



      ____________________________________________________________________________________
[[elided Yahoo spam]]

        [[alternative HTML version deleted]]


______________________________________________
R-devel@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Re: Matrix with random number

by Barry Rowlingson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>          x(i,j) = call myrbeta(1,4,5) ! It's correct?

> The code of the error in R is:
> dyn.load("func.so")
> Error in dyn.load("func.so") :
>   unable to load shared library '/home/julio/Orientados/Fabio/Fortran/mat-fortran/func.so':
>   /home/julio/Orientados/Fabio/Fortran/mat-fortran/func.so: undefined symbol: callmyrbeta_

 Fortran doesn't use 'CALL' when calling a function. Try:

      x(i,j) = myrbeta(1,4,5)

Fortran has SUBROUTINEs (which are CALLed and don't return a value)
and FUNCTIONs - which are just used like any other function.

Barry

______________________________________________
R-devel@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Parent Message unknown Re: Matrix with random number

by Fabio Mathias Corrêa :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Mr. Barry Rowlingson

However, the matrix appears to zeros!

 Notice the code below! Please!

Code in fortran

    subroutine mat(x,l,c,a)
    integer l,c
    double precision x(l,c), a
    integer i,j
     do j = 1, c
       do i = 1, l
        call fseedi()
         x(i,j) = myrbeta(a,1,2)
        call fseedo()
           enddo
          enddo
    end

In R:

dyn.load("func.so")
x <- matrix(0,5,6)
l <- nrow(x)
c <-
 ncol(x)
a <- 0

..Fortran("mat", x = x, l, c, as.double(a))

Results:

$x
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    0    0    0    0    0    0
[2,]    0    0    0    0    0    0
[3,]    0    0    0    0    0    0
[4,]    0    0    0    0    0    0
[5,]    0    0    0    0    0    0

[[2]]
[1] 5

[[3]]
[1] 6

[[4]]
[1] 1


Thanks!!!


             Fábio Mathias Corrêa                       UFLA




      ____________________________________________________________________________________
[[elided Yahoo spam]]

        [[alternative HTML version deleted]]


______________________________________________
R-devel@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Re: Matrix with random number

by Kjell Konis-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Fabio,

Your function myrbeta returns void so assigning the output isn't going  
to work.  Instead you need to call it like a FORTRAN subroutine.  
Also, I added arguments for the parameters of the beta and moved the  
fseedi and fseedo calls outside of the loop.

This is a pretty basic FORTRAN programming question and there are lots  
of books that can help you learn FORTRAN.


     subroutine mat(x,l,c,pa,pb)
     integer l,c
     double precision x(l,c), a
     integer i,j
     call fseedi()
     do j = 1, c
        do i = 1, l
          call myrbeta(x(i,j),pa,pb)
         enddo
       enddo
     call fseedo()
     end


In R call it like this:

storage.mode(x) <- "double"
.Fortran("mat", x = x, as.integer(l), as.integer(c), as.double(1),  
as.double(2))


Cheers,
Kjell



On 30 juin 09, at 20:02, Fabio Mathias wrote:

> Thanks Mr. Barry Rowlingson
>
> However, the matrix appears to zeros!
>
> Notice the code below! Please!
>
> Code in fortran
>
>     subroutine mat(x,l,c,a)
>     integer l,c
>     double precision x(l,c), a
>     integer i,j
>      do j = 1, c
>        do i = 1, l
>         call fseedi()
>          x(i,j) = myrbeta(a,1,2)
>         call fseedo()
>            enddo
>           enddo
>     end
>
> In R:
>
> dyn.load("func.so")
> x <- matrix(0,5,6)
> l <- nrow(x)
> c <-
> ncol(x)
> a <- 0
>
> ..Fortran("mat", x = x, l, c, as.double(a))
>
> Results:
>
> $x
>      [,1] [,2] [,3] [,4] [,5] [,6]
> [1,]    0    0    0    0    0    0
> [2,]    0    0    0    0    0    0
> [3,]    0    0    0    0    0    0
> [4,]    0    0    0    0    0    0
> [5,]    0    0    0    0    0    0
>
> [[2]]
> [1] 5
>
> [[3]]
> [1] 6
>
> [[4]]
> [1] 1
>
>
> Thanks!!!
>
>
>              Fábio Mathias Corrêa                       UFLA
>
>
>
>
>      
> ____________________________________________________________________________________
> [[elided Yahoo spam]]
>
> [[alternative HTML version deleted]]
>
> <ATT00001.txt>

______________________________________________
R-devel@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Parent Message unknown Re: Matrix with random number

by Fabio Mathias Corrêa :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks very much!

             Fábio Mathias Corrêa                       UFLA


--- Em qua, 1/7/09, Kjell Konis <kjell.konis@...> escreveu:

De: Kjell Konis <kjell.konis@...>
Assunto: Re: [Rd] Matrix with random number
Para: "Fabio Mathias" <fabio.ufla@...>
Cc: r-devel@...
Data: Quarta-feira, 1 de Julho de 2009, 8:30

Hi Fabio,

Your function myrbeta returns void so assigning the output isn't going to work.  Instead you need to call it like a FORTRAN subroutine.  Also, I added arguments for the parameters of the beta and moved the fseedi and fseedo calls outside of the loop.

This is a pretty basic FORTRAN programming question and there are lots of books that can help you learn FORTRAN.


    subroutine mat(x,l,c,pa,pb)
    integer l,c
    double precision x(l,c), a
    integer i,j
    call fseedi()
    do j = 1, c
       do i = 1, l
         call myrbeta(x(i,j),pa,pb)
        enddo
      enddo
    call fseedo()
    end


In R call it like this:

storage.mode(x) <- "double"
..Fortran("mat", x = x, as.integer(l), as.integer(c), as.double(1), as.double(2))


Cheers,
Kjell



On 30 juin 09, at 20:02, Fabio Mathias wrote:

> Thanks Mr. Barry Rowlingson
>
> However, the matrix appears to zeros!
>
> Notice the code below! Please!
>
> Code in fortran
>
>     subroutine mat(x,l,c,a)
>     integer l,c
>     double precision x(l,c), a
>     integer i,j
>      do j = 1, c
>        do i = 1, l
>         call fseedi()
>          x(i,j) = myrbeta(a,1,2)
>         call fseedo()
>            enddo
>           enddo
>     end
>
> In R:
>
> dyn.load("func.so")
> x <- matrix(0,5,6)
> l <- nrow(x)
> c <-
> ncol(x)
> a <- 0
>
> ..Fortran("mat", x = x, l, c, as.double(a))
>
> Results:
>
> $x
>      [,1] [,2] [,3] [,4] [,5] [,6]
> [1,]    0    0    0    0    0    0
> [2,]    0    0    0    0    0    0
> [3,]    0    0    0    0    0    0
> [4,]    0    0    0    0    0    0
> [5,]    0    0    0    0    0    0
>
> [[2]]
> [1] 5
>
> [[3]]
> [1] 6
>
> [[4]]
> [1] 1
>
>
> Thanks!!!
>
>
>              Fábio Mathias Corrêa                       UFLA
>
>
>
>
>      ____________________________________________________________________________________
> [[elided Yahoo spam]]
>
>     [[alternative HTML version deleted]]
>
> <ATT00001.txt>



      ____________________________________________________________________________________
[[elided Yahoo spam]]

        [[alternative HTML version deleted]]


______________________________________________
R-devel@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel