Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/astro-algo.rb

Instance Method Summary collapse

Instance Method Details

#poly_eval(x) ⇒ Object

Evaluate polynomial using Horner’s method. Array consists of coefficients of a polynomial, the coefficient of the highest order term first, the constant coefficient last. Returns evaluation of polynomial at x.

Example: evaluate the polynomial x**2 - 0.5*x + 3.0 where x = 2.0

[1.0, -0.5, 3.0].poly_eval(2.0)   # => 6.0


81
82
83
# File 'lib/astro-algo.rb', line 81

def poly_eval(x)
    self.inject(0.0) {|p, a| p*x + a}
end