-
Notifications
You must be signed in to change notification settings - Fork 7
/
math.go
196 lines (158 loc) · 3.97 KB
/
math.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package casso
import "sync/atomic"
type SymbolKind uint8
const (
External SymbolKind = iota
Slack
Error
Dummy
)
var SymbolTable = [...]string{
External: "External",
Slack: "Slack",
Error: "Error",
Dummy: "Dummy",
}
func (s SymbolKind) Restricted() bool { return s == Slack || s == Error }
func (s SymbolKind) String() string { return SymbolTable[s] }
type Symbol uint64
var (
count uint64
zero Symbol
)
func New() Symbol {
return next(External)
}
func next(typ SymbolKind) Symbol {
return Symbol((atomic.AddUint64(&count, 1) & 0x3fffffffffffffff) | (uint64(typ) << 62))
}
func (sym Symbol) Kind() SymbolKind { return SymbolKind(sym >> 62) }
func (sym Symbol) Zero() bool { return sym == zero }
func (sym Symbol) Restricted() bool { return !sym.Zero() && sym.Kind().Restricted() }
func (sym Symbol) External() bool { return !sym.Zero() && sym.Kind() == External }
func (sym Symbol) Slack() bool { return !sym.Zero() && sym.Kind() == Slack }
func (sym Symbol) Error() bool { return !sym.Zero() && sym.Kind() == Error }
func (sym Symbol) Dummy() bool { return !sym.Zero() && sym.Kind() == Dummy }
func (sym Symbol) T(coeff float64) Term { return Term{coeff: coeff, id: sym} }
func (sym Symbol) EQ(val float64) Constraint { return NewConstraint(EQ, -val, sym.T(1.0)) }
func (sym Symbol) GTE(val float64) Constraint { return NewConstraint(GTE, -val, sym.T(1.0)) }
func (sym Symbol) LTE(val float64) Constraint { return NewConstraint(LTE, -val, sym.T(1.0)) }
type Priority float64
const (
Weak Priority = 1
Medium = 1e3 * Weak
Strong = 1e3 * Medium
Required = 1e3 * Strong
)
type Op uint8
const (
EQ Op = iota
GTE
LTE
)
var OpTable = [...]string{
EQ: "=",
GTE: ">=",
LTE: "<=",
}
func (o Op) String() string { return OpTable[o] }
type Constraint struct {
op Op
expr Expr
}
func NewConstraint(op Op, constant float64, terms ...Term) Constraint {
return Constraint{op: op, expr: NewExpr(constant, terms...)}
}
func (c Constraint) clone() Constraint {
res := Constraint{op: c.op, expr: c.expr.clone()}
return res
}
type Term struct {
coeff float64
id Symbol
}
type Expr struct {
constant float64
terms []Term
}
func NewExpr(constant float64, terms ...Term) Expr {
return Expr{constant: constant, terms: terms}
}
func (c Expr) clone() Expr {
res := Expr{constant: c.constant, terms: make([]Term, len(c.terms))}
copy(res.terms, c.terms)
return res
}
func (c Expr) find(id Symbol) int {
for i := 0; i < len(c.terms); i++ {
if c.terms[i].id == id {
return i
}
}
return -1
}
func (c *Expr) delete(idx int) {
copy(c.terms[idx:], c.terms[idx+1:])
c.terms = c.terms[:len(c.terms)-1]
}
func (c *Expr) addSymbol(coeff float64, id Symbol) {
idx := c.find(id)
if idx == -1 {
if !eqz(coeff) {
c.terms = append(c.terms, Term{coeff: coeff, id: id})
}
return
}
c.terms[idx].coeff += coeff
if eqz(c.terms[idx].coeff) {
c.delete(idx)
}
}
func (c *Expr) addExpr(coeff float64, other Expr) {
c.constant += coeff * other.constant
for i := 0; i < len(other.terms); i++ {
c.addSymbol(coeff*other.terms[i].coeff, other.terms[i].id)
}
}
func (c *Expr) negate() {
c.constant = -c.constant
for i := 0; i < len(c.terms); i++ {
c.terms[i].coeff = -c.terms[i].coeff
}
}
func (c *Expr) solveFor(id Symbol) {
idx := c.find(id)
if idx == -1 {
return
}
// 1. delete variable symbol entry from expression
// 2. reverse all signs and divide all coefficients by symbol coefficient
coeff := -1.0 / c.terms[idx].coeff
c.delete(idx)
if coeff == 1.0 {
return
}
c.constant *= coeff
for i := 0; i < len(c.terms); i++ {
c.terms[i].coeff *= coeff
}
}
func (c *Expr) solveForSymbols(lhs, rhs Symbol) {
c.addSymbol(-1.0, lhs)
c.solveFor(rhs)
}
func (c *Expr) substitute(id Symbol, other Expr) {
idx := c.find(id)
if idx == -1 {
return
}
coeff := c.terms[idx].coeff
c.delete(idx)
c.addExpr(coeff, other)
}
func eqz(val float64) bool {
if val < 0 {
return -val < 1.0e-8
}
return val < 1.0e-8
}