Outer Product: Difference between revisions

Jump to navigation Jump to search
4,707 bytes added ,  08:31, 26 August 2023
Add section about J's Table; relate OP to Cartesian product; fix a typo
No edit summary
Tags: Mobile edit Mobile web edit
(Add section about J's Table; relate OP to Cartesian product; fix a typo)
Line 1: Line 1:
{{Built-in|Outer Product|<nowiki>∘.</nowiki>}}, 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 [[wikipedia:for loop|for loop]]s.
{{Built-in|Outer Product|<nowiki>∘.</nowiki>}}, 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 [[wikipedia:for loop|for loop]]s, and as a generalization of the [[wikipedia:Cartesian product|Cartesian product]] to operations other than [[catenate|catenation]].


=== Syntax ===
=== Syntax ===
Outer Product differs from all other [[monadic operator]]s, which are written as a single [[glyph]], with the operand on the left. For [[backwards compatibility|historical reasons]], the outer product operator is a [[bi-glyph]] denoted as <syntaxhighlight lang=apl inline>∘.</syntaxhighlight>, and its appears on the right. This special notation is derived from the <syntaxhighlight lang=apl inline>f.g</syntaxhighlight> notation of [[inner product]]:<ref>[[Adin Falkoff|Falkoff, A.D.]] and [[Ken Iverson|K.E. Iverson]]. [https://www.jsoftware.com/papers/APL360TerminalSystem1.htm#ip The APL\360 Terminal System: Inner and Outer Products]. Research Report RC-1922. [[IBM]] Watson Research Center. 1967-10-16.</ref>
Outer Product differs from all other [[monadic operator]]s, which are written as a single [[glyph]], with the operand on the left. For [[backwards compatibility|historical reasons]], the outer product operator is a [[bi-glyph]] denoted as <syntaxhighlight lang=apl inline>∘.</syntaxhighlight>, and its operand appears on the right. This special notation is derived from the <syntaxhighlight lang=apl inline>f.g</syntaxhighlight> notation of [[inner product]]:<ref>[[Adin Falkoff|Falkoff, A.D.]] and [[Ken Iverson|K.E. Iverson]]. [https://www.jsoftware.com/papers/APL360TerminalSystem1.htm#ip The APL\360 Terminal System: Inner and Outer Products]. Research Report RC-1922. [[IBM]] Watson Research Center. 1967-10-16.</ref>
<blockquote>
<blockquote>
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 <code>a∘.×b</code> .
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 <code>a∘.×b</code> .
Line 84: Line 84:
</syntaxhighlight>
</syntaxhighlight>
Here there are faster solutions such as the [[wikipedia:Sieve of Eratosthenes|Sieve of Eratosthenes]].
Here there are faster solutions such as the [[wikipedia:Sieve of Eratosthenes|Sieve of Eratosthenes]].
=== Differences between dialects ===
[[J]]'s Table differs from APL's Outer Product by enabling Cartesian pairing between [[cell]]s of [[rank]]s other than 0, and between cells of different rank for the left vs. right arguments, without needing to first [[enclose]] the relevant cells. It is defined as <syntaxhighlight lang=j inline>u"(lu, _)</syntaxhighlight>, where <syntaxhighlight lang=j inline>lu</syntaxhighlight> is the left rank of operand <syntaxhighlight lang=j inline>u</syntaxhighlight> (<syntaxhighlight lang=j inline>_</syntaxhighlight> denotes infinity in J). The results of the individual applications of u are collectively [[frame]]d by the catenation of the frames of the left and right arguments relative to their lu- and ru-cells respectively, where lu and ru are the dyadic ranks of the operand u. Unlike APL's Outer Product, J's Table does not use an implicit [[Each]], so the results of the individual applications of its operand are not boxed (unless the given operand itself does so).
<syntaxhighlight lang=j>
  NB. Cartesian pairing of rows
  x=: 100+i.2 2
  y=: i.3 4
  x ,"1/ y      NB. same as x ,"1"1 _ y
100 101 0 1  2  3
100 101 4 5  6  7
100 101 8 9 10 11
102 103 0 1  2  3
102 103 4 5  6  7
102 103 8 9 10 11
  NB. Cartesian pairing of x-matrices and y-cubes
  x=: 100+i.2 2 2
  y=: i.2 2 2 4
  x ;"2 3/ y    NB. same as x ;"2 3"2 _ y
┌───────┬───────────┐
│100 101│ 0  1  2  3│
│102 103│ 4  5  6  7│
│      │          │
│      │ 8  9 10 11│
│      │12 13 14 15│
├───────┼───────────┤
│100 101│16 17 18 19│
│102 103│20 21 22 23│
│      │          │
│      │24 25 26 27│
│      │28 29 30 31│
└───────┴───────────┘
┌───────┬───────────┐
│104 105│ 0  1  2  3│
│106 107│ 4  5  6  7│
│      │          │
│      │ 8  9 10 11│
│      │12 13 14 15│
├───────┼───────────┤
│104 105│16 17 18 19│
│106 107│20 21 22 23│
│      │          │
│      │24 25 26 27│
│      │28 29 30 31│
└───────┴───────────┘
</syntaxhighlight>
{{Works in|[[J]]}}
In APL one can accomplish this behavior by enclosing the relevant cells of each argument before applying the outer product operation (or by successive applications of [[rank_(operator)|Rank]], e.g. <syntaxhighlight lang=apl inline>f⍤2 99⍤99 1</syntaxhighlight>). Conversely, the behavior of APL's Outer Product can be accomplished in terms of J's Table by adding the Each modifier, as in <syntaxhighlight lang=j inline>u&.>/</syntaxhighlight> or <syntaxhighlight lang=j inline>u&>/</syntaxhighlight>, depending on whether the given operand is a [[scalar function]].
The Cartesian pairing behavior of J's Table results from each <syntaxhighlight lang=j inline>"(lu, _)</syntaxhighlight> cell-pairing being processed by <syntaxhighlight lang=j inline>u</syntaxhighlight>, which applies its own ranks to divide up the argument into cells.
However, it does not allow for creating a Cartesian pairing involving cells of the left argument specified with negative left rank L (except trivially, in cases in which Table's application has no effect, e.g. <syntaxhighlight lang=j inline>u"2 _"_ 1/</syntaxhighlight>). This is because negative assigned rank in J is encoded as infinite rank; in J's terminology, negative rank is "internal rank" only.
<syntaxhighlight lang=j>
  +b.0
0 0 0
  }.+"_1 _2 b.0  NB. the dyadic ranks of derived verb +"_1 _2 as seen by Table
_ _
</syntaxhighlight>
Given the <syntaxhighlight lang=j inline>u"(lu, _)</syntaxhighlight> definition of Table, even in a hypothetical J dialect in which assignment of negative rank is visible as such to modifiers, a left negative rank L used with Table would be applied twice (once by the ranks added by Table, and once by the operand itself). This would result in the L-cells ''of each of the left argument's L-cells'' being paired ''1-to-1'' with the specified cells of the right argument—not a Cartesian pairing. If a J dialect both allowed externally visible negative rank and defined Table to instead be <syntaxhighlight lang=j inline>u"lu _"_ ru</syntaxhighlight>, one could express e.g. the Cartesian pairing of the [[major cells]] of the left and right arguments as <syntaxhighlight lang=j inline>u"_1/</syntaxhighlight>.
== External links ==
== External links ==


trusted
83

edits

Navigation menu