Skip to content

Instantly share code, notes, and snippets.

@tomgidden
Last active March 12, 2022 11:40
Show Gist options
  • Save tomgidden/1d8145ed8576c99f70975d930895ca6a to your computer and use it in GitHub Desktop.
Save tomgidden/1d8145ed8576c99f70975d930895ca6a to your computer and use it in GitHub Desktop.
2D Lyapunov in Python using ANSI terminal colors
#!/usr/bin/env python3
import math
from sys import stdout
import shutil
w,h = shutil.get_terminal_size()
a0 = 3.0
a1 = 4.0
b0 = 3.5
b1 = 4.0
sequence = "AABAB"
settle = 10
iters = 100
scale = 2.0/(iters)
colors = 18
def plot(c):
r = 0 if c<0 else c if c<6 else 5
g = 0 if c<6 else c-6 if c<12 else 5
b = -c%5 if c<0 else 0 if c<12 else c-12 if c<18 else 5
stdout.write(f"\033[48;5;{16+36*r+6*g+b}m ")
l = len(sequence)
for y in range(h):
b = b0 + (b1-b0) * y / h
for x in range(w):
a = a0 + (a1-a0) * x / w
z = 0
c = 0.5
for i in range(iters):
r = a if sequence[i % l]=='A' else b
c = r * c * (1 - c)
if i > settle:
r = abs(r - 2.0 * r * c)
z = z + math.log10(r)
plot(int(-z * scale * colors))
stdout.write("\033[0m\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment