[Rcpp-devel] Integer matrices in Rcpp and Armadillo

Simon Zehnder szehnder at uni-bonn.de
Sun Jun 9 14:01:15 CEST 2013

Hi Dirk,

thank you very much for this suggestion. I will time several approaches when I have finished the logic of my algorithm. 


On Jun 8, 2013, at 10:23 PM, Dirk Eddelbuettel <edd at debian.org> wrote:

> 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