class Polymath::Nomial::Monomial

Constants

PlaceholderVar

Attributes

cof[RW]
deg[RW]
var[RW]

Public Class Methods

new(cof:1, deg:0, var: ::PlaceholderVar) click to toggle source
# File lib/polymath/nomial/monomial.rb, line 9
def initialize(cof:1, deg:0, var: ::PlaceholderVar)
  @cof, @deg, @var = cof, deg, var
end

Public Instance Methods

+(other) click to toggle source
# File lib/polymath/nomial/monomial.rb, line 42
def +(other)
  raise "error" unless deg == other.deg
  raise "error" unless var == other.var
  Monomial.new(cof:@cof + other.cof, deg:deg, var:var)
end
/(other) click to toggle source
# File lib/polymath/nomial/monomial.rb, line 34
def /(other)
  Monomial.new(
    cof: cof / other.cof,
    deg: deg - other.deg,
    var: var
  )
end
gcd(other) click to toggle source
# File lib/polymath/nomial/monomial.rb, line 26
def gcd(other)
  Monomial.new(
    cof: cof.gcd(other.cof),
    deg: [deg, other.deg].min,
    var: var
  )
end
homogenize!(new_var) click to toggle source
# File lib/polymath/nomial/monomial.rb, line 13
def homogenize!(new_var)
  @cof = 1       unless cof
  @deg = 0       unless deg
  @var = new_var
end
merge!(other) click to toggle source
# File lib/polymath/nomial/monomial.rb, line 19
def merge!(other)
  @cof *= other.cof if other.cof
  @deg += other.deg if other.deg
  @var = other.var  if other.var != ::PlaceholderVar
  self
end
to_s() click to toggle source
# File lib/polymath/nomial/monomial.rb, line 48
def to_s
  if deg > 0
    "#{cof == 1 ? "" : cof}" + "#{var}" + (deg > 1 ? "^#{deg}" : "")
  else
    "#{cof}"
  end
end