Skip to content

Commit

Permalink
Merge pull request #352 from BerriJ/fix-field_dims
Browse files Browse the repository at this point in the history
Fix dim problem with arma::field inputs
  • Loading branch information
eddelbuettel authored Nov 10, 2021
2 parents 524b2af + ec2e7ec commit 018a8be
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
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 ) ;
#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
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

0 comments on commit 018a8be

Please sign in to comment.