Reduce: Difference between revisions

Jump to navigation Jump to search
1,091 bytes added ,  01:08, 28 July 2020
No edit summary
Line 11: Line 11:
== Examples ==
== 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.
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 <math>a\div(b\div(c\div(d\div\cdots))) = a\div b\times c\div d\times\cdots</math>.


<source lang=apl>
<source lang=apl>
Line 25: Line 25:


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 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]]).
Although Reduce is <source lang=text inline>foldr1</source> in nature, one can use it like <source lang=text inline>foldr</source>, where a designated starting value is modified by the rest of the values in sequence. In this case, the start value (enclosed if not a [[simple scalar]]) is attached to the right end of the [[vector]] of "modifiers", and then the entire vector is reduced.
<source lang=apl>
      (⍉∘⌽↓)/2 1 2 1,⊂5 6⍴⍳30  ⍝ Trim a matrix from all four sides, by rotating the matrix after each trim
┌─────┐
│ 9 10│
│15 16│
│21 22│
└─────┘
      ○/1 ¯2,⊂0 0.25 0.5 0.75 1  ⍝ sin∘arccos of multiple values
┌──────────────────────────────────────────┐
│1 0.9682458366 0.8660254038 0.6614378278 0│
└──────────────────────────────────────────┘
</source>


Reduction over an empty axis gives the [[identity element]] of the operand.
Reduction over an empty axis gives the [[identity element]] of the operand.
trusted
183

edits

Navigation menu