Class: Ilp::Term

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-cbc/ilp/term.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(var, mult = 1) ⇒ Term

Returns a new instance of Term.



6
7
8
9
# File 'lib/ruby-cbc/ilp/term.rb', line 6

def initialize(var, mult = 1)
  @mult = mult
  @var = var
end

Instance Attribute Details

#multObject

Returns the value of attribute mult.



4
5
6
# File 'lib/ruby-cbc/ilp/term.rb', line 4

def mult
  @mult
end

#varObject (readonly)

Returns the value of attribute var.



3
4
5
# File 'lib/ruby-cbc/ilp/term.rb', line 3

def var
  @var
end

Instance Method Details

#*(other) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
# File 'lib/ruby-cbc/ilp/term.rb', line 38

def *(other)
  raise ArgumentError, "Argument is not numeric" unless other.is_a? Numeric
  Ilp::Term.new(@var, @mult * other)
end

#+(other) ⇒ Object



11
12
13
# File 'lib/ruby-cbc/ilp/term.rb', line 11

def +(other)
  Ilp::TermArray.new([self]) + other
end

#-(other) ⇒ Object



15
16
17
# File 'lib/ruby-cbc/ilp/term.rb', line 15

def -(other)
  Ilp::TermArray.new([self]) - other
end

#<=(other) ⇒ Object



23
24
25
# File 'lib/ruby-cbc/ilp/term.rb', line 23

def <=(other)
  Ilp::TermArray.new([self]) <= other
end

#==(other) ⇒ Object



19
20
21
# File 'lib/ruby-cbc/ilp/term.rb', line 19

def ==(other)
  Ilp::TermArray.new([self]) == other
end

#>=(other) ⇒ Object



27
28
29
# File 'lib/ruby-cbc/ilp/term.rb', line 27

def >=(other)
  Ilp::TermArray.new([self]) >= other
end

#coerce(num) ⇒ Object



43
44
45
# File 'lib/ruby-cbc/ilp/term.rb', line 43

def coerce(num)
  [Ilp::TermArray.new([self]), num]
end

#combine_in(other_term) ⇒ Object



31
32
33
34
35
36
# File 'lib/ruby-cbc/ilp/term.rb', line 31

def combine_in(other_term)
  return Term.new(var, mult) unless other_term
  raise "Terms cannot be combined: #{self} and #{other_term}" unless var.equal? other_term.var
  other_term.mult += mult
  other_term
end

#to_sObject



47
48
49
50
51
# File 'lib/ruby-cbc/ilp/term.rb', line 47

def to_s
  str = "++-"[mult <=> 0] << " "
  str << mult.abs.to_s << " " if mult != 1
  str << var.to_s
end