Skip to content

Commit 3e67f5a

Browse files
committed
making demo block 2d rule more efficient
1 parent 599b4ac commit 3e67f5a

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

demos/block2d_basic_ca_demo.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
]])
2929

3030

31-
def block2d_rule(n, t):
32-
n = tuple(tuple(i) for i in n)
31+
def make_block2d_rule():
3332
base_rules = {
3433
((0, 0), (0, 0)): ((0, 0), (0, 0)),
3534
((0, 0), (0, 2)): ((2, 0), (0, 0)),
@@ -65,9 +64,13 @@ def block2d_rule(n, t):
6564
v = ((v[1][0], v[0][0]), (v[1][1], v[0][1]))
6665
if r not in rules:
6766
rules[r] = v
68-
return rules[n]
67+
def _apply_rule(n, t):
68+
n = tuple(tuple(i) for i in n)
69+
return rules[n]
70+
return _apply_rule
6971

7072

71-
ca = cpl.evolve2d_block(initial_conditions, block_size=(2, 2), timesteps=40, apply_rule=block2d_rule)
73+
ca = cpl.evolve2d_block(initial_conditions, block_size=(2, 2),
74+
timesteps=40, apply_rule=make_block2d_rule())
7275

7376
cpl.plot2d_animate(ca)

demos/block2d_rotated_ca_demo.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
initial_conditions = np.array([initial_conditions])
1111

1212

13-
def block2d_rule(n, t):
14-
n = tuple(tuple(i) for i in n)
13+
def make_block2d_rule():
1514
base_rules = {
1615
((0, 0), (0, 0)): ((0, 0), (0, 0)),
1716
((0, 0), (0, 2)): ((2, 0), (0, 0)),
@@ -49,9 +48,13 @@ def block2d_rule(n, t):
4948
v = ((v[1][0], v[0][0]), (v[1][1], v[0][1]))
5049
if r not in rules:
5150
rules[r] = v
52-
return rules[n]
51+
def _apply_rule(n, t):
52+
n = tuple(tuple(i) for i in n)
53+
return rules[n]
54+
return _apply_rule
5355

5456

55-
ca = cpl.evolve2d_block(initial_conditions, block_size=(2, 2), timesteps=251, apply_rule=block2d_rule)
57+
ca = cpl.evolve2d_block(initial_conditions, block_size=(2, 2),
58+
timesteps=251, apply_rule=make_block2d_rule())
5659

5760
cpl.plot2d_animate(ca)

doc/block_ca.rst

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ The code for this Block CA is given below:
9494
elif n == (2, 1): return 2, 1
9595
elif n == (1, 2): return 1, 2
9696
97-
ca = cpl.evolve_block(initial_conditions, block_size=2, timesteps=200, apply_rule=block_rule)
97+
ca = cpl.evolve_block(initial_conditions, block_size=2,
98+
timesteps=200, apply_rule=block_rule)
9899
99100
cpl.plot(ca)
100101
@@ -112,11 +113,12 @@ The code for this 2D Block CA is given below:
112113
import cellpylib as cpl
113114
import numpy as np
114115
116+
# visit https://github.com/lantunes/cellpylib/tree/master/demos
117+
# for the initial conditions file
115118
initial_conditions = np.loadtxt('block2d_rotated_initial_conditions.txt', dtype=int)
116119
initial_conditions = np.array([initial_conditions])
117120
118-
def block2d_rule(n, t):
119-
n = tuple(tuple(i) for i in n)
121+
def make_block2d_rule():
120122
base_rules = {
121123
((0, 0), (0, 0)): ((0, 0), (0, 0)),
122124
((0, 0), (0, 2)): ((2, 0), (0, 0)),
@@ -154,9 +156,13 @@ The code for this 2D Block CA is given below:
154156
v = ((v[1][0], v[0][0]), (v[1][1], v[0][1]))
155157
if r not in rules:
156158
rules[r] = v
157-
return rules[n]
159+
def _apply_rule(n, t):
160+
n = tuple(tuple(i) for i in n)
161+
return rules[n]
162+
return _apply_rule
158163
159-
ca = cpl.evolve2d_block(initial_conditions, block_size=(2, 2), timesteps=251, apply_rule=block2d_rule)
164+
ca = cpl.evolve2d_block(initial_conditions, block_size=(2, 2),
165+
timesteps=251, apply_rule=make_block2d_rule())
160166
161167
cpl.plot2d_animate(ca)
162168

0 commit comments

Comments
 (0)