[Rcpp-devel] Pointer troubles
Dirk Eddelbuettel
edd at debian.org
Fri Aug 19 05:28:06 CEST 2011
On 18 August 2011 at 19:27, Christian Gunning wrote:
| > Comment: A concise example that uses an XPtr in a C++ function is
| > still lacking.
|
| Sorry I missed Manuel's example when I last posted. I guess I was
| thinking something like below, which is a direct analog of apply. This
| concisely mirrors the semantics in RcppDE (interchangable work
| function for a well-defined task). Here, the user needs only change
| inc. Only thing is, this example seems annoyingly long for inclusion
| in the quickref.
Well do not have a publisher-mandated page limit, so I'd say the more the
merrier!
| Also, I'm not sure whether using inline here is more
| or less confusing to the overall idea...
I like it. Maybe add a comment or two more, but the basic idea comes across
nicely.
Dirk
| Any thoughts on documentation priorities?
| -xian
|
| require(inline); require(Rcpp)
|
| inc <- '
| // Define a simple function
| double myFun(SEXP xs) { // sum
| Rcpp::NumericVector x(xs);
| double sum = std::accumulate(x.begin(), x.end(), 0.0);
| return(sum);
| }
| '
|
| src <- '
| // Define a pointer to the above function
| typedef double (*funcPtr)(SEXP);
| return(XPtr<funcPtr>(new funcPtr(&myFun)));
| '
|
| src1 <- '
| // Define a pointer to the above function
| typedef double (*funcPtr)(SEXP);
| XPtr<funcPtr> myptr(infun);
| // apply myptr to rows/columns of x
| NumericMatrix xx(x);
| int margin = as<int>(margin_);
| int nr = xx.nrow();
| int nc = xx.ncol();
| if ( margin == 1 ) {
| NumericVector ret(nr);
| for( int i = 0; i<nr; i++) {
| ret[i] = (*myptr)( wrap(xx(i,_)) );
| };
| return(ret);
| };
| if ( margin == 2 ) {
| NumericVector ret(nc);
| for( int i = 0; i<nc; i++) {
| ret[i] = (*myptr)( wrap(xx(_,i)) );
| };
| return(ret);
| };
| '
|
| mksum <- cxxfunction(signature(), body = src, includes = inc, plugin="Rcpp")
| myapply <- cxxfunction(signature(x='numeric', margin_='integer', infun='XPtr'),
| body = src1, includes = inc, plugin="Rcpp")
|
| aa = matrix(1:100, nrow=5)
| res1 = myapply(aa, 1, mksum() )
| res1R = apply(aa, 1, sum)
| res2 = myapply(aa, 2, mksum() )
| res2R = apply(aa, 2, sum)
|
| --
| A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!
--
Two new Rcpp master classes for R and C++ integration scheduled for
New York (Sep 24) and San Francisco (Oct 8), more details are at
http://dirk.eddelbuettel.com/blog/2011/08/04#rcpp_classes_2011-09_and_2011-10
http://www.revolutionanalytics.com/products/training/public/rcpp-master-class.php
More information about the Rcpp-devel
mailing list