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.
8 9 10 |
# File 'lib/ruby-cbc/ilp/term_array.rb', line 8 def initialize(terms) @terms = terms end |
Instance Attribute Details
#terms ⇒ Object
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
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
59 60 61 |
# File 'lib/ruby-cbc/ilp/term_array.rb', line 59 def <=(other) Ilp::Constraint.new(self, Ilp::Constraint::LESS_OR_EQ, other) end |
#==(other) ⇒ Object
67 68 69 |
# File 'lib/ruby-cbc/ilp/term_array.rb', line 67 def ==(other) Ilp::Constraint.new(self, Ilp::Constraint::EQUALS, other) end |
#>=(other) ⇒ Object
63 64 65 |
# File 'lib/ruby-cbc/ilp/term_array.rb', line 63 def >=(other) Ilp::Constraint.new(self, Ilp::Constraint::GREATER_OR_EQ, other) end |
#coerce(value) ⇒ Object
71 72 73 |
# File 'lib/ruby-cbc/ilp/term_array.rb', line 71 def coerce(value) [Ilp::Constant.new(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 57 |
# 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 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
75 76 77 |
# File 'lib/ruby-cbc/ilp/term_array.rb', line 75 def to_s @terms.map(&:to_s).join(" ") end |
#vars ⇒ Object
79 80 81 |
# File 'lib/ruby-cbc/ilp/term_array.rb', line 79 def vars @terms.map(&:var) end |