Skip to content

Instantly share code, notes, and snippets.

@IlievskiV
Created May 2, 2021 17:17
Show Gist options
  • Save IlievskiV/553a7c5bec8ae42c95805061ff465b41 to your computer and use it in GitHub Desktop.
Save IlievskiV/553a7c5bec8ae42c95805061ff465b41 to your computer and use it in GitHub Desktop.
import numpy as np
powers_of_two = np.array([[4], [2], [1]]) # shape (3, 1)
def step(x, rule_binary):
"""Makes one step in the cellular automaton.
Args:
x (np.array): current state of the automaton
rule_binary (np.array): the update rule
Returns:
np.array: updated state of the automaton
"""
x_shift_right = np.roll(x, 1) # circular shift to right
x_shift_left = np.roll(x, -1) # circular shift to left
y = np.vstack((x_shift_right, x, x_shift_left)).astype(np.int8) # stack row-wise, shape (3, cols)
z = np.sum(powers_of_two * y, axis=0).astype(np.int8) # LCR pattern as number
return rule_binary[7 - z]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment