[Rcpp-devel] Question concerning use of Raster in C vi Rcpp plugin and inline

Christian Gunning xian at unm.edu
Wed Nov 17 10:47:10 CET 2010


On Tue, Nov 16, 2010 at 4:00 AM,
<rcpp-devel-request at lists.r-forge.r-project.org> wrote:
>    for ( y in 1:maxy ) {
>      cat(y, " ")
>      for (x in 1:maxx) {
>        s <- seeds[x:(x+dx2), y:(y+dy2)]
>        if (all(is.na(s))) {
>          dispSeeds[x,y] <- NA
>        } else {
>          dispSeeds[x,y] <- fx2(s, s, sd2D)
>        }
>      }
>    }

If maxy and maxx are large, I'm *guessing* that you could see
significant speed improvements from pushing these inner loops in the
C++ code.  Of course, profiling is always a nice way to *see* the
difference!

Ultimately, you'll want to control the random number seed from within
your C++ function.  See previous posts on the mailing list for
examples.

"You attached below a file named disp.cpp, which indicates to me that
your c code is in a separate file - is this the case? And if yes, how
can I do that with the inline command? Or do I have to compile
externally and use Rcpp directly?"

Honestly, if you plan to develop and use this extensively, make a
package with Rcpp.package.skeleton or RcppArmadillo.package.skeleton,
and drop said file there.  Much easier to edit, debug, and ultimately
document. See vignette('Rcpp-package', package='Rcpp') for details.

Lastly,
For "s <- seeds[x:(x+dx2), y:(y+dy2)]" :
Look at RcppArmadillo - If you turn seeds into a arma::matrix, you can
use arma's accessors to get native sub-raster access, something like
this, for an Rseeds input matrix to the c++ function:

NumericMatrix cseeds(Rseeds);
arma::mat aseeds(cseeds.begin(), cseeds.nrow(), cseeds.ncol());
...
    s = aseeds.submat( x, y, x+dx2, y+dy2 )
...
For details, see http://arma.sourceforge.net/docs.html#submat

best,
Christian, who is finding the Rcpp mailing list this week a
fascinating distraction from comprehensive exams.
-- 
A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!


More information about the Rcpp-devel mailing list