Skip to content

Instantly share code, notes, and snippets.

@nassimhaddad
Created February 11, 2013 19:54
Show Gist options
  • Save nassimhaddad/4757073 to your computer and use it in GitHub Desktop.
Save nassimhaddad/4757073 to your computer and use it in GitHub Desktop.
custom "filter" = cumulative sum with multiplier, written with inline C. It is incredibly fast
# looping through vector ####
library(inline)
sign <- signature(x="numeric", n="integer", d="numeric")
code <- "
for (int i=1; i < *n; i++) {
x[i] = x[i-1]*d[0] + x[i];
}"
c_fn <- cfunction(sign,
code,
convention=".C"
)
# usage: CCustomCumsum(1:5, 0.5)
CCustomCumsum <- function(vector, decay){
c_fn(x=vector, n=length(vector), d=decay)$x
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment