|
|
Thinking inside the box | |||||
|
Bio
Code Papers Talks Linux Quantian About Blog
|
Rcpp and inline example
The following R code defines a character
variable ## now use Rcpp to pass down a parameter for the seed, and a vector size gslrng <- ' int seed = RcppSexp(s).asInt(); int len = RcppSexp(n).asInt(); gsl_rng *r; gsl_rng_env_setup(); std::vector<double> v(len); r = gsl_rng_alloc (gsl_rng_default); gsl_rng_set (r, (unsigned long) seed); for (int i=0; i<len; i++) { v[i] = gsl_rng_get (r); } gsl_rng_free(r); return RcppSexp(v).asSexp(); ' ## turn into a function that R can call ## compileargs redundant on Debian/Ubuntu as gsl headers are found anyway funx <- cfunction(signature(s="numeric", n="numeric"), gslrng, includes="#include <gsl/gsl_rng.h>", Rcpp=TRUE, cppargs="-I/usr/include", libargs="-lgsl -lgslcblas") print(funx(0, 5))
The As the R level, we now have a function of two arguments returning a vector of RNG draws of the given lenth and using the given seed.
Also note how we tell Finally, we simply call our freshly compiled, linked and loaded C++ function with arguments zero for the seed and five for the length, and print the results vector returned to R from C++. /computers/linux/debian/packages | permanent link |
|||||