module Nuggets::Array::InterpolateMixin

Public Instance Methods

interpolate([x0[, xN]]) → anArray click to toggle source

Simple linear interpolation of missing values in array.

Example:

[1, nil, 3, nil, 5].interpolate #=> [1, 2, 3, 4, 5]
# File lib/nuggets/array/interpolate_mixin.rb, line 40
def interpolate(x0 = 0, xN = x0)
  res, xX, interpolate = dup, [], lambda { |x|
    m = (x - x0) / xX.size.succ.to_f unless xX.empty?
    xX.each_with_index { |j, k| res[j] = m * k.succ + x0 }.clear
    x
  }

  each_with_index { |x, i| x ? x0 = interpolate[x] : xX << i }
  interpolate[xN] if xN
  res
end