Reduce

From APL Wiki
Revision as of 05:57, 21 July 2020 by Bubbler (talk | contribs)
Jump to navigation Jump to search

Reduce (/, ), also called Reduction or Insert, is a primitive monadic operator which takes a dyadic function operand, inserts it between the elements of the argument, and evaluates it into a single array in right-to-left order. This operation is known as Fold, or more specifically foldr1, in other functional programming languages such as Haskell.

Description

When applied to a vector argument, f/x evaluates to the expression a f b f c f d … where a, b, c, d, … are the elements of x. In general, Reduce reduces one chosen axis (either implied by using the last-axis form f/ or first-axis f⌿, or explicitly by using function axis f/[x]) by evaluating each vector along the chosen axis into a scalar.

In nested array model, Reduce has a strong property that the reduced axis is removed from the shape of the argument, which forces it to enclose each non-simple result in the returned array.

In leading axis model, Reduce only has the first-axis form, and it reduces the major cells of the entire array, not the individual elements. It does not enclose the result either. Instead, reduction over an axis other than the first is performed via the Rank operator, which mixes the results into a flat array.

Examples

Reduce is mainly used for aggregation, such as sum (using Add) or product (using Times). If used with Subtract, it computes the alternating sum, since  . Using with Divide gives similar effect, returning the alternating product.

      +/1 2 3 4 5
15
      ×/1 2 3 4 5
120
      -/1 2 3 4 5
3
      ÷/1 2 3 4 5
1.875

Reduction by Minimum or Maximum gives the minimum or maximum over several numbers. Same goes for And, Or, GCD, LCM, and XOR (Not Equal on Booleans).

Reduction over an empty axis gives the identity element of the operand.

      +/⍬
0
      +/2 3 0⍴0
0 0 0
0 0 0

FinnAPL idiom library contains over 100 entries which use Reduce in some way.

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