Created
December 28, 2014 20:46
-
-
Save jjallaire/9e9912a109e1e12fe97b to your computer and use it in GitHub Desktop.
Rcpp modules with sourceCpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Rcpp.h> | |
using namespace Rcpp; | |
using namespace Rcpp; | |
class Uniform { | |
public: | |
Uniform(double min_, double max_) : min(min_), max(max_) {} | |
NumericVector draw(int n) const { | |
RNGScope scope; | |
return runif( n, min, max ); | |
} | |
double min, max; | |
}; | |
double uniformRange( Uniform* w) { | |
return w->max - w->min; | |
} | |
RCPP_MODULE(unif_module) { | |
class_<Uniform>( "Uniform" ) | |
.constructor<double,double>() | |
.field( "min", &Uniform::min ) | |
.field( "max", &Uniform::max ) | |
.method( "draw", &Uniform::draw ) | |
.method( "range", &uniformRange ) | |
; | |
} | |
/*** R | |
u <- new( Uniform, 0, 10 ) | |
u$draw(10L) | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment