Skip to content

Commit b1a620a

Browse files
author
Kwesi Rutledge
committed
Copied More Files from MatProInterface.go; Much more debugging needed
1 parent 91c34fc commit b1a620a

11 files changed

+227
-122
lines changed

symbolic/constant.go

+14-17
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ func (c K) Plus(rightIn interface{}, errors ...error) (Expression, error) {
7070
return K(c.Constant() + right.Constant()), nil
7171
case Variable:
7272
return right.Plus(c)
73-
case ScalarLinearExpr:
74-
return right.Plus(c)
75-
case ScalarQuadraticExpression:
76-
return right.Plus(c) // Very compact, but potentially confusing to read?
7773
default:
7874
return c, fmt.Errorf("Unexpected type in K.Plus() for constant %v: %T", right, right)
7975
}
@@ -156,19 +152,6 @@ func (c K) Multiply(term1 interface{}, errors ...error) (Expression, error) {
156152
term1AsSLE := right.ToScalarLinearExpression()
157153

158154
return c.Multiply(term1AsSLE)
159-
case ScalarLinearExpr:
160-
// Scale all vectors and constants
161-
sleOut := right.Copy()
162-
sleOut.L.ScaleVec(float64(c), &sleOut.L)
163-
sleOut.C = right.C * float64(c)
164-
165-
return sleOut, nil
166-
case ScalarQuadraticExpression:
167-
// Scale all matrices and constants
168-
var sqeOut ScalarQuadraticExpression
169-
sqeOut.Q.Scale(float64(c), &right.Q)
170-
sqeOut.L.ScaleVec(float64(c), &right.L)
171-
sqeOut.C = float64(c) * right.C
172155

173156
return sqeOut, nil
174157
case KVector:
@@ -243,3 +226,17 @@ func (c K) Check() error {
243226
func (c K) Transpose() Expression {
244227
return c
245228
}
229+
230+
/*
231+
ToMonomial
232+
Description:
233+
234+
Converts the constant into a monomial.
235+
*/
236+
func (c K) ToMonomial() Monomial {
237+
return Monomial{
238+
Coefficient: float64(c),
239+
VariableFactors: []Variable{},
240+
Degrees: []int{},
241+
}
242+
}

symbolic/errors.go

+54
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,57 @@ func (uie UnexpectedInputError) Error() string {
6565
uie.InputInQuestion,
6666
)
6767
}
68+
69+
/*
70+
CheckErrors
71+
Description:
72+
*/
73+
func CheckErrors(extras []error) error {
74+
// Constants
75+
76+
// Check all of the extras to see if one of them contains an error
77+
switch {
78+
case len(extras) == 1:
79+
return extras[0]
80+
81+
case len(extras) > 1:
82+
return fmt.Errorf(
83+
"did not expect to receive more than one element in 'extras' input; received %v",
84+
len(extras),
85+
)
86+
}
87+
88+
// If extras has length 0, then return nil
89+
return nil
90+
}
91+
92+
func CheckDimensionsInMultiplication(left, right Expression) error {
93+
// Check that the # of columns in left
94+
// matches the # of rows in right
95+
if left.Dims()[1] != right.Dims()[0] {
96+
return DimensionError{
97+
Operation: "Multiply",
98+
Arg1: left,
99+
Arg2: right,
100+
}
101+
}
102+
// If dimensions match, then return nothing.
103+
return nil
104+
}
105+
106+
func CheckDimensionsInAddition(left, right Expression) error {
107+
// Check that the size of columns in left and right agree
108+
dimsAreMatched := (left.Dims()[0] == right.Dims()[0]) && (left.Dims()[1] == right.Dims()[1])
109+
dimsAreMatched = dimsAreMatched || IsScalarExpression(left)
110+
dimsAreMatched = dimsAreMatched || IsScalarExpression(right)
111+
112+
if !dimsAreMatched {
113+
return DimensionError{
114+
Operation: "Plus",
115+
Arg1: left,
116+
Arg2: right,
117+
}
118+
}
119+
// If dimensions match, then return nothing.
120+
return nil
121+
}

symbolic/expression.go

+29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package symbolic
22

3+
import "fmt"
4+
35
/*
46
Expression
57
Description:
@@ -43,6 +45,33 @@ type Expression interface {
4345
Comparison(rightIn interface{}, sense ConstrSense, errors ...error) (Constraint, error)
4446
}
4547

48+
/*
49+
NumVariables
50+
Description:
51+
52+
The number of distinct variables.
53+
*/
4654
func NumVariables(e Expression) int {
4755
return len(e.Variables())
4856
}
57+
58+
/*
59+
IsExpression
60+
Description:
61+
62+
Tests whether or not the input variable is one of the expression types.
63+
*/
64+
func IsExpression(e interface{}) bool {
65+
return IsScalarExpression(e) || IsVectorExpression(e)
66+
}
67+
68+
func ToExpression(e interface{}) (Expression, error) {
69+
switch {
70+
case IsScalarExpression(e):
71+
return ToScalarExpression(e)
72+
case IsVectorExpression(e):
73+
return ToVectorExpression(e)
74+
default:
75+
return K(Infinity), fmt.Errorf("the input expression is not recognized as a scalar or vector expression.")
76+
}
77+
}

symbolic/monomial.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package symbolic
2+
3+
/*
4+
monomial.go
5+
Description:
6+
This file defines the function associated with the Monomial object.
7+
*/
8+
9+
/*
10+
Type Definition
11+
*/
12+
type Monomial struct {
13+
Coefficient float64
14+
Degrees []int
15+
VariableFactors []Variable
16+
}

symbolic/polynomial.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package symbolic
2+
3+
/*
4+
polynomial.go
5+
Description:
6+
This file defines the function associated with the Polynomial object.
7+
*/
8+
9+
/*
10+
Type Definition
11+
*/
12+
type Polynomial struct {
13+
Monomials []Monomial
14+
}

symbolic/scalar_expression.go

-7
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ type ScalarExpression interface {
1212
// Variables returns the variables included in the scalar expression
1313
Variables() []Variable
1414

15-
// NumVars returns the number of variables in the expression
16-
NumVars() int
17-
1815
// Vars returns a slice of the Var ids in the expression
1916
IDs() []uint64
2017

@@ -84,10 +81,6 @@ func IsScalarExpression(e interface{}) bool {
8481
return true
8582
case Variable:
8683
return true
87-
case ScalarLinearExpr:
88-
return true
89-
case ScalarQuadraticExpression:
90-
return true
9184
default:
9285
return false
9386

symbolic/variable.go

+4-70
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type Variable struct {
1111
ID uint64
1212
Lower float64
1313
Upper float64
14-
Vtype VarType
14+
Type VarType
1515
}
1616

1717
/*
@@ -214,7 +214,7 @@ func (v *Variable) Upper() float64 {
214214
215215
// Type returns the type of variable (continuous, binary, integer, etc)
216216
func (v *Variable) Type() VarType {
217-
return v.Vtype
217+
return v.Type
218218
}
219219
*/
220220

@@ -302,77 +302,11 @@ func (v Variable) Multiply(val interface{}, errors ...error) (Expression, error)
302302
}
303303
return sqeOut, nil
304304

305-
case ScalarLinearExpr:
306-
// Algorithm
307-
sqeOut := ScalarQuadraticExpression{
308-
X: VarVector{
309-
UniqueVars(append(e.X.Elements, v)),
310-
},
311-
C: 0.0,
312-
}
313-
sqeOut.Q = ZerosMatrix(sqeOut.X.Len(), sqeOut.X.Len())
314-
sqeOut.L = ZerosVector(sqeOut.X.Len())
315-
316-
// Update Q
317-
vIndex, _ := FindInSlice(v, e.X.Elements) // err should be nil
318-
vIndexInSQE, _ := FindInSlice(v, sqeOut.X.Elements) // err should be nil
319-
for xIndex := 0; xIndex < e.L.Len(); xIndex++ {
320-
// Check to make sure index is not the vIndex
321-
if vIndex == xIndex {
322-
// If v is in the original slice (i.e., we now need to represent v^2)
323-
324-
sqeOut.Q.Set(vIndexInSQE, vIndexInSQE, e.L.AtVec(vIndex))
325-
} else {
326-
// If xIndex is not for v, then create off-diagonal elements
327-
xIndexInSQE, _ := FindInSlice(e.X.AtVec(xIndex), sqeOut.X.Elements)
328-
329-
// Create a pair of off-diagonal elements
330-
sqeOut.Q.Set(vIndexInSQE, xIndexInSQE, e.L.AtVec(xIndex)*0.5)
331-
sqeOut.Q.Set(xIndexInSQE, vIndexInSQE, e.L.AtVec(xIndex)*0.5)
332-
333-
}
334-
}
335-
336-
// Update L
337-
sqeOut.L.SetVec(vIndexInSQE, e.C)
338-
339-
return sqeOut, nil
340-
341-
case ScalarQuadraticExpression:
342-
// Return error
343-
return ScalarQuadraticExpression{}, fmt.Errorf("Can not multiply Variable with ScalarQuadraticExpression. MatProInterface can not represent polynomials higher than degree 2.")
344-
345-
case VectorLinearExpressionTranspose:
346-
return ScalarQuadraticExpression{}, fmt.Errorf(
347-
"cannot currently multiply a variable with a vector to create a quadratic expression; file an issue if you are interested in seeing a feature like this.",
348-
)
349-
350305
default:
351306
return v, fmt.Errorf("Unexpected input to v.Multiply(): %T", val)
352307
}
353308
}
354309

355-
/*
356-
ToScalarLinearExpression
357-
Description:
358-
359-
Converting the variable into a scalar linear Expression.
360-
*/
361-
func (v Variable) ToScalarLinearExpression() ScalarLinearExpr {
362-
// Constants
363-
364-
// Create components
365-
vars := []Variable{v}
366-
coeffs := []float64{v.Coeffs()[0]}
367-
368-
// Create sle
369-
return ScalarLinearExpr{
370-
X: VarVector{vars},
371-
L: *mat.NewVecDense(1, coeffs),
372-
C: 0,
373-
}
374-
}
375-
376310
/*
377311
Dims
378312
Description:
@@ -439,7 +373,7 @@ func NewContinuousVariable(envs ...Environment) Variable {
439373
ID: uint64(nextIdx),
440374
Lower: float64(-Infinity),
441375
Upper: float64(+Infinity),
442-
Vtype: Continuous,
376+
Type: Continuous,
443377
}
444378

445379
}
@@ -469,7 +403,7 @@ func NewBinaryVariable(envs ...Environment) Variable {
469403
ID: uint64(nextIdx),
470404
Lower: 0.0,
471405
Upper: 1.0,
472-
Vtype: Binary,
406+
Type: Binary,
473407
}
474408

475409
}

symbolic/vector_expression.go

+13-25
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ type VectorExpression interface {
2828
// IDs returns a slice of the Var ids in the expression
2929
IDs() []uint64
3030

31-
// Coeffs returns a slice of the coefficients in the expression
32-
LinearCoeff() mat.Dense
31+
//// Coeffs returns a slice of the coefficients in the expression
32+
//LinearCoeff() mat.Dense
3333

3434
// Constant returns the constant additive value in the expression
3535
Constant() mat.VecDense
@@ -72,17 +72,17 @@ type VectorExpression interface {
7272
Dims() []int
7373
}
7474

75-
/*
76-
NewVectorExpression
77-
Description:
78-
79-
NewExpr returns a new expression with a single additive constant value, c,
80-
and no variables. Creating an expression like sum := NewVectorExpr(0) is useful
81-
for creating new empty expressions that you can perform operatotions on later
82-
*/
83-
func NewVectorExpression(c mat.VecDense) VectorLinearExpr {
84-
return VectorLinearExpr{C: c}
85-
}
75+
///*
76+
//NewVectorExpression
77+
//Description:
78+
//
79+
// NewExpr returns a new expression with a single additive constant value, c,
80+
// and no variables. Creating an expression like sum := NewVectorExpr(0) is useful
81+
// for creating new empty expressions that you can perform operatotions on later
82+
//*/
83+
//func NewVectorExpression(c mat.VecDense) VectorLinearExpr {
84+
// return VectorLinearExpr{C: c}
85+
//}
8686

8787
//func (e VectorExpression) getVarsPtr() *uint64 {
8888
//
@@ -112,18 +112,6 @@ func IsVectorExpression(e interface{}) bool {
112112
switch e.(type) {
113113
case mat.VecDense:
114114
return true
115-
case KVector:
116-
return true
117-
case KVectorTranspose:
118-
return true
119-
case VarVector:
120-
return true
121-
case VarVectorTranspose:
122-
return true
123-
case VectorLinearExpr:
124-
return true
125-
case VectorLinearExpressionTranspose:
126-
return true
127115
default:
128116
return false
129117

0 commit comments

Comments
 (0)