Outer Product: Difference between revisions
(Added the Outer Product wiki page) |
m (→Application: Added tacit version of dedup) |
||
Line 26: | Line 26: | ||
</source> | </source> | ||
=== | === Applications === | ||
Outer product is useful for solving problems that intuitively requires a [https://en.wikipedia.org/wiki/Time_complexity#Polynomial_time polynomial time] algorithm. | Outer product is useful for solving problems that intuitively requires a [https://en.wikipedia.org/wiki/Time_complexity#Polynomial_time polynomial time] algorithm. | ||
However, this also indicates that such algorithm might not be the fastest solution. | However, this also indicates that such algorithm might not be the fastest solution. | ||
Line 49: | Line 49: | ||
∪((+/x∘.=x)≥2)/x ⍝ everything above in one line | ∪((+/x∘.=x)≥2)/x ⍝ everything above in one line | ||
2 (∪((2≤(+/∘.=⍨))(/⍨⍨)⊢)) x ⍝ point-free/tacit version | |||
2 | 2 | ||
</source> | </source> | ||
''Note: due to [[function-operator overloading]]'', to use [[replicate]] in a [[fork]], we have to use the workaround <source lang=apl inline>/⍨⍨</source>. |
Revision as of 09:04, 5 September 2021
Outer Product
Outer product is a monadic operator, which will produce a dyadic function when applied with a dyadic function. In APL, the outer product is a generalisation of the matrix product, which allows not only multiplication, but any dyadic function given.
Syntax
By right, a monadic operator should be a monograph (i.e. consist of only one character), and the operand should be on the left. However, due to legacy reason, the outer product operator is not only a diagraph denoted as ∘.
, the operand also appears on the right.
Notably, this syntactical inconsistency is resolved in BQN, where the outer product operator ⌜
abides with the usual operator syntax. Also note that it is called table in BQN.
Examples
x ← 1 2 3 y ← 4 5 6 x ∘., y ⍝ visualizing outer product ┌───┬───┬───┐ │1 4│1 5│1 6│ ├───┼───┼───┤ │2 4│2 5│2 6│ ├───┼───┼───┤ │3 4│3 5│3 6│ └───┴───┴───┘ x ∘.× y ⍝ matrix multiplication 4 5 6 8 10 12 12 15 18
Applications
Outer product is useful for solving problems that intuitively requires a polynomial time algorithm. However, this also indicates that such algorithm might not be 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 count ← +/matrix ⍝ get the number of occurence of each element 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 2 ∪((+/x∘.=x)≥2)/x ⍝ everything above in one line 2 (∪((2≤(+/∘.=⍨))(/⍨⍨)⊢)) x ⍝ point-free/tacit version 2
Note: due to function-operator overloading, to use replicate in a fork, we have to use the workaround /⍨⍨
.