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

Dirk Eddelbuettel edd at debian.org
Thu Sep 22 22:56:54 CEST 2016


Hi Jouni,

On 22 September 2016 at 20:38, Helske, Jouni wrote:
| 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).

I see. That is a potentially more hairy question because now we at the
'conversion from SEXP' (as a member of Rcpp::List) level.

The nice thing is that we have an existence proof in what Rcpp Attributes
does, so you could always re-run snippets like the one I have below with
verbose=TRUE (and maybe rebuild=TRUE).  In this case we see

  Rcpp::traits::input_parameter< arma::cube& >::type x(xSEXP);
  test2(x);

The differences between vec and mat on the one hand and cube on the other are
probably accidental. If you have time to chase this and clean it up a PR
would be great.

Dirk


| 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

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


More information about the Rcpp-devel mailing list