class Polynomials::Polynomial
Attributes
terms[RW]
Public Class Methods
new(*args)
click to toggle source
# File lib/polynomials/polynomial.rb, line 17 def initialize(*args) self.terms = Hash.new { |hash, key| hash[key] = Term.new(key) } unless args.empty? args.reverse.each.with_index do |coefficient,exponent| self.terms[exponent].coefficient += coefficient end end end
parse(string)
click to toggle source
# File lib/polynomials/polynomial.rb, line 8 def self.parse(string) polynomial = self.new string.split(/(?=[-+])/).each do |term| parsed = Term.parse(term) polynomial.terms[parsed.exponent].coefficient += parsed.coefficient end return polynomial end
Public Instance Methods
==(other)
click to toggle source
# File lib/polynomials/polynomial.rb, line 69 def ==(other) check_if_zero = proc { |_,t| t.coefficient.zero? } self.terms.delete_if(&check_if_zero) == other.terms.delete_if(&check_if_zero) end
calculate(x)
click to toggle source
# File lib/polynomials/polynomial.rb, line 26 def calculate(x) self.terms.values.inject(0.0) do |acc,t| acc + (t.coefficient.to_f * (x**t.exponent)) end end
Also aliased as: call
degree()
click to toggle source
# File lib/polynomials/polynomial.rb, line 58 def degree self.terms.keys.max || 0 end
derivative()
click to toggle source
# File lib/polynomials/polynomial.rb, line 33 def derivative new_function = self.alter do |new_function, term| (new_function.terms[term.exponent - 1].coefficient += term.exponent * term.coefficient) unless term.exponent.zero? end end
gt()
click to toggle source
greatest term
# File lib/polynomials/polynomial.rb, line 75 def gt self.terms[self.degree] end
lt()
click to toggle source
lowest term
# File lib/polynomials/polynomial.rb, line 80 def lt self.terms[ self.terms.reject { |_,t| t.coefficient.zero? } .min_by{ |_,t| t.exponent}.first ] end
roots()
click to toggle source
# File lib/polynomials/polynomial.rb, line 39 def roots if terms[0].coefficient.zero? and !terms.values.map(&:coefficient).all?(&:zero?) self.alter { |nf, term| nf.terms[term.exponent-self.lt.exponent].coefficient = term.coefficient }.roots << Root.new(0.0) else case self.degree when 1 Set[-self.terms[0].coefficient / self.terms[1].coefficient] when 2 Formulas.roots_of_quadratic_function(*coefficients_till(2)) when 3 Formulas.roots_of_cubic_function(*coefficients_till(3)) when 4 Formulas.roots_of_quartic_function(*coefficients_till(4)) else Set[] end.map { |x| Root.new(x) }.to_set end end
to_s()
click to toggle source
# File lib/polynomials/polynomial.rb, line 62 def to_s terms.sort_by { |_,t| -t.exponent }.inject("") do |string,(_,term)| string << term.to_s unless term.coefficient.zero? && !term.exponent.zero? string end.strip.sub(/\A\+\s/, '') end
Protected Instance Methods
alter() { |new_function, term| ... }
click to toggle source
# File lib/polynomials/polynomial.rb, line 96 def alter new_function = self.class.new self.terms.values.each do |term| yield new_function, term end return new_function end
Private Instance Methods
coefficients_till(n)
click to toggle source
# File lib/polynomials/polynomial.rb, line 87 def coefficients_till(n) coefficients = [] (0..n).each do |e| coefficients << self.terms[e].coefficient end return coefficients.reverse end