def day_and_night(state, k=None): """ 'Day & night' automata state transition http://www.conwaylife.com/wiki/Day_%26_Night """ # set up kernel if not given if k == None: m, n = state.shape k = np.zeros((m, n)) k[m / 2 - 1:m / 2 + 2, n / 2 - 1:n / 2 + 2] = np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]]) # computes sums around each pixel b = fft_convolve2d(state, k).round() c = np.zeros(b.shape) c[np.where((b == 3) & (state == 1))] = 1 c[np.where((b == 6) & (state == 1))] = 1 c[np.where((b == 7) & (state == 1))] = 1 c[np.where((b == 8) & (state == 1))] = 1 c[np.where((b == 3) & (state == 0))] = 1 c[np.where((b == 4) & (state == 0))] = 1 c[np.where((b == 6) & (state == 0))] = 1 c[np.where((b == 7) & (state == 0))] = 1 c[np.where((b == 8) & (state == 0))] = 1 # return new state return c
def two_by_two(state, k=None): """ '2x2' automata state transition http://www.conwaylife.com/wiki/2x2 """ if k == None: m, n = state.shape k = np.zeros((m, n)) k[m / 2 - 1:m / 2 + 2, n / 2 - 1:n / 2 + 2] = np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]]) # computes sums around each pixel b = fft_convolve2d(state, k).round() c = np.zeros(b.shape) # checks the values, and sets alive vs. dead state c[np.where((b == 1) & (state == 1))] = 1 c[np.where((b == 2) & (state == 1))] = 1 c[np.where((b == 5) & (state == 1))] = 1 c[np.where((b == 3) & (state == 0))] = 1 c[np.where((b == 6) & (state == 0))] = 1 # return new state return c
def automata(state, rule = 'B3/S23', k=None): """ Apply a custom automata transition from a rulestring """ # set up kernel if not given if k == None: m, n = state.shape k = np.zeros((m, n)) k[m/2-1 : m/2+2, n/2-1 : n/2+2] = np.array([[1,1,1],[1,0,1],[1,1,1]]) # computes sums around each pixel b = fft_convolve2d(state,k).round() c = np.zeros(b.shape) # checks the values, and sets alive vs. dead state born = [int(i) for i in rule.split('/')[0].replace('B','')] survive = [int(i) for i in rule.split('/')[1].replace('S','')] for i in born: c[np.where((b == i) & (state == 0))] = 1 for i in survive: c[np.where((b == i) & (state == 1))] = 1 # return new state return c
def day_and_night(state, k=None): """ 'Day & night' automata state transition http://www.conwaylife.com/wiki/Day_%26_Night """ # set up kernel if not given if k == None: m, n = state.shape k = np.zeros((m, n)) k[m/2-1 : m/2+2, n/2-1 : n/2+2] = np.array([[1,1,1],[1,0,1],[1,1,1]]) # computes sums around each pixel b = fft_convolve2d(state,k).round() c = np.zeros(b.shape) c[np.where((b == 3) & (state == 1))] = 1 c[np.where((b == 6) & (state == 1))] = 1 c[np.where((b == 7) & (state == 1))] = 1 c[np.where((b == 8) & (state == 1))] = 1 c[np.where((b == 3) & (state == 0))] = 1 c[np.where((b == 4) & (state == 0))] = 1 c[np.where((b == 6) & (state == 0))] = 1 c[np.where((b == 7) & (state == 0))] = 1 c[np.where((b == 8) & (state == 0))] = 1 # return new state return c
def automata(state, rule='B3/S23', k=None): """ Apply a custom automata transition from a rulestring """ # set up kernel if not given if k == None: m, n = state.shape k = np.zeros((m, n)) k[m / 2 - 1:m / 2 + 2, n / 2 - 1:n / 2 + 2] = np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]]) # computes sums around each pixel b = fft_convolve2d(state, k).round() c = np.zeros(b.shape) # checks the values, and sets alive vs. dead state born = [int(i) for i in rule.split('/')[0].replace('B', '')] survive = [int(i) for i in rule.split('/')[1].replace('S', '')] for i in born: c[np.where((b == i) & (state == 0))] = 1 for i in survive: c[np.where((b == i) & (state == 1))] = 1 # return new state return c
def test_uniform(self): A = np.random.randn(10, 10) K = np.ones((10, 10)) a, b = fft_convolve2d(A, K).max(), A.sum() self.assertAlmostEqual(a, b)
def test_uniform(self): A = np.random.randn(10,10) K = np.ones((10,10)) a,b = fft_convolve2d(A,K).max(), A.sum() self.assertAlmostEqual(a, b)
def seeds(state, k): """ 'Seeds' cellular automaton state transition http://www.conwaylife.com/wiki/Seeds """ b = fft_convolve2d(state,k).round() c = np.zeros(b.shape) # checks the values, and sets alive vs. dead state c[np.where((b == 2) & (state == 0))] = 1 # return new state return c
def replicator(state, k=None): """ 'Replicator' cellular automaton state transition http://www.conwaylife.com/wiki/Replicator_(CA) """ if k == None: m, n = state.shape k = np.zeros((m, n)) k[m/2-1 : m/2+2, n/2-1 : n/2+2] = np.array([[1,1,1],[1,0,1],[1,1,1]]) b = fft_convolve2d(state,k).round() c = np.zeros(b.shape) # checks the values, and sets alive vs. dead state c[np.where((b + 1) % 2 == 0)] = 1 # return new state return c
def replicator(state, k=None): """ 'Replicator' cellular automaton state transition http://www.conwaylife.com/wiki/Replicator_(CA) """ if k == None: m, n = state.shape k = np.zeros((m, n)) k[m / 2 - 1:m / 2 + 2, n / 2 - 1:n / 2 + 2] = np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]]) b = fft_convolve2d(state, k).round() c = np.zeros(b.shape) # checks the values, and sets alive vs. dead state c[np.where((b + 1) % 2 == 0)] = 1 # return new state return c
def conway(state, k=None): """ Conway's game of life state transition """ # set up kernel if not given if k == None: m, n = state.shape k = np.zeros((m, n)) k[m/2-1 : m/2+2, n/2-1 : n/2+2] = np.array([[1,1,1],[1,0,1],[1,1,1]]) # computes sums around each pixel b = fft_convolve2d(state,k).round() c = np.zeros(b.shape) c[np.where((b == 2) & (state == 1))] = 1 c[np.where((b == 3) & (state == 1))] = 1 c[np.where((b == 3) & (state == 0))] = 1 # return new state return c
def high_life(state, k=None): """ 'HighLife' automata state transition http://www.conwaylife.com/wiki/HighLife """ if k == None: m, n = state.shape k = np.zeros((m, n)) k[m/2-1 : m/2+2, n/2-1 : n/2+2] = np.array([[1,1,1],[1,0,1],[1,1,1]]) # computes sums around each pixel b = fft_convolve2d(state,k).round() c = np.zeros(b.shape) c[np.where((b == 2) & (state == 1))] = 1 c[np.where((b == 3) & (state == 1))] = 1 c[np.where((b == 3) & (state == 0))] = 1 c[np.where((b == 6) & (state == 0))] = 1 # return new state return c
def conway(state, k=None): """ Conway's game of life state transition """ # set up kernel if not given if k == None: m, n = state.shape k = np.zeros((m, n)) k[m / 2 - 1:m / 2 + 2, n / 2 - 1:n / 2 + 2] = np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]]) # computes sums around each pixel b = fft_convolve2d(state, k).round() c = np.zeros(b.shape) c[np.where((b == 2) & (state == 1))] = 1 c[np.where((b == 3) & (state == 1))] = 1 c[np.where((b == 3) & (state == 0))] = 1 # return new state return c
def two_by_two(state, k=None): """ '2x2' automata state transition http://www.conwaylife.com/wiki/2x2 """ if k == None: m, n = state.shape k = np.zeros((m, n)) k[m/2-1 : m/2+2, n/2-1 : n/2+2] = np.array([[1,1,1],[1,0,1],[1,1,1]]) # computes sums around each pixel b = fft_convolve2d(state,k).round() c = np.zeros(b.shape) # checks the values, and sets alive vs. dead state c[np.where((b == 1) & (state == 1))] = 1 c[np.where((b == 2) & (state == 1))] = 1 c[np.where((b == 5) & (state == 1))] = 1 c[np.where((b == 3) & (state == 0))] = 1 c[np.where((b == 6) & (state == 0))] = 1 # return new state return c