class Polymath::Nomial::Polynomial
@brief Class for a polynomial with integer coefficients and degrees
Constants
- DefaultVar
- UnitMonomial
- ZeroMonomial
Attributes
Public Class Methods
@brief Constructs a Polynomial
object
@param exp the string polynomial expression
@return a new Polynomial
object
# File lib/polymath/nomial/polynomial.rb, line 36 def initialize(exp) @exp = Parser.sanitize(exp) @monomials = Parser.parse(@exp) @monomials << ::ZeroMonomial if @monomials.length == 0 @variable = Parser.guess_variable(self) @homogenized_exp = Parser.set_variable(exp, @variable) @gcd = ::UnitMonomial end
Public Instance Methods
@brief uses a symbol classification method to classify a polynomial
@return a hash
# File lib/polymath/nomial/polynomial.rb, line 173 def classification if (1..3).include?(monomials.length) basic = [ :monomial, :binomial, :trinomial ][monomials.length - 1] if basic == :monomial if monomials.first.cof == 0 special = :zero elsif monomials.first.deg == 0 special = :constant end end end if (0..10).include?(deg) degree = [ :undefinded, :linear, :quadratic, :qubic, :quartic, :quintic, :hexic, :heptic, :octic, :nonic, :decic ][deg] end basic ||= :polynomial special ||= :normal degree ||= :"#{deg}th_degree" { :special => special, :deg => degree, :len => basic } end
@brief homogenizes, collects terms, and orders a polynomial in place
@return nil
# File lib/polymath/nomial/polynomial.rb, line 93 def cleanup! homogenize! @monomials = order @monomials = collect_terms @monomials = factor_gcd end
@brief returns an array of the polynomial's coefficients
@return an array of integers
# File lib/polymath/nomial/polynomial.rb, line 148 def coefficients monomials.map { |monomial| monomial.cof } end
@brief collects terms of the same degree
@return an array of monomials where all like degrees are merged
# File lib/polymath/nomial/polynomial.rb, line 79 def collect_terms c = (0..deg).map { |n| collected = monomials_where_deg(n).reduce(:+) collected.cof == 0 ? nil : collected if collected }.compact.reverse c.empty? ? [::ZeroMonomial] : c end
@brief returns the value of the polynomial's constant
@param cof The cof
@return an integer
# File lib/polymath/nomial/polynomial.rb, line 130 def constant (monomials_where_deg(0).first || Monomial.new(cof: 0)).cof end
@brief returns the degree of the polynomial
@return an integer degree
# File lib/polymath/nomial/polynomial.rb, line 119 def deg order.first.deg end
@brief facotrs the gcd out of the polynomial
@return nil
# File lib/polymath/nomial/polynomial.rb, line 157 def factor_gcd cls = classification if cls[:special] == :zero or cls[:len] == :monomial return monomials end @gcd = monomials.reduce(:gcd) monomials.map { |monomial| monomial / @gcd } end
@brief homogenizes each monomial in place
@return an array of monomials
# File lib/polymath/nomial/polynomial.rb, line 61 def homogenize! monomials.each { |monomial| monomial.homogenize!(variable) } end
@brief returns the value of the polynomial's leading coefficient
@return an integer
# File lib/polymath/nomial/polynomial.rb, line 139 def leading_coefficient monomials_where_deg(deg).first.cof end
@brief checks if the supplied expression string was auto-corrected
@return boolean
# File lib/polymath/nomial/polynomial.rb, line 52 def modified_expression? homogenized_exp != exp end
@brief returns all monomials with the specified degree
@param n the specified degree
@return an array of monomials with degree == n
# File lib/polymath/nomial/polynomial.rb, line 110 def monomials_where_deg(n) monomials.select { |monomial| monomial.deg == n } end
@brief orders a polynomial expression in descending order by degree
@return an array of monomials in descending order
# File lib/polymath/nomial/polynomial.rb, line 70 def order monomials.sort_by { |monomial| -monomial.deg } end
@brief displys a string form of the polynomial
@return a string
# File lib/polymath/nomial/polynomial.rb, line 218 def to_s expression = monomials.collect { |monomial| monomial.to_s }.reduce { |m, t| joiner = t[0] == "-" ? "" : "+" m += joiner + t } if @gcd.cof == 1 and @gcd.deg == 0 expression else "#{@gcd}(#{expression})" end end