Created
February 11, 2013 19:54
-
-
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
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
# 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