Skip to content

Instantly share code, notes, and snippets.

@doobwa
doobwa / rcpp.modules.r
Created January 9, 2012 19:35
Rcpp Module example
# Borrowed from http://dirk.eddelbuettel.com/papers/rcpp_workshop_part_3_advanced.pdf
require(Rcpp)
require(inline)
require(testthat)
fx <- cxxfunction(,"",includes=
'
double norm( double x, double y ){
return sqrt( x*x + y*y) ;
@doobwa
doobwa / dfapply
Created October 3, 2011 04:03
dfapply: apply an expression to each row of a data.frame
# Chris DuBois
# October 2, 2011
# dfapply: apply an expression to each row of a data.frame
# This can be especially helpful when organizing experimental setups and you want to plot the results from a data.frame.
# s: data.frame, where each row has argument values
# expr: expression to evaluate on each row of s
@doobwa
doobwa / log.sum.exp.R
Created April 25, 2011 20:15
Computing log-sum-exponential
log.sum.exp<- function(x) {
# Computes log(sum(exp(x))
# Uses offset trick to avoid numeric overflow: http://jblevins.org/notes/log-sum-exp
offset <- x[which(x == max(abs(x)))]
log(sum(exp(x - offset))) + offset
}