[Rcpp-devel] Returning a list of matrices: Interesting difference: Sometimes need wrap(); sometimes not

Dirk Eddelbuettel edd at debian.org
Sun Sep 9 13:45:47 CEST 2012


Interesting.  I don't think it has anything to do with inline or not (see
below).  If in doubt, do use wrap() as the 'it dropped the dim' reveals a
fallback to vector defaults.

When I use your last solution (the function) in inline, it gets the same
result:

edd at max:~$ r /tmp/soren.r 
Loading required package: methods
$X
     [,1] [,2]
[1,]    1    3
[2,]    2    4

$Xinv
     [,1] [,2]
[1,]   -2  1.5
[2,]    1 -0.5

$X
     [,1] [,2]
[1,]    1    3
[2,]    2    4

$Xinv
     [,1] [,2]
[1,]   -2  1.5
[2,]    1 -0.5

edd at max:~$ 

So I doubt we can blame inline. Code below.

Dirk


library(inline)

code <- '
arma::mat X    = as<arma::mat>(X_);
arma::mat Xinv = inv( X );
return Rcpp::List::create(Rcpp::Named("X", X),Rcpp::Named("Xinv", Xinv));'
sol <- cxxfunction(signature(X_ = "matrix"), code, plugin = "RcppArmadillo", verbose=F)
M <- matrix(c(1,2,3,4),nr=2)
print(sol(M))

inc <- '
RcppExport SEXP sol2 ( SEXP X_ ){
  arma::mat X    = as<arma::mat>(X_);
  arma::mat Xinv = inv( X );
  return Rcpp::List::create(Rcpp::Named("X", wrap(X)),Rcpp::Named("Xinv", wrap(Xinv)));
}
'
sol3 <- cxxfunction(signature(X_ = "matrix"), plugin = "RcppArmadillo", inc=inc, body='return sol2(X_);')
print(sol3(M))

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


More information about the Rcpp-devel mailing list