class MadMath::NewtonPolynomial

Public Class Methods

new(data = []) click to toggle source
# File lib/mad_math/newton_polynomial.rb, line 3
def initialize(data = [])
  @xs = []
  @ys = []
  @calc = []

  #pp data
  #puts '-' * 80

  data.each do |el|
    raise "Element is missing." if el.length != 2
    add el.first, el.last
  end
end

Public Instance Methods

add(x, y) click to toggle source
# File lib/mad_math/newton_polynomial.rb, line 17
def add(x, y)
  @xs << x
  @ys << y

  calc(x) if @xs.length > 1
end
value_for(x) click to toggle source
# File lib/mad_math/newton_polynomial.rb, line 24
def value_for(x)
  y = @ys.first
  #print "#{@ys.first} + "
  @calc.each_with_index do |col, i|
    #print "#{col[0]}"
    xs = (i + 1).times.to_a.map do |j|
      #print " * (#{x} - #{@xs[j]})"
      x - @xs[j]
    end.reduce(:*)
    y += col[0] * xs
    #print " + "
  end
  #puts
  #puts '=' * 80
  y
end

Private Instance Methods

calc(lx) click to toggle source
# File lib/mad_math/newton_polynomial.rb, line 48
def calc(lx)
  rows = @xs.count

  @calc << []

  col = 0
  xn = rows - 2

  while @calc[col]
    y2 = (@calc[col - 1] && @calc[col - 1][-1]) || @ys[-1]
    y1 = (@calc[col - 1] && @calc[col - 1][-2]) || @ys[-2]

    @calc[col] << solve(lx, y2, @xs[xn], y1)

    col += 1
    xn -= 1
    #puts '-' * 80
  end
  #pp @calc
  #puts '-' * 80
end
solve(x2, y2, x1, y1) click to toggle source
# File lib/mad_math/newton_polynomial.rb, line 43
def solve(x2, y2, x1, y1)
  #puts "(#{y2} - #{y1}) / (#{x2} - #{x1})"
  (y2 - y1) / (x2 - x1).to_f
end