Skip to content
Prev 6032 / 10988 Next

[Rcpp-devel] Integer matrices in Rcpp and Armadillo

Hi Romain,

I considered the implicit use of Rcpp::as<>() inside of the arma::mat() constructor. My target was to reuse memory and try to use a very simple setting, i.e. (at least in my opinion) not too much lines of code. 

You answered to my question

"On the other side I then assume, that the Rcpp:as<class M>() function makes a cast and therefore creates a copy with new allocated memory of the type included in M? Therefore a reuse of memory with this function is not possible":

Depends what is M, but most of the time yes, as<> copies data.

What I then tried, was this:

setClass("myclass", representation(par = "list"))
l <- list(lambda = array(0, dim = c(10,2)))
mclass <- new("myclass", par = l)
cfunction <- cxxfunction(signature(myClass_R = "array"), body = 'Rcpp::S4 myclass(myClass_R); Rcpp::List parL((SEXP) myclass.slot("par")); arma::mat armaPar(Rcpp::as<Rcpp::NumericMatrix>(parL["lambda"]).begin(), 10, 2, false, true); armaPar(0,0) = 1.2 ;return Rcpp::wrap(myclass);', plugin = "RcppArmadillo")
cfunction(mclass)
An object of class "myclass"
Slot "par":
$lambda
      [,1] [,2]
 [1,]  1.2    0
 [2,]  0.0    0
 [3,]  0.0    0
 [4,]  0.0    0
 [5,]  0.0    0
 [6,]  0.0    0
 [7,]  0.0    0
 [8,]  0.0    0
 [9,]  0.0    0
[10,]  0.0    0

So, it seems, that calling Rcpp::as<>() implicitly inside the arma::mat() constructor, enables the reuse of memory with only a few lines of code, which is pretty nice. So in case of M being Rcpp::NumericMatrix, Rcpp::as<>() seems not to copy data. But it also could be, that I misunderstood your answer above, or that we were talking about two different things there. In this case please apologize my confusion.


Best

Simon
On Jun 8, 2013, at 8:08 AM, Romain Francois <romain at r-enthusiasts.com> wrote:

            

Thread (24 messages)

Simon Zehnder Integer matrices in Rcpp and Armadillo Jun 6 Dirk Eddelbuettel Integer matrices in Rcpp and Armadillo Jun 6 Romain Francois Integer matrices in Rcpp and Armadillo Jun 6 Simon Zehnder Integer matrices in Rcpp and Armadillo Jun 6 Romain Francois Integer matrices in Rcpp and Armadillo Jun 6 Dirk Eddelbuettel Integer matrices in Rcpp and Armadillo Jun 6 Simon Zehnder Integer matrices in Rcpp and Armadillo Jun 6 Simon Zehnder Integer matrices in Rcpp and Armadillo Jun 6 Romain Francois Integer matrices in Rcpp and Armadillo Jun 6 Dirk Eddelbuettel Integer matrices in Rcpp and Armadillo Jun 6 Dirk Eddelbuettel Integer matrices in Rcpp and Armadillo Jun 6 Simon Zehnder Integer matrices in Rcpp and Armadillo Jun 7 Romain Francois Integer matrices in Rcpp and Armadillo Jun 7 Simon Zehnder Integer matrices in Rcpp and Armadillo Jun 7 Simon Zehnder Integer matrices in Rcpp and Armadillo Jun 7 Romain Francois Integer matrices in Rcpp and Armadillo Jun 7 Simon Zehnder Integer matrices in Rcpp and Armadillo Jun 7 Romain Francois Integer matrices in Rcpp and Armadillo Jun 7 Simon Zehnder Integer matrices in Rcpp and Armadillo Jun 8 Kevin Ushey Integer matrices in Rcpp and Armadillo Jun 8 Dirk Eddelbuettel Integer matrices in Rcpp and Armadillo Jun 8 Dirk Eddelbuettel Integer matrices in Rcpp and Armadillo Jun 8 Simon Zehnder Integer matrices in Rcpp and Armadillo Jun 9 Simon Zehnder Integer matrices in Rcpp and Armadillo Jun 9