[Rcpp-devel] Rcpp / RcppArmadillo storage

Gregor Kastner gregor.kastner at wu.ac.at
Tue Feb 18 15:28:47 CET 2014


Dear all,

I suppose this is a common task, nevertheless I could not find any
posts/documentation on it. Please excuse me if I missed something, I'd
appreciate any pointers.

What I'd like to do is the following:

I have a function "myfun" (that I don't want to modify because it's used
elsewhere) which is called often, thus copies of variables should be
minimized. The results of the computations in myfun should be stored in an
RcppArmadillo cube, where results from call 1 are stored in slice 1, results
from call 2 are stored in slice 2 etc. The current slice is also needed as
input to the function, its values should be simply overwritten.

To be more concrete: I'd like to call

void myfun(NumericMatrix &data) {
 // some complex operations
 data = data + data;
 // some more complex operations
}

as follows:

arma::cube results(10, 20, 100, fill::randu);

for (int i = 0; i < 100; i++) {
 myfun(results.slice(i));
}

This fails with:

error: invalid initialization of reference of type ‘Rcpp::NumericMatrix& {aka
Rcpp::Matrix<14>&}’ from expression of type ‘arma::Mat<double>’

If instead I use:

void myfun(arma::mat &data) {
 // some complex operations
 data = data + data;
 // some more complex operations
}

everything works fine (but, like said above, I can't modify myfun).

Any ideas, suggestions?

Thanks,
Gregor


More information about the Rcpp-devel mailing list