Class: Polymath::Nomial::Polynomial

Inherits:
Object
  • Object
show all
Defined in:
lib/polymath/nomial/polynomial.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exp) ⇒ Object

Returns a new Polynomial object.

Parameters:

  • exp

    the string polynomial expression



36
37
38
39
40
41
42
43
44
45
# 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

Instance Attribute Details

#expObject (readonly)

Returns the value of attribute exp.



22
23
24
# File 'lib/polymath/nomial/polynomial.rb', line 22

def exp
  @exp
end

#homogenized_expObject (readonly)

Returns the value of attribute homogenized_exp.



22
23
24
# File 'lib/polymath/nomial/polynomial.rb', line 22

def homogenized_exp
  @homogenized_exp
end

#monomialsObject (readonly)

Returns the value of attribute monomials.



22
23
24
# File 'lib/polymath/nomial/polynomial.rb', line 22

def monomials
  @monomials
end

#variableObject

Returns the value of attribute variable.



23
24
25
# File 'lib/polymath/nomial/polynomial.rb', line 23

def variable
  @variable
end

Instance Method Details

#classificationObject

Returns a hash.

Returns:

  • a hash



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# 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

#cleanup!Object

Returns nil.

Returns:

  • nil



93
94
95
96
97
98
99
100
101
# File 'lib/polymath/nomial/polynomial.rb', line 93

def cleanup!
  homogenize!

  @monomials = order

  @monomials = collect_terms

  @monomials = factor_gcd
end

#coefficientsObject

Returns an array of integers.

Returns:

  • an array of integers



148
149
150
# File 'lib/polymath/nomial/polynomial.rb', line 148

def coefficients
  monomials.map { |monomial| monomial.cof }
end

#collect_termsObject

Returns an array of monomials where all like degrees are merged.

Returns:

  • an array of monomials where all like degrees are merged



79
80
81
82
83
84
85
86
# 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

#constantObject

Returns an integer.

Parameters:

  • cof

    The cof

Returns:

  • an integer



130
131
132
# File 'lib/polymath/nomial/polynomial.rb', line 130

def constant
  (monomials_where_deg(0).first || Monomial.new(cof: 0)).cof
end

#degObject

Returns an integer degree.

Returns:

  • an integer degree



119
120
121
# File 'lib/polymath/nomial/polynomial.rb', line 119

def deg
  order.first.deg
end

#factor_gcdObject

Returns nil.

Returns:

  • nil



157
158
159
160
161
162
163
164
165
166
# 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

#homogenize!Object

Returns an array of monomials.

Returns:

  • an array of monomials



61
62
63
# File 'lib/polymath/nomial/polynomial.rb', line 61

def homogenize!
  monomials.each { |monomial| monomial.homogenize!(variable) }
end

#leading_coefficientObject

Returns an integer.

Returns:

  • an integer



139
140
141
# File 'lib/polymath/nomial/polynomial.rb', line 139

def leading_coefficient
  monomials_where_deg(deg).first.cof
end

#modified_expression?Boolean

Returns boolean.

Returns:

  • (Boolean)

    boolean



52
53
54
# File 'lib/polymath/nomial/polynomial.rb', line 52

def modified_expression?
  homogenized_exp != exp
end

#monomials_where_deg(n) ⇒ Object

Returns an array of monomials with degree == n.

Parameters:

  • n

    the specified degree

Returns:

  • an array of monomials with degree == n



110
111
112
# File 'lib/polymath/nomial/polynomial.rb', line 110

def monomials_where_deg(n)
  monomials.select { |monomial| monomial.deg == n }
end

#orderObject

Returns an array of monomials in descending order.

Returns:

  • an array of monomials in descending order



70
71
72
# File 'lib/polymath/nomial/polynomial.rb', line 70

def order
  monomials.sort_by { |monomial| -monomial.deg }
end

#to_sObject

Returns a string.

Returns:

  • a string



218
219
220
221
222
223
224
225
226
227
228
# 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