Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
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
Ejemplo n.º 11
0
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
Ejemplo n.º 12
0
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
Ejemplo n.º 13
0
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
Ejemplo n.º 14
0
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
Ejemplo n.º 15
0
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
Ejemplo n.º 16
0
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