/
main.go
553 lines (489 loc) · 12.2 KB
/
main.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package main
import (
"fmt"
"github.com/banthar/Go-SDL/sdl"
"github.com/banthar/gl"
"math"
"math/rand"
"time"
"flag"
"log"
"os"
"runtime/pprof"
"strconv"
)
func feq(x float64, y float64, within int) bool {
z := x
for i := 0; i < within; i++ {
z = math.Nextafter(z, y)
if y == z {
return true
}
}
return false
}
func fgteq(x float64, y float64, within int) bool {
return feq(x, y, within) || x > y
}
func flteq(x float64, y float64, within int) bool {
return feq(x, y, within) || x < y
}
type F func(Matrix) Matrix
func lyapunov_exponent(fn F, start Matrix) float64 {
const (
ignore int = 500
)
var (
p1 Matrix = MakeMatrix(1, start.Height())
p2 Matrix = MakeMatrix(1, start.Height())
p3 Matrix = MakeMatrix(1, start.Height())
d0, d1, ltot, dim_sum float64
i, its, dim int
)
// Start with any initial condition in the basin of attraction.
copy(p1.Cols()[0], start.Cols()[0])
// Iterate until the orbit is on the attractor.
for i = 0; i < ignore; i++ {
p1 = fn(p1)
}
// Select (almost any) nearby point (separated by d0).
// it's about 1000 MIN_FLOATs away from d
// sqrt 1000 = 31
copy(p2.Cols()[0], p1.Cols()[0])
for i = 0; i < 31; i++ {
for dim = 0; dim < p2.Height(); dim++ {
p2.Cols()[0][dim] = math.Nextafter(p2.Cols()[0][dim], 1000)
}
}
// Get a d0 of the right scale
d0 = p1.Cols()[0][0] - p2.Cols()[0][0]
//fmt.Println(x1, y1, " - ", x2, y2, x1 - x2, y1 - y2)
for its = 0; its < 10000; its++ {
for dim = 0; dim < p2.Height(); dim++ {
if math.Abs(p1.Cols()[0][dim]) > 10000 ||
math.Abs(p2.Cols()[0][dim]) > 10000 ||
math.IsNaN(p1.Cols()[0][dim]) ||
math.IsNaN(p2.Cols()[0][dim]) {
return 0
}
}
// Advance both orbits one iteration and calculate the new separation d1.
//fmt.Println("L1", its, p1[dim], y1)
//fmt.Println("L2", its, x2, y2)
p1 = fn(p1)
p3 = fn(p2)
for dim = 0; dim < p2.Height(); dim++ {
if feq(p1.Cols()[0][dim], p3.Cols()[0][dim], 2) {
continue
}
}
dim_sum = 0
for dim = 0; dim < p2.Height(); dim++ {
dim_sum += math.Pow(p3.Cols()[0][dim]-p1.Cols()[0][dim], 2)
}
d1 = math.Sqrt(dim_sum)
// Evaluate log |d1/d0| in any convenient base.
ltot += math.Log2(math.Abs(d1 / d0))
// Readjust one orbit so its separation is d0 in the same direction as d1.
for dim = 0; dim < p2.Height(); dim++ {
p2.Cols()[0][dim] = p1.Cols()[0][dim] +
d0*(p3.Cols()[0][dim]-p1.Cols()[0][dim])/d1
}
}
// Repeat many times and calculate the average
return ltot / float64(its)
}
var (
//rot float64 = 45.0
xoff, yoff, zoff float64 = 0, 0, 0
xrot, yrot, zrot float64 = 0, 0, 0
scale float64 = 0.5
)
func make_plot_fn(fn F, start Matrix, disguard, plot int) func() {
return func() {
var (
p Matrix = MakeMatrix(1, start.Height())
i int
)
for i = 0; i < start.Width(); i++ {
p.Cols()[i][0] = start.Cols()[i][0]
}
for i = 0; i < disguard; i++ {
p = fn(p)
}
for i = 0; i < plot; i++ {
p = fn(p)
gl.Vertex3d(p.Cols()[0][0], p.Cols()[1][0], p.Cols()[2][0])
}
}
}
func generate_list(fn func()) {
gl.Begin(gl.POINTS)
gl.Color4d(1, 1, 1, 0.25)
fn()
gl.End()
}
func plot_list(list uint) {
gl.Enable(gl.BLEND)
gl.Enable(gl.POINT_SMOOTH)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
gl.PointSize(1.0)
gl.LoadIdentity()
gl.Rotated(xrot, 1, 0, 0)
gl.Rotated(yrot, 0, 1, 0)
gl.Rotated(zrot, 0, 0, 1)
gl.Scaled(scale, scale, scale)
gl.Translated(xoff, yoff, zoff)
gl.CallList(list)
gl.Flush()
}
func nCoeffs(dimension, order int) int {
switch {
case dimension == 0 || order == 0:
return 0
case dimension == 1:
return order
case order == 1:
return dimension
default:
return 2*nCoeffs(dimension-1, order) -
nCoeffs(dimension-2, order) +
nCoeffs(dimension, order-1) -
nCoeffs(dimension-1, order-1)
}
return 0
}
func coeffCombinations(dimension, order int) [][]int {
var (
vals []int = make([]int, order)
vals_lt_dimension bool = true
results [][]int = make([][]int, 0)
add_result func([]int) = func(new_result []int) {
new_copy := make([]int, order)
copy(new_copy, new_result)
results = append(results, new_copy)
}
)
for vals_lt_dimension {
vals_lt_dimension = false
for i := 0; i < order; i++ {
if vals[i] < dimension {
vals[i]++
vals_lt_dimension = true
add_result(vals)
break
} else {
for j := i + 1; j < order; j++ {
if vals[j] < dimension {
vals[j]++
for k := i; k < j; k++ {
vals[k] = vals[j]
}
add_result(vals)
i = -1
break
}
}
}
}
}
return results
}
func makeMapFn(dimension, order int, coeffs []float64) F {
var (
coeffCombs [][]int = coeffCombinations(dimension, order)
nCoeffCombs int = nCoeffs(dimension, order)
)
return func(p Matrix) Matrix {
var (
product float64
coeff int = 0
next Matrix = MakeMatrix(1, int(math.Max(3, float64(dimension))))
)
for d := 0; d < dimension; d++ {
next.Cols()[0][d] = coeffs[coeff]
coeff++
for c := 0; c < nCoeffCombs; c++ {
product = coeffs[coeff]
coeff++
for ord := 0; ord < order; ord++ {
if coeffCombs[c][ord] > 0 {
product *= p.Cols()[0][coeffCombs[c][ord]-1]
}
}
next.Cols()[0][d] += product
}
}
return next
}
}
func find_map_with_L(dimension, order int, min, max float64) (coeffs []float64, start Matrix) {
var (
L float64 = 0
rejected int = -1
)
coeffs = make([]float64, dimension+nCoeffs(order, dimension)*dimension)
start = MakeMatrix(1, int(math.Max(3, float64(dimension))))
for i := 0; i < dimension; i++ {
start.Cols()[0][i] = rand.Float64()
}
for L < min || L > max {
rejected++
for i := range coeffs {
coeffs[i] = 1.2 - 2.4*rand.Float64()
}
L = lyapunov_exponent(makeMapFn(dimension, order, coeffs), start)
}
fmt.Println("Rejected:", rejected)
fmt.Println("L:", L)
fmt.Println(coeffs)
return
}
func MakePointMatrix(fn F, start Matrix, disguard, plot int) Matrix {
var (
res Matrix = MakeMatrix(plot, start.Height())
p Matrix = MakeMatrix(1, start.Height())
i int
)
copy(p.Cols()[0], start.Cols()[0])
for i = 0; i < disguard; i++ {
p = fn(p)
}
for i = 0; i < plot; i++ {
p = fn(p)
copy(res.Cols()[i], p.Cols()[0])
}
return res
}
var (
xwrot float64 = 0
)
func testPlot(dimension, order int) {
var (
iterations float64 = 0
start_t = time.Now()
split time.Time = start_t
total time.Duration
fps float64 = 1.0
new_attractor, redraw bool = true, true
npoints int = 1e5
coeffs []float64
//offsets, offset_coeffs []float64
start Matrix = MakeMatrix(1, int(math.Max(3, float64(dimension))))
points, points2/*, points3*/ Matrix
//attractor = gl.GenLists(1);
)
//offsets = make([]float64, ncoeffs(order))
//offset_coeffs = make([]float64, ncoeffs(order))
//coeffs = make([]float64, dimension + nCoeffs(order, dimension) * dimension)
//for i := range offsets { offsets[i] += rand.Float64() }
for handleEvents(&new_attractor, &redraw, &npoints) {
xoff += xvel / fps
yoff += yvel / fps
zoff += zvel / fps
xrot += xrotvel / fps
yrot += yrotvel / fps
zrot += zrotvel / fps
scale += svel / fps
if new_attractor {
coeffs, start = find_map_with_L(dimension, order, 0.1, 0.4)
redraw = true
new_attractor = false
}
if redraw {
points = MakePointMatrix(makeMapFn(dimension, order, coeffs), start, 500, npoints)
points2 = MakeMatrix(npoints, points.Height())
//points3 = MakeMatrix(npoints, points.Height())
redraw = false
fmt.Println("Redraw", npoints, "points")
}
//points2 = points
//RotationXW(xwrot).Apply(points, points2)
//RotationYW(xwrot + 0.25).Apply(points2, points3)
//RotationZW(xwrot + 0.50).Apply(points3, points2)
if dimension == 4 {
RotationZW(xwrot).Apply(points, points2)
xwrot += 0.05
ApplyW(points2, points2)
} else {
points2 = points
}
gl.Enable(gl.BLEND)
gl.Enable(gl.POINT_SMOOTH)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
gl.PointSize(1.0)
gl.LoadIdentity()
gl.Rotated(xrot, 1, 0, 0)
gl.Rotated(yrot, 0, 1, 0)
gl.Rotated(zrot, 0, 0, 1)
gl.Scaled(scale, scale, scale)
gl.Translated(xoff, yoff, zoff)
gl.Color4d(1, 1, 1, 0.25)
gl.EnableClientState(gl.VERTEX_ARRAY)
if dimension > 3 {
gl.VertexPointer(3, (dimension-3)*32, points2.FlatCols())
} else {
gl.VertexPointer(3, 0, points2.FlatCols())
}
gl.DrawArrays(gl.POINTS, 0, points2.Width())
gl.DisableClientState(gl.VERTEX_ARRAY)
sdl.GL_SwapBuffers()
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
iterations++
if time.Since(split).Seconds() > 1.0 {
total = time.Since(start_t)
fps = iterations / total.Seconds()
fmt.Println(iterations, "iterations in", total.Seconds(), "gives",
fps, "fps")
split = time.Now()
}
}
}
/**************************************************/
func initScreen() {
sdl.Init(sdl.INIT_VIDEO)
const (
resx int = 640
resy int = 480
)
var (
screen = sdl.SetVideoMode(resx, resy, 16, sdl.OPENGL)
)
if screen == nil {
sdl.Quit()
panic("Couldn't set GL video mode: " + sdl.GetError() + "\n")
}
if gl.Init() != 0 {
panic("gl error")
}
gl.MatrixMode(gl.PROJECTION)
//gl.MatrixMode(gl.MODELVIEW)
gl.Viewport(0, 0, int(screen.W), int(screen.H))
gl.LoadIdentity()
gl.Ortho(0, float64(screen.W), float64(screen.H), 0, -1.0, 1.0)
//gl.DepthRange(-1, 1)
gl.ClearColor(0, 0, 0, 0)
gl.Clear(gl.COLOR_BUFFER_BIT)
}
// sdl.GetModState() doesn't work properly so we store state here :(
var (
mod = sdl.KMOD_NONE
xvel, yvel, zvel, xrotvel, yrotvel, zrotvel, svel float64 = 0, 0, 0, 0, 0, 0, 0
)
func handleEvents(new_attractor, redraw *bool, npoints *int) bool {
for ev := sdl.PollEvent(); ev != nil; ev = sdl.PollEvent() {
switch e := ev.(type) {
case *sdl.QuitEvent:
return false
case *sdl.KeyboardEvent:
//fmt.Println(sdl.Key(e.Keysym.Sym), ":", sdl.GetKeyName(sdl.Key(e.Keysym.Sym)))
switch e.Type {
case sdl.KEYUP:
switch sdl.Key(e.Keysym.Sym) {
case sdl.K_LCTRL:
mod = sdl.KMOD_NONE
case sdl.K_UP, sdl.K_DOWN:
yvel = 0
case sdl.K_LEFT, sdl.K_RIGHT:
xvel = 0
svel = 0
case sdl.K_KP4, sdl.K_KP6:
yrotvel = 0
case sdl.K_KP8, sdl.K_KP2:
xrotvel = 0
case sdl.K_KP7, sdl.K_KP9:
zrotvel = 0
}
case sdl.KEYDOWN:
switch mod {
case sdl.KMOD_NONE:
switch sdl.Key(e.Keysym.Sym) {
case sdl.K_LCTRL:
mod = sdl.KMOD_LCTRL
case sdl.K_ESCAPE:
return false
case sdl.K_UP:
yvel = -1 / scale
case sdl.K_DOWN:
yvel = 1 / scale
case sdl.K_LEFT:
xvel = 1 / scale
case sdl.K_RIGHT:
xvel = -1 / scale
case sdl.K_KP4:
yrotvel = -1
case sdl.K_KP6:
yrotvel = +1
case sdl.K_KP8:
xrotvel = -1
case sdl.K_KP2:
xrotvel = +1
case sdl.K_KP7:
zrotvel = -1
case sdl.K_KP9:
zrotvel = +1
case sdl.K_n:
*new_attractor = true
case sdl.K_z:
xoff, yoff, zoff, xrot, yrot, zrot = 0, 0, 0, 0, 0, 0
scale = 0.5
case sdl.K_COMMA:
*npoints -= 1e5
*redraw = true
case sdl.K_PERIOD:
*npoints += 1e5
*redraw = true
default:
}
case sdl.KMOD_LCTRL:
switch sdl.Key(e.Keysym.Sym) {
case sdl.K_LEFT:
svel -= 1
case sdl.K_RIGHT:
svel += 1
default:
}
}
}
default:
}
}
return true
}
/************************/
var (
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
orderflag = flag.String("order", "5", "Order of attractors to generate")
dimensionflag = flag.String("dimension", "2", "Dimension of attractors to generate")
)
func main() {
var (
order, dimension int
err error
)
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
initScreen()
if *orderflag != "" {
order, err = strconv.Atoi(*orderflag)
if err != nil {
fmt.Println(err)
return
}
}
if *dimensionflag != "" {
dimension, err = strconv.Atoi(*dimensionflag)
if err != nil {
fmt.Println(err)
return
}
}
testPlot(dimension, order)
}