class Ilp::TermArray

Attributes

terms[RW]

Public Class Methods

new(terms) click to toggle source
# File lib/ruby-cbc/ilp/term_array.rb, line 10
def initialize(terms)
  @terms = terms
end

Public Instance Methods

*(other) click to toggle source
# File lib/ruby-cbc/ilp/term_array.rb, line 35
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) click to toggle source
# File lib/ruby-cbc/ilp/term_array.rb, line 14
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) click to toggle source
# File lib/ruby-cbc/ilp/term_array.rb, line 31
def -(other)
  self + -1 * other
end
<=(other) click to toggle source
# File lib/ruby-cbc/ilp/term_array.rb, line 61
def <=(other)
  Ilp::Constraint.new(self, Ilp::Constraint::LESS_OR_EQ, other)
end
==(other) click to toggle source
# File lib/ruby-cbc/ilp/term_array.rb, line 69
def ==(other)
  Ilp::Constraint.new(self, Ilp::Constraint::EQUALS, other)
end
>=(other) click to toggle source
# File lib/ruby-cbc/ilp/term_array.rb, line 65
def >=(other)
  Ilp::Constraint.new(self, Ilp::Constraint::GREATER_OR_EQ, other)
end
coerce(value) click to toggle source
# File lib/ruby-cbc/ilp/term_array.rb, line 73
def coerce(value)
  [Ilp::Constant.new(value), self]
end
normalize!() click to toggle source

cste + nb * var + nb * var…

# File lib/ruby-cbc/ilp/term_array.rb, line 42
def normalize!
  constant = 0
  hterms = {}
  @terms.each do |term|
    case term
    when Numeric
      constant += term
    when Ilp::Term
      v = term.var
      hterms[v] ||= Ilp::Term.new(v, 0)
      hterms[v].mult += term.mult
    end
  end
  reduced = hterms.map { |_, term| term unless term.mult.zero? }
  reduced.compact!
  @terms = [constant].concat reduced
  self
end
to_s() click to toggle source
# File lib/ruby-cbc/ilp/term_array.rb, line 77
def to_s
  @terms.map(&:to_s).join(" ")
end
vars() click to toggle source
# File lib/ruby-cbc/ilp/term_array.rb, line 81
def vars
  @terms.map(&:var)
end

Private Instance Methods

pop_constant() click to toggle source

Must be normalized!

# File lib/ruby-cbc/ilp/term_array.rb, line 88
def pop_constant
  terms.slice!(0)
end