Class: Polymath::Nomial::Polynomial
- Inherits:
-
Object
- Object
- Polymath::Nomial::Polynomial
- Defined in:
- lib/polymath/nomial/polynomial.rb
Instance Attribute Summary collapse
-
#exp ⇒ Object
readonly
Returns the value of attribute exp.
-
#homogenized_exp ⇒ Object
readonly
Returns the value of attribute homogenized_exp.
-
#monomials ⇒ Object
readonly
Returns the value of attribute monomials.
-
#variable ⇒ Object
Returns the value of attribute variable.
Instance Method Summary collapse
-
#classification ⇒ Object
A hash.
-
#cleanup! ⇒ Object
Nil.
-
#coefficients ⇒ Object
An array of integers.
-
#collect_terms ⇒ Object
An array of monomials where all like degrees are merged.
-
#constant ⇒ Object
An integer.
-
#deg ⇒ Object
An integer degree.
-
#factor_gcd ⇒ Object
Nil.
-
#homogenize! ⇒ Object
An array of monomials.
-
#initialize(exp) ⇒ Object
constructor
A new Polynomial object.
-
#leading_coefficient ⇒ Object
An integer.
-
#modified_expression? ⇒ Boolean
Boolean.
-
#monomials_where_deg(n) ⇒ Object
An array of monomials with degree == n.
-
#order ⇒ Object
An array of monomials in descending order.
-
#to_s ⇒ Object
A string.
Constructor Details
#initialize(exp) ⇒ Object
Returns a new Polynomial object.
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
#exp ⇒ Object (readonly)
Returns the value of attribute exp.
22 23 24 |
# File 'lib/polymath/nomial/polynomial.rb', line 22 def exp @exp end |
#homogenized_exp ⇒ Object (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 |
#monomials ⇒ Object (readonly)
Returns the value of attribute monomials.
22 23 24 |
# File 'lib/polymath/nomial/polynomial.rb', line 22 def monomials @monomials end |
#variable ⇒ Object
Returns the value of attribute variable.
23 24 25 |
# File 'lib/polymath/nomial/polynomial.rb', line 23 def variable @variable end |
Instance Method Details
#classification ⇒ Object
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.
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 |
#coefficients ⇒ Object
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_terms ⇒ Object
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 |
#constant ⇒ Object
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 |
#deg ⇒ Object
Returns an integer degree.
119 120 121 |
# File 'lib/polymath/nomial/polynomial.rb', line 119 def deg order.first.deg end |
#factor_gcd ⇒ Object
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.
61 62 63 |
# File 'lib/polymath/nomial/polynomial.rb', line 61 def homogenize! monomials.each { |monomial| monomial.homogenize!(variable) } end |
#leading_coefficient ⇒ Object
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.
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.
110 111 112 |
# File 'lib/polymath/nomial/polynomial.rb', line 110 def monomials_where_deg(n) monomials.select { |monomial| monomial.deg == n } end |
#order ⇒ Object
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_s ⇒ Object
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 |