Created
May 31, 2016 11:16
-
-
Save badbye/b20b9925355aae7a3c1b5dab9a8b1c7b to your computer and use it in GitHub Desktop.
Learn to write cpp in R. [High performance functions with Rcpp](http://adv-r.had.co.nz/Rcpp.html)
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 <Rcpp.h> | |
using namespace Rcpp; | |
//[[Rcpp::export]] | |
bool allC(LogicalVector x){ | |
int n = x.size(); | |
for (int i = 0; i < n; i++){ | |
if (x[i] == false){ | |
return false; | |
} | |
} | |
return true; | |
} | |
/*** | |
library(microbenchmark) | |
x = 1:1000 < 800 | |
microbenchmark(all(x), allC(x)) # slower | |
all(all(x) == allC(x)) # TRUE | |
*/ | |
//[[Rcpp::export]] | |
NumericVector cumprodC(NumericVector x){ | |
int n = x.size(); | |
NumericVector out(n); | |
out[0] = x[0]; | |
for (int i = 1; i < n; i++){ | |
out[i] = out[i - 1] * x[i]; | |
} | |
return out; | |
} | |
/*** | |
x = 1:5 | |
microbenchmark(cumprod(x), cumprodC(x)) # slower | |
all(cumprod(x) == cumprodC(x)) # TRUE | |
*/ | |
//[[Rcpp::export]] | |
NumericVector cumminC(NumericVector x){ | |
int n = x.size(); | |
NumericVector out(n); | |
out[0] = x[0]; | |
for (int i = 1; i < n; i++){ | |
if (x[i] < out[i - 1]){ | |
out[i] = x[i]; | |
}else{ | |
out[i] = out[i-1]; | |
} | |
} | |
return out; | |
} | |
/*** | |
x = c(4, 5, 3, 6, 1) | |
microbenchmark(cumminC(x), cummin(x)) # slower | |
all(cumminC(x) == cummin(x)) # TRUE | |
*/ | |
//[[Rcpp::export]] | |
NumericVector cummaxC(NumericVector x){ | |
int n = x.size(); | |
NumericVector out(n); | |
out[0] = x[0]; | |
for (int i = 1; i < n; i++){ | |
if (x[i] > out[i - 1]){ | |
out[i] = x[i]; | |
}else{ | |
out[i] = out[i-1]; | |
} | |
} | |
return out; | |
} | |
/*** | |
x = c(4, 5, 3, 6, 1) | |
microbenchmark(cummaxC(x), cummax(x)) # slower | |
all(cummaxC(x) == cummax(x)) # TRUE | |
*/ | |
//[[Rcpp::export]] | |
NumericVector diffC(NumericVector x, int lag=1){ | |
int n = x.size() - lag; | |
NumericVector out(n); | |
for (int i = 0; i < n; i++){ | |
out[i] = x[i + lag] - x[i]; | |
} | |
return out; | |
} | |
/*** | |
x = c(4, 5, 3, 6, 1) | |
microbenchmark(diffC(x), diff(x)) | |
all(diffC(x, lag=1) == diff(x)) # TRUE | |
microbenchmark(diffC(x), diff(x, 2)) | |
all(diffC(x, lag=2) == diff(x, 2)) # TRUE | |
*/ | |
//[[Rcpp::export]] | |
NumericVector rangeC(NumericVector x){ | |
int n = x.size(); | |
NumericVector out(2); | |
out[0] = out[1] = x[0]; | |
for (int i=0; i < n; i++){ | |
if (x[i] < out[0]){ | |
out[0] = x[i]; | |
} | |
if (x[i] > out[1]){ | |
out[1] = x[i]; | |
} | |
} | |
return out; | |
} | |
/*** | |
x = c(4, 5, 3, 6, 1) | |
microbenchmark(rangeC(x), range(x)) # faster | |
all(rangeC(x) == range(x)) # TRUE | |
*/ | |
//[[Rcpp::export]] | |
NumericVector varC(NumericVector x){ | |
int n = x.size(); | |
double out = 0, total = 0; | |
for (int i=0; i < n; i++){ | |
out += pow(x[i], 2); | |
total += x[i]; | |
} | |
double res = (out - pow(total, 2) / n) / (n - 1); | |
return res; | |
} | |
/*** | |
x = c(4, 5, 3, 6, 1) | |
microbenchmark(varC(x), var(x)) # faster | |
all(varC(x) - var(x) < 1e-9) # TRUE | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment