# Re: [Rd] inline C/C++ in R: question and suggestion

From: Simon Urbanek <simon.urbanek_at_r-project.org>
Date: Tue, 22 May 2007 15:16:32 -0400

Oleg,

On May 22, 2007, at 1:59 PM, Oleg Sklyar wrote:

> We've been discussing in the group that it would be nice to have a
> mechanism for something like "inline" C/C++ function calls in R. I
> do not want to reinvent the wheel, therefore, if something like
> that already exists, please give me a hint -- I could not find
> anything. If not, here is a working solution, please criticise so I
> could improve it.
>
> Example: I work on images (Bioconductor:EBImage) and just came to a
> point where I need to apply certain functions to image data, which
> are grey scale intensities in the range [0,1]. Assume I want to
> transform my image data from I(x,y,i) to exp(-(d/s)^2)*I(x,y,i),
> where I is the original intensity in dependence on coordinates x,y
> and frame i; s is a given value and d^2=(x-centre.x)^2+(y-centre.y)
> ^2 for a given centre. Trying an R loop will run forever already on
> moderate image sizes as I do not see how to vectorize it.
>

d=(cx-rep(1:dim(I),dim(I)*dim(I)))^2+(cy-rep(1:dim(I) ,each=dim(I),times=dim(I)))^2
I=I*exp(-(d/s^2))

Clearly the drawback is the use of memory, but you could vectorize per frame if you wish. At any rate it's not that slow anyway:

> I=array(runif(100*100*10),c(100,100,10))  > system.time({d=(cx-rep(1:dim(I),dim(I)*dim(I)))^2+(cy-rep (1:dim(I),each=dim(I),times=dim(I)))^2;I=I*exp(-(d/s^2))})

user system elapsed
0.022 0.010 0.032
> system.time(funx(I,15,c(30,35)))

user system elapsed
0.008 0.001 0.010

Of course C wins, no doubt about that :).

> Now, below is the solution using the "inline" C code, completely in
> R and runs instantly. I created a small package "inline" that
> simply encapsulates a quite simple function "cfunction". The
> package source is available from http://www.ebi.ac.uk/~osklyar/
> inline -- please give it a try and I would be happy to hear your
> calls and on the current one.

I really like the idea! Except for the fact that it's forcing the use of C++ which adds unnecessary overhead :P I'd like a configurable extension [including .m] and the ability to prepend functions as code. What would be very useful now is a set of tools that would allow you to construct the source with R commands, so that you could compute on it, edit it etc. That would be really cool ... you could even imagine compiling a very restricted set of R into C code ... yes, I know I'm dreaming ;)

Thanks for the good idea!

Cheers,
Simon

R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue 22 May 2007 - 19:20:16 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 23 May 2007 - 11:33:40 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.