Stencil Operator

General APL language issues

Stencil Operator

Two questions regarding the Stencil operator

1)

m←3 4⍴⍳12

({⊂⍵}⌺2 2) m
is
┌────┬─────┬─────┐
│1 2 │2 3 │3 4 │
│5 6 │6 7 │7 8 │
├────┼─────┼─────┤
│5 6│ 6 7│ 7 8│
│9 10│10 11│11 12│
└────┴─────┴─────┘

Why isn't (⊂m)⌺ 2 2 the same if the right argument ⍵ is just m?
(⊂m) ⌺ 2 2
is
1 2 3 4 ⌺ 2 2
5 6 7 8
9 10 11 12

2)

({⊂⍺}⌺2 2) m
is
┌───┬───┬───┐
│0 0│0 0│0 0│
├───┼───┼───┤
│0 0│0 0│0 0│
└───┴───┴───┘

What is ⍺ in this context? What is the meaning of the result of this operation?
+←--------------------------------------------------------------→
+ Jay Moskowitz
+←--------------------------------------------------------------→
+

jmosk

Posts: 69
Joined: Thu Jul 18, 2013 5:15 am

Re: Stencil Operator

Firstly, I suggest you have a look at APL Wiki on operators.

The general syntax for Stencil is
Code: Select all
(WhatToDoOnEachWindow ⌺ windowSpecifications) data

WhatToDoOnEachWindow has to be a function. It is called by ⌺ over and over again, on each window of the data array, according to the windowSpecifications.

In your case, windowSpecifications is 2 2 which means that every contiguous 2-by-2 sub-matrix of data becomes a right argument of WhatToDoOnEachWindow.

The left argument of WhatToDoOnEachWindow is the amount of padding that ⌺ had to add to that sub-array. For windows sizes of 2 or smaller, no padding is ever necessary, so you get 0 0 which means no rows and no columns added.

However, if we increase the window size to 3 3, you should be able to spot the pattern:
Code: Select all
({⊂⍺⍵}⌺3 3)3 4⍴⍳12
┌─────────────┬──────────────┬───────────────┬───────────────┐
│┌───┬─────┐  │┌───┬─────┐   │┌───┬─────┐    │┌────┬─────┐   │
││1 1│0 0 0│  ││1 0│0 0 0│   ││1 0│0 0 0│    ││1 ¯1│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│0 1  2│ ││0 0│1  2  3│ ││0 0│ 2  3  4│ ││0 ¯1│ 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│ │
│└───┴──────┘ │└───┴───────┘ │└───┴────────┘ │└────┴───────┘ │
├─────────────┼──────────────┼───────────────┼───────────────┤
│┌────┬──────┐│┌────┬───────┐│┌────┬────────┐│┌─────┬───────┐│
││¯1 1│0 5  6│││¯1 0│5  6  7│││¯1 0│ 6  7  8│││¯1 ¯1│ 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││
│└────┴──────┘│└────┴───────┘│└────┴────────┘│└─────┴───────┘│
└─────────────┴──────────────┴───────────────┴───────────────┘