On Stack Overflow the question was asked how to numerically integrate a function over a infinite range in Rcpp, e.g. by using RcppNumerical. As an example, the integral
\[ \int_{-\infty}^{\infty} \mathrm{d}x \exp\left(-\frac{(x-\mu)^4}{2}\right) \]
was given. Using RcppNumerical is straight forward. One defines a class that extends Numer::Func for the function and an interface function that calls Numer::integrate on it:
// [[Rcpp::depends(RcppEigen)]] // [[Rcpp::depends(RcppNumerical)]] #include <RcppNumerical.h> class exp4: public Numer::Func { private: double mean; public: exp4(double mean_) : mean(mean_) {} double operator()(const double& x) const { return exp(-pow(x-mean, 4) / 2); } }; // [[Rcpp::export]] Rcpp::NumericVector integrate_exp4(const double &mean, const double &lower, const double &upper) { exp4 function(mean); double err_est; int err_code; const double result = Numer::integrate(function, lower, upper, err_est, err_code); return Rcpp::NumericVector::create(Rcpp::Named("result") = result, Rcpp::Named("error") = err_est); } This works fine for finite ranges:
I have blogged about dqrng before, but I forgot to write about the v0.2.0 release, even though that brought a lot of changes including the fast sampling methods to CRAN:
Add R side support for selecting multiple streams for parallel usage. Implement long_jump() for Xo(ro)shiro as alternative to jump() providing fewer streams with longer period. Handle R’s RNG scope properly during initialisation. New functions dqsample and dqsample.int using an unbiased sampling algorithm.