Outer Product: Difference between revisions

Jump to navigation Jump to search
No edit summary
Line 53: Line 53:
=== Applications ===
=== 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 an 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.
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.
Line 71: Line 71:


       ∪((+/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≤(+/∘.=⍨)) 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>.
Using similar techniques, we can define a function that generates prime numbers by using an outer product of [[Residue]].
 
Using similar techniques, we can define a function that generate prime numbers by using an outer product of [[Residue]].
<source lang=apl>
<source lang=apl>
     primes ← {x←1↓⍳⍵ ⋄ (2>+⌿0=x∘.|x)/x}
     primes ← {x←1↓⍳⍵ ⋄ (2>+⌿0=x∘.|x)/x}