[Rcpp-devel] Integer matrices in Rcpp and Armadillo

Simon Zehnder szehnder at uni-bonn.de
Thu Jun 6 18:03:37 CEST 2013


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
--------------------------------------------------------
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



More information about the Rcpp-devel mailing list