Convolution: Difference between revisions

From NARS2000
Jump to navigationJump to search
No edit summary
mNo edit summary
 
Line 62: Line 62:
<p>Polynomial multiplication is illustrated in the above example using the functions <apll>+</apll> and <apll>×</apll>.  Overlapping string searching uses the functions <apll>∧</apll> and <apll>=</apll> as in</p>
<p>Polynomial multiplication is illustrated in the above example using the functions <apll>+</apll> and <apll>×</apll>.  Overlapping string searching uses the functions <apll>∧</apll> and <apll>=</apll> as in</p>


<apll><pre>
<table border="0" cellpadding="0" cellspacing="0" rules="none" summary="">
      L←'abababc' ⋄ R←'aba'
<tr>
      (0⌈¯1+(⍴L)⌊⍴R)↓L∧⍡=⌽R
  <td><apll>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;L←'abababc' ⋄ R←'aba'</apll></td>
1 0 1 0 0 0 0</pre></apll>
</tr>
<tr>
  <td><apll>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(0⌈¯1+(⍴L)⌊⍴R)↓L∧⍡=⌽R</apll></td>
</tr>
<tr>
  <td><apll>1 0 1 0 0 0 0</apll></td>
</tr>
</table>


where we need to drop leading elements of the convolution result to remove the leading prefix comparisons.
where <apll>(0⌈¯1+(⍴L)⌊⍴R)↓</apll> is needed to remove the leading prefix comparisons.

Latest revision as of 19:36, 18 August 2026

Z←L f⍡g R returns the convolution (moving window inner product) of L vs. R.
L and R are vectors; scalars are promoted to one-element vectors.
f and g are functions.
Z is a vector whose shape is (⍴L)+(⍴R)-1.

The result is obtained by dragging the reverse of the shorter argument through all positions of the longer argument (as in a moving window) and performing an f.g inner product between the two, including leading and trailing prefixes. For example,

      L←1 3 2 1 ⋄ R←2 1 ⋄ L+⍡×R

2 1 2 1 2 1 2 1    multiply the rows together
1 3 2 1 1 3 2 1 1 3 2 1 1 3 2 1 1 3 2 1

2 1 6 3 4 2 2 1    add the products

L+⍡×R 2 7 7 4 1

Interestingly, this algorithm solves a diverse set of problems such as weighted moving average (used to remove pixelization from a digital image), polynomial multiplication, and overlapping string searching.

Polynomial multiplication is illustrated in the above example using the functions + and ×. Overlapping string searching uses the functions and = as in

      L←'abababc' ⋄ R←'aba'
      (0⌈¯1+(⍴L)⌊⍴R)↓L∧⍡=⌽R
1 0 1 0 0 0 0

where (0⌈¯1+(⍴L)⌊⍴R)↓ is needed to remove the leading prefix comparisons.