-
Notifications
You must be signed in to change notification settings - Fork 0
/
term.go
103 lines (78 loc) · 2.84 KB
/
term.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
// SPDX-License-Identifier: BSD-3-Clause
package kiwi
import "strconv"
type Term struct {
Variable *Variable
Coefficient float64
}
var _ Constrainer = Term{}
func (t Term) GetValue() float64 {
return t.Coefficient * t.Variable.Value
}
func (t Term) Multiply(coefficient float64) Term {
return Term{Variable: t.Variable, Coefficient: t.Coefficient * coefficient}
}
func (t Term) Divide(denominator float64) Term {
return Term{Variable: t.Variable, Coefficient: t.Coefficient / denominator}
}
func (t Term) Negate() Term {
return Term{Variable: t.Variable, Coefficient: -t.Coefficient}
}
func (t Term) AddConstant(constant float64) Expression {
return Expression{Terms: []Term{t}, Constant: constant}
}
func (t Term) AddVariable(variable *Variable) Expression {
other := Term{Variable: variable, Coefficient: 1.0}
return Expression{Terms: []Term{t, other}, Constant: 0.0}
}
func (t Term) AddTerm(term Term) Expression {
return Expression{Terms: []Term{t, term}, Constant: 0.0}
}
func (t Term) AddExpression(expression Expression) Expression {
terms := make([]Term, 1+len(expression.Terms))
terms[0] = t
copy(terms[1:], expression.Terms)
return Expression{Terms: terms, Constant: expression.Constant}
}
func (t Term) EqualsConstant(constant float64) *Constraint {
return NewConstraint(t.AddConstant(-constant), EQ)
}
func (t Term) EqualsVariable(variable *Variable) *Constraint {
return NewConstraint(t.AddTerm(variable.Negate()), EQ)
}
func (t Term) EqualsTerm(term Term) *Constraint {
return NewConstraint(t.AddTerm(term.Negate()), EQ)
}
func (t Term) EqualsExpression(expression Expression) *Constraint {
return expression.EqualsTerm(t)
}
func (t Term) LessThanOrEqualsConstant(constant float64) *Constraint {
return NewConstraint(t.AddConstant(-constant), LE)
}
func (t Term) LessThanOrEqualsVariable(variable *Variable) *Constraint {
return NewConstraint(t.AddTerm(variable.Negate()), LE)
}
func (t Term) LessThanOrEqualsTerm(term Term) *Constraint {
return NewConstraint(t.AddTerm(term.Negate()), LE)
}
func (t Term) LessThanOrEqualsExpression(expression Expression) *Constraint {
return NewConstraint(t.AddExpression(expression.Negate()), LE)
}
func (t Term) GreaterThanOrEqualsConstant(constant float64) *Constraint {
return NewConstraint(t.AddConstant(-constant), GE)
}
func (t Term) GreaterThanOrEqualsVariable(variable *Variable) *Constraint {
return NewConstraint(t.AddTerm(variable.Negate()), GE)
}
func (t Term) GreaterThanOrEqualsTerm(term Term) *Constraint {
return NewConstraint(t.AddTerm(term.Negate()), GE)
}
func (t Term) GreaterThanOrEqualsExpression(expression Expression) *Constraint {
return NewConstraint(t.AddExpression(expression.Negate()), GE)
}
func (t Term) String() string {
if t.Coefficient == 1.0 {
return t.Variable.String()
}
return strconv.FormatFloat(t.Coefficient, 'f', -1, 64) + " * " + t.Variable.String()
}