Last active
January 22, 2016 13:06
-
-
Save rvalieris/24ad10caac132757ff31 to your computer and use it in GitHub Desktop.
cube sum test example
This file contains hidden or 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::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)) | |
*/ |
This file contains hidden or 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
# 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