GHK Algorithm for Simulation of Multivariate Normal Rectangle Probabilities in RCPP

When starting new projects in RCPP, the biggest hurdle is to re-implement existing code snippets for standard tasks that are not (yet) part of RCPP. I have implementations for certain full-conditional draws coded up efficiently in vectorized R. I have implementations in plain C and C++ that were previously accessed in compiled form throug dyn.load or .C. I started to slowly transition more and more snippets to RCPP C++.
Here's a reimplementation of the GHK simulator from bayesm. Some minor mofidications were necessary. Now it is ready for implementation within larger RCPP projects.

Have fun writing MCMC samplers or doing simulated ML.

brackets [[ for RCPP

One cool feature of R is the ease of data handling. Lists, data frames and matrices can easily be accessed and indexed, making vector-oriented programming easy. When switching over to C/RCPP, things are a little different. Personally, I like the Armadillo library, since it gets me as close to R or Matlab as possible. One thing I have been missing though is quick access to matrix elements through brackets and logical statements. I have written some functions for illustration:

Try it out

Truncated Normal [RcppArmadillo]

Rcpp and especially RcppArmadillo with its nice syntax have developed into an extremely powerful tool for R. The concept of vectors and matrices is transported into C, and the extra effort in coding isn't that big anymore. RStudio even syntax highlights the C++ code and offers a 'source' button which runs the sourceCpp comand from the Rcpp package.
More tutorials are being written, but I just want to provide some simple examples. Let's start with a simple inverse CDF approach to drawing from a univariate truncated Normal distribution:


Then it is straightforward to source the code in R.