Skip to content

Instantly share code, notes, and snippets.

@rvalieris
Last active January 22, 2016 13:06
Show Gist options
  • Save rvalieris/24ad10caac132757ff31 to your computer and use it in GitHub Desktop.
Save rvalieris/24ad10caac132757ff31 to your computer and use it in GitHub Desktop.
cube sum test example
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::mat cube_sum1(arma::cube c) {
arma::mat ss(c.n_cols, c.n_slices, arma::fill::zeros);
for(int i = 0; i < c.n_rows; i++) {
ss += c.tube(arma::span(i), arma::span::all);
}
return ss;
}
// [[Rcpp::export]]
arma::mat cube_sum2(arma::cube c) {
arma::mat ss(c.n_cols, c.n_slices, arma::fill::zeros);
for(int i = 0; i < c.n_rows; i++) {
for(int j = 0; j < c.n_cols; j++) {
for(int k = 0; k < c.n_slices; k++) {
ss(j,k) += c(i,j,k);
}}}
return ss;
}
/*** R
c <- array(runif(96*21*1),c(96,21,1))
print(cube_sum1(c))
print(cube_sum2(c))
*/
# run as R -d valgrind -f test.R
library(Rcpp)
sourceCpp("test.cpp")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment