Created
February 26, 2024 14:09
-
-
Save rutj3/5f0dcd634903861fa3c7070da8c20cb1 to your computer and use it in GitHub Desktop.
Peter de Jong attractor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "id": "418e5aae-9c46-470d-b16c-13916ffa89c6", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import numpy as np\n", | |
| "from numba import njit\n", | |
| "%config InlineBackend.print_figure_kwargs = {'bbox_inches': None}\n", | |
| "import matplotlib.pyplot as plt\n", | |
| "import matplotlib as mpl" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "id": "671b4fc6-2042-4526-ad36-71768580a2ae", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "@njit\n", | |
| "def next_step(y, x, a, b, c, d):\n", | |
| " \"\"\"Peter De Jong attractor\n", | |
| " \n", | |
| " source:\n", | |
| " https://paulbourke.net/fractals/peterdejong/\n", | |
| " \"\"\"\n", | |
| " return (\n", | |
| " np.sin(c*x) - np.cos(d*y), # y+1\n", | |
| " np.sin(a*y) - np.cos(b*x), # x+1\n", | |
| " )\n", | |
| "\n", | |
| "@njit(boundscheck=True)\n", | |
| "def update_grid_bilinear(grid, y, x, ys, xs):\n", | |
| " \"\"\"Increment the grid using bilinear interpolation\"\"\"\n", | |
| " \n", | |
| " dx = x - int(x)\n", | |
| " dy = y - int(y)\n", | |
| "\n", | |
| " x_px = int(x)\n", | |
| " y_px = int(y)\n", | |
| " \n", | |
| " if (y_px >= 0) & (x_px >= 0) & (y_px < ys) & (x_px < xs):\n", | |
| " grid[y_px, x_px] += (1 - dy) * (1 - dx)\n", | |
| " \n", | |
| " if (y_px >= 0) & (x_px >= -1) & (y_px < ys) & (x_px < xs-1):\n", | |
| " grid[y_px, x_px+1] += (1 - dy) * dx\n", | |
| " \n", | |
| " if (y_px >= -1) & (x_px >= 0) & (y_px < ys-1) & (x_px < xs):\n", | |
| " grid[y_px+1, x_px] += dy * (1 - dx)\n", | |
| " \n", | |
| " if (y_px >= -1) & (x_px >= -1) & (y_px < ys-1) & (x_px < xs-1):\n", | |
| " grid[y_px+1, x_px+1] += dy * dx\n", | |
| " \n", | |
| "@njit\n", | |
| "def run_attractor(grid, a, b, c, d, n_steps=2000, scale=100):\n", | |
| " \"\"\"Iterate over an attractor\"\"\"\n", | |
| " \n", | |
| " ys, xs = grid.shape\n", | |
| " yhw = ys / 2\n", | |
| " xhw = xs / 2\n", | |
| "\n", | |
| " # init\n", | |
| " x = 0.0\n", | |
| " y = 0.0\n", | |
| " \n", | |
| " for _ in range(n_steps):\n", | |
| " \n", | |
| " y, x = next_step(y, x, a, b, c, d)\n", | |
| " \n", | |
| " # scale \"attractor coordinates\" to grid\n", | |
| " x_px = x * scale + xhw\n", | |
| " y_px = y * scale + yhw\n", | |
| " \n", | |
| " update_grid_bilinear(grid, y_px, x_px, ys, xs)\n", | |
| "\n", | |
| " return grid" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "0b884624-43a4-4ca4-9561-df4a21185480", | |
| "metadata": {}, | |
| "source": [ | |
| "## Process attractor" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "id": "e06ff61a-71b3-48f6-84e2-d6bf1d0a50dd", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# attractor parameters\n", | |
| "# from https://paulbourke.net/fractals/peterdejong/\n", | |
| "a = 1.4\n", | |
| "b = -2.3\n", | |
| "c = 2.4\n", | |
| "d = -2.1\n", | |
| "\n", | |
| "# size of the output image\n", | |
| "ys = 1024\n", | |
| "xs = 1024\n", | |
| "grid = np.zeros((ys, xs), dtype=np.float64)\n", | |
| "\n", | |
| "# running takes a minute, reduce n_steps to 10_000 when fiddling\n", | |
| "img = run_attractor(grid, a, b, c, d, n_steps=1_000_000_000, scale=ys/4.2)\n", | |
| "\n", | |
| "# convert to log for visualization\n", | |
| "tiny = 0.0000001\n", | |
| "img = np.log(img + tiny)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "98929adc-657c-40c8-8054-dcbef3ff6271", | |
| "metadata": {}, | |
| "source": [ | |
| "## Visualize" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "id": "6a3ecb9d-bb2b-4274-840f-17420d0d9f31", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment