Created
May 2, 2021 17:17
-
-
Save IlievskiV/553a7c5bec8ae42c95805061ff465b41 to your computer and use it in GitHub Desktop.
This file contains 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
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