{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Elementary Cellular Automata [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/maxencefaldor/cax/blob/main/examples/10_elementary.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Installation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You will need Python 3.12 or later, and a working JAX installation. For example, you can install JAX with:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%pip install -U \"jax[cuda]\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then, install CAX from PyPi:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%pip install -U \"cax[examples]\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import jax.numpy as jnp\n", "import mediapy\n", "from flax import nnx\n", "\n", "from cax.cs.elementary import Elementary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Configuration" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "seed = 0\n", "\n", "num_steps = 512\n", "spatial_dims = (1_024,)\n", "wolfram_code_int = 110 # Rule 110\n", "\n", "rngs = nnx.Rngs(seed)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Instantiate system" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "wolfram_code = Elementary.wolfram_code_from_rule_number(wolfram_code_int)\n", "wolfram_code" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cs = Elementary(wolfram_code=wolfram_code, rngs=rngs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sample initial state" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def sample_state():\n", "\t\"\"\"Sample a state with a single active cell.\"\"\"\n", "\tstate = jnp.zeros((*spatial_dims, 1))\n", "\treturn state.at[spatial_dims[0] // 2].set(1.0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Run" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "state_init = sample_state()\n", "state_final = cs(state_init, num_steps=num_steps, sow=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visualize" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "intermediates = nnx.pop(cs, nnx.Intermediate)\n", "states = intermediates.state[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "states = jnp.concatenate([state_init[None], states])\n", "frame = cs.render(states)\n", "\n", "mediapy.show_image(frame)" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.0" } }, "nbformat": 4, "nbformat_minor": 2 }