Windowed Reduce: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
No edit summary
(correct init descr)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Built-ins|Windowed Reduce|/|⌿}}, also called '''N-wise Reduce''', is a [[primitive operator|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.
{{Built-ins|Windowed Reduce|/|⌿}}, also called '''N-wise Reduce''', is a [[primitive operator|primitive]] [[monadic operator]] which takes a [[dyadic function]], and derives a [[dyadic function]] which [[reduce]]s each overlapping "window" in its right [[argument]] of the size given by its left argument.


== Description ==
== Description ==


When applied to a [[vector]] argument, <source lang=apl inline>n f/x</source> evaluates to the expression <source lang=apl inline>(a f b f c)(b f c f d)</source>… where <source lang=apl inline>a</source>, <source lang=apl inline>b</source>, <source lang=apl inline>c</source>, <source lang=apl inline>d</source>, … are the elements of <source lang=apl inline>x</source>, grouped into windows of size <source lang=apl inline>n</source>. It works like [[Reduce]], except applied on overlapping segments of an array, and borrows most of its functionality from it. When  
When applied to a [[vector]] argument, <syntaxhighlight lang=apl inline>n f/x</syntaxhighlight> evaluates to the expression <syntaxhighlight lang=apl inline>(a f b f c)(b f c f d)</syntaxhighlight>… where <syntaxhighlight lang=apl inline>a</syntaxhighlight>, <syntaxhighlight lang=apl inline>b</syntaxhighlight>, <syntaxhighlight lang=apl inline>c</syntaxhighlight>, <syntaxhighlight lang=apl inline>d</syntaxhighlight>, … are the elements of <syntaxhighlight lang=apl inline>x</syntaxhighlight>, grouped into windows of size <syntaxhighlight 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</source>is negative, each window is reversed before the reduction is done.
<syntaxhighlight lang=apl inline>n</syntaxhighlight> is negative, each window is reversed before the reduction is done.
 
The magnitude of <syntaxhighlight lang=apl inline>n</syntaxhighlight> must be no more than 1 greater than the size of <syntaxhighlight lang=apl inline>x</syntaxhighlight> along the relevant [[axis]].
 
== Examples ==
== Examples ==


Windowed reduce is used to apply functions on overlapping sections of array e.g. when you need the deltas of an array.
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>
<syntaxhighlight lang=apl>
       3+/5 1 4 1 8
       3+/5 1 4 1 8
10 6 13
10 6 13
Line 20: Line 23:
│35 56 67 79│56 67 79 91│
│35 56 67 79│56 67 79 91│
└───────────┴───────────┘
└───────────┴───────────┘
</source>
</syntaxhighlight>


== Notable uses ==
== 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</source> indicates the elements that differ from their neighbour on the left. For a Boolean vector <source lang=apl inline>b</source>, the expression <source lang=apl inline>2</0,b</source> indicates the first 1 in each contiguous run of 1s.
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, <syntaxhighlight lang=apl inline>1,2≠/v</syntaxhighlight> indicates the elements that differ from their neighbour on the left. For a Boolean vector <syntaxhighlight lang=apl inline>b</syntaxhighlight>, the expression <syntaxhighlight lang=apl inline>2</0,b</syntaxhighlight> indicates the first 1 in each contiguous run of 1s.


== See also ==
== 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</source> is equivalent to <source lang=apl inline>n f/ v</source> except in how they deal with the ends of the vector; Stencil includes "shards" and Windowed Reduce does not.
* [[Stencil]] which can be seen as a generalisation of Windowed Reduce in that for a vector argument, <syntaxhighlight lang=apl inline>({⊂f/⍵}⌺n)v</syntaxhighlight> is equivalent to <syntaxhighlight 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 ==
== External links ==

Latest revision as of 21:47, 18 May 2024

/

Windowed Reduce (/, ), also called N-wise Reduce, is a primitive monadic operator which takes a dyadic function, and derives a dyadic function which reduces each overlapping "window" in its right argument of the size given by its left argument.

Description

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

The magnitude of n must be no more than 1 greater than the size of x along the relevant axis.

Examples

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

      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│
└───────────┴───────────┘

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, 1,2≠/v indicates the elements that differ from their neighbour on the left. For a Boolean vector b, the expression 2</0,b 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, ({⊂f/⍵}⌺n)v is equivalent to n f/ v except in how they deal with the ends of the vector; Stencil includes "shards" and Windowed Reduce does not.

External links

Lessons

Documentation


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