strange attractors

Visualizing Attractors

An attractor is a set of values to which a numerical system tends to evolve. An attractor is called a strange attractor if the resulting pattern has a fractal structure. This notebook shows how to calculate and plot two-dimensional attractors of a variety of types, using code and parameters primarily from Lázaro Alonso , François Pacull , Jason Rampe , Paul Bourke , and James A. Bednar .

Clifford Attractors

For example, a Clifford Attractor is a strange attractor defined by two iterative equations that determine the x,y locations of discrete steps in the path of a particle across a 2D space, given a starting point (x0,y0) and the values of four parameters (a,b,c,d) :

\begin{equation} x_{n +1} = \sin(a y_{n}) + c \cos(a x_{n})\\ y_{n +1} = \sin(b x_{n}) + d \cos(b y_{n}) \end{equation}

At each time step, the equations define the location for the following time step, and the accumulated locations show the areas of the 2D plane most commonly visited by the imaginary particle.

It's easy to calculate these values in Python using Numba . First, we define the iterative attractor equation:

In [1]:
import numpy as np, pandas as pd, datashader as ds
from datashader import transfer_functions as tf
from datashader.colors import inferno, viridis
from numba import jit
from math import sin, cos, sqrt, fabs

@jit
def Clifford(x, y, a, b, c, d, *o):
    return sin(a * y) + c * cos(a * x), \
           sin(b * x) + d * cos(b * y)

We then evaluate this equation 10 million times, creating a set of x,y coordinates visited. The @jit here and above is optional, but it makes the code 50x faster.

In [2]:
n=10000000

@jit
def trajectory(fn, x0, y0, a, b=0, c=0, d=0, e=0, f=0, n=n):
    x, y = np.zeros(n), np.zeros(n)
    x[0], y[0] = x0, y0
    for i in np.arange(n-1):
        x[i+1], y[i+1] = fn(x[i], y[i], a, b, c, d, e, f)
    return pd.DataFrame(dict(x=x,y=y))
In [3]:
%%time
df = trajectory(Clifford, 0, 0, -1.3, -1.3, -1.8, -1.9)
CPU times: user 1.06 s, sys: 189 ms, total: 1.25 s
Wall time: 1.09 s
In [4]:
df.tail()
Out[4]:
x y
9999995 0.798129 0.098356
9999996 -1.042485 -2.745660
9999997 -0.799859 2.705656
9999998 -0.544501 2.629746
9999999 -1.094023 2.477735

We can now aggregate these 10,000,000 continuous coordinates into a discrete 2D rectangular grid with Datashader , counting each time a point fell into that grid cell:

In [5]:
%%time

cvs = ds.Canvas(plot_width = 700, plot_height = 700)
agg = cvs.points(df, 'x', 'y')
print(agg.values[190:195,190:195],"\n")
[[ 44  33  33  29  32]
 [ 34  34  44  33  39]
 [127  39  33  38  33]
 [157 199 114  57  57]
 [ 84  97 136 123 108]]

CPU times: user 931 ms, sys: 8.16 ms, total: 939 ms
Wall time: 509 ms

A small portion of that grid is shown above, but it's difficult to see the grid's structure from the numerical values. To see the entire array at once, we can turn each grid cell into a pixel, using a greyscale value from white to black:

In [6]:
ds.transfer_functions.Image.border=0

tf.shade(agg, cmap = ["white", "black"])
Out[6]: