Stride tricks for the Game of Life
This is similar to [:../SegmentAxis:Segment axis], but for 2D arrays with 2D windows.
The Game of Life is a cellular automaton devised by the British mathematician John Horton Conway in 1970, see [1].
It consists of a rectangular grid of cells which are either dead or alive, and a transition rule for updating the cells’ state. To update each cell in the grid, the state of the 8 neighbouring cells needs to be examined, i.e. it would be desirable to have an easy way of accessing the 8 neighbours of all the cells at once without making unnecessary copies. The code snippet below shows how to use the devious stride tricks for that purpose.
[1] Game of Life) at Wikipedia
import numpy as np
from numpy.lib import stride_tricks
x = np.arange(20).reshape([4, 5])
xx = stride_tricks.as_strided(x, shape=(2, 3, 3, 3), strides=x.strides + x.strides)
x
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])
xx
array([[[[ 0, 1, 2],
[ 5, 6, 7],
[10, 11, 12]],
[[ 1, 2, 3],
[ 6, 7, 8],
[11, 12, 13]],
[[ 2, 3, 4],
[ 7, 8, 9],
[12, 13, 14]]],
[[[ 5, 6, 7],
[10, 11, 12],
[15, 16, 17]],
[[ 6, 7, 8],
[11, 12, 13],
[16, 17, 18]],
[[ 7, 8, 9],
[12, 13, 14],
[17, 18, 19]]]])
xx[0,0]
array([[ 0, 1, 2],
[ 5, 6, 7],
[10, 11, 12]])
xx[1,2]
array([[ 7, 8, 9],
[12, 13, 14],
[17, 18, 19]])
x.strides
(40, 8)
xx.strides
(40, 8, 40, 8)