Skip to main content

↕ — Pairs / Windows

Function. Keys: Alt-K. Hanzi: 窗. Ranks: ∞ monadic, ∞ ∞ dyadic

↕Y gives the adjacent pairs of major cells, as 2↕Y does. -/₁ then subtracts within each pair.

↕1 4 9 16            ⍝ [1 4◇4 9◇9 16]
-/₁↕1 4 9 16         ⍝ ¯3 ¯5 ¯7

S↕Y: windows of sizes S on leading axes. Trailing axes stay inside each window.

3↕ι5ₓ                ⍝ [0 1 2◇1 2 3◇2 3 4]ₓ
2 2↕2 3ρι6ₓ          ⍝ 1 2 2 2ρ[0 1 3 4 1 2 4 5]ₓ
2↕3 2ρι6ₓ            ⍝ 2 2 2ρ[0 1 2 3 2 3 4 5]ₓ

Shape: position frame, window sizes, trailing axes.

4↕"ab"               ⍝ 0 4ρ' '
0↕"ab"               ⍝ 3 0ρ' '
⍬↕2 3ρι6ₓ            ⍝ 2 3ρι6ₓ

Oversized full windows give empty frames. Zero sizes give n+1 empty windows. ⍬↕Y is Y.

A two-row left argument gives sizes, then movements. A window starts every M positions.

[[3]◇[2]]↕ι8ₓ       ⍝ [0 1 2◇2 3 4◇4 5 6]ₓ

With a negative size, Windows pads that axis. Each window is centred on a position, as in Stencil, with the array’s prototype beyond the edges. Each axis is padded only if its size is negative.

¯3↕1 2 3 4           ⍝ [0 1 2◇1 2 3◇2 3 4◇3 4 0]
[[¯3]◇[2]]↕↦8       ⍝ [0 1 2◇2 3 4◇4 5 6◇6 7 8]
ρ¯3 2↕3 4ρι12ₓ        ⍝ [3 3 3 2]ₓ

An axis of length n has 0⌈≥⌊M÷⍨n-S full windows, or ⌈M÷⍨0⌈n-0=2|S padded windows. Movements default to 1.

An options record gives the sizes as size, the movements as step, an edge mode as edge, and a fill as fill. With an edge mode, every window is centred on a position, and a position past an end is read by that mode, one of •edge’s: "fill", "clamp", "wrap", "reflect" or "symmetric". "fill" pads as a negative size does. Without an edge mode, size and step follow the rules above. Padding uses the record’s fill in place of the array’s prototype, as with a fill that ⍠ sets.

["size":3 "edge":"wrap"]↕1 2 3 4           ⍝ [4 1 2◇1 2 3◇2 3 4◇3 4 1]
["size":3 "edge":"clamp"]↕1 2 3 4          ⍝ [1 1 2◇1 2 3◇2 3 4◇3 4 4]
["size":3 "step":2 "edge":"reflect"]↕ι6ₓ   ⍝ [1 0 1◇1 2 3◇3 4 5]ₓ
["size":¯3 "fill":¯1]↕1 2 3 4              ⍝ [¯1 1 2◇1 2 3◇2 3 4◇3 4 ¯1]

Folds of windows

On a large argument, the result of ↕ is pending. A fold along the windows’ axis then slides over the argument and never builds the windows. A fold of windows of up to 8 items runs directly. Wider windows use van Herk-Gil-Werman’s method, at a cost for each item that doesn’t grow with the width. This covers + × ⌈ ⌊ of floats, + ⌈ ⌊ of integers, and the Boolean folds. Windows of a matrix, as in +/+/3 3↕m, are folded in one pass along each axis. A fold of windows that don’t overlap, as in 2 by 2 pooling, ⌈/⌈/[[2 2]◇[2 2]]↕m, reads the argument in place and costs what the same fold of a reshape costs. Folds of padded windows slide over one copy of the argument with its edges added, whatever their edge mode. Under ∂, a maximum or minimum of windows slides too, and each window’s derivative goes to its first extreme item: {+/⌈/3↕ω}∂ 1 1 1 1 1 is 1 1 1 0 0. •explain reports each sliding fold among its routes.

v←¿100000ρ0
(•explain "+/50↕v").routes.route         ⍝ ["sliding fold"]

Products of windows

A matrix product of windows, as in a convolution written ([f c]ρ3 3↕img)+.×k, reads the windows a block of rows at a time and never builds them. Under ∂, the gradient with respect to k reads the windows again in blocks. The gradient with respect to img adds each window’s gradient back where the window was read, an accumulation that •explain reports.

w←[99998 3]ρ3↕¿100000ρ0
k←¿3 4ρ0
(•explain "w+.×k").forced.function   ⍝ 0ρ⊂""
(•storage w).pending                 ⍝ $t

Inverse

↕⁻Y rebuilds an array from its pairs Y. N↕⁻Y rebuilds it from its windows of size N.

↕⁻↕1 4 9       ⍝ 1 4 9
3↕⁻3↕"abcde"   ⍝ "abcde"

Errors

  • RANK: sizes not a unit, vector or two-row matrix, or too many axes
  • LENGTH: a matrix without two rows
  • DOMAIN: a size that isn’t whole, nonpositive movement, a padded size of 0 or of at least twice the length of an axis that isn’t empty, an options record with no size, an unknown option or edge mode, or a fill that doesn’t keep its value as Y’s type