Skip to content

Instantly share code, notes, and snippets.

@robbielamb
Created March 17, 2010 20:04
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 robbielamb/335661 to your computer and use it in GitHub Desktop.
Save robbielamb/335661 to your computer and use it in GitHub Desktop.
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