/
scalar_expression.go
139 lines (115 loc) · 3.8 KB
/
scalar_expression.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package optim
import (
"fmt"
"github.com/MatProGo-dev/SymbolicMath.go/symbolic"
)
// ScalarExpression represents a linear general expression of the form
// c0 * x0 + c1 * x1 + ... + cn * xn + k where ci are coefficients and xi are
// variables and k is a constant. This is a base interface that is implemented
// by single variables, constants, and general linear expressions.
type ScalarExpression interface {
// Variables returns the variables included in the scalar expression
Variables() []Variable
// NumVars returns the number of variables in the expression
NumVars() int
// Vars returns a slice of the Var ids in the expression
IDs() []uint64
// Coeffs returns a slice of the coefficients in the expression
Coeffs() []float64
// Constant returns the constant additive value in the expression
Constant() float64
// Plus adds the current expression to another and returns the resulting
// expression
Plus(rightIn interface{}, errors ...error) (Expression, error)
// Mult multiplies the current expression to another and returns the
// resulting expression
//Mult(c float64, errors ...error) (Expression, error)
// LessEq returns a less than or equal to (<=) constraint between the
// current expression and another
LessEq(rhsIn interface{}, errors ...error) (Constraint, error)
// GreaterEq returns a greater than or equal to (>=) constraint between the
// current expression and another
GreaterEq(rhsIn interface{}, errors ...error) (Constraint, error)
// Eq returns an equality (==) constraint between the current expression
// and another
Eq(rhsIn interface{}, errors ...error) (Constraint, error)
//Comparison
// Compares the receiver expression rhs with the expression rhs in the sense of sense.
Comparison(rhsIn interface{}, sense ConstrSense, errors ...error) (Constraint, error)
//Multiply
// Multiplies the given scalar expression with another expression
Multiply(rightIn interface{}, errors ...error) (Expression, error)
//Dims
// Returns the dimensions of the scalar expression (should always be 1,1)
Dims() []int
//Transpose returns the transpose of the given vector expression
Transpose() Expression
//ToSymbolic Returns the symbolic version of the scalar expression
// (i.e., the expression when declared using the symbolic math toolbox).
ToSymbolic() (symbolic.Expression, error)
// Check, checks the expression for any errors
Check() error
}
// NewExpr returns a new expression with a single additive constant value, c,
// and no variables. Creating an expression like sum := NewExpr(0) is useful
// for creating new empty expressions that you can perform operatotions on
// later
func NewScalarExpression(c float64) ScalarExpression {
return ScalarLinearExpr{C: c}
}
/*
IsScalarExpression
Description:
Determines whether or not an input object is a
valid "ScalarExpression" according to MatProInterface.
*/
func IsScalarExpression(e interface{}) bool {
// Check each type
switch e.(type) {
case float64:
return true
case K:
return true
case Variable:
return true
case ScalarLinearExpr:
return true
case ScalarQuadraticExpression:
return true
default:
return false
}
}
/*
ToScalarExpression
Description:
Converts the input expression to a valid type that
implements "ScalarExpression".
*/
func ToScalarExpression(e interface{}) (ScalarExpression, error) {
// Input Processing
if !IsScalarExpression(e) {
return K(1.0), fmt.Errorf(
"the input interface is of type %T, which is not recognized as a ScalarExpression.",
e,
)
}
// Convert
switch e2 := e.(type) {
case float64:
return K(e2), nil
case K:
return e2, nil
case Variable:
return e2, nil
case ScalarLinearExpr:
return e2, nil
case ScalarQuadraticExpression:
return e2, nil
default:
return K(1.0), fmt.Errorf(
"unexpected scalar expression conversion requested for type %T!",
e,
)
}
}