Windowed Reduce

From APL Wiki
Revision as of 10:26, 11 September 2022 by Adám Brudzewsky (talk | contribs) (Text replacement - "</source>" to "</syntaxhighlight>")
Jump to navigation Jump to search

Windowed Reduce (/, ), also called N-wise Reduce, is a primitive dyadic operator which takes a dyadic function, and a number as its left argument, inserts it between the overlapping "windows" of the size of its left argument, and evaluates it into a single array in right-to-left order.


When applied to a vector argument, <source lang=apl inline>n f/x</syntaxhighlight> evaluates to the expression <source lang=apl inline>(a f b f c)(b f c f d)</syntaxhighlight>… where <source lang=apl inline>a</syntaxhighlight>, <source lang=apl inline>b</syntaxhighlight>, <source lang=apl inline>c</syntaxhighlight>, <source lang=apl inline>d</syntaxhighlight>, … are the elements of <source lang=apl inline>x</syntaxhighlight>, grouped into windows of size <source lang=apl inline>n</syntaxhighlight>. It works like Reduce, except applied on overlapping segments of an array, and borrows most of its functionality from it. When <source lang=apl inline>n</syntaxhighlight>is negative, each window is reversed before the reduction is done.


Windowed reduce is used to apply functions on overlapping sections of array e.g. when you need the deltas of an array.

<source lang=apl>

     3+/5 1 4 1 8

10 6 13

     2-/1 2 3 4 5

¯1 ¯1 ¯1 ¯1

     ¯2-/1 2 3 4 5

1 1 1 1

     4,/35 56 67 79 91

┌───────────┬───────────┐ │35 56 67 79│56 67 79 91│ └───────────┴───────────┘ </syntaxhighlight>

Notable uses

Windowed Reduce is especially common with a left argument of 2 or ¯2, as it is then a pair-wise application of the operand between neighbouring elements, and especially so with comparison functions. For example, <source lang=apl inline>1,2≠/v</syntaxhighlight> indicates the elements that differ from their neighbour on the left. For a Boolean vector <source lang=apl inline>b</syntaxhighlight>, the expression <source lang=apl inline>2</0,b</syntaxhighlight> indicates the first 1 in each contiguous run of 1s.

See also

  • Stencil which can be seen as a generalisation of Windowed Reduce in that for a vector argument, <source lang=apl inline>({⊂f/⍵}⌺n)v</syntaxhighlight> is equivalent to <source lang=apl inline>n f/ v</syntaxhighlight> except in how they deal with the ends of the vector; Stencil includes "shards" and Windowed Reduce does not.

External links



APL built-ins [edit]
Primitive functions
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentitySelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndex
Selector Index generatorGradeIndex OfInterval IndexIndicesDeal
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Primitive operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductPowerAtUnderRankDepthVariantStencilCut (J)
Quad names
Arrays Index originMigration levelAtomic vector
Functions Name classCase convertUnicode convert
Operators SearchReplace