Skip to content

Commit 7a5932d

Browse files
committed
adding docstrings to block CA evolve functions
1 parent 8264781 commit 7a5932d

File tree

7 files changed

+69
-12
lines changed

7 files changed

+69
-12
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Added `evolve_block` function
13+
- Added `evolve2d_block` function
14+
- Added block CA demos
1015

1116
## [2.3.1] - 2021-12-25
1217

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,35 @@ For more information about Conway's Game of Life, see:
265265

266266
> Conway, J. (1970). The game of life. Scientific American, 223(4), 4.
267267
268+
### Block Cellular Automata
269+
270+
Instead of a rule applying to a single cell at a time (given its neighbourhood), a rule can apply to a block of cells,
271+
given only the states of the cells in the block. Such a system is called a block cellular automaton. For example, the
272+
following code reproduces the block CA at the bottom of page 460 of [Wolfram's NKS](https://www.wolframscience.com/nks/):
273+
274+
```python
275+
import cellpylib as cpl
276+
import numpy as np
277+
278+
initial_conditions = np.array([[0]*13 + [1]*2 + [0]*201])
279+
280+
def block_rule(n, t):
281+
if n == (1, 1): return 1, 1
282+
elif n == (1, 0): return 1, 0
283+
elif n == (0, 1): return 0, 0
284+
elif n == (0, 0): return 0, 1
285+
286+
ca = cpl.evolve_block(initial_conditions, block_size=2,
287+
timesteps=200, apply_rule=block_rule)
288+
cpl.plot(ca)
289+
```
290+
291+
<img src="https://raw.githubusercontent.com/lantunes/cellpylib/master/resources/block_ca_1d.png" width="50%"/>
292+
293+
Block cellular automata can also exist in 2 dimensions, as the following example demonstrates:
294+
295+
<img src="https://raw.githubusercontent.com/lantunes/cellpylib/master/resources/block_ca_2d.gif" width="65%"/>
296+
268297
### Increasing Execution Speed with Memoization
269298

270299
Memoization is expected to provide an increase to execution speed when there is some overhead involved when invoking

cellpylib/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
For complete documentation, see: https://cellpylib.org
88
"""
99

10-
__version__ = "2.3.1"
10+
__version__ = "2.4.0"
1111

1212
from .ca_functions import BaseRule, AsynchronousRule, ReversibleRule, binary_rule, init_simple, nks_rule, \
1313
totalistic_rule, plot_multiple, bits_to_int, int_to_bits, init_random, plot, evolve, until_fixed_point, NKSRule, \

cellpylib/ca_functions.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,24 @@ def plot_multiple(ca_list, titles, *, colormap='Greys', xlabel='', ylabel='time'
6060

6161
def evolve_block(cellular_automaton, block_size, timesteps, apply_rule):
6262
"""
63-
TODO
63+
Evolves the given block cellular automaton for the specified time steps. Applies the given function to each block
64+
during the evolution. A cellular automaton is represented here as an array of arrays, or matrix. This function
65+
expects an array containing the initial time step (i.e. initial condition, an array) for the cellular automaton.
66+
The final result is a matrix, where the number of rows equal the number of time steps specified.
6467
65-
:param cellular_automaton:
66-
:param block_size:
67-
:param timesteps:
68-
:param apply_rule:
68+
:param cellular_automaton: the cellular automaton starting condition representing the first time step,
69+
e.g. [[0,0,0,0,1,0,0,0,0]]
70+
71+
:param block_size: the number of cells in the block; the total number of cells in the CA must be divisible by the
72+
block size
73+
74+
:param timesteps: the number of time steps in this evolution; this value refers to the total number of time steps
75+
in this cellular automaton evolution, which includes the initial condition
76+
77+
:param apply_rule: a function representing the rule to be applied to each block during the evolution; this function
78+
will be given two arguments, in the following order: a tuple containing the activities of the
79+
cells in the block, and a scalar representing the timestep in the evolution; this function must
80+
return a tuple with the new activities for the block
6981
7082
:return: a matrix, containing the results of the evolution, where the number of rows equal the number of time steps
7183
specified

cellpylib/ca_functions2d.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,27 @@ def _add_grid_lines(ca, ax, show_grid):
260260

261261
def evolve2d_block(cellular_automaton, block_size, timesteps, apply_rule):
262262
"""
263-
TODO
264-
:param cellular_automaton:
263+
Evolves the given block cellular automaton for the specified time steps. Applies the given function to each block
264+
during the evolution. A cellular automaton is represented here as an array of arrays, or matrix. This function
265+
expects an array containing the initial time step (i.e. initial condition, an array) for the cellular automaton.
266+
The final result is a matrix, where the number of rows equal the number of time steps specified.
265267
266-
:param block_size:
268+
:param cellular_automaton: the cellular automaton starting condition representing the first time step
267269
268-
:param timesteps:
270+
:param block_size: a 2-tuple representing the number of rows and columns in the block; the total number of cells in
271+
the CA must be divisible by the block size
269272
270-
:param apply_rule:
273+
:param timesteps: the number of time steps in this evolution; this value refers to the total number of time steps
274+
in this cellular automaton evolution, which includes the initial condition
271275
272-
:return:
276+
:param apply_rule: a function representing the rule to be applied to each block during the evolution; this function
277+
will be given two arguments, in the following order: the block, which is a numpy 2D array with
278+
the number of rows and columns specified in the `block_size`, and the time step, which is a
279+
scalar representing the time step in the evolution; this function must return a 2D array
280+
containing the new values of the block
281+
282+
:return: a list of matrices, containing the results of the evolution, where the number of rows equal the number
283+
of time steps specified
273284
"""
274285
initial_conditions = cellular_automaton[-1]
275286
_, rows, cols = cellular_automaton.shape

resources/block_ca_1d.png

1.08 MB
Loading

resources/block_ca_2d.gif

1.1 MB
Loading

0 commit comments

Comments
 (0)