Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dim problem with arma::field inputs #352

Merged
merged 4 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions inst/include/RcppArmadilloAs.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,29 @@ namespace traits {
class Exporter< arma::field<T> > {
public:
Exporter(SEXP x) : data(x){}

inline arma::field<T> get() {
size_t n = data.size() ;
arma::field<T> out( n ) ;
for(size_t i=0; i<n; i++){
out[i] = as<T>(data[i]) ;
size_t n = data.size();
arma::field<T> out(n);
# if defined(RCPP_ARMADILLO_FIX_FieldExporter)
if(!Rf_isNull(data.attr("dim"))){
arma::ivec dims = data.attr("dim");
if (dims.n_elem == 1)
{
out.set_size(n);
}
else if (dims.n_elem == 2)
{
out.set_size(dims(0), dims(1));
}
else if (dims.n_elem == 3)
{
out.set_size(dims(0), dims(1), dims(2));
}
}
# endif
for (size_t i = 0; i < n; i++)
{
out(i) = as<T>(data[i]);
}
return out ;
}
Expand Down
9 changes: 9 additions & 0 deletions inst/include/RcppArmadilloConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,14 @@
//#define RCPP_ARMADILLO_RETURN_ROWVEC_AS_VECTOR
//#define RCPP_ARMADILLO_RETURN_ANYVEC_AS_VECTOR

// To preserve all dims of arma::field when passing to R the following macro
// can be defined before including RcppArmadillo.h.
// see https://github.com/RcppCore/RcppArmadillo/pull/352
// #define RCPP_ARMADILLO_FIX_FieldImporter

// To preserve all dims of and arma::field input argument when passing to C++
// the following macro can be defined before including RcppArmadillo.h.
// see https://github.com/RcppCore/RcppArmadillo/pull/352
// #define RCPP_ARMADILLO_FIX_FieldExporter

#endif
6 changes: 5 additions & 1 deletion inst/include/RcppArmadilloWrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ namespace Rcpp{
template <typename T>
SEXP wrap( const arma::field<T>& data){
RObject x = wrap( RcppArmadillo::FieldImporter<T>( data ) ) ;
x.attr("dim" ) = Dimension( data.n_rows, data.n_cols ) ;
# if defined(RCPP_ARMADILLO_FIX_FieldImporter)
x.attr("dim" ) = Dimension( data.n_rows, data.n_cols, data.n_slices ) ;
BerriJ marked this conversation as resolved.
Show resolved Hide resolved
#else
x.attr("dim" ) = Dimension( data.n_rows, data.n_cols ) ;
#endif
return x ;
}

Expand Down
1 change: 1 addition & 0 deletions inst/tinytest/test_rcpparmadillo.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ expect_equal( res[[2]][[2]], matrix(0, ncol = 5, nrow=1))#, msg = "zeros<fmat>(5
expect_equal( res[[3]][[1]], matrix(0, ncol = 1, nrow=5))#, msg = "zeros<mat>(1,5)" )
expect_equal( res[[3]][[2]], matrix(0, ncol = 1, nrow=5))#, msg = "zeros<mat>(1,5)" )

# TODO: Revisit field tests later and fix see https://github.com/RcppCore/RcppArmadillo/pull/352
expect_equal( res[[4]][[1]], matrix(0:3, ncol = 2, nrow=2))#, msg = "field<int>" )
expect_equal( res[[4]][[2]], matrix(letters[1:4], ncol = 2, nrow=2))#, msg = "field<std::string>" )

Expand Down
5 changes: 5 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

using namespace Rcpp;

#ifdef RCPP_USE_GLOBAL_ROSTREAM
BerriJ marked this conversation as resolved.
Show resolved Hide resolved
Rcpp::Rostream<true>& Rcpp::Rcout = Rcpp::Rcpp_cout_get();
Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
#endif

// armadillo_version
Rcpp::IntegerVector armadillo_version(bool single);
RcppExport SEXP _RcppArmadillo_armadillo_version(SEXP singleSEXP) {
Expand Down