How do I use pi in RcppEigen?

I am very new to Rcpp

, or rather, RcppEigen

and am struggling with how to use pi

as a constant in my code. The code runs many times in the MCMC algorithm, so any speed improvement would be ideal. Currently I am passing pi every time I call the function like in the following code:

require(RcppEigen)
require(inline)

I.Cpp <- "
using Eigen::Map;
using Eigen::MatrixXd;
using Eigen::VectorXd;
using Rcpp::NumericVector;

const Map<MatrixXd> delta(as<Map<MatrixXd> >(delta0));
const Map<VectorXd> d(as<Map<VectorXd> >(DD));
const Rcpp::NumericVector tpi(pie);
double pi = tpi[0];

const MatrixXd I = delta.transpose() * d.asDiagonal() * pi * pi;
return wrap(I);
"

I.cpp <- cxxfunction(signature(delta0 = "matrix", DD = "numeric", pie = "numeric"), I.Cpp, plugin = "RcppEigen")


delta0 <- matrix(rnorm(25), 5)
DD <- rnorm(5)

I.cpp(delta0, DD, pi) # this piece of code gets called multiple times?

      

My question is, how can I use a constant pi

internally RcppEigen

without passing it in on every call I.cpp

?

+3


source to share


1 answer


First of all grep for pi

in /usr/share/R/include/

, and you will find for example

  #define M_PI        3.141592653589793238462643383280    /* pi */

      

so you have where R is used, for example here with Rcpp and RcppEigen.



Example:

R> getpi <- cppFunction('double twopi() { return M_PI; } ')
R> getpi()
[1] 3.142
R> print(getpi(), digits=20)
[1] 3.141592653589793116
R> 

      

I'm sure it's in the headlines too. [Checks: Yup, starting at math.h

. ] Perhaps several times. Grepping through other sources can also be fruitful.

+3


source







All Articles