Skip to content

Commit b8c16dc

Browse files
author
Kwesi Rutledge
committed
Added the Substitute() and SubstituteAccordingTo() methods for constraints to simplify some operations
1 parent fbfc97e commit b8c16dc

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

symbolic/constraint.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ type Constraint interface {
1313
ConstrSense() ConstrSense
1414
Check() error
1515
IsLinear() bool
16+
Substitute(vIn Variable, seIn ScalarExpression) Constraint
17+
SubstituteAccordingTo(subMap map[Variable]Expression) Constraint
1618
}
1719

1820
func IsConstraint(c interface{}) bool {

symbolic/matrix_constraint.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package symbolic
22

33
import (
44
"fmt"
5+
56
"github.com/MatProGo-dev/SymbolicMath.go/smErrors"
67
)
78

@@ -145,3 +146,48 @@ Description:
145146
func (mc MatrixConstraint) IsLinear() bool {
146147
return IsLinear(mc.RightHandSide) && IsLinear(mc.LeftHandSide)
147148
}
149+
150+
/*
151+
Substitute
152+
Description:
153+
154+
Substitutes the variable vIn with the scalar expression seIn
155+
*/
156+
func (mc MatrixConstraint) Substitute(vIn Variable, seIn ScalarExpression) Constraint {
157+
// Check that the constraint is well formed.
158+
err := mc.Check()
159+
if err != nil {
160+
panic(err)
161+
}
162+
163+
// Substitute the variable in the left hand side
164+
newLHS := mc.LeftHandSide.Substitute(vIn, seIn).(MatrixExpression)
165+
166+
// Substitute the variable in the right hand side
167+
newRHS := mc.RightHandSide.Substitute(vIn, seIn).(MatrixExpression)
168+
169+
return MatrixConstraint{newLHS, newRHS, mc.Sense}
170+
}
171+
172+
/*
173+
SubstituteAccordingTo
174+
Description:
175+
176+
Substitutes the variables in the map with the corresponding expressions
177+
in the given scalar constraint.
178+
*/
179+
func (mc MatrixConstraint) SubstituteAccordingTo(subMap map[Variable]Expression) Constraint {
180+
// Check that the constraint is well formed.
181+
err := mc.Check()
182+
if err != nil {
183+
panic(err)
184+
}
185+
186+
// Substitute the variable in the left hand side
187+
newLHS := mc.LeftHandSide.SubstituteAccordingTo(subMap).(MatrixExpression)
188+
189+
// Substitute the variable in the right hand side
190+
newRHS := mc.RightHandSide.SubstituteAccordingTo(subMap).(MatrixExpression)
191+
192+
return MatrixConstraint{newLHS, newRHS, mc.Sense}
193+
}

symbolic/scalar_constraint.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,59 @@ func (sc ScalarConstraint) LinearEqualityConstraintRepresentation(wrt ...[]Varia
208208
// Return
209209
return C, d
210210
}
211+
212+
/*
213+
Substitute
214+
Description:
215+
216+
Substitutes the variable vIn with the scalar expression seIn in the
217+
given scalar constraint.
218+
*/
219+
func (sc ScalarConstraint) Substitute(vIn Variable, seIn ScalarExpression) Constraint {
220+
// Check that the constraint is well formed.
221+
err := sc.Check()
222+
if err != nil {
223+
panic(err)
224+
}
225+
226+
// Substitute the variable in the left hand side
227+
newLHS := sc.LeftHandSide.Substitute(vIn, seIn).(ScalarExpression)
228+
229+
// Substitute the variable in the right hand side
230+
newRHS := sc.RightHandSide.Substitute(vIn, seIn).(ScalarExpression)
231+
232+
// Return the new constraint
233+
return ScalarConstraint{
234+
LeftHandSide: newLHS,
235+
RightHandSide: newRHS,
236+
Sense: sc.Sense,
237+
}
238+
}
239+
240+
/*
241+
SubstituteAccordingTo
242+
Description:
243+
244+
Substitutes the variables in the map with the corresponding expressions
245+
in the given scalar constraint.
246+
*/
247+
func (sc ScalarConstraint) SubstituteAccordingTo(subMap map[Variable]Expression) Constraint {
248+
// Check that the constraint is well formed.
249+
err := sc.Check()
250+
if err != nil {
251+
panic(err)
252+
}
253+
254+
// Substitute the variable in the left hand side
255+
newLHS := sc.LeftHandSide.SubstituteAccordingTo(subMap).(ScalarExpression)
256+
257+
// Substitute the variable in the right hand side
258+
newRHS := sc.RightHandSide.SubstituteAccordingTo(subMap).(ScalarExpression)
259+
260+
// Return the new constraint
261+
return ScalarConstraint{
262+
LeftHandSide: newLHS,
263+
RightHandSide: newRHS,
264+
Sense: sc.Sense,
265+
}
266+
}

symbolic/vector_constraint.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,45 @@ func (vc VectorConstraint) LinearEqualityConstraintRepresentation(wrt ...[]Varia
257257
// Return the tuple
258258
return C, d
259259
}
260+
261+
/*
262+
Substitute
263+
Description:
264+
265+
Substitutes the variable vIn with the scalar expression seIn in the vector constraint.
266+
*/
267+
func (vc VectorConstraint) Substitute(vIn Variable, seIn ScalarExpression) Constraint {
268+
// Check that the constraint is well formed.
269+
err := vc.Check()
270+
if err != nil {
271+
panic(err)
272+
}
273+
274+
// Substitute the variable in the left and right hand sides.
275+
newLHS := vc.LeftHandSide.Substitute(vIn, seIn).(VectorExpression)
276+
newRHS := vc.RightHandSide.Substitute(vIn, seIn).(VectorExpression)
277+
278+
// Return the new constraint
279+
return VectorConstraint{newLHS, newRHS, vc.Sense}
280+
}
281+
282+
/*
283+
SubstituteAccordingTo
284+
Description:
285+
286+
Substitutes the variables in the map with the corresponding expressions
287+
*/
288+
func (vc VectorConstraint) SubstituteAccordingTo(subMap map[Variable]Expression) Constraint {
289+
// Check that the constraint is well formed.
290+
err := vc.Check()
291+
if err != nil {
292+
panic(err)
293+
}
294+
295+
// Substitute the variable in the left and right hand sides.
296+
newLHS := vc.LeftHandSide.SubstituteAccordingTo(subMap).(VectorExpression)
297+
newRHS := vc.RightHandSide.SubstituteAccordingTo(subMap).(VectorExpression)
298+
299+
// Return the new constraint
300+
return VectorConstraint{newLHS, newRHS, vc.Sense}
301+
}

0 commit comments

Comments
 (0)