Class: Ilp::TermArray

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*terms) ⇒ TermArray

Returns a new instance of TermArray.



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

def initialize(*terms)
  @terms = terms
end

Instance Attribute Details

#termsObject

Returns the value of attribute terms.



5
6
7
# File 'lib/ruby-cbc/ilp/term_array.rb', line 5

def terms
  @terms
end

Instance Method Details

#*(mult) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
# File 'lib/ruby-cbc/ilp/term_array.rb', line 30

def *(mult)
  raise ArgumentError, 'Argument is not numeric' unless mult.is_a? Numeric
  @terms.map! { |term| term * mult }
  self
end

#+(vars) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ruby-cbc/ilp/term_array.rb', line 11

def +(vars)
  if vars.is_a? Numeric
    @terms << vars
  elsif vars.is_a? Ilp::Var
    @terms << Ilp::Term.new(vars)
  elsif vars.is_a? Ilp::Term
    @terms << vars
  elsif vars.is_a? Ilp::TermArray
    @terms += vars.terms
  else
    raise ArgumentError, "Argument is not allowed: #{vars} of type #{vars.class}"
  end
  self
end

#-(vars) ⇒ Object



26
27
28
# File 'lib/ruby-cbc/ilp/term_array.rb', line 26

def -(vars)
  self + -1 * vars
end

#<=(value) ⇒ Object



50
51
52
# File 'lib/ruby-cbc/ilp/term_array.rb', line 50

def <=(value)
  Ilp::Constraint.new(self, Ilp::Constraint::LESS_OR_EQ, value)
end

#==(value) ⇒ Object



58
59
60
# File 'lib/ruby-cbc/ilp/term_array.rb', line 58

def ==(value)
  Ilp::Constraint.new(self, Ilp::Constraint::EQUALS, value)
end

#>=(value) ⇒ Object



54
55
56
# File 'lib/ruby-cbc/ilp/term_array.rb', line 54

def >=(value)
  Ilp::Constraint.new(self, Ilp::Constraint::GREATER_OR_EQ, value)
end

#coerce(value) ⇒ Object



62
63
64
# File 'lib/ruby-cbc/ilp/term_array.rb', line 62

def coerce(value)
  [Ilp::Constant.new(value), self]
end

#each(&block) ⇒ Object



66
67
68
# File 'lib/ruby-cbc/ilp/term_array.rb', line 66

def each(&block)
  @terms.each(&block)
end

#normalize!Object

cste + nb * var + nb * var…



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby-cbc/ilp/term_array.rb', line 37

def normalize!
  constant = @terms.select{ |t| t.is_a? Numeric }.inject(:+)
  hterms = @terms.select{ |t| t.is_a? Ilp::Term }.group_by(&:var)
  @terms = []
  constant ||= 0
  @terms << constant
  hterms.each do |v, ts| 
    t = ts.inject(Ilp::Term.new(v, 0)) { |v1, v2| v1.mult += v2.mult; v1 }
    terms << t if t.mult != 0
  end
  self
end

#to_sObject



70
71
72
# File 'lib/ruby-cbc/ilp/term_array.rb', line 70

def to_s
  @terms.map(&:to_s).join(' ')
end