programlibs/Equations/demos/convolution.xtl
Discrete convolution and correlation as the sums they are, on an asymmetric signal and kernel so the two cannot be confused: the kernel slides reversed in a convolution and as written in a correlation (what a CNN layer computes). Each mode, named every time, and what the paper prints. Run with "just demo Equations convolution"; xetal expand shows the sum each call becomes.
x : Int
x ← 1 2 3 4
h : Int
h ← 1 2 3
M : Int
What the paper prints: the valid cases, written out as equations over the same arrays (their values agree with the macros), and the LaTeX of each.
(M, L) ← (3, 4)
L : Int
What the paper prints: the valid cases, written out as equations over the same arrays (their values agree with the macros), and the LaTeX of each.
(M, L) ← (3, 4)
conv : Int
(conv, ctex) ← ᵉb̲oth< y_{n} = \sum_{m=0}^{M - 1} x_{n+M-1-m} \, h_{m}
ᵉb̲oth< expands to
((({ @ → g69:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g68:c̲heck ← { (A, name, k, from) e → n ← k s̲elect s̲hape A bad ← w̲here n̲ot r̲avel (0 ≤ e) ∧ e < n 0 = t̲ally bad ? e "index" ⎕S̲IGNAL name c̲at " axis " c̲at (f̲ormat k) c̲at ": index " c̲at (f̲ormat (f̲irst bad) s̲elect r̲avel e) c̲at " is outside 0.." c̲at (f̲ormat n − 1) c̲at " (from " c̲at from c̲at ")" } g67:r̲ank ← { (A, name, q, ref) → (t̲ally s̲hape A) = q ? A "rank" ⎕S̲IGNAL ref c̲at " indexes " c̲at (f̲ormat q) c̲at " axes, but " c̲at name c̲at " has rank " c̲at f̲ormat t̲ally s̲hape A } g70:grid ← ((L − M) + 1) g71:g̲rid ← { k → g70:grid r̲eshape (o̲ffsets k s̲elect g70:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g70:grid) r̲eshape 0 } g70:grid r̲eshape (({ @ → g72:grid ← ((L − M) + 1) c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..M-1" } (1 + (M − 1) − 0)) g73:g̲rid ← { k → g72:grid r̲eshape (o̲ffsets k s̲elect g72:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g72:grid) r̲eshape 0 } '+ r̲/₂ g72:grid r̲eshape (( ((g67:r̲ank (x, "x", 1, "x[n+M-1-m]")) g69:a̲t ( ((x, "x", 1, "n+M-1-m") g68:c̲heck ((((0 + g73:g̲rid 1) + M) − 1) − (0 + g73:g̲rid 2))))) × ((g67:r̲ank (h, "h", 1, "h[m]")) g69:a̲t ( ((h, "h", 1, "m") g68:c̲heck (0 + g73:g̲rid 2)))))) } @)) } @), "y_{n} = \\sum_{m=0}^{M - 1} x_{n+M-1-m} \\, h_{m}"))
ctex : Char
(conv, ctex) ← ᵉb̲oth< y_{n} = \sum_{m=0}^{M - 1} x_{n+M-1-m} \, h_{m}
ᵉb̲oth< expands to
((({ @ → g69:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g68:c̲heck ← { (A, name, k, from) e → n ← k s̲elect s̲hape A bad ← w̲here n̲ot r̲avel (0 ≤ e) ∧ e < n 0 = t̲ally bad ? e "index" ⎕S̲IGNAL name c̲at " axis " c̲at (f̲ormat k) c̲at ": index " c̲at (f̲ormat (f̲irst bad) s̲elect r̲avel e) c̲at " is outside 0.." c̲at (f̲ormat n − 1) c̲at " (from " c̲at from c̲at ")" } g67:r̲ank ← { (A, name, q, ref) → (t̲ally s̲hape A) = q ? A "rank" ⎕S̲IGNAL ref c̲at " indexes " c̲at (f̲ormat q) c̲at " axes, but " c̲at name c̲at " has rank " c̲at f̲ormat t̲ally s̲hape A } g70:grid ← ((L − M) + 1) g71:g̲rid ← { k → g70:grid r̲eshape (o̲ffsets k s̲elect g70:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g70:grid) r̲eshape 0 } g70:grid r̲eshape (({ @ → g72:grid ← ((L − M) + 1) c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..M-1" } (1 + (M − 1) − 0)) g73:g̲rid ← { k → g72:grid r̲eshape (o̲ffsets k s̲elect g72:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g72:grid) r̲eshape 0 } '+ r̲/₂ g72:grid r̲eshape (( ((g67:r̲ank (x, "x", 1, "x[n+M-1-m]")) g69:a̲t ( ((x, "x", 1, "n+M-1-m") g68:c̲heck ((((0 + g73:g̲rid 1) + M) − 1) − (0 + g73:g̲rid 2))))) × ((g67:r̲ank (h, "h", 1, "h[m]")) g69:a̲t ( ((h, "h", 1, "m") g68:c̲heck (0 + g73:g̲rid 2)))))) } @)) } @), "y_{n} = \\sum_{m=0}^{M - 1} x_{n+M-1-m} \\, h_{m}"))
corr : Int
(corr, rtex) ← ᵉb̲oth< y_{n} = \sum_{m=0}^{M - 1} x_{n+m} \, h_{m}
ᵉb̲oth< expands to
((({ @ → g76:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g75:c̲heck ← { (A, name, k, from) e → n ← k s̲elect s̲hape A bad ← w̲here n̲ot r̲avel (0 ≤ e) ∧ e < n 0 = t̲ally bad ? e "index" ⎕S̲IGNAL name c̲at " axis " c̲at (f̲ormat k) c̲at ": index " c̲at (f̲ormat (f̲irst bad) s̲elect r̲avel e) c̲at " is outside 0.." c̲at (f̲ormat n − 1) c̲at " (from " c̲at from c̲at ")" } g74:r̲ank ← { (A, name, q, ref) → (t̲ally s̲hape A) = q ? A "rank" ⎕S̲IGNAL ref c̲at " indexes " c̲at (f̲ormat q) c̲at " axes, but " c̲at name c̲at " has rank " c̲at f̲ormat t̲ally s̲hape A } g77:grid ← ((L − M) + 1) g78:g̲rid ← { k → g77:grid r̲eshape (o̲ffsets k s̲elect g77:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g77:grid) r̲eshape 0 } g77:grid r̲eshape (({ @ → g79:grid ← ((L − M) + 1) c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..M-1" } (1 + (M − 1) − 0)) g80:g̲rid ← { k → g79:grid r̲eshape (o̲ffsets k s̲elect g79:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g79:grid) r̲eshape 0 } '+ r̲/₂ g79:grid r̲eshape (( ((g74:r̲ank (x, "x", 1, "x[n+m]")) g76:a̲t ( ((x, "x", 1, "n+m") g75:c̲heck ((0 + g80:g̲rid 1) + (0 + g80:g̲rid 2))))) × ((g74:r̲ank (h, "h", 1, "h[m]")) g76:a̲t ( ((h, "h", 1, "m") g75:c̲heck (0 + g80:g̲rid 2)))))) } @)) } @), "y_{n} = \\sum_{m=0}^{M - 1} x_{n+m} \\, h_{m}"))
rtex : Char
(corr, rtex) ← ᵉb̲oth< y_{n} = \sum_{m=0}^{M - 1} x_{n+m} \, h_{m}
ᵉb̲oth< expands to
((({ @ → g76:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g75:c̲heck ← { (A, name, k, from) e → n ← k s̲elect s̲hape A bad ← w̲here n̲ot r̲avel (0 ≤ e) ∧ e < n 0 = t̲ally bad ? e "index" ⎕S̲IGNAL name c̲at " axis " c̲at (f̲ormat k) c̲at ": index " c̲at (f̲ormat (f̲irst bad) s̲elect r̲avel e) c̲at " is outside 0.." c̲at (f̲ormat n − 1) c̲at " (from " c̲at from c̲at ")" } g74:r̲ank ← { (A, name, q, ref) → (t̲ally s̲hape A) = q ? A "rank" ⎕S̲IGNAL ref c̲at " indexes " c̲at (f̲ormat q) c̲at " axes, but " c̲at name c̲at " has rank " c̲at f̲ormat t̲ally s̲hape A } g77:grid ← ((L − M) + 1) g78:g̲rid ← { k → g77:grid r̲eshape (o̲ffsets k s̲elect g77:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g77:grid) r̲eshape 0 } g77:grid r̲eshape (({ @ → g79:grid ← ((L − M) + 1) c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..M-1" } (1 + (M − 1) − 0)) g80:g̲rid ← { k → g79:grid r̲eshape (o̲ffsets k s̲elect g79:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g79:grid) r̲eshape 0 } '+ r̲/₂ g79:grid r̲eshape (( ((g74:r̲ank (x, "x", 1, "x[n+m]")) g76:a̲t ( ((x, "x", 1, "n+m") g75:c̲heck ((0 + g80:g̲rid 1) + (0 + g80:g̲rid 2))))) × ((g74:r̲ank (h, "h", 1, "h[m]")) g76:a̲t ( ((h, "h", 1, "m") g75:c̲heck (0 + g80:g̲rid 2)))))) } @)) } @), "y_{n} = \\sum_{m=0}^{M - 1} x_{n+m} \\, h_{m}"))
t : Float
A smoothing is a correlation with a kernel of weights; a difference filter, a correlation with 1 0 -1.
t ← 2.0 4.0 6.0 8.0 10.0 12.0
third : Float
third ← 0.25 0.5 0.25 ⍝ a smoothing kernel: weights that sum to 1