# -*- coding: utf-8 -*- """ Created on Apr 3 20:02:40 2021 @author: neil, Cannonical 1D cellular automata This program will perform a 1D cellular automata on a vector of 0s and 1s The automata only uses the values of the cell, plus the 2 adjacent cells to determine the next value of the cell. We save each new vector for viewing. Important: since we are using only the values of 3 cells, and there are only 0s and 1s, there are only 8 possible values for these cells, and since the result can only be only 0 or 1, then there are 2**8, or 256 possible 'rules' for the automata(!) and the rules are named by their binary representation, for example, below illustrates rule '30' """ import matplotlib.pyplot as plt import numpy as np size = 1000 # length of 1D vector # rule 30 # 111 110 101 100 011 010 001 000 , eight possible values of 3 cells # 0 0 0 1 1 1 1 0 , note this 00010110 or 22 in binary # initiallize a size//2xsize space to store the intial vector and the # subsequent vectors created from the first S = np.zeros([size//2,size],dtype='int') S[0,size//2]=1 # put a single '1' in the middle of the vector to start for i in range(size//2-1): #step forward in 'time', make a new row based on old for j in range(1,size-1): if S[i,j-1] + S[i,j] + S[i,j+1] == 1: S[i+1,j]=1 #put a '1' if and only if previous row was 100, 010 or 001 if S[i,j-1] + S[i,j] == 2 and S[i,j+1] == 0: S[i+1,j]=1 #put a '1' if and only if previous row was 011 plt.spy(S) # show as a spy plot which plots locations of the '1's