-
Notifications
You must be signed in to change notification settings - Fork 1
/
solve.go
438 lines (326 loc) · 9.16 KB
/
solve.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package polygo
import (
"log"
"math"
"math/rand"
)
// CauchyBound returns Cauchy's root bound of p.
//
// If p(x) = 0, then |x| <= p.CauchyBound().
//
// Panics for constant p.
func (p Poly) CauchyBound() float64 {
if p.deg == 0 {
log.Panic("CauchyBound: constant polynomial.")
}
// Compute Cauchy's bound.
leadrecip := 1 / p.coef[p.deg]
maxi := math.Abs(p.coef[0] * leadrecip)
var tmp float64
for i := 1; i < p.deg; i++ {
tmp = math.Abs(p.coef[i] * leadrecip)
if tmp > maxi {
maxi = tmp
}
}
return 1 + maxi
}
// CountSturm returns the number of distinct real roots of p on the interval (a, b].
func (p Poly) CountSturm(a, b float64) int {
return cacheSturmChain(p).count(a, b)
}
// SolveNewtonRaphson implements the Newton-Raphson method for a single root with the intial guess
// and given number of iterations. An approximated root is returned.
//
// Panics for negative iterations.
func (p Poly) SolveNewtonRaphson(guess float64, iterations int) float64 {
if iterations < 0 {
log.Panicf("SolveNewtonRaphson: negative iterations %d.", iterations)
}
if iterations == 0 {
return guess
}
d := p.Derivative()
var dvalue float64
for ; iterations > 0; iterations-- {
dvalue = d.At(guess)
if equalAbs(dvalue, 0, 0.001) {
// Exit iteration if dvalue is close enough to zero.
break
}
guess -= p.At(guess) / dvalue
}
return guess
}
// SolveBisect returns all distinct real roots of p on the interval [left, right] with given
// precision.
//
// Panics for invalid intervals and negative precision.
func (p Poly) SolveBisect(left, right, precision float64) float64 {
if left > right {
log.Panicf("SolveBisect: invalid interval [%f, %f].", left, right)
}
if precision < 0 {
log.Panicf("SolveBisect: negative precision %f.", precision)
}
var mid float64
for right-left > bisectPrecision {
mid = 0.5 * (left + right)
if p.CountSturm(left, mid) == 1 {
right = mid
} else {
// If the root doesn't lie on the left-mid side, then it must lie on the
// mid-right side.
left = mid
}
}
return mid
}
type CountAlgorithm int
type IsolateAlgorithm int
type SearchAlgorithm int
const (
// ALG_COUNT represents the algorithm used for counting the number of roots on a half-open
// interval (a, b].
ALG_COUNT_STURM CountAlgorithm = iota
// ALG_ISOLATE represents the algorithm used for isolating single roots to a sequence of
// non-overlapping half-open intervals with form (a, b].
ALG_ISOLATE_BISECT IsolateAlgorithm = iota
// ALG_SEARCH represents the algorithm used for finding a single root on a half-open interval
// (a, b].
ALG_SEARCH_NEWTON SearchAlgorithm = iota
ALG_SEARCH_BISECT
)
var (
newtonIterations = 500
bisectPrecision = 1e-6
)
func (a CountAlgorithm) String() string {
switch a {
case ALG_COUNT_STURM:
return "ALG_COUNT_STURM"
}
return "ALG_COUNT_UNKNOWN"
}
func (a IsolateAlgorithm) String() string {
switch a {
case ALG_ISOLATE_BISECT:
return "ALG_ISOLATE_BISECT"
}
return "ALG_ISOLATE_UNKNOWN"
}
func (a SearchAlgorithm) String() string {
switch a {
case ALG_SEARCH_NEWTON:
return "ALG_SEARCH_NEWTON"
case ALG_SEARCH_BISECT:
return "ALG_SEARCH_BISECT"
}
return "ALG_SEARCH_UNKNOWN"
}
// HalfOpenInterval represents a half-open interval (L, R].
type HalfOpenInterval struct {
L, R float64
}
// Solver represents a collection of methods used to obtain information about polynomial equations.
type Solver struct {
counter CountAlgorithm
isolator IsolateAlgorithm
searcher SearchAlgorithm
// Optional attributes (depends on algorithms used).
chainCache map[uint32]sturmChain
}
// NewSolver returns a Solver equipped with the given root counting, isolation, and searching
// algorithms.
func NewSolver(counter CountAlgorithm, isolator IsolateAlgorithm, searcher SearchAlgorithm) Solver {
return Solver{
counter: counter,
isolator: isolator,
searcher: searcher,
chainCache: make(map[uint32]sturmChain),
}
}
// NewSolverDefault returns a default solver.
//
// Specifically, the defaults are:
// - Root counting algorithm: ALG_COUNT_STURM
// - Root isolation algorithm: ALG_ISOLATE_BISECT
// - Root search algorithm: ALG_SEARCH_BISECT
func NewSolverDefault() Solver {
return NewSolver(ALG_COUNT_STURM, ALG_ISOLATE_BISECT, ALG_SEARCH_BISECT)
}
func (s Solver) cacheSturmChain(p Poly) sturmChain {
id := p.id()
cache := s.chainCache
for key := range cache {
if id == key {
return cache[id]
}
}
// Sturm chain has not been cached.
cache[id] = new_sturmChain(p)
return cache[id]
}
func (s Solver) CountRootsWithin(p Poly, a, b float64) int {
var ret int
switch s.counter {
case ALG_COUNT_STURM:
ret = s.cacheSturmChain(p).count(a, b)
}
return ret
}
// IsolateRoots returns a partition of the half-open interval (a, b] such that each half-open
// subinterval of the partition contains exactly one root of p.
func (s Solver) IsolateRootsWithin(p Poly, a, b float64) []HalfOpenInterval {
partition := []HalfOpenInterval{}
switch s.isolator {
case ALG_ISOLATE_BISECT:
c := s.CountRootsWithin(p, a, b)
if c == 0 {
return []HalfOpenInterval{}
}
if c == 1 {
return []HalfOpenInterval{{a, b}}
}
m := 0.5 * (a + b)
partition = append(s.IsolateRootsWithin(p, a, m), s.IsolateRootsWithin(p, m, b)...)
}
return partition
}
// solve_linear returns the root of linear p.
func solve_linear(p Poly) []float64 {
return []float64{-p.coef[0] / p.coef[1]}
}
// solve_quadratic returns the real roots of quadratic p.
func solve_quadratic(p Poly) []float64 {
c, negb, a := p.coef[0], -p.coef[1], p.coef[2]
d := negb*negb - 4*a*c
recip := 1 / (2 * a)
if d > 0 {
sqrtd := math.Sqrt(d)
return []float64{(negb + sqrtd) * recip, (negb - sqrtd) * recip}
}
if d == 0 {
return []float64{negb * recip}
}
return []float64{}
}
// solve_random_sample_newton returns the approximated roots of p on the interval (left, right].
func solve_random_sample_newton(p Poly, left, right float64) float64 {
// Implement Newton-Raphson with random guess sampling.
pprime := p.Derivative()
root := left
for !(left < root && root <= right) {
root = left + rand.Float64()*(right-left)
for i := 0; i < newtonIterations; i++ {
fprimeroot := pprime.At(root)
if fprimeroot == 0 {
break
}
root -= p.At(root) / fprimeroot
}
}
return root
}
// solve_bisect returns the approximated roots of p on the interval (left, right].
func solve_bisect(p Poly, left, right float64, counter func(Poly, float64, float64) int) float64 {
var mid float64
for right-left > bisectPrecision {
mid = 0.5 * (left + right)
if counter(p, left, mid) == 1 {
right = mid
} else {
// If the root doesn't lie on the left-mid side, then it must lie on the
// mid-right side.
left = mid
}
}
return mid
}
// FindRootsWithin returns the distinct roots of p on the half-open interval (a, b].
//
// Panics for invalid intervals and infinite solutions.
func (s Solver) FindRootsWithin(p Poly, a, b float64) []float64 {
if b < a {
log.Panicf("FindRootsWithin: invalid interval (%f, %f].", a, b)
}
roots := []float64{}
// For deg(p) = 0, 1, 2, just solve exactly.
if p.deg == 0 {
if p.coef[0] == 0 {
log.Panicf("FindRootsWithin: infinite solutions for %v.", p)
}
return []float64{}
}
// TODO: Check bounds a, b.
if p.deg == 1 {
return solve_linear(p)
}
if p.deg == 2 {
return solve_quadratic(p)
}
intervals := s.IsolateRootsWithin(p, a, b)
switch s.searcher {
case ALG_SEARCH_NEWTON:
for _, h := range intervals {
roots = append(roots, solve_random_sample_newton(p, h.L, h.R))
}
case ALG_SEARCH_BISECT:
for _, h := range intervals {
roots = append(roots, solve_bisect(p, h.L, h.R, s.CountRootsWithin))
}
}
return roots
}
// FindRoots returns all distinct roots of p.
func (s Solver) FindRoots(p Poly) []float64 {
bound := p.CauchyBound()
return s.FindRootsWithin(p, -bound, bound)
}
// Point represents a 2D Cartesian coordinate.
type Point struct {
X, Y float64
}
// FindIntersectionsWithin returns the intersections of p and q on the half-open interval (a, b].
//
// Panics for invalid intervals.
func (s Solver) FindIntersectionsWithin(p, q Poly, a, b float64) []Point {
if b < a {
log.Panicf("FindIntersectionsWithin: invalid interval (%f, %f].", a, b)
}
xinter := s.FindRootsWithin(p.Sub(q), a, b)
points := make([]Point, len(xinter))
for i, x := range xinter {
points[i] = Point{X: x, Y: p.At(x)}
}
return points
}
// FindIntersections returns all intersections of p and q.
func (s Solver) FindIntersections(p, q Poly) []Point {
xinter := s.FindRoots(p.Sub(q))
points := make([]Point, len(xinter))
for i, x := range xinter {
points[i] = Point{X: x, Y: p.At(x)}
}
return points
}
// SetNewtonSearchIterations sets the Newton's method root search algorithm iterations to v.
//
// Panics for negative v.
func SetNewtonSearchIterations(v int) {
if v < 0 {
log.Panic("SetNewtonSearchIterations: negative v.")
}
newtonIterations = v
}
// SetBisectSearchPrecision sets the bisect root search algorithm precision to v.
//
// The closer ot zero v is, the more accurate the roots.
//
// Panics for negative v.
func SetBisectSearchPrecision(v float64) {
if v < 0 {
log.Panic("SetBisectSearchPrecision: negative v.")
}
bisectPrecision = v
}