Reduce: Difference between revisions
Jump to navigation
Jump to search
(Created page with "{{Built-ins|Reduce|/|⌿}}, also called '''Reduction''' or '''Insert''', is a primitive monadic operator which takes a dyadic function operand...") |
No edit summary |
||
Line 8: | Line 8: | ||
In [[leading axis model]], Reduce only has the first-axis form, and it reduces the [[major cell|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)|Rank operator]], which [[mix|mixes]] the results into a flat array. | In [[leading axis model]], Reduce only has the first-axis form, and it reduces the [[major cell|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)|Rank operator]], which [[mix|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 <math>a-(b-(c-(d-\cdots))) = a-b+c-d+\cdots</math>. Using with [[Divide]] gives similar effect, returning the alternating product. | |||
<source lang=apl> | |||
+/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 | |||
</source> | |||
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. | |||
<source lang=apl> | |||
+/⍬ | |||
0 | |||
+/2 3 0⍴0 | |||
0 0 0 | |||
0 0 0 | |||
</source> | |||
[[FinnAPL idiom library]] contains over 100 entries which use Reduce in some way. | |||
== External links == | == External links == |