Class: Ilp::TermArray
- Inherits:
-
Object
- Object
- Ilp::TermArray
- Extended by:
- Forwardable
- Defined in:
- lib/ruby-cbc/ilp/term_array.rb
Instance Attribute Summary collapse
-
#terms ⇒ Object
Returns the value of attribute terms.
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #<=(other) ⇒ Object
- #==(other) ⇒ Object
- #>=(other) ⇒ Object
- #coerce(value) ⇒ Object
-
#initialize(terms) ⇒ TermArray
constructor
A new instance of TermArray.
-
#normalize! ⇒ Object
cste + nb * var + nb * var…
- #to_s ⇒ Object
- #vars ⇒ Object
Constructor Details
#initialize(terms) ⇒ TermArray
Returns a new instance of TermArray.
10 11 12 |
# File 'lib/ruby-cbc/ilp/term_array.rb', line 10 def initialize(terms) @terms = terms end |
Instance Attribute Details
#terms ⇒ Object
Returns the value of attribute terms.
7 8 9 |
# File 'lib/ruby-cbc/ilp/term_array.rb', line 7 def terms @terms end |
Instance Method Details
#*(other) ⇒ Object
35 36 37 38 39 |
# 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) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# 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) ⇒ Object
31 32 33 |
# File 'lib/ruby-cbc/ilp/term_array.rb', line 31 def -(other) self + -1 * other end |
#<=(other) ⇒ Object
61 62 63 |
# File 'lib/ruby-cbc/ilp/term_array.rb', line 61 def <=(other) Ilp::Constraint.new(self, Ilp::Constraint::LESS_OR_EQ, other) end |
#==(other) ⇒ Object
69 70 71 |
# File 'lib/ruby-cbc/ilp/term_array.rb', line 69 def ==(other) Ilp::Constraint.new(self, Ilp::Constraint::EQUALS, other) end |
#>=(other) ⇒ Object
65 66 67 |
# 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) ⇒ Object
73 74 75 |
# File 'lib/ruby-cbc/ilp/term_array.rb', line 73 def coerce(value) [Ilp::Constant.new(value), self] end |
#normalize! ⇒ Object
cste + nb * var + nb * var…
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# 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 ⇒ Object
77 78 79 |
# File 'lib/ruby-cbc/ilp/term_array.rb', line 77 def to_s @terms.map(&:to_s).join(" ") end |
#vars ⇒ Object
81 82 83 |
# File 'lib/ruby-cbc/ilp/term_array.rb', line 81 def vars @terms.map(&:var) end |