Skip to content
Prev 4280 / 10988 Next

[Rcpp-devel] Getting and setting array dimensions - simpler ways?

Dear list,

I want to do some operations on an array, here exemplified by taking log (the operations I have in mind are made using RcppArmadillo):
B
A            b1         b2
  a1 -2.3025851 -0.9162907
  a2 -0.9162907 -2.3025851

It is essential that dim and dimnames attributes are preserved. My take on this task is:

src <-'
 using namespace arma;
 using namespace Rcpp;
 NumericVector p(pp_);
 // Some computations using RcppArmadillo
 vec pp = as<vec>(pp_);
 vec aa = log(pp);
 // Get the result back
 NumericVector ans(p.length());
 ans = aa;
 // Set attributes
 ans.attr("dim")      = p.attr("dim");
 ans.attr("dimnames") = p.attr("dimnames");
 return(wrap(ans));   
 '
B
A            b1         b2
  a1 -2.3025851 -0.9162907
  a2 -0.9162907 -2.3025851

This works, but if anyone can spot a more elegant (fewer lines) way of doing it, I would be happy to know...

Best regards
S?ren