Inner Product: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
No edit summary
Line 5: Line 5:
       x ← 1 2 3
       x ← 1 2 3
       y ← 4 5 6
       y ← 4 5 6
       x ,.(,) y ⍝ visualizing inner product
       x ,.(,) y ⍝ visualizing of pairing
┌─────────────┐
┌─────────────┐
│┌───┬───┬───┐│
│┌───┬───┬───┐│
Line 11: Line 11:
│└───┴───┴───┘│
│└───┴───┴───┘│
└─────────────┘
└─────────────┘
      x {⊂⍺,'+',⍵}.{⊂⍺,'×',⍵} y ⍝ visualizing function application in matrix multiplication
┌───────────────────────────┐
│┌─────────────────────────┐│
││┌─────┬─┬───────────────┐││
│││1 × 4│+│┌─────┬─┬─────┐│││
│││    │ ││2 × 5│+│3 × 6││││
│││    │ │└─────┴─┴─────┘│││
││└─────┴─┴───────────────┘││
│└─────────────────────────┘│
└───────────────────────────┘
       x+.×y ⍝ matrix multiplication
       x+.×y ⍝ matrix multiplication
32     
32     
</source>
</source>


Note that for inner product between N-dimensional arrays, their dimension must be compatible with each other.  
Note that the [[shape]]s 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 <source lang=apl inline>X f.g Y</source> it must be that <source lang=apl inline>(¯1↑⍴X)≡(1↑⍴Y)</source>. The shape of the result is <source lang=apl inline>(¯1↓⍴X),(1↓⍴Y)</source>.


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 on two [[matrix|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.
<source lang=apl>
<source lang=apl>
       ⎕  ← x ← 2 3⍴⍳10
       ⎕  ← x ← 2 3⍴⍳10
Line 26: Line 36:
3 4
3 4
5 6
5 6
7 8  
7 8
       x+.×y  
       x+.×y  
LENGTH ERROR
LENGTH ERROR
Line 36: Line 46:
49 64
49 64
</source>
</source>
== External links ==
=== Documentation ===
* [https://help.dyalog.com/latest/#Language/Primitive%20Operators/Inner%20Product.htm Dyalog]
* [https://microapl.com/apl_help/ch_020_020_880.htm APLX]
* J [https://www.jsoftware.com/help/dictionary/d300.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/dot#dyadic NuVoc]
{{APL built-ins}}[[Category:Primitive operators]]

Revision as of 05:59, 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 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

Note that 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). 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

External links

Documentation


APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentityStopSelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndexCartesian ProductSort
Selector Index generatorGradeIndex OfInterval IndexIndicesDealPrefix and suffix vectors
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)
Quad names Index originComparison toleranceMigration levelAtomic vector