## / Reduction

When used with a function operand the / operator is known as Reduction (see the entry for Compression for the other functions derived from /). The context in which the / is used should make clear the operation being carried out. / can be applied to any dyadic function , including user defined functions. When used with a scalar or one-element vector integer left argument, the / operator is used to perform 'N-wise reduction'.

The left operand of / is inserted between all elements of the array right argument. In the absence of an axis specification, the operand is inserted between items along the last axis (see also the entry for [], the Axis operator).

                 +/ 2 4 6                (This is the same as 2+4+6)
           12
                 SALES„25 5.7 8 50 101 74 19
                 +/SALES
           282.7                         (The sum of the numbers in SALES)
                 —/82 66 93 13           (The same as 82 — 66 — 93 — 13.
           93                             The result of 93—13 is compared
                                          with 66; the result of this comparison
                                          is compared with 82; the result of the
                                          last comparison is the largest)
                 Ÿ/0 1 1 0 0             (The same as 0 Ÿ 1 Ÿ 1 Ÿ 0 Ÿ 0)
           1                             (Used to test if there are any 1s)
                 ^/0 1 1 0 0             (Are there any 1's?)
                 ,/ 'ABC' 'DEF' 'HIJ'
            ABCDEFHIJ
                 ½,/'ABC' 'DEF' 'HIJ'    (Result is a scalar)
            EMPTY
                 TABLE
           1 2 3
           4 5 6
                 ×/TABLE                 (Multiply is applied to the elements
           6 120                          of a matrix. Since no dimension is
                                          specified, it works on the last
                                          dimension, the columns. 6 is the
                                          result of multiplying the columns in
                                          row 1. 120 is the product of those
                                          in row 2)

/ applies by default to the last dimension, whilst the similar operator, š, applies by default to the first dimension.

                 ×/[1]TABLE              (The [1] specifies that the operation
           4 10 18                        is to apply across the 1st dimension,
                 ×šTABLE                  the rows. Each element in row 1 is
           4 10 18                        multiplied by the corresponding
                                          element in row 2.)

### N-Wise Reduction

The definition of N-wise Reduction is very similar to the definition of Reduction. The left argument, an integer scalar or length one vector, is used to specify the length of successive subsets of the right argument on which the Reduction operation is performed. If the left argument is negative, each subset is reversed before the reduction operation is carried out.

For a left argument of absolute value n and the selected axis of the right argument of length m, the number of subsets to which the reduction can be applied are:

                  1 + m - n

and thus the limiting case is where the sample size is 1 greater than the length of the selected axis, giving a empty result.

                 2+/¼10                  (Add up the numbers 2 at a time, starting
           3 5 7 9 11 13 15 17 19         at the beginning of the vector)
                 5+/¼10                  (5 at a time)
           15 20 25 30 35 40
                 10+/¼10                 (10 at a time - the same as ordinary
           55                             Reduction)
                 11+/¼10                 (Sample size 1 greater than right argument
                                         empty result)
                 DATA„3 4½¼12
                 DATA
           1  2  3  4
           5  6  7  8
           9 10 11 12
                 2+/[2]DATA              (Add up 2 at a time across the columns
           3  5  7                        the second dimension)
          11 13 15
          19 21 23
                 2+/[1]DATA              (Add up 2 at a time across the rows, the
           6  8 10 12                     the fist dimension)
          14 16 18 20
                 NUMS„10?10
                 NUMS
          2 8 5 6 3 1 7 10 4 9
                 2-/NUMS                 (Subtract sucessive pairs of elements)
          ¯6 3 ¯1 3 2 ¯6 ¯3 6 ¯5         (Reverse the elements before subtracting)
                 ¯2-/NUMS
          6 ¯3 1 ¯3 ¯2 6 3 ¯6 5
                 2,/'AB' 'CD' 'EF' 'HI'  (Join elements, 2 at a time)
          ABCD CDEF EFHI
                 3,/'AB' 'CD' 'EF' 'HI'
          ABCDEF CDEFHI

N-wise reduction is commonly used for moving averages. For example, if SALES is a vector of monthly sales figures, then

                 (12+/SALES)÷12

gives the annualised moving average sales figures by month.

Topic: APLX Help : Help on APL language : APL Primitives : / Reduce

[ Previous | Next | Contents | Index ]