Dirk Eddelbuettel — written Jan 21, 2013 — source
Baptiste asked on StackOverflow about letting users supply C++ functions
for use with Armadillo / RcppArmadillo. This posts helps with an
extended answer. There is nothing specific about Armadillo here,
this would the same way with Eigen, the GSL or any other library a
user wants to support (and provides his or her own as<>()
and
wrap()
converters which we already have for Armadillo, Eigen and
the GSL).
To set the stage, let us consider two simple functions of a vector
These are pretty boring and standard functions, and we could simple
switch between them via if/else statements. Where it gets
interesting is via the SEXP
wrapping offered by XPtr
below.
But before we get there, let us do this one step at a time.
This typedef is important and just says that funcPtr
will take a
const reference to a vec and return a vector – just like our two
functions above
The following function takes a string argument, picks a function and returns it
wrapped as an external pointer SEXP
. We could return this to R as well.
A simple test of this function follows. First a function using it:
And then a call, showing access to both functions:
[,1] [1,] 2 [2,] 4 [3,] 6
[,1] [1,] 10 [2,] 20 [3,] 30
But more interestingly, we can also receive a function pointer via the SEXP
wrapping:
And use it in this function which no longer switches:
As seen here:
[,1] [1,] 2 [2,] 4 [3,] 6 [4,] 8
This is a reasonably powerful and generic framework offered by Rcpp and sitting on top of R’s external pointers.
tags: armadillo
Tweet