Skip to content
Prev 6033 / 10988 Next

[Rcpp-devel] Integer matrices in Rcpp and Armadillo

I'm pretty the sure the Rcpp / RcppArmadillo way is to reuse memory
whenever the types match between the as'ed containers, and copy if type
coercion makes sense. Ie, an R integer vector --> an Rcpp IntegerVector
would reuse the same memory, while an R integer vector --> an Rcpp
NumericVector would require a copy and a coercion from integer to numeric.
Similar things will apply between R and RcppArmadillo. Just remember that
with R, numeric <-> double, integer <-> signed int.

A simple example to illustrate (also shows how you can discover these
things yourself):

---

#include <Rcpp.h>

// [[Rcpp::export]]
Rcpp::NumericVector test_numeric(Rcpp::NumericVector x) {

  x = x + 1;
  return x;

}

// [[Rcpp::export]]
Rcpp::IntegerVector test_integer(Rcpp::IntegerVector x) {
  x = x + 1;
  return x;
}

/*** R
x <- c(1, 2, 3)

test_numeric(x) ## returns c(2, 3, 4)
print(x) ## x is now c(2, 3, 4)

test_integer(x) ## returns c(3, 4, 5)
print(x) ## x is still c(2, 3, 4)
*/

---

Since this thread has become rather long, I suggest generating your own
minimal examples whenever you need to discover / be certain whether you're
reusing memory or forcing a copy.

-Kevin
On Sat, Jun 8, 2013 at 12:22 PM, Simon Zehnder <szehnder at uni-bonn.de> 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