Created
November 3, 2021 19:42
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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