Blog Archives

Generating a multivariate gaussian distribution using RcppArmadillo

March 12, 2013
By
Generating a multivariate gaussian distribution using RcppArmadillo

There are many ways to simulate a multivariate gaussian distribution assuming that you can simulate from independent univariate normal distributions. One of the most popular method is based on the Cholesky decomposition. Let’s see how Rcpp and Armadillo perform on this task. #include <RcppArmadillo.h> // ]using namespace Rcpp;// ] arma::mat mvrnormArma(int n, arma::vec mu, arma::mat sigma) { int ncols...

Read more »

Generating a multivariate gaussian distribution using RcppArmadillo

March 12, 2013
By
Generating a multivariate gaussian distribution using RcppArmadillo

There are many ways to simulate a multivariate gaussian distribution assuming that you can simulate from independent univariate normal distributions. One of the most popular method is based on the Cholesky decomposition. Let’s see how Rcpp and Armadillo perform on this task. #include <RcppArmadillo.h> // ]using namespace Rcpp;// ] arma::mat mvrnormArma(int n, arma::vec mu, arma::mat sigma) { int ncols...

Read more »

Using Rcpp with Boost.Regex for regular expression

March 1, 2013
By
Using Rcpp with Boost.Regex for regular expression

Gabor asked about Rcpp use with regular expression libraries. This post shows a very simple example, based onone of the Boost.RegEx examples.We need to set linker options. This can be as simple as Sys.setenv("PKG_LIBS"="-lboost_regex")With that, the following example can be built: // cf www.boost.org/doc/libs/1_53_0/libs/regex/example/snippets/credit_card_example.cpp#include <Rcpp.h>#include <string> #include <boost/regex.hpp>bool validate_card_format(const std::string& s) { static const boost::regex e("(\\d{4}){3}\\d{4}"); ...

Read more »

Using Rcpp with Boost.Regex for regular expression

March 1, 2013
By
Using Rcpp with Boost.Regex for regular expression

Gabor asked about Rcpp use with regular expression libraries. This post shows a very simple example, based onone of the Boost.RegEx examples.We need to set linker options. This can be as simple as Sys.setenv("PKG_LIBS"="-lboost_regex")With that, the following example can be built: // cf www.boost.org/doc/libs/1_53_0/libs/regex/example/snippets/credit_card_example.cpp#include <Rcpp.h>#include <string> #include <boost/regex.hpp>bool validate_card_format(const std::string& s) { static const boost::regex e("(\\d{4}){3}\\d{4}"); ...

Read more »

Fast factor generation with Rcpp

February 27, 2013
By
Fast factor generation with Rcpp

Recall that factors are really just integer vectors with ‘levels’, i.e., character labels that get mapped to each integer in the vector. How can we take an arbitrary character, integer, numeric, or logical vector and coerce it to a factor with Rcpp? It’s actually quite easy with Rcpp sugar: #include <Rcpp.h> using namespace Rcpp;template <int RTYPE> IntegerVector fast_factor_template( const Vector<RTYPE>& x )...

Read more »

Fast factor generation with Rcpp

February 27, 2013
By
Fast factor generation with Rcpp

Recall that factors are really just integer vectors with ‘levels’, i.e., character labels that get mapped to each integer in the vector. How can we take an arbitrary character, integer, numeric, or logical vector and coerce it to a factor with Rcpp? It’s actually quite easy with Rcpp sugar: #include <Rcpp.h> using namespace Rcpp;template <int RTYPE> IntegerVector fast_factor_template( const Vector<RTYPE>& x )...

Read more »

Using Boost via the new BH package

January 31, 2013
By
Using Boost via the new BH package

Earlier today the new BH package arrived on CRAN. Over the years, Jay Emerson, Michael Kane and I had numerous discussions about a basic Boost infrastructure package providing Boost headers for other CRAN packages. JJ and Romain chipped in as well, and Jay finally took the lead by first creating a repo on...

Read more »

Sorting Numeric Vectors in C++ and R

January 31, 2013
By
Sorting Numeric Vectors in C++ and R

Consider the problem to sort all elements of the given vector in ascending order. We can simply use the function std::sort from the C++ STL. #include <Rcpp.h> using namespace Rcpp;// ] NumericVector stl_sort(NumericVector x) { NumericVector y = clone(x); std::sort(y.begin(), y.end()); return y; } library(rbenchmark) set.seed(123) z <- rnorm(100000) x <- rnorm(100)# check that stl_sort is the same as sort stopifnot(all.equal(stl_sort(x), sort(x)))#...

Read more »

Using Boost via the new BH package

January 31, 2013
By
Using Boost via the new BH package

Earlier today the new BH package arrived on CRAN. Over the years, Jay Emerson, Michael Kane and I had numerous discussions about a basic Boost infrastructure package providing Boost headers for other CRAN packages. JJ and Romain chipped in as well, and Jay finally took the lead by first creating a repo on...

Read more »

Sorting Numeric Vectors in C++ and R

January 31, 2013
By
Sorting Numeric Vectors in C++ and R

Consider the problem to sort all elements of the given vector in ascending order. We can simply use the function std::sort from the C++ STL. #include <Rcpp.h> using namespace Rcpp;// ] NumericVector stl_sort(NumericVector x) { NumericVector y = clone(x); std::sort(y.begin(), y.end()); return y; }library(rbenchmark) set.seed(123) z <- rnorm(100000) x <- rnorm(100)# check that stl_sort is the same as sort stopifnot(all.equal(stl_sort(x), sort(x)))#...

Read more »