Outer Product

From APL Wiki
Revision as of 22:31, 10 September 2022 by Adám Brudzewsky (talk | contribs) (Text replacement - "<source" to "<syntaxhighlight")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Outer Product (∘.), or Table is a monadic operator, which will produce a dyadic function when applied with a dyadic function. Outer product applies the operand function on each element of the left array with each element of the right array. It can be described as a shortcut for constructing nested for loops.


Outer Product differs from all other monadic operators, which are written as a single glyph, with the operand on the left. For historical reasons, the outer product operator is a bi-glyph denoted as ∘., and its appears on the right. This special notation is derived from the f.g notation of inner product:[1]

The result of an inner product is an array with rank two less than the sum of the argument ranks. The result of an outer product, on the other hand, is always an array of rank equal to the sum of the argument ranks. This follows from the fact that the reduction operation, which collapses two dimensions in an inner product, is not used in the outer product. The notation for outer product reflects this by canonically using a small circle as the first symbol. Thus, the ordinary outer product is written as a∘.×b .

This syntactical inconsistency is resolved in J and BQN, where the outer product operator, called Table, and denoted / and respectively, has the usual operator syntax.


      x  1 2 3
      y  4 5 6
      x ∘., y ⍝ visualizing outer product
1 41 51 6
2 42 52 6
3 43 53 6

      ⍝ works for multi-dimensional arrays as well
      y2 3  'abcdef'
      x2 2  4
1 a1 b1 c
1 d1 e1 f
2 a2 b2 c
2 d2 e2 f
3 a3 b3 c
3 d3 e3 f
4 a4 b4 c
4 d4 e4 f


Outer product is useful for solving problems that intuitively require a polynomial time algorithm. This may also indicate that such an algorithm is not the fastest solution.

For example, suppose we want to find duplicated elements in an non-nested array. Intuitively speaking, the easiest way to solve this problem is to compare each element of the array with all other elements, which is exactly what an outer product does.

      x  1 2 3 2
        matrix  x∘.=x ⍝ compare elements with each other using equal
1 0 0 0
0 1 0 1
0 0 1 0
0 1 0 1
        count  +/matrix ⍝ get the number of occurence of each element
1 2 1 2
        indices  count  2 ⍝ get the indices of elements which occured more than once
0 1 0 1
        duplicated   indices/x 

      ((+/x∘.=x)2)/x ⍝ everything above in one line
      (⊢∪⍤/⍨2(+/∘.=)) x ⍝ point-free/tacit version

Using similar techniques, we can define a function that generates prime numbers by using an outer product of Residue.

     primes  {x1↓⍳  (2>+0=x∘.|x)/x}
     primes 10
2 3 5 7
      primes 20
2 3 5 7 11 13 17 19

Here there are faster solutions such as the Sieve of Eratosthenes.

External links



  1. Falkoff, A.D. and K.E. Iverson. The APL\360 Terminal System: Inner and Outer Products. Research Report RC-1922. IBM Watson Research Center. 1967-10-16.
APL built-ins [edit]
Primitive functions
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentitySelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndex
Selector Index generatorGradeIndex OfInterval IndexIndicesDeal
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Primitive operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductPowerAtUnderRankDepthVariantStencilCut (J)
Quad names
Arrays Index originMigration levelAtomic vector
Functions Name classCase convertUnicode convert
Operators SearchReplace