# Inner Product: Difference between revisions

 .

Inner Product (.) is a dyadic operator that produces a dyadic function when applied with two dyadic functions. It's a generalisation of the matrix product, allowing not just addition-multiplication, but any dyadic functions given as operands.

## Examples

      x ← 1 2 3
y ← 4 5 6
x ,.(⊂,) y ⍝ visualizing of pairing
┌─────────────┐
│┌───┬───┬───┐│
││1 4│2 5│3 6││
│└───┴───┴───┘│
└─────────────┘
x {⊂⍺,'+',⍵}.{⊂⍺,'×',⍵} y ⍝ visualizing function application in matrix multiplication
┌───────────────────────────┐
│┌─────────────────────────┐│
││┌─────┬─┬───────────────┐││
│││1 × 4│+│┌─────┬─┬─────┐│││
│││     │ ││2 × 5│+│3 × 6││││
│││     │ │└─────┴─┴─────┘│││
││└─────┴─┴───────────────┘││
│└─────────────────────────┘│
└───────────────────────────┘
x+.×y ⍝ matrix multiplication
32


The shapes of the arguments must be compatible with each other: The last axis of the left argument must have the same length as the first axis of the right argument, or formally, for X f.g Y it must be that (¯1↑⍴X)≡(1↑⍴Y). Although this rule differs from conformability, the arguments may also be subject to scalar or singleton extension. The shape of the result is (¯1↓⍴X),(1↓⍴Y).

For example, when applying inner product on two matrices, the number of columns in the left array must match with number of rows in the right array, otherwise we will get an error.

      ⎕  ← x ← 2 3⍴⍳10
1 2 3
4 5 6
⎕ ← y ← 4 2⍴⍳10
1 2
3 4
5 6
7 8
x+.×y
LENGTH ERROR
x+.×y
∧
⎕ ← y ← 3 2⍴⍳10 ⍝ reshape y to be compatible with x
x+.×y
22 28
49 64


## History

Inner product appeared in early Iverson Notation as ${\displaystyle _{g}^{f}}$ and applied even to non-scalar functions, like Compress, Iverson bringing:[1]

{\displaystyle {\begin{aligned}{\text{For example, if}}\\{\bf {A}}&={\begin{pmatrix}1&3&2&0\\2&1&0&1\\2&0&0&2\\\end{pmatrix}}\qquad {\text{and}}\qquad {\bf {{B}={\begin{pmatrix}4&1\\0&3\\0&2\\2&0\\\end{pmatrix}}}}\\{\text{then}}\qquad {\bf {{A}\;_{\times }^{+}\,{\bf {B}}}}&={\begin{pmatrix}4&14\\10&5\\20&4\\\end{pmatrix}},\quad {\bf {{A}\;_{=}^{\land }\,{\bf {{B}={\begin{pmatrix}0&1\\0&0\\1&0\\\end{pmatrix}}{\text{,}}}}}}\\{\bf {{A}\;_{\neq }^{\lor }\;{\bf {B}}}}&={\begin{pmatrix}1&0\\1&1\\0&1\\\end{pmatrix}},\qquad {\text{and}}\qquad ({\bf {{A}\neq 0)\;_{\,/}^{+}\,{\bf {{B}={\begin{pmatrix}4&6\\6&4\\6&1\\\end{pmatrix}}{\text{.}}}}}}\end{aligned}}}

When the inner product notation was linearised (made to fit on a single line of code) the glyph . was chosed to denote what was previously indicated by positioning the two operands vertically aligned. Thus, the above correspond to the following modern APL:

⍝ For example, if

     A←3 4⍴1 3 2 0 2 1 0 1 4 0 0 2
B←4 2⍴4 1 0 3 0 2 2 0


⍝ then

     A +.× B
4 14


10 5 20 4

     A ∧.= B


0 1 0 0 1 0

     A ∨.≠ B


1 0 1 1 0 1

     (A ≠ 0) +./ B


4 6 6 4 6 1 </source> Note that some dialects implement Compress (/) as a monadic operator rather than as a function, which means it cannot be an operand in the inner product. Instead, a cover function is necessary:

∇z←a Compress b
z←\/b
∇


## Differences between dialects

Implementations differ on the exact behaviour of inner product when the right operand is not a scalar function. It follows from page 121 of the ISO/IEC 13751:2001(E) standard specifies that X f.g Y is equivalent to ⊃⍤0 f/¨ (⊂[⍴⍴x]x)∘.g ⊂[1]y. This is indeed what APL2, APLX, and ngn/apl follow, while Dyalog APL and GNU APL use ⊃⍤0 f/¨ (⊂[⍴⍴x]x)∘.(g¨) ⊂[1]y.