Class: Polymath::Nomial::Monomial

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cof: 1, deg: 0, var: ::PlaceholderVar) ⇒ Monomial

Returns a new instance of Monomial.



9
10
11
# File 'lib/polymath/nomial/monomial.rb', line 9

def initialize(cof:1, deg:0, var: ::PlaceholderVar)
  @cof, @deg, @var = cof, deg, var
end

Instance Attribute Details

#cofObject

Returns the value of attribute cof.



5
6
7
# File 'lib/polymath/nomial/monomial.rb', line 5

def cof
  @cof
end

#degObject

Returns the value of attribute deg.



5
6
7
# File 'lib/polymath/nomial/monomial.rb', line 5

def deg
  @deg
end

#varObject

Returns the value of attribute var.



5
6
7
# File 'lib/polymath/nomial/monomial.rb', line 5

def var
  @var
end

Instance Method Details

#+(other) ⇒ Object



42
43
44
45
46
# 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) ⇒ Object



34
35
36
37
38
39
40
# 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) ⇒ Object



26
27
28
29
30
31
32
# 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) ⇒ Object



13
14
15
16
17
# 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) ⇒ Object



19
20
21
22
23
24
# 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_sObject



48
49
50
51
52
53
54
# 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