make: *** [file6fc2ab39965.o] Error 1
In addition: Warning message:
running command '/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file6fc2ab39965.cpp 2> file6fc2ab39965.cpp.err.txt' had status 1
-----------------------------------------------------------
It seems, that the SEXP inside the List is not yet an Rcpp::NumericMatrix, so I make it explicit to the arma::mat() constructor:
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(), 100, 10, false, true); armaPar(0,0) = 1.2 ;return Rcpp::wrap(myclass);', plugin = "RcppArmadillo")
That compiles.
If we let it run it gives us:
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, we can see, that implicitly calling Rcpp::as<>() inside the constructor arma::mat() avoids a copy by Rcpp::as<>() - which is just beautiful!
Best
Simon
On Jun 7, 2013, at 1:19 PM, Simon Zehnder <szehnder at uni-bonn.de> wrote:
Thank you Romain!
All clear now!
Best
Simon
On Jun 7, 2013, at 1:13 PM, Romain Francois <romain at r-enthusiasts.com> wrote:
Le 07/06/13 13:09, Simon Zehnder a ?crit :
HI Dirk, hi Romain,
allright, this is now clear to me, if I want to reuse memory, the allocated memory from R (so implicitly in C) must of course have the same type - otherwise the memory has a different size.
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.
Best
Simon
On Jun 6, 2013, at 8:31 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
On 6 June 2013 at 13:17, Dirk Eddelbuettel wrote:
|
| On 6 June 2013 at 19:05, Simon Zehnder wrote:
| | sorry I had overseen this message from you. Okay, so the explicit cast to SEXP together with the assignment operator makes the deal. But it still includes the reuse of memory right, i.e. the '=' does not call the copy constructor?
|
| But how could an _unsigned int_ from Armadillo possibly have the same value
| as a _signed int_ in R?
|
| Either you are efficient (no copy), or you are correct (with a copy). I do
| not see how you could have both.
Sorry -- please ignore this message.
I had mistakenly assumed that you were still thinking about arma::umat to
integer. For direct int-to-int this is indeed efficient just like the
double-to-double is for arma::mat to NumericMatrix (and dito for vectors). I
though that was a given -- maybe need to make this (even) more explicit in
the documentation.
So in sum: you get correct and efficient behaviour if and only if you stick
with the types natively supported in Armadillo and R.
Dirk
--
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com