Class: Cassowary::LinearExpression
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#*(x) ⇒ Object
-
#+(x) ⇒ Object
-
#-(x) ⇒ Object
-
#/(x) ⇒ Object
-
#add_expression(expr, times, subject = nil, solver = nil) ⇒ Object
-
#add_variable(variable, coefficient, subject = nil, solver = nil) ⇒ Object
-
#any_variable ⇒ Object
-
#as_linear_expression ⇒ Object
-
#change_subject(old, new) ⇒ Object
-
#coefficient_for(variable) ⇒ Object
-
#constant? ⇒ Boolean
-
#each_variable_and_coefficient(&block) ⇒ Object
-
#increment_constant(num) ⇒ Object
-
#initialize ⇒ LinearExpression
constructor
A new instance of LinearExpression.
-
#inspect ⇒ Object
-
#new_subject(subject) ⇒ Object
-
#substitute_variable(var, expr, subject, solver) ⇒ Object
Methods included from Equalities
#cn_equal, #cn_geq, #cn_leq
Constructor Details
Returns a new instance of LinearExpression.
15
16
17
18
|
# File 'lib/linear_expression.rb', line 15
def initialize
self.constant = 0.0
self.terms = {}
end
|
Instance Attribute Details
Returns the value of attribute constant.
7
8
9
|
# File 'lib/linear_expression.rb', line 7
def constant
@constant
end
|
Returns the value of attribute terms.
7
8
9
|
# File 'lib/linear_expression.rb', line 7
def terms
@terms
end
|
Class Method Details
.new_with_symbolic_weight ⇒ Object
9
10
11
12
13
|
# File 'lib/linear_expression.rb', line 9
def self.new_with_symbolic_weight
result = self.new
result.constant = SymbolicWeight::Zero
result
end
|
Instance Method Details
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/linear_expression.rb', line 107
def *(x)
return x * constant if constant?
n = if x.is_a? Numeric
x.to_f
else
expr = x.as_linear_expression
raise NonLinearResult unless expr.constant?
expr.constant
end
result = LinearExpression.new
result.constant = n * constant
terms.each_pair do |v, c|
result.terms[v] = n * c
end
result
end
|
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/linear_expression.rb', line 131
def +(x)
expr = x.as_linear_expression
result = LinearExpression.new
result.constant = constant + expr.constant
terms.each_pair do |v, c|
result.terms[v] = c
end
expr.each_variable_and_coefficient do |v, c|
result.add_variable(v, c)
end
result
end
|
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/linear_expression.rb', line 144
def -(x)
expr = x.as_linear_expression
result = LinearExpression.new
result.constant = constant - expr.constant
terms.each_pair do |v, c|
result.terms[v] = c
end
expr.each_variable_and_coefficient do |v, c|
result.add_variable(v, -c)
end
result
end
|
125
126
127
128
129
|
# File 'lib/linear_expression.rb', line 125
def /(x)
expr = x.as_linear_expression
raise NonLinearResult unless expr.constant?
self * (1.0 / expr.constant)
end
|
#add_expression(expr, times, subject = nil, solver = nil) ⇒ Object
59
60
61
62
63
64
|
# File 'lib/linear_expression.rb', line 59
def add_expression(expr, times, subject = nil, solver = nil)
increment_constant(times * expr.constant)
expr.each_variable_and_coefficient do |v, c|
add_variable(v, times * c, subject, solver)
end
end
|
#add_variable(variable, coefficient, subject = nil, solver = nil) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/linear_expression.rb', line 44
def add_variable(variable, coefficient, subject = nil, solver = nil)
if terms.has_key? variable
new_coeff = coefficient + terms[variable]
if new_coeff.cl_approx_zero
terms.delete variable
solver.note_removed_variable(variable, subject) if solver
else
terms[variable] = new_coeff
end
else
terms[variable] = coefficient
solver.note_added_variable(variable, subject) if solver
end
end
|
#any_variable ⇒ Object
20
21
22
23
24
25
26
|
# File 'lib/linear_expression.rb', line 20
def any_variable
if terms.any?
terms.keys.first
else
raise InternalError, "expression is constant"
end
end
|
#as_linear_expression ⇒ Object
28
29
30
|
# File 'lib/linear_expression.rb', line 28
def as_linear_expression
self
end
|
#change_subject(old, new) ⇒ Object
74
75
76
77
78
79
80
81
82
|
# File 'lib/linear_expression.rb', line 74
def change_subject(old, new)
reciprocal = 1.0 / terms.delete(new)
nreciprocal = -reciprocal
self.constant *= nreciprocal
terms.each_pair do |v, c|
terms[v] = c * nreciprocal
end
terms[old] = reciprocal
end
|
#coefficient_for(variable) ⇒ Object
32
33
34
|
# File 'lib/linear_expression.rb', line 32
def coefficient_for(variable)
terms[variable] || 0.0
end
|
#constant? ⇒ Boolean
36
37
38
|
# File 'lib/linear_expression.rb', line 36
def constant?
terms.empty?
end
|
#each_variable_and_coefficient(&block) ⇒ Object
40
41
42
|
# File 'lib/linear_expression.rb', line 40
def each_variable_and_coefficient(&block)
terms.each_pair(&block)
end
|
#increment_constant(num) ⇒ Object
84
85
86
|
# File 'lib/linear_expression.rb', line 84
def increment_constant(num)
self.constant += num
end
|
157
158
159
160
161
|
# File 'lib/linear_expression.rb', line 157
def inspect
terms.keys.inject(constant.inspect) do |str, v|
"#{str}+#{terms[v].inspect}*#{v.inspect}"
end
end
|
#new_subject(subject) ⇒ Object
66
67
68
69
70
71
72
|
# File 'lib/linear_expression.rb', line 66
def new_subject(subject)
nreciprocal = -(1.0 / terms.delete(subject))
self.constant *= nreciprocal
terms.each_pair do |v, c|
terms[v] = c * nreciprocal
end
end
|
#substitute_variable(var, expr, subject, solver) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/linear_expression.rb', line 88
def substitute_variable(var, expr, subject, solver)
multiplier = terms.delete(var)
increment_constant(multiplier * expr.constant)
expr.each_variable_and_coefficient do |v, c|
if old_coeff = terms[v]
new_coeff = old_coeff + (multiplier * c)
if new_coeff.cl_approx_zero
terms.delete v
solver.note_removed_variable v, subject
else
terms[v] = new_coeff
end
else
terms[v] = multiplier * c
solver.note_added_variable v, subject
end
end
end
|