Skip to content

Instantly share code, notes, and snippets.

@BerriJ
Created November 3, 2021 19:42
Show Gist options
  • Save BerriJ/940419b119b97481038fb23bf7333db4 to your computer and use it in GitHub Desktop.
Save BerriJ/940419b119b97481038fb23bf7333db4 to your computer and use it in GitHub Desktop.
Demo of handling fields of different dimensions between C++ (via RcppArmadillo) and R
#include <RcppArmadillo.h>
// [[Rcpp::export]]
arma::field<arma::mat> fieldtest1()
{
arma::field<arma::mat> out(5);
arma::mat B(5,4);
B.fill(10);
out(1) = B;
return out;
}
// [[Rcpp::export]]
arma::field<arma::mat> fieldtest2()
{
arma::field<arma::mat> out(5,2);
arma::mat B(5,4);
B.fill(10);
out(1,1) = B;
return out;
}
// [[Rcpp::export]]
arma::field<arma::mat> fieldtest3()
{
arma::field<arma::mat> out(5,2,3);
arma::mat B(5,4);
B.fill(10);
out(1,1,2) = B;
return out;
}
// [[Rcpp::export]]
arma::field<arma::mat> FieldInfieldOut(arma::field<arma::mat> x)
{
arma::cout << x.n_rows << "x rows" << arma::endl;
arma::cout << x.n_cols << "x cols" << arma::endl;
arma::cout << x.n_slices << "x slices" << arma::endl;
return x;
}
field1 <- fieldtest1()
dim(field1)
field2 <- fieldtest2()
dim(field2)
field3 <- fieldtest3()
dim(field3)
field1out <- FieldInfieldOut(field1)
dim(field1out)
field2out <- FieldInfieldOut(field2)
dim(field2out)
field3out <- FieldInfieldOut(field3)
dim(field3out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment