Class: Amor::Expression
- Inherits:
-
Object
- Object
- Amor::Expression
- Defined in:
- lib/amor/expression.rb
Instance Attribute Summary collapse
-
#factors ⇒ Object
readonly
Returns the value of attribute factors.
Instance Method Summary collapse
- #+(value) ⇒ Object
- #-(value) ⇒ Object
- #-@ ⇒ Object
- #<=(value) ⇒ Object
- #==(value) ⇒ Object
- #>=(value) ⇒ Object
- #constant_factor ⇒ Object
- #eql?(value) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(value) ⇒ Expression
constructor
A new instance of Expression.
- #lp_string ⇒ Object
- #remove_constants ⇒ Object
- #simplified ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(value) ⇒ Expression
Returns a new instance of Expression.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/amor/expression.rb', line 6 def initialize(value) if value.is_a? Array @factors = value elsif value.is_a? Expression @factors = value.factors elsif value.is_a? Variable @factors = [[1, value]] elsif value.is_a? Numeric @factors = [[value, :constant]] end end |
Instance Attribute Details
#factors ⇒ Object (readonly)
Returns the value of attribute factors.
4 5 6 |
# File 'lib/amor/expression.rb', line 4 def factors @factors end |
Instance Method Details
#+(value) ⇒ Object
18 19 20 |
# File 'lib/amor/expression.rb', line 18 def +(value) Expression.new(self.factors + Expression.new(value).factors) end |
#-(value) ⇒ Object
22 23 24 |
# File 'lib/amor/expression.rb', line 22 def -(value) self + -value end |
#-@ ⇒ Object
26 27 28 |
# File 'lib/amor/expression.rb', line 26 def -@ return Expression.new(self.factors.map{|factor| [-factor[0], factor[1]]}) end |
#<=(value) ⇒ Object
42 43 44 |
# File 'lib/amor/expression.rb', line 42 def <= value Constraint.new(self, :lesser_equal, value) end |
#==(value) ⇒ Object
38 39 40 |
# File 'lib/amor/expression.rb', line 38 def == value Constraint.new(self, :equal, value) end |
#>=(value) ⇒ Object
46 47 48 |
# File 'lib/amor/expression.rb', line 46 def >= value Constraint.new(self, :greater_equal, value) end |
#constant_factor ⇒ Object
62 63 64 |
# File 'lib/amor/expression.rb', line 62 def constant_factor @factors.select{|factor| !factor[1].is_a?(Variable)}.inject(0) {|m, factor| m + factor[0]} end |
#eql?(value) ⇒ Boolean
34 35 36 |
# File 'lib/amor/expression.rb', line 34 def eql? value self.hash == value.hash end |
#hash ⇒ Object
30 31 32 |
# File 'lib/amor/expression.rb', line 30 def hash @factors.hash end |
#lp_string ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/amor/expression.rb', line 66 def lp_string result = '' factor_strings = self.simplified.factors.each_with_index.map do |factor, i| scalar = factor[0] if scalar < 0 sign = '- ' scalar = -scalar elsif i > 0 sign = '+ ' else sign = '' end if factor[1].is_a? Variable "#{sign}#{scalar} x#{factor[1].internal_index+1}" else "#{sign}#{scalar}" end end return factor_strings.join(' ') end |
#remove_constants ⇒ Object
58 59 60 |
# File 'lib/amor/expression.rb', line 58 def remove_constants Expression.new(@factors.select{|factor| factor[1].is_a?(Variable)}) end |
#simplified ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/amor/expression.rb', line 50 def simplified summed_scalars = Hash.new @factors.each do |factor| summed_scalars[factor[1]] = (summed_scalars[factor[1]] || 0) + factor[0] end Expression.new(summed_scalars.map{|var, scalar| [scalar, var]}.select{|factor| !factor[0].zero? }) end |
#to_s ⇒ Object
88 89 90 |
# File 'lib/amor/expression.rb', line 88 def to_s factors.map{|factor| factor[1].is_a?(Variable) ? "#{factor[0]} #{factor[1]}" : "#{factor[0]}"}.join(" ") end |