Stencil: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
m (Remove closing pre HTML tags that were within the source tags and displaying, which looks unintended.)
Line 3: Line 3:
== Description ==
== Description ==


For a call <source lang=apl inline>f⌺s</source> the right operand <source lang=apl inline>s</source> 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 <source lang=apl inline>f</source> and the second row describes the movement along the different [[axis|axes]]. When using stencil <source lang=apl inline>(f⌺s)Y</source> the right operand <source lang=apl inline>s</source> in general has <source lang=apl inline>≢⍴Y</source> columns. If it has fewer, the rectangles are cut out of the [[major cell]]s of the <source lang=apl inline>(≢⍴Y) - ≢1⌷s</source> axis of <source lang=apl inline>Y</source>. If <source lang=apl inline>s</source> is a vector or scalar it describes only the size of the rectangles and the movement defaults to 1. Rectangles are centred on [[element]]s of <source lang=apl inline>Y</source> whose [[index|indices]] differ by the movements in <source lang=apl inline>s</source> (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 [[cell]]s. Along the edges of the [[argument]] array, the windows are thus subject to be padded with [[fill element]]s. The number of fill elements in along each axis is given as a vector left argument on each call of <source lang=apl inline>f</source> for the respective [[subarray]].
For a call <source lang=apl inline>f⌺s</source> the right operand <source lang=apl inline>s</source> 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 <source lang=apl inline>f</source> and the second row describes the movement along the different [[axis|axes]]. When using stencil <source lang=apl inline>(f⌺s)Y</source> the right operand <source lang=apl inline>s</source> in general has <source lang=apl inline>≢⍴Y</source> columns. If it has fewer, the rectangles are cut out of the [[major cell]]s of the <source lang=apl inline>(≢⍴Y) - ≢1⌷s</source> axis of <source lang=apl inline>Y</source>. If <source lang=apl inline>s</source> is a vector or scalar it describes only the size of the rectangles and the movement defaults to 1. Rectangles are centred on [[element]]s of <source lang=apl inline>Y</source> whose [[index|indices]] differ by the movements in <source lang=apl inline>s</source> (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 [[cell]]s. Along the edges of the [[argument]] array, the windows are thus subject to be padded with [[fill element]]s.
 
The number of fill elements in along each axis is given as a vector left argument on each call of <source lang=apl inline>f</source> for the respective [[subarray]]. This is designed such that [[Drop]] (<source lang=apl inline>↓</source>) can take the left and right arguments to remove padding:
<source lang-apl>
    ({⊂⍵}⌺3 3)3 4⍴⍳12
┌──────┬───────┬────────┬───────┐
│0 0 0 │0 0 0  │0 0 0  │0 0 0  │
│0 1 2 │1 2 3  │2 3 4  │3 4 0  │
│0 5 6 │5 6 7  │6 7 8  │7 8 0  │
├──────┼───────┼────────┼───────┤
│0 1  2│1  2  3│ 2  3  4│ 3  4 0│
│0 5  6│5  6  7│ 6  7  8│ 7  8 0│
│0 9 10│9 10 11│10 11 12│11 12 0│
├──────┼───────┼────────┼───────┤
│0 5  6│5  6  7│ 6  7  8│ 7  8 0│
│0 9 10│9 10 11│10 11 12│11 12 0│
│0 0  0│0  0  0│ 0  0  0│ 0  0 0│
└──────┴───────┴────────┴───────┘
    ({⊂⍺}⌺3 3)3 4⍴⍳12
┌────┬────┬────┬─────┐
│1 1 │1 0 │1 0 │1 ¯1 │
├────┼────┼────┼─────┤
│0 1 │0 0 │0 0 │0 ¯1 │
├────┼────┼────┼─────┤
│¯1 1│¯1 0│¯1 0│¯1 ¯1│
└────┴────┴────┴─────┘
    ({⊂⍺↓⍵}⌺3 3)3 4⍴⍳12
┌────┬───────┬────────┬─────┐
│1 2 │1 2 3  │2 3 4  │3 4  │
│5 6 │5 6 7  │6 7 8  │7 8  │
├────┼───────┼────────┼─────┤
│1  2│1  2  3│ 2  3  4│ 3  4│
│5  6│5  6  7│ 6  7  8│ 7  8│
│9 10│9 10 11│10 11 12│11 12│
├────┼───────┼────────┼─────┤
│5  6│5  6  7│ 6  7  8│ 7  8│
│9 10│9 10 11│10 11 12│11 12│
└────┴───────┴────────┴─────┘
</source>


== Examples ==
== Examples ==

Revision as of 15:21, 28 June 2021

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 (;.3),[1] which in turn originates in the 3-cut mentioned in A Dictionary of APL[2]

Description

For a call f⌺s the right operand s 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 f and the second row describes the movement along the different axes. When using stencil (f⌺s)Y the right operand s in general has ≢⍴Y columns. If it has fewer, the rectangles are cut out of the major cells of the (≢⍴Y) - ≢1⌷s axis of Y. If s 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 Y whose indices differ by the movements in s (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 f for the respective subarray. This is designed such that Drop () can take the left and right arguments to remove padding:

     ({⊂⍵}⌺3 3)3 4⍴⍳12
┌──────┬───────┬────────┬───────┐
│0 0 0 │0 0 0  │0 0 0   │0 0 0  │
│0 1 2 │1 2 3  │2 3 4   │3 4 0  │
│0 5 6 │5 6 7  │6 7 8   │7 8 0  │
├──────┼───────┼────────┼───────┤
│0 1  2│1  2  3│ 2  3  4│ 3  4 0│
│0 5  6│5  6  7│ 6  7  8│ 7  8 0│
│0 9 10│9 10 11│10 11 12│11 12 0│
├──────┼───────┼────────┼───────┤
│0 5  6│5  6  7│ 6  7  8│ 7  8 0│
│0 9 10│9 10 11│10 11 12│11 12 0│
│0 0  0│0  0  0│ 0  0  0│ 0  0 0│
└──────┴───────┴────────┴───────┘
     ({⊂⍺}⌺3 3)3 4⍴⍳12
┌────┬────┬────┬─────┐
│1 1 │1 0 │1 0 │1 ¯1 │
├────┼────┼────┼─────┤
│0 1 │0 0 │0 0 │0 ¯1 │
├────┼────┼────┼─────┤
│¯1 1│¯1 0│¯1 0│¯1 ¯1│
└────┴────┴────┴─────┘
     ({⊂⍺↓⍵}⌺3 3)3 4⍴⍳12
┌────┬───────┬────────┬─────┐
│1 2 │1 2 3  │2 3 4   │3 4  │
│5 6 │5 6 7  │6 7 8   │7 8  │
├────┼───────┼────────┼─────┤
│1  2│1  2  3│ 2  3  4│ 3  4│
│5  6│5  6  7│ 6  7  8│ 7  8│
│9 10│9 10 11│10 11 12│11 12│
├────┼───────┼────────┼─────┤
│5  6│5  6  7│ 6  7  8│ 7  8│
│9 10│9 10 11│10 11 12│11 12│
└────┴───────┴────────┴─────┘

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:

      {⊂⍵}⌺3 3⊢3 3⍴⍳9
┌─────┬─────┬─────┐
│0 0 0│0 0 0│0 0 0│
│0 1 2│1 2 3│2 3 0│
│0 4 5│4 5 6│5 6 0│
├─────┼─────┼─────┤
│0 1 2│1 2 3│2 3 0│
│0 4 5│4 5 6│5 6 0│
│0 7 8│7 8 9│8 9 0│
├─────┼─────┼─────┤
│0 4 5│4 5 6│5 6 0│
│0 7 8│7 8 9│8 9 0│
│0 0 0│0 0 0│0 0 0│
└─────┴─────┴─────┘

In the following example, each neighbourhood is immediately ravelled and summed:

      {+/,⍵}⌺3 3⊢3 3⍴⍳9
12 21 16 27 45 33 24 39 28

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:

      {⊂⍵}⌺(⍪2 2)⊢10 6⍴⍳100
┌──────────────┬─────────────────┬─────────────────┬─────────────────┬─────────────────┐
│1 2 3  4  5  6│13 14 15 16 17 18│25 26 27 28 29 30│37 38 39 40 41 42│49 50 51 52 53 54│
│7 8 9 10 11 12│19 20 21 22 23 24│31 32 33 34 35 36│43 44 45 46 47 48│55 56 57 58 59 60│
└──────────────┴─────────────────┴─────────────────┴─────────────────┴─────────────────┘

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:

      ⎕IO←0
      life ← {3=s-⍵∧4=s←{+/,⍵}⌺3 3⊢⍵}

      {'.⍠'[⍵]}¨ (⍳8) {life⍣⍺⊢⍵}¨ ⊂glider
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐
│..⍠..│.⍠...│..⍠..│.....│.....│.....│.....│.....│
│⍠.⍠..│..⍠⍠.│...⍠.│.⍠.⍠.│...⍠.│..⍠..│...⍠.│.....│
│.⍠⍠..│.⍠⍠..│.⍠⍠⍠.│..⍠⍠.│.⍠.⍠.│...⍠⍠│....⍠│..⍠.⍠│
│.....│.....│.....│..⍠..│..⍠⍠.│..⍠⍠.│..⍠⍠⍠│...⍠⍠│
│.....│.....│.....│.....│.....│.....│.....│...⍠.│
└─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘

See also

External links

Documentation

Publications

References

APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare RootRound
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentityStopSelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndexCartesian ProductSort
Selector Index generatorGradeIndex OfInterval IndexIndicesDealPrefix and suffix vectors
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axisIdentity (Null, Ident)
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)Identity (Lev, Dex)
Quad names Index originComparison toleranceMigration levelAtomic vector