Skip to content

Instantly share code, notes, and snippets.

@kevinushey
Created March 5, 2014 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinushey/9358992 to your computer and use it in GitHub Desktop.
Save kevinushey/9358992 to your computer and use it in GitHub Desktop.
gsl vs. R
// [[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