Created
March 5, 2014 00:48
-
-
Save kevinushey/9358992 to your computer and use it in GitHub Desktop.
gsl vs. 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
// [[Rcpp::depends(RcppGSL)]] | |
#include <gsl/gsl_randist.h> | |
#include <Rcpp.h> | |
using namespace Rcpp; | |
// [[Rcpp::export]] | |
NumericVector gsl_dexp(NumericVector x) { | |
int n = x.size(); | |
NumericVector output = no_init(n); | |
for (int i=0; i < n; ++i) { | |
output[i] = log( gsl_ran_exponential_pdf(x[i], 2.0) ); | |
} | |
return output; | |
} | |
// [[Rcpp::export]] | |
NumericVector R_dexp(NumericVector x) { | |
int n = x.size(); | |
NumericVector output = no_init(n); | |
for (int i=0; i < n; ++i) { | |
output[i] = Rf_dexp(x[i], 2.0, 1); | |
} | |
return output; | |
} | |
/*** R | |
x <- seq(0, 1, by=0.01) | |
gsl <- gsl_dexp(x) | |
R <- R_dexp(x) | |
all.equal(gsl, R) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment