[Rcpp-devel] Integer matrices in Rcpp and Armadillo

Dirk Eddelbuettel edd at debian.org
Sat Jun 8 22:23:12 CEST 2013


Simon,

You (in my eyes needlessly) complicate matters further with the S4 wrapping.

The base case is simple, shown/used in src/fastLm.cpp right in the middle of
RcppArmadillo and discussed a million times:

extern "C" SEXP fastLm(SEXP Xs, SEXP ys) {

    try {
	Rcpp::NumericVector yr(ys);                     // creates Rcpp vector from SEXP
	Rcpp::NumericMatrix Xr(Xs);                     // creates Rcpp matrix from SEXP
	int n = Xr.nrow(), k = Xr.ncol();
	arma::mat X(Xr.begin(), n, k, false);           // reuses memory and avoids extra copy
	arma::colvec y(yr.begin(), yr.size(), false);

We use two operations to go from SEXP to Rcpp::Numeric{Vector,Matrix} and
then to arma::{colvec,mat}.  This is marginally faster than doing a single
as<arma::mat*>(Xs) cast.

The case for integer vectors and matrices is identical.

It might be a good idea for you to time alternative approaches.

Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list