[Rcpp-devel] Integer matrices in Rcpp and Armadillo

Romain Francois romain at r-enthusiasts.com
Thu Jun 6 19:47:46 CEST 2013


Le 06/06/13 19:05, Simon Zehnder a écrit :
> Hi Romain,
>
> 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?

Yep. No copy of data is made unless what goes in is not an integer matrix.

For example, if you pass in a numeric matrix, then data will be copied.

> Further, this only solves the ambiguity for the conversion from the class slot to an Rcpp::IntegerMatrix. in the next step then, I create the arma::it matrix:
>
> cfunction <- cxxfunction(signature(myClass_R = "array"), body = 'Rcpp::S4 myclass(myClass_R); Rcpp::IntegerMatrix iS = (SEXP) myclass.slot("S"); arma::imat armaS(iS.begin(), 100, 10, false, true); armaS.print("armaS:"); return Rcpp::wrap(1);', plugin = "RcppArmadillo")

Well, IIRC the purpose of this constructor ot Mat is to reuse the memory

> The result is an arma:imat matrix reusing the memory of the array in R (which of course has then to be array(integer(), dim = c(100,10)))?

yes

> Best
>
> Simon
>
>
> On Jun 6, 2013, at 6:37 PM, Romain Francois <romain at r-enthusiasts.com> wrote:
>
>> Le 06/06/13 18:03, Simon Zehnder a écrit :
>>> Hi Rcpp:Users, and Rcpp::Devels,
>>>
>>> I encountered the following problem when working today/yesterday on my project:
>>>
>>> 1. In R create an S4-object with the following slot:
>>>
>>> setClass("myclass", representation(S = "array"))
>>>
>>> mclass <- new("myclass", S = array(0, dim = c(100, 10)))
>>>
>>> 2. In C++ compile a function which does the following:
>>>
>>> library(inline)
>>> cfunction <- cxxfunction(signature(myClass_R = "array"), body = 'Rcpp::S4 myclass(myClass_R); Rcpp::IntegerMatrix iS(myclass.slot("S")); arma::umat armaS(iS.begin(), 100, 10, false, true); armaS.print("armaS:"); return Rcpp::wrap(1);', plugin = "RcppArmadillo")
>>>
>>> I get the error:
>>>
>>> Error in compileCode(f, code, language = language, verbose = verbose) :
>>>    Compilation ERROR, function(s)/method(s) not created! file2508289bd859.cpp: In function ‘SEXPREC* file2508289bd859(SEXP)’:
>>> file2508289bd859.cpp:30:70: error: call of overloaded ‘Matrix(Rcpp::RObject::SlotProxy)’ is ambiguous
>>>   Rcpp::S4 myclass(myClass_R); Rcpp::IntegerMatrix iS(myclass.slot("S")); arma::umat armaS(iS.begin(), 100, 10, false, true); armaS.print("armaS:"); return Rcpp::wrap(1);
>>>                                                                        ^
>>> file2508289bd859.cpp:30:70: note: candidates are:
>>> In file included from /Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include/Rcpp/Vector.h:60:0,
>>>                   from /Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include/Rcpp/Promise.h:26,
>>>                   from /Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include/Rcpp.h:34,
>>>                   from /Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include/RcppArmadillo.
>>> In addition: Warning message:
>>> running command '/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file2508289bd859.cpp 2> file2508289bd859.cpp.err.txt' had status 1
>>
>> There is compiler ambiguity, one way to deal with it is to use this;
>>
>> IntegerMatrix iS = (SEXP)myClass.slot("S");
>>
>> The (SEXP) will deal with the ambiguity.
>>
>>> --------------------------------------------------------
>>> NEXT APPROACH:
>>>
>>> 1. Set the S to integers:
>>>
>>> mclass <- new("myclass", S = array(integer(), dim = c(100, 10)))
>>>
>>> 2. Compile the C++ function
>>>
>>> cfunction <- cxxfunction(signature(myClass_R = "array"), body = 'Rcpp::S4 myclass(myClass_R); Rcpp::IntegerMatrix iS(myclass.slot("S")); arma::umat armaS(iS.begin(), 100, 10, false, true); armaS.print("armaS:"); return Rcpp::wrap(1);', plugin = "RcppArmadillo")
>>>
>>> I get the error:
>>>
>>> Error in compileCode(f, code, language = language, verbose = verbose) :
>>>    Compilation ERROR, function(s)/method(s) not created! file2508f98cfe3.cpp: In function ‘SEXPREC* file2508f98cfe3(SEXP)’:
>>> file2508f98cfe3.cpp:30:70: error: call of overloaded ‘Matrix(Rcpp::RObject::SlotProxy)’ is ambiguous
>>>   Rcpp::S4 myclass(myClass_R); Rcpp::IntegerMatrix iS(myclass.slot("S")); arma::umat armaS(iS.begin(), 100, 10, false, true); armaS.print("armaS:"); return Rcpp::wrap(1);
>>>                                                                        ^
>>> file2508f98cfe3.cpp:30:70: note: candidates are:
>>> In file included from /Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include/Rcpp/Vector.h:60:0,
>>>                   from /Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include/Rcpp/Promise.h:26,
>>>                   from /Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include/Rcpp.h:34,
>>>                   from /Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include/RcppArmadillo.h:30
>>> In addition: Warning message:
>>> running command '/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file2508f98cfe3.cpp 2> file2508f98cfe3.cpp.err.txt' had status 1
>>> --------------------------------------------------------
>>> NEXT APPROACH:
>>>
>>> 1. Create a class object:
>>>
>>> mclass <- new("myclass", S = array(0, dim = c(100, 10)))
>>>
>>> 2. Compile C++-function by using Rcpp::as<>() for type casting:
>>>
>>> cfunction <- cxxfunction(signature(myClass_R = "array"), body = 'Rcpp::S4 myclass(myClass_R); Rcpp::IntegerMatrix iS(Rcpp::as<Rcpp::IntegerMatrix>(myclass.slot("S"))); arma::umat armaS(iS.begin(), 100, 10, false, true); armaS.print("armaS:"); return Rcpp::wrap(1);', plugin = "RcppArmadillo")
>>>
>>> I get the error:
>>>
>>> Error in compileCode(f, code, language = language, verbose = verbose) :
>>>    Compilation ERROR, function(s)/method(s) not created! file250879784814.cpp: In function ‘SEXPREC* file250879784814(SEXP)’:
>>> file250879784814.cpp:30:130: error: invalid conversion from ‘Rcpp::Matrix<13>::iterator {aka int*}’ to ‘unsigned int*’ [-fpermissive]
>>>   Rcpp::S4 myclass(myClass_R); Rcpp::IntegerMatrix iS(Rcpp::as<Rcpp::IntegerMatrix>(myclass.slot("S"))); arma::umat armaS(iS.begin(), 100, 10, false, true); armaS.print("armaS:"); return Rcpp::wrap(1);
>>>                                                                                                                                    ^
>>> In file included from /Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include/armadillo:138:0,
>>>                   from /Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include/RcppArmadilloForward.h:36,
>>>                   from /Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include/RcppArmadillo.h:29,
>>>
>>> In addition: Warning message:
>>> running command '/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file250879784814.cpp 2> file250879784814.cpp.err.txt' had status 1
>>>
>>> Further, I get the same errors for
>>>
>>> setClass("myclass", representation(S = "matrix")) instead of setClass("myclass", representation(S = "array"))
>>>
>>> --------------------------------------------------------
>>>
>>> It runs though, when I use the following:
>>>
>>> 1. Set the class:
>>>
>>>   setClass("myclass", representation(S = "array"))
>>> mclass <- new("myclass", S = array(0, dim = c(100, 10)))
>>>
>>> 2. Compile the function in C++:
>>>
>>> cfunction <- cxxfunction(signature(myClass_R = "array"), body = 'Rcpp::S4 myclass(myClass_R); Rcpp::NumericMatrix iS(Rcpp::as<Rcpp::NumericMatrix>(myclass.slot("S"))); arma::mat armaS(iS.begin(), 100, 10, false, true); armaS.print("armaS:"); return Rcpp::wrap(1);', plugin = "RcppArmadillo")
>>>
>>> 3. Run the function:
>>>
>>> cfunction(mclass)
>>>
>>>
>>> ------------------------------------------------------
>>>
>>> What I need is an arma::umat in C++ and if possible in addition an integer array in R. Is this possible?
>>>
>>>
>>> Best
>>>
>>> Simon
>>>
>>> _______________________________________________
>>> Rcpp-devel mailing list
>>> Rcpp-devel at lists.r-forge.r-project.org
>>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>>>
>>
>>
>> --
>> Romain Francois
>> Professional R Enthusiast
>> +33(0) 6 28 91 30 30
>>
>> R Graph Gallery: http://gallery.r-enthusiasts.com
>>
>> blog:            http://blog.r-enthusiasts.com
>> |- http://bit.ly/Zs97qg  : highlight 0.4.1
>> `- http://bit.ly/10X94UM : Mobile version of the graph gallery
>>
>> _______________________________________________
>> Rcpp-devel mailing list
>> Rcpp-devel at lists.r-forge.r-project.org
>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
>


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30

R Graph Gallery: http://gallery.r-enthusiasts.com

blog:            http://blog.r-enthusiasts.com
|- http://bit.ly/Zs97qg  : highlight 0.4.1
`- http://bit.ly/10X94UM : Mobile version of the graph gallery



More information about the Rcpp-devel mailing list