Windows

𝕩 5↕𝕩 0 1 2 3 4 5 6 7 0 1 2 3 4 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7

The Windows function returns all slices, or contiguous subarrays, with shape (well, shape prefix) 𝕨 from 𝕩. It might also be seen as sliding a moving window along 𝕩.

This function replaces APL's Windowed Reduction, J's more general Infix operator, and Dyalog APL's Stencil, which is adapted from one case of J's Cut operator. In BQN, it's strongly preferred to use functions, and not modifiers, for array manipulation. Functions are simpler with fewer moving parts, and more concrete, since the array results can always be viewed right away.

Basic case

We'll start with the one-axis case. Here 𝕨 is a number between 0 and 1+≠𝕩. The result is composed of slices of 𝕩 (contiguous sections of major cells) with length 𝕨, starting at each possible index in order.

↗️
    5↕"abcdefg"
β”Œβ”€       
β•΅"abcde  
  bcdef  
  cdefg" 
        β”˜

There are 1+(≠𝕩)-𝕨, or (≠𝕩)¬𝕨, of these sections, because the starting index must be at least 0 and at most (≠𝕩)-𝕨. Another way to find this result is to look at the number of cells in or before a given slice: there are always 𝕨 in the slice and there are only ≠𝕩 in total, so the number of slices is the range spanned by these two endpoints.

A single slice of an array 𝕩 with length l and starting index i is l↑i↓𝕩, using Take and Drop. The Prefixes function returns all the slices that end at the end of the array ((≠𝕩)=i+l), and Suffixes gives the slices that start at the beginning (i=0). Windows gives yet another collection of slices: the ones that have a fixed length l=𝕨. Selecting one cell from its result gives the slice starting at that cell's index:

↗️
    2⊏5↕"abcdefg"
"cdefg"

    5↑2↓"abcdefg"
"cdefg"

Windows differs from Prefixes and Suffixes in that it doesn't add a layer of nesting (it doesn't enclose each slice). This is possible because the slices have a fixed size, so they fit together as cells of an array.

Windowed reduction

Windows can be followed up with Insert on each slice to give a windowed reduction or fold. Here we take running sums of 3 values.

↗️
    +˝˘3↕ ⟨2,6,0,1,4,3⟩
⟨ 8 7 5 8 ⟩

A common task is to act on windows with an initial or final element so the total length stays the same. When using windows of length 2, the best way to accomplish this is with a shift Β« or Β». If the window length is longer or variable, then a trick with Windows works better: add the elements, and then use windows matching the original length. Here we invert Plus Scan +`, which requires we take pairwise differences starting at initial value 0.

↗️
    -⟜(0»⊒) +` 3β€Ώ2β€Ώ1β€Ώ1
⟨ 3 2 1 1 ⟩

    (-ΛœΛβ‰ β†•0∾⊒) +` 3β€Ώ2β€Ώ1β€Ώ1
⟨ 3 2 1 1 ⟩

With Windows, we can modify the 3-element running sum from before to keep the length constant by starting with two zeros.

↗️
    (+˝≠↕(2β₯Š0)⊸∾) ⟨2,6,0,1,4,3⟩
⟨ 2 8 8 7 5 8 ⟩

Symmetry

Let's look at the first example, paired with its Transpose (⍉).

↗️
    β‹ˆβŸœβ‰ 5↕"abcdefg"
β”Œβ”€                   
Β· β”Œβ”€        β”Œβ”€       
  β•΅"abcde   β•΅"abc    
    bcdef     bcd    
    cdefg"    cde    
          β”˜   def    
              efg"   
                  β”˜  
                    β”˜

Although the two arrays have different shapes, they're identical in the 3Γ—3 region where they overlap.

↗️
    ≑○(3β€Ώ3βŠΈβ†‘)βŸœβ‰ 5↕"abcdefg"
1

More concretely, the ith element of slice j is the same as the jth element of slice i: it's the i+jth element of the argument. So transposing still gives a possible result of Windows, but with a different slice length. The two lengths are related by Span, which converts between length and number of slices.

↗️
    {(5↕𝕩)≑⍉(3↕𝕩)}"abcdefg"
1

    (β‰ "abcdefg") Β¬ 3
5

Multiple dimensions

The right argument can have rank more than 1, and it's viewed as a list of major cells following leading axis principles. As an example, Windows can take two-row slices of a shape 3β€Ώ4 array.

↗️
        2↕["0123","abcd","ABCD"]
β”Œβ”€      
β•Ž"0123  
  abcd  
        
 Β·abcd  
  ABCD" 
       β”˜

    <βŽ‰2 2↕["0123","abcd","ABCD"]
β”Œβ”€                   
Β· β”Œβ”€       β”Œβ”€        
  β•΅"0123   β•΅"abcd    
    abcd"    ABCD"   
         β”˜        β”˜  
                    β”˜

In the second version we've enclosed each slice with <βŽ‰2 for viewingβ€”a slice has rank 2, the same as 𝕩. Passing a list as the left argument to Windows takes slices along any number of leading axes. Here are all the shape 2β€Ώ2 slices:

↗️
    <βŽ‰2 2β€Ώ2↕["0123","abcd","ABCD"]
β”Œβ”€                      
β•΅ β”Œβ”€     β”Œβ”€     β”Œβ”€      
  β•΅"01   β•΅"12   β•΅"23    
    ab"    bc"    cd"   
       β”˜      β”˜      β”˜  
  β”Œβ”€     β”Œβ”€     β”Œβ”€      
  β•΅"ab   β•΅"bc   β•΅"cd    
    AB"    BC"    CD"   
       β”˜      β”˜      β”˜  
                       β”˜

The slices are naturally arranged along multiple dimensions according to their starting index. Once again the equivalence i⊏l↕x ←→ l↑i↓x holds, provided i and l have the same length.

If 𝕨 has length 0, then 𝕩 is not sliced along any dimensions. The only slice that resultsβ€”the entire argumentβ€”is then arranged along an additional zero dimensions. In the end, the result is 𝕩, unchanged.

Here's a more formal definition: 𝕩 is an array. 𝕨 is a number, or numeric list or unit, with length l←≠𝕨 so that l≀=𝕩. The result z has shape 𝕨 ∾ Β¬βŸœπ•¨βŒΎ(lβŠΈβ†‘)≒𝕩, and element iβŠ‘z is jβŠ‘π•©, with j←+´¨(lβˆΎβ—‹β†•=𝕩)βŠ”i. That is, the index list i starts with two length-l sequences that are added together to produce the first l values in j. We might also say that each of the first l values in j is split into two values in i.