Created
March 17, 2010 20:04
-
-
Save robbielamb/335661 to your computer and use it in GitHub Desktop.
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
class FractionalIntegrator | |
# Positive q is derivation | |
# Negative q is integration | |
def initialize(q = 0.5, history_length = 1000) | |
@q = q | |
@history = [] | |
@weights = [] | |
initialize_weights(q, history_length) | |
end | |
def add_number(number) | |
@history.unshift number | |
if @history.length > @weights.length | |
# Do the coopmans approximation. Sorry cal. | |
last = @history.pop | |
@history[-1] += (last * @weights[-1])/2.0 | |
end | |
self | |
end | |
alias :<< :add_number | |
def max_history | |
@weights.length | |
end | |
def to_f | |
(0..@history.length-1).inject(0){ |sum,i| sum + @weights[i] * @history[i] } | |
end | |
def to_s | |
to_f.to_s | |
end | |
private | |
def initialize_weights(q, count) | |
@weights[0] = 1.0 | |
(1..count-1).each do |k| | |
@weights[k] = @weights[k-1] * ((k.to_f - 1.0 - q) / k.to_f) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment