Class: Expressions

Inherits:
Object show all
Defined in:
lib/rulp/expression.rb

Overview

An LP Expression. A mathematical expression. Can be a constraint or can be the objective function of a LP or MIP problem.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expressions) ⇒ Expressions

Returns a new instance of Expressions.



7
8
9
# File 'lib/rulp/expression.rb', line 7

def initialize(expressions)
  @expressions = expressions
end

Instance Attribute Details

#expressionsObject

Returns the value of attribute expressions.



6
7
8
# File 'lib/rulp/expression.rb', line 6

def expressions
  @expressions
end

Class Method Details

.[](value) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/rulp/expression.rb', line 44

def self.[](value)
  case value
  when LV then Expressions.new([Fragment.new(value, 1)])
  when Fragment then Expressions.new([value])
  when Expressions then value
  end
end

Instance Method Details

#+(other) ⇒ Object



40
41
42
# File 'lib/rulp/expression.rb', line 40

def +(other)
  Expressions.new(self.expressions + Expressions[other].expressions)
end

#-(other) ⇒ Object



35
36
37
38
# File 'lib/rulp/expression.rb', line 35

def -(other)
  other = -other
  self + other
end

#-@Object



30
31
32
33
# File 'lib/rulp/expression.rb', line 30

def -@
  -self.expressions[0]
  self
end

#evaluateObject



52
53
54
# File 'lib/rulp/expression.rb', line 52

def evaluate
  self.expressions.map(&:evaluate).inject(:+)
end

#to_sObject



11
12
13
14
15
16
17
18
# File 'lib/rulp/expression.rb', line 11

def to_s
  as_str = @expressions[0].to_s
  as_str = as_str[1] == '+' ? as_str[3..-1] : as_str.dup
  (@expressions.length - 1).times do |i|
    as_str << @expressions[i + 1].to_s
  end
  as_str
end

#variablesObject



20
21
22
# File 'lib/rulp/expression.rb', line 20

def variables
  @expressions.map(&:variable)
end