Hi,<div>I've just come across Travis Oliphant's array version of the game of life here <<a href="https://gist.github.com/3353411">https://gist.github.com/3353411</a>>. It's a really nice example of how to efficiently find neighbours using numpy and family. However, I wanted to apply these ideas to higher order arrays (eg GRID being three dimensional rather than two dimensional). My previous attempts at this include the (sorry for the ensuing horror!)</div>
<div><div>      neighbours = np.array( [ \</div><div>            # Lower row...</div><div>            x[:-2, :-2, :-2], x[:-2, :-2, 1:-1], x[:-2, :-2, 2: ], </div><div>            x[:-2, 1:-1,:-2], x[:-2, 1:-1, 1:-1], x[:-2, 1:-1, 2:], </div>
<div>            x[:-2, 2:,:-2], x[:-2, 2:, 1:-1], x[:-2, 2:, 2:], </div><div>            # Middle row</div><div>            x[1:-1, :-2, :-2], x[1:-1, :-2, 1:-1], x[1:-1, :-2, 2: ], </div><div>            x[1:-1, 1:-1,:-2], x[1:-1, 1:-1, 2:], </div>
<div>            x[1:-1, 2:,:-2], x[1:-1, 2:, 1:-1], x[1:-1, 2:, 2:], </div><div>            # Top row</div><div>            x[2:, :-2, :-2], x[2:, :-2, 1:-1], x[2:, :-2, 2: ], </div><div>            x[2:, 1:-1,:-2], x[2:, 1:-1, 1:-1], x[2:, 1:-1, 2:], </div>
<div>            x[2:, 2:,:-2], x[2:, 2:, 1:-1], x[2:, 2:, 2:] ] )</div></div><div><br></div><div>I think this works (it's been a while! ;D), but it's a bit messy and the boundary condition is a bit crude. I was wondering whether there's some other nice way of doing it, like demonstrated in the above link. Also, what if you want to have a larger neighbourhood (say 5x5x2 instead of 3x3x3)?</div>
<div><br></div><div>While I appreciate these index tricks, I also find them quite mind boggling!</div><div>Thanks!</div><div>Jose</div>