Topic: APLX Help : Help on APL language : APL Primitives : . Inner product
[ Previous | Next | Contents | Index | APL Home ]

www.microapl.co.uk

. Inner product


Inner product takes the form:

          DATA1  FN1 . FN2  DATA2

Where the operands, FN1 and FN2, are both dyadic functions, including user- defined functions. Inner product first combines the data along the last axis of the left argument with the data along the first axis of the right argument in an 'Outer Product' operation with the right operand. Finally a 'reduction' operation is applied to each element of the result.

If the two arguments are vectors of the same size, then the inner product gives the same result as FN2 being applied to the data and then FN1 being applied to the result in a reduction operation. (See / for reduction.)

             X ← 1 3 5 7
             Y ← 2 3 6 7
             X +.= Y                 (This finds and totals the agreements
       2                              between X and Y)

The above statement is equivalent to +/X=Y and involves the following steps:

             X=Y                     (Compares X and Y)
       0 1 0 1                       (1 means agreement between elements)
             +/0 1 0 1               (Sums the agreements)
       2

Using the same values of X and Y as above:

             X^.=Y                   (Returns a 1 if all elements in
       0                              X equal all elements in Y)
             X^.=1 3 5 7
       1

When applied to data of more than one dimension, such as matrices, the operation is more complex. For matrix arguments the shape of the result of the operation is given by deleting the two inner axes and joining the others in order. For example if we have:

             TABA of 4 rows and  columns
and          TABB of 5 rows and 6 columns

The inner dimensions are used by the inner product operation, and the result will be a 4-row 6-column matrix.

The operations take place between the rows and columns of the two matrices and are therefore the same as inner product operations between vectors as described above.

             TABLE1                         TABLE2
             1    2                         6  2  3  4
             5    4                         7  0  1  8
             3    0
             RESULT←TABLE1 +.× TABLE2
             RESULT
       20   2   5   20
       58  10  19   52
       18   6   9   12

The first number in RESULT is produced from row 1 of TABLE1 and column 1 of TABLE2.

             1 2 +.× 6 7             (Equivalent to +/1 2 × 6 7)
       20

Row 1 of TABLE1 is then used with each remaining column in TABLE2 to produce the first row of RESULT. Then row 2 of TABLE1 is used with each column of TABLE2 to produce the second row of RESULT and so on. So the 10 highlighted in row 2 of RESULT is derived from row 2 of TABLE1 and column 2 of TABLE2:

             5 4 +.× 2 0             (Equivalent to +/ 5 4 × 2 0)
       10

The operation shown above is the Matrix Multiplication operation. The operation can have non-scalar operands:

             X
       1 2 3
       4 5 6
             Y
       1 2 3
       4 5 6
       7 8 9
             X+.,Y                   (Columns of Y catenated to rows of X
       18 21 24                       and the results added up)
       27 30 33
             ⍴X+.,Y
       2 3

Other useful combinations are:

             A^.=B   Instances of vector B in matrix A
             A^.≠B   Finds where there is no single match of vector B in
                     in matrix A
             A+.=B   Gives a count of agreements between A and B
             A+.∊B   Give a count of memberships of B in A

These may, of course be extended to higher dimensional arguments. The general definition of inner product is given below. For the inner product operation

       DATA1 FN1.FN2 DATA2

the result is defined as

       FN1/¨ (⊂[⍴⍴DATA1]DATA1)∘.FN2 ⊂[1]DATA2

Topic: APLX Help : Help on APL language : APL Primitives : . Inner product
[ Previous | Next | Contents | Index | APL Home ]