Class: Ilp::TermArray

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ruby-cbc/ilp/term_array.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(terms) ⇒ TermArray



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

def initialize(terms)
  @terms = terms
end

Instance Attribute Details

#termsObject (readonly)

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

#*(other) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
# File 'lib/ruby-cbc/ilp/term_array.rb', line 33

def *(other)
  raise ArgumentError, 'Argument is not numeric' unless other.is_a? Numeric
  new_terms = terms.map { |term| term * other }
  TermArray.new(new_terms)
end

#+(other) ⇒ Object



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

def +(other)
  new_terms = terms.dup
  case other
  when Numeric
    new_terms << other
  when Ilp::Var
    new_terms << Ilp::Term.new(other)
  when Ilp::Term
    new_terms << other
  when Ilp::TermArray
    new_terms.concat(other.terms)
  else
    raise ArgumentError, "Argument is not allowed: #{other} of type #{other.class}"
  end
  TermArray.new(new_terms)
end

#-(other) ⇒ Object



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

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

#<=(other) ⇒ Object



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

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

#==(other) ⇒ Object



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

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

#>=(other) ⇒ Object



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

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

#coerce(value) ⇒ Object



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

def coerce(value)
  [value, self]
end

#normalize!Object

cste + nb * var + nb * var…



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby-cbc/ilp/term_array.rb', line 40

def normalize!
  constant = 0
  hterms = {}
  @terms.each do |term|
    case term
    when Numeric
      constant += term
    when Ilp::Term
      variable = term.var
      hterms[variable] = term.combine_in(hterms[variable])
    end
  end
  reduced = hterms.map { |_, term| term unless term.mult.zero? }
  reduced.compact!
  @terms = [constant].concat reduced
  self
end

#to_sObject



74
75
76
# File 'lib/ruby-cbc/ilp/term_array.rb', line 74

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

#varsObject



78
79
80
# File 'lib/ruby-cbc/ilp/term_array.rb', line 78

def vars
  @terms.map(&:var)
end