Inner Product: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
(Created page with "{{Built-in|Inner Product|<nowiki>.</nowiki>}}, is a dyadic operator, which will produce a dyadic function when applied with two dyadic functions. In APL, the inner...")
 
Line 19: Line 19:
For example, when applying inner-product to a 2D array, the column count of the left array must match with the row count of the right array, otherwise we will get an error.
For example, when applying inner-product to a 2D array, the column count of the left array must match with the row count of the right array, otherwise we will get an error.
<source lang=apl>
<source lang=apl>
       x ← 2 3⍴⍳10
       ⎕  ← x ← 2 3⍴⍳10
       y ← 4 2⍴⍳19
1 2 3
4 5 6
       ⎕ ← y ← 4 2⍴⍳10
1 2
3 4
5 6
7 8 
       x+.×y  
       x+.×y  
LENGTH ERROR
LENGTH ERROR
       x+.×y
       x+.×y
         ∧
         ∧
       y ← 3 2⍴⍳10 ⍝ reshape y to be compatible with x
       ⎕ ← y ← 3 2⍴⍳10 ⍝ reshape y to be compatible with x
       x+.×y
       x+.×y
22 28
22 28
49 64
49 64
</source>
</source>

Revision as of 02:30, 6 September 2021

.

Inner Product (.), is a dyadic operator, which will produce a dyadic function when applied with two dyadic functions. In APL, the inner product is a generalisation of the matrix product, which allows not only addition-multiplication, but any dyadic functions given.

Examples

      x ← 1 2 3
      y ← 4 5 6
      x ,.(↓,) y ⍝ visualizing inner product
┌─────────────┐
│┌───┬───┬───┐│
││1 4│2 5│3 6││
│└───┴───┴───┘│
└─────────────┘
      x+.×y ⍝ matrix multiplication
32

Note that for inner product between N-dimensional arrays, their dimension must be compatible with each other.

For example, when applying inner-product to a 2D array, the column count of the left array must match with the row count of 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