# Working with stencils using `ndarray`

I'm learning Rust by doing. As my first programming project I decided to implement the classic Conway's Game of Life - Wikipedia. To store the cells I use two dimensional arrays from an `ndarray` module. Can you please tell me, how do I efficiently extract a stencil from an `ndarray`? At the moment I'm doing the following:

``````// population array
let mut cells = Array2::<u8>::zeros((M,N));
...
// for each cell with coordinates i, j extract stencil
let vicinity = cells.slice(s![i-1..=i+1, j-1..=j+1]);
``````

This works, but I'm not sure, how efficient this is. Also I would like to exclude "myself" (the current cell with coordinates `i, j`) from the stencil. Would that be possible? How would I do that?

Finally, I would like to implement periodic boundary conditions, i.e. if the cell is on the top boundary of the grid (`i=IMIN`) its stencil will have the coordinates `cells.slice( s![IMIN, j-1..=j+1] )` and `cells.slice( s![i..=i+1, j-1..=j+1] )`. In other words, the stencil will be composed of two slices. Is it an efficient approach to extract two slices and join them to perform further processing? How do I join these slices? I couldn't find this information in the `ndarray` documentation.

Ideally, it would be great, if slices implemented "wrapping around" and the syntax `i-1..=i+1, j-1..=j+1` would automatically pick up `imax` value when `i-1` would go beyond the minimum of `imin`. Can you please point me in the right direction, if such "wrapping around" already exists in the `ndarray` module?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.