[Rcpp-devel] Initializing Armadillo vec/cube from NumericVector using as conversion

Helske, Jouni jouni.helske at jyu.fi
Thu Sep 22 22:38:13 CEST 2016


Hi Dirk,

yes I am familiar with new style (thank you for that, it makes things much prettier), but I still have to (or do I?...) use the as<>() when I am picking the pieces out of Rcpp::List (https://github.com/helske/bssm/blob/master/src/gssm.cpp):

....
gssm::gssm(const List model, unsigned int seed) :
  y(as<arma::vec>(model["y"])), Z(as<arma::mat>(model["Z"])),
 H(as<arma::vec>(model["H"])), T(as<arma::cube>(model["T"])), ...

I don't want to modify objects on the R side, so I had to use clone for the List model. I was just wondering why the behaviour is different for cube (tries to avoid a copy) than for vec/mat when using as<>() (always? forces a copy).

Jouni

________________________________________
Lähettäjä: Dirk Eddelbuettel [edd at debian.org]
Lähetetty: 22. syyskuuta 2016 23:09
Vastaanottaja: Dirk Eddelbuettel
Kopio: Helske, Jouni; rcpp-devel at lists.r-forge.r-project.org
Aihe: Re: [Rcpp-devel] Initializing Armadillo vec/cube from NumericVector using as conversion

Oh, and it should work the same for cube types:

R> cppFunction("void test2(arma::cube & x) { x(0,0,0) = 0.5; }",
R> depends="RcppArmadillo")
R> v <- array(sqrt(1:8), c(2, 2, 2))
R> v
, , 1

        [,1]    [,2]
        [1,] 1.00000 1.73205
        [2,] 1.41421 2.00000

, , 2

        [,1]    [,2]
        [1,] 2.23607 2.64575
        [2,] 2.44949 2.82843

R> test2(v)
R> v
, , 1

        [,1]    [,2]
        [1,] 0.50000 1.73205
        [2,] 1.41421 2.00000

, , 2

        [,1]    [,2]
        [1,] 2.23607 2.64575
        [2,] 2.44949 2.82843

R>

Proxy object that still connected to the outside object, unless a cast is
needed (int <-> double), or a copy is enforced (clone(), possibly via as<>()).

Dirk

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


More information about the Rcpp-devel mailing list