Stencil

is a primitive dyadic operator that applies its left operand to (possibly overlapping) rectangular views of the right argument array. The shape and movement of the rectangular views are dictated by the right operand. It was introduced to Dyalog APL in version 16.0 and is also known as tessellation, moving window, stencil code or cut. The operator can be used for computations involving the immediate neighbours of items in an array and has applications in image processing (particularly convolution), artificial neural networks and, most famously, cellular automata. The operator derives from a subset (specifically, case 3) of the functionality of J's Cut operator, which in turn originates in the 3-cut defined in Rationalized APL.

Description
For a call  the right operand   can in general be a two-row matrix of strictly positive integers, where the first row describes the dimensions of the rectangles that will be passed to   and the second row describes the movement along the different axes. When using stencil  the right operand   in general has   columns. If it has fewer, the rectangles are cut out of the major cells of the  axis of. If  is a vector or scalar it describes only the size of the rectangles and the movement defaults to 1. Rectangles are centred on elements of  whose indices differ by the movements in   (defaulting to 1), starting with the first element in ravel order. For a matrix, this is the top left. For even window sizes, the centring is instead on the space between elements or cells. Along the edges of the argument array, the windows are thus subject to be padded with fill elements.

The number of fill elements in along each axis is given as a vector left argument on each call of  for the respective subarray. This is designed such that Drop can take the left and right arguments to remove padding:

Examples
With a default movement in every direction of 1, the rectangles are centred on neighbouring elements of the array. Here, we simply enclose each neighbourhood: In the following example, each neighbourhood is immediately ravelled and summed: If the number of columns in the right operand to stencil does not match the rank of the right argument, the windowing is applied on the major cells: Stencil allows for a very succinct expression of a dfn that calculates the next iteration in Conway's Game of Life (on a rectangle bound by zeros). Inspired by an algorithm from Arthur Whitney written in K and adapted to APL by Jay Foad:

Documentation

 * Dyalog

Publications

 * Dyalog '16 presentation by Roger Hui: New Primitive Functions and Operators
 * Dyalog blog posts by Roger Hui: Stencil Lives, Towards Improvements to Stencil