[R] How to create a function returning an array ?

Kaptue Tchuente, Armel armel.kaptue at sdstate.edu
Fri Jun 28 17:21:46 CEST 2013


Your comments #2 and 3 are correct.
The closing brace is missing and. Normally the end of the pr after the instruction 
For the comment #1 relate to the declaration img, I don't know if there is another way to import data from fortran in R.

Armel

-----Original Message-----
From: Duncan Murdoch [mailto:murdoch.duncan at gmail.com] 
Sent: Friday, June 28, 2013 9:53 AM
To: Kaptue Tchuente, Armel
Cc: r-help at r-project.org
Subject: Re: [R] How to create a function returning an array ?

On 28/06/2013 10:46 AM, Kaptue Tchuente, Armel wrote:
> Please could you explain what you mean by several typos in my code?

1. img is passed as an argument, but never used.
2. There is no closing brace on the function body.
3.  You set the dimension to n*m, when I believe you wanted c(n, m).

Duncan Murdoch

>
> Anyway, it works very well and keep in minds that here I just wrote a very simple code to illustrate what I really want since in the reality, the program is more complex than what you see.
> May be I'm wrong but I also prefer to call this fortran function because I realized that (i) R is not very efficient for large data sets reading and processing and (ii) the speed of execution in Fortran is faster than in C.
>
> Armel
>
> -----Original Message-----
> From: Duncan Murdoch [mailto:murdoch.duncan at gmail.com]
> Sent: Friday, June 28, 2013 9:30 AM
> To: Kaptue Tchuente, Armel
> Cc: r-help at r-project.org
> Subject: Re: [R] How to create a function returning an array ?
>
> On 28/06/2013 10:18 AM, Kaptue Tchuente, Armel wrote:
> > @ Duncan, I have already used the syntax that you proposed before 
> > asking for help by writing something like
> > > read_ts<-function(n,m,img) {
> > >  out<-.Fortran("read_ts",
> > >                as.integer(n),
> > >                as.integer(m),
> > >                img=as.single(rnorm(n*m)))
> > > return(out$img)
> > > alpha<-read_ts(n,m)
> > > dim(alpha)<-c(n*m)
> > > alpha<-t(alpha)
> > My worry with this syntax is that (i) the program is not very efficient because n and m are very big and these two additional instructions (dim(alpha)<-c(n*m and alpha<-t(alpha) can be skipped just by directly declaring img as an array in fortran instead of a vector and (ii) the syntax will become more complex dealing with a multidimensional array instead of a matrix as in this example.
> > And this is why I'm looking for the correct instruction to declare img as an array instead of a vector.
>
> There are several typos in your code above, but I think your intention is clear.
>
> You can do what you are asking for, but not with .Fortran.  It only 
> handles vectors for input and output.  You'll need to use .Call (which 
> means writing in C or C++).  If you're familiar with C++, using Rcpp 
> is probably the easiest way to do this.  If not, I'd rewrite the 
> Fortran code to avoid the need for the transpose at the end, and do
>
> dim(out$img) <- c(n,m)
> return(out$img)
>
> within your read_ts function.  I think this is reasonably efficient.
>
> Duncan Murdoch
>
>
> >
> > Armel
> >
> > -----Original Message-----
> > From: Duncan Murdoch [mailto:murdoch.duncan at gmail.com]
> > Sent: Friday, June 28, 2013 8:16 AM
> > To: David Winsemius
> > Cc: Kaptue Tchuente, Armel; r-help at r-project.org
> > Subject: Re: [R] How to create a function returning an array ?
> >
> > On 27/06/2013 11:38 PM, David Winsemius wrote:
> > > On Jun 27, 2013, at 8:04 PM, Kaptue Tchuente, Armel wrote:
> > >
> > > > Hi there,
> > > >
> > > > I would like to know how to change the line 
> > > > "img=as.single(rnorm(m)))" such that instead of being a vector 
> > > > of length m as it is now, img is an array of dimension c=(n,m,o) 
> > > > for instance
> > > >
> > > > ---------------------------------
> > > > read_ts<-function(n,m,o,img) {
> > > >   out<-.Fortran("read_ts",
> > > >                as.integer(n),
> > > >                as.integer(m),
> > > >                as.integer(o),
> > > >                img=as.single(rnorm(n)))
> > > >   return(out$img)
> > > > ------------------------------------------
> > > >
> > >
> > > Well, assuming that  the 'out$img' object has a R-length of n*m*o , wouldn't if be simpler to just change the return call to:
> >
> > In fact, out$img has a length of n, same as on input.  .Fortran won't change the length of its arguments.
> >
> > Duncan Murdoch
> >
> > >
> > > return( array( out$img, dim=c(n,m,o) )
> > >
> > > I don't think you wnat start naming your dimension vectors "c".
> > >
> >
>



More information about the R-help mailing list