Topic: APLX Help : Help on APL language : APL Fundamentals : Operators
[ Previous | Next | Contents | Index | APL Home ]

www.microapl.co.uk

Primitive Operators


An 'operator' modifies the behaviour of a primitive or user-defined function. It has an operand or operands that are primitive, derived or user-defined functions or data. The result of using an operator is known as a derived function which can then be applied monadically or dyadically to data or alternatively it may be, in turn, used as an argument to another operator. Operators can themselves be monadic or dyadic. Monadic operators will be placed to the right of their operands:

             +/                      (Monadic / operator)
             ⍴¨                      (Monadic ¨ operator)
             +.×                     (Dyadic . operator)

Operators form a powerful extension to the repertoire of the language. They can be used to specify the way in which a function or functions are to be applied to data - they allow a function to be applied repeatedly and cumulatively over all the elements of a vector, matrix or multidimensional array.

The primitive operators available are:

OperatorName
/Reduce or Compress
\Scan or Expand
.Inner Product
.Outer Product
¨Each
[ ]Axis

Reduce and scan

When used with functions as their operand, slash and backslash are known as reduce and scan. Reduce and scan apply a single function to all the elements of an argument. For example, to add up a vector of arguments, you can either type:

      22 + 93 + 4.6 + 10 + 3.3
132.9

or alternatively:

      +/22 93 4.6 10 3.3
132.9

The / operator in the last example had the effect of inserting a plus sign between all the elements in the vector to its right.

The \ operator is similar except that it works cumulatively on the data, and gives all the intermediate results. So:

      +\22 93 4.6 10 3.3
 22 115 119.6 129.6 132.9

from the results of:

      22 (22+93) (115+4.6) (119.6+10) (129.6+3.3)

Compress and Expand

When used with one or more numbers as their operand, slash and backslash carry out operations known as compress and expand.

Compress can be used to select all or part of an object, according to the value of the numbers forming its operand. For example, to select some characters from a vector:

      1 0 1 1 0 1 / 'ABCDEF'
ACDF

Conversely, expand will insert fill data into objects:

      TAB ← 2 3⍴⍳6
      TAB
1 2 3
4 5 6
      1 0 1 0 1 \[2]TAB
1 0 2 0 3
4 0 5 0 6

Columns are inserted in positions indicated by the 0s. (Note also the use of the axis operator).

Outer and inner products

The product operators allow APL functions to be applied between all the elements in one argument and all the elements in another.

This is an important extension because previously functions have only applied to corresponding elements as in this example:

      1 2 3 + 4 5 6
5 7 9

The outer product gives the result of applying the function to all combinations of the elements in the two arguments. For example, to find the outer product of the two arguments used in the last example:

      1 2 3 ∘.+ 4 5 6
5 6 7
6 7 8
7 8 9

The first row is the result of adding the first element on the left to every element on the right, the second row is the result of adding the second element in the left to every element on the right and so on till all combinations are exhausted.

This example works out a matrix of powers:

      1 2 3 4 ∘.*1 2 3 4
1  1  1   1
2  4  8  16
3  9 27  81
4 16 64 256

as can be seen more clearly if we lay it out like this:

 |  1   2   3    4
 |¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
1|  1   2   3    4
2|  2   4   6    8
3|  3   9  27   81
4|  4  16  64  256

(Since the outer product involves operations between all elements, rather than just between corresponding elements, it's not necessary for the arguments to conform in shape or size.)

The inner product allows two functions to be applied to the arguments. The operations take place between the last dimension of the left argument and the first dimension of the right argument, hence 'inner' product since the two inner dimensions are used.

In the case of matrices, first each row of the left argument is applied to each column of the right argument using the rightmost function of the inner product, then the leftmost function is applied to the result, in a reduction (/) operation.

Given that you can use a combination of any two suitable functions, there are many possible inner products. These can perform a variety of useful operations. Some of the more common uses are:

  • locating incidences of given character strings within textual data

  • evaluation of polynomials

  • matrix multiplication

  • product of powers

Each

As its name implies, the each operator will apply a function to each element of an array.

So, to find the lengths of an array of vectors

      ⍴¨(1 2 3) (1 2) (1 2 3 4 5)
3 2 5

As with other operators, each can be used for user-defined functions. Here we use an 'average' function on an array of vectors.

      AVERAGE 1 2 3
2
      AVERAGE ¨ (1 2 3) (4 5 6) (10 100 1000)
2 5 370

Topic: APLX Help : Help on APL language : APL Fundamentals : Operators
[ Previous | Next | Contents | Index | APL Home ]