A script to write cellular automata as arithmetic expressions
Posted: June 28th, 2024, 1:29 am
The following Python script accepts a rulestring of a Life-like cellular automaton, and prints an arithmetic expression giving the state of the cell after one tick. The expression can involve c (the current state of the cell; 0 = off, 1 = on) and n (the number of alive neighbours of the cell).confocaloid wrote: June 24th, 2024, 3:00 am The rules of Conway's Game of Life, written as a single rule that involves only four arithmetic operations:
"If the cell's current state is c and the number of alive neighbours is n, then the cell's state will be f(c,n) in the next generation."
Code: Select all
f(c,n) = (n-8) (n-7) (n-6) (n-5) (n-4) (n-1) n (cn-2n-3c+4) / 1440Code: Select all
>>> [ [ (n-8)*(n-7)*(n-6)*(n-5)*(n-4)*(n-1)*n*(c*n-2*n-3*c+4) // 1440 for n in range(9) ] for c in range(2) ] [[0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0, 0]]
Example I/O:llcapoly.py wrote:Code: Select all
import sympy import re C, N = sympy.symbols(('c', 'n')) def mk(n): numer = sympy.Integer(1) denom = 1 for k in range(9): if k != n: numer *= (N - k) denom *= (n - k) return numer / denom def parse_llca_rulestring(rs): m = re.search('^[Bb]([0-8]*)/?[Ss]([0-8]*)$', rs) if m is None: return None b, s = m.groups() b = tuple(sorted(set(int(c) for c in b))) s = tuple(sorted(set(int(c) for c in s))) return (b, s) def main(): running = True while running: print("enter a rulestring (example: B3/S23) or 'exit':") rs = input(">>> ") if rs.strip().lower() == "exit": running = False else: llca_def = parse_llca_rulestring(rs) if llca_def is None: print("invalid input.") else: birth_conditions, survi_conditions = llca_def F0 = sum([mk(n) for n in birth_conditions], sympy.Integer(0)) F1 = sum([mk(n) for n in survi_conditions], sympy.Integer(0)) F = (1 - C) * F0 + C * F1 #print(' f0(n) = {}'.format(F0.factor())) #print(' f1(n) = {}'.format(F1.factor())) print(' f(c, n) = {}'.format(F.expand().factor())) main()
python3 llcapoly.py wrote:Code: Select all
enter a rulestring (example: B3/S23) or 'exit': >>> B3/S23 f(c, n) = n*(n - 8)*(n - 7)*(n - 6)*(n - 5)*(n - 4)*(n - 1)*(c*n - 3*c - 2*n + 4)/1440 enter a rulestring (example: B3/S23) or 'exit': >>> B2/S f(c, n) = -n*(c - 1)*(n - 8)*(n - 7)*(n - 6)*(n - 5)*(n - 4)*(n - 3)*(n - 1)/1440 enter a rulestring (example: B3/S23) or 'exit': >>> B2/S0 f(c, n) = -(n - 8)*(n - 7)*(n - 6)*(n - 5)*(n - 4)*(n - 3)*(n - 1)*(27*c*n + 2*c - 28*n)/40320 enter a rulestring (example: B3/S23) or 'exit': >>> B34/S34 f(c, n) = n*(n - 8)*(n - 7)*(n - 6)*(n - 5)*(n - 2)*(n - 1)*(n + 1)/2880 enter a rulestring (example: B3/S23) or 'exit': >>> B/S f(c, n) = 0 enter a rulestring (example: B3/S23) or 'exit': >>> B/S012345678 f(c, n) = c enter a rulestring (example: B3/S23) or 'exit': >>> B012345678/S f(c, n) = 1 - c enter a rulestring (example: B3/S23) or 'exit': >>> exit