Last active
February 12, 2020 14:52
-
-
Save mikmart/66bf16f0bd329ec468aade6bf81fee96 to your computer and use it in GitHub Desktop.
Different ways to calculate row/col sums in C++
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
#define RCPP_ARMADILLO_RETURN_ANYVEC_AS_VECTOR | |
#include <RcppArmadillo.h> | |
// [[Rcpp::depends(RcppArmadillo)]] | |
using namespace Rcpp; | |
// Cols ---------------- | |
// [[Rcpp::export]] | |
arma::rowvec Arma_colSums(const arma::mat& x) { | |
return arma::sum(x, 0); | |
} | |
// [[Rcpp::export]] | |
NumericVector Sugar_colSums(const NumericMatrix& x) { | |
return colSums(x); | |
} | |
// [[Rcpp::export]] | |
NumericVector Cpp_colSums(const NumericMatrix& x) { | |
int nr = x.nrow(), nc = x.ncol(); | |
NumericVector ans(nc); | |
for (int j = 0; j < nc; j++) { | |
double sum = 0.0; | |
for (int i = 0; i < nr; i++) { | |
sum += x(i, j); | |
} | |
ans[j] = sum; | |
} | |
return ans; | |
} | |
// Rows ---------------- | |
// [[Rcpp::export]] | |
arma::colvec Arma_rowSums(const arma::mat& x) { | |
return arma::sum(x, 1); | |
} | |
// [[Rcpp::export]] | |
NumericVector Sugar_rowSums(const NumericMatrix& x) { | |
return rowSums(x); | |
} | |
// [[Rcpp::export]] | |
NumericVector Cpp_rowSums(const NumericMatrix& x) { | |
int nr = x.nrow(), nc = x.ncol(); | |
NumericVector ans(nr); | |
for (int j = 0; j < nc; j++) { | |
for (int i = 0; i < nr; i++) { | |
ans[i] += x(i, j); | |
} | |
} | |
return ans; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment