Skip to content

Instantly share code, notes, and snippets.

@pati-ni
Last active March 13, 2018 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pati-ni/f253e36e3ed681894c5ebe04fd962d49 to your computer and use it in GitHub Desktop.
Save pati-ni/f253e36e3ed681894c5ebe04fd962d49 to your computer and use it in GitHub Desktop.
Rcpp with armadillo
// advanced constructor
// mat(ptr_aux_mem, n_rows, n_cols, copy_aux_mem = true, strict = false)
#include <RcppArmadillo.h>
#include <Rcpp.h>
// Pass by reference, readonly
arma::mat testmx( const arma::mat& dat)
{
return dat;
}
// Pass by reference, can be modified
arma::mat testmx2(arma::mat& dat)
{
return dat;
}
arma::mat testmx(const arma::mat& dat);
RcppExport SEXP _testmx(SEXP datSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< const arma::mat& >::type dat(datSEXP);
rcpp_result_gen = Rcpp::wrap(testmx(dat));
return rcpp_result_gen;
END_RCPP
}
arma::mat testmx2(arma::mat& dat);
RcppExport SEXP _testmx2(SEXP datSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< arma::mat& >::type dat(datSEXP);
rcpp_result_gen = Rcpp::wrap(testmx2(dat));
return rcpp_result_gen;
END_RCPP
}
@pati-ni
Copy link
Author

pati-ni commented Mar 13, 2018

Rcpp takes care of the memory management by wrapping the arrays accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment