Replies: 3 comments 17 replies
|
It may help to step back. 'Plain old scalar' types like Otherwise what you do in your post seems roughly correct. The |
|
For Armadillo matrices, you can use a small lambda expression, initialise with arma advanced ctor, and invoke it immediately: cppFunction("
arma::mat foo(Nullable<NumericMatrix> m = R_NilValue) {
const arma::mat& x = [&m] {
if (m.isNotNull()) {
NumericMatrix M(m);
arma::mat x(M.begin(), M.nrow(), M.ncol(), false, true); // copy_aux_mem is set to false, no copying
return x;
} else {
arma::mat x(0, 0, arma::fill::none);
return x;
}
}(); // invoke!
return x;
}
/*** R
foo(m = NULL)
foo(m = matrix(1:4, ncol = 2))
*/
", depends="RcppArmadillo")From R console: foo()
<0 x 0 matrix>
foo(matrix(1:4, ncol = 2))
[,1] [,2]
[1,] 1 3
[2,] 2 4 |
|
To sum up, and circle back to the original point by @m-muecke : Going via SEXP to Rcpp::NumericMatrix to (advanced) ctor for arma::mat with re-use --- which used to be the default for the Armadillo package until an interface rewrite / enhancement in the early 2010s -- seems to be the only 'non-copy way'. We can transfer arma::mat object that way (including via the I don't think that is a big deal as Nullable is chiefly a helper for function signature objects which tend to be small (and often scalar). And when one needs to transfer a yuge matrix and is afraid of copies, the moer verbose alternate exists. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I found the gallery post on working with Nullable, but couldn't find guidance specific to scalar types (integers and doubles) or Armadillo matrices. I wanted to check if the pattern below is a recommended approach.
All reactions