sourcelibs/Equations/demos/convolution.xtl
1⍝!/usr/bin/env xetal
2⍝│ Discrete convolution and correlation as the sums they are, on an
3⍝│ asymmetric signal and kernel so the two cannot be confused: the
4⍝│ kernel slides reversed in a convolution and as written in a
5⍝│ correlation (what a CNN layer computes). Each mode, named every
6⍝│ time, and what the paper prints. Run with "just demo Equations
7⍝│ convolution"; xetal expand shows the sum each call becomes.
8"e:" u̲se< "Equations"
9x ← 1 2 3 4
10h ← 1 2 3
11⍝│ Every overlap (full), the center (same, as numpy centers it), the complete overlaps (valid).
12 ᵉc̲onvolve< y_{n} = \sum_{m} x_{n - m} \, h_{m} \quad \text{(full)}
13 ᵉc̲onvolve< y_{n} = \sum_{m} x_{n - m} \, h_{m} \quad \text{(same)}
14 ᵉc̲onvolve< y_{n} = \sum_{m} x_{n - m} \, h_{m} \quad \text{(valid)}
15 ᵉc̲orrelate< y_{n} = \sum_{m} x_{n + m} \, h_{m} \quad \text{(full)}
16 ᵉc̲orrelate< y_{n} = \sum_{m} x_{n + m} \, h_{m} \quad \text{(same)}
17 ᵉc̲orrelate< y_{n} = \sum_{m} x_{n + m} \, h_{m} \quad \text{(valid)}
18⍝│ What the paper prints: the valid cases, written out as equations
19⍝│ over the same arrays (their values agree with the macros), and the
20⍝│ LaTeX of each.
21(M, L) ← (3, 4)
22(conv, ctex) ← ᵉb̲oth< y_{n} = \sum_{m=0}^{M - 1} x_{n+M-1-m} \, h_{m}
23(corr, rtex) ← ᵉb̲oth< y_{n} = \sum_{m=0}^{M - 1} x_{n+m} \, h_{m}
24conv c̲at corr
25"$$" c̲at ctex c̲at "$$"
26"$$" c̲at rtex c̲at "$$"
27⍝│ A smoothing is a correlation with a kernel of weights; a difference
28⍝│ filter, a correlation with 1 0 -1.
29t ← 2.0 4.0 6.0 8.0 10.0 12.0
30third ← 0.25 0.5 0.25 ⍝ a smoothing kernel: weights that sum to 1
31diff ← 1.0 0.0 -1.0
32 ᵉc̲orrelate< a_{n} = \sum_{m} t_{n + m} \, third_{m} \quad \text{(valid)}
33 ᵉc̲orrelate< d_{n} = \sum_{m} t_{n + m} \, diff_{m} \quad \text{(valid)}
ᵉc̲onvolve< expands to
({ @ → g4:L ← t̲ally x g3:M ← t̲ally h g2:c ← (g3:M − 1) d̲iv 2 g1:p ← ((g3:M − 1) r̲eshape 0 × 1 t̲ake x) c̲at x c̲at (g3:M − 1) r̲eshape 0 × 1 t̲ake x ({ @ → g7:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g6: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 ")" } g5: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 } g8:grid ← ((g4:L + g3:M) − 1) g9:g̲rid ← { k → g8:grid r̲eshape (o̲ffsets k s̲elect g8:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g8:grid) r̲eshape 0 } g8:grid r̲eshape (({ @ → g10:grid ← ((g4:L + g3:M) − 1) c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..g3:M-1" } (1 + (g3:M − 1) − 0)) g11:g̲rid ← { k → g10:grid r̲eshape (o̲ffsets k s̲elect g10:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g10:grid) r̲eshape 0 } '+ r̲/₂ g10:grid r̲eshape (( ((g5:r̲ank (g1:p, "g1:p", 1, "g1:p[n+g3:M-1-m]")) g7:a̲t ( ((g1:p, "g1:p", 1, "n+g3:M-1-m") g6:c̲heck ((((0 + g11:g̲rid 1) + g3:M) − 1) − (0 + g11:g̲rid 2))))) × ((g5:r̲ank (h, "h", 1, "h[m]")) g7:a̲t ( ((h, "h", 1, "m") g6:c̲heck (0 + g11:g̲rid 2)))))) } @)) } @) } @)
ᵉc̲onvolve< expands to
({ @ → g15:L ← t̲ally x g14:M ← t̲ally h g13:c ← (g14:M − 1) d̲iv 2 g12:p ← ((g14:M − 1) r̲eshape 0 × 1 t̲ake x) c̲at x c̲at (g14:M − 1) r̲eshape 0 × 1 t̲ake x ({ @ → g18:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g17: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 ")" } g16: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 } g19:grid ← g15:L g20:g̲rid ← { k → g19:grid r̲eshape (o̲ffsets k s̲elect g19:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g19:grid) r̲eshape 0 } g19:grid r̲eshape (({ @ → g21:grid ← g15:L c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..g14:M-1" } (1 + (g14:M − 1) − 0)) g22:g̲rid ← { k → g21:grid r̲eshape (o̲ffsets k s̲elect g21:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g21:grid) r̲eshape 0 } '+ r̲/₂ g21:grid r̲eshape (( ((g16:r̲ank (g12:p, "g12:p", 1, "g12:p[n+g14:M-1+g13:c-m]")) g18:a̲t ( ((g12:p, "g12:p", 1, "n+g14:M-1+g13:c-m") g17:c̲heck (((((0 + g22:g̲rid 1) + g14:M) − 1) + g13:c) − (0 + g22:g̲rid 2))))) × ((g16:r̲ank (h, "h", 1, "h[m]")) g18:a̲t ( ((h, "h", 1, "m") g17:c̲heck (0 + g22:g̲rid 2)))))) } @)) } @) } @)
ᵉc̲onvolve< expands to
({ @ → g26:L ← t̲ally x g25:M ← t̲ally h g24:c ← (g25:M − 1) d̲iv 2 g23:p ← ((g25:M − 1) r̲eshape 0 × 1 t̲ake x) c̲at x c̲at (g25:M − 1) r̲eshape 0 × 1 t̲ake x _ ← { @ → g25:M ≤ g26:L ? @◆ "valid-mode" ⎕S̲IGNAL "valid: the kernel (" c̲at (f̲ormat g25:M) c̲at ") is longer than the signal (" c̲at (f̲ormat g26:L) c̲at ")" } @ ({ @ → g29:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g28: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 ")" } g27: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 } g30:grid ← ((g26:L − g25:M) + 1) g31:g̲rid ← { k → g30:grid r̲eshape (o̲ffsets k s̲elect g30:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g30:grid) r̲eshape 0 } g30:grid r̲eshape (({ @ → g32:grid ← ((g26:L − g25:M) + 1) c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..g25:M-1" } (1 + (g25:M − 1) − 0)) g33:g̲rid ← { k → g32:grid r̲eshape (o̲ffsets k s̲elect g32:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g32:grid) r̲eshape 0 } '+ r̲/₂ g32:grid r̲eshape (( ((g27:r̲ank (x, "x", 1, "x[n+g25:M-1-m]")) g29:a̲t ( ((x, "x", 1, "n+g25:M-1-m") g28:c̲heck ((((0 + g33:g̲rid 1) + g25:M) − 1) − (0 + g33:g̲rid 2))))) × ((g27:r̲ank (h, "h", 1, "h[m]")) g29:a̲t ( ((h, "h", 1, "m") g28:c̲heck (0 + g33:g̲rid 2)))))) } @)) } @) } @)
ᵉc̲orrelate< expands to
({ @ → g37:L ← t̲ally x g36:M ← t̲ally h g35:c ← (g36:M − 1) d̲iv 2 g34:p ← ((g36:M − 1) r̲eshape 0 × 1 t̲ake x) c̲at x c̲at (g36:M − 1) r̲eshape 0 × 1 t̲ake x ({ @ → g40:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g39: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 ")" } g38: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 } g41:grid ← ((g37:L + g36:M) − 1) g42:g̲rid ← { k → g41:grid r̲eshape (o̲ffsets k s̲elect g41:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g41:grid) r̲eshape 0 } g41:grid r̲eshape (({ @ → g43:grid ← ((g37:L + g36:M) − 1) c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..g36:M-1" } (1 + (g36:M − 1) − 0)) g44:g̲rid ← { k → g43:grid r̲eshape (o̲ffsets k s̲elect g43:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g43:grid) r̲eshape 0 } '+ r̲/₂ g43:grid r̲eshape (( ((g38:r̲ank (g34:p, "g34:p", 1, "g34:p[n+m]")) g40:a̲t ( ((g34:p, "g34:p", 1, "n+m") g39:c̲heck ((0 + g44:g̲rid 1) + (0 + g44:g̲rid 2))))) × ((g38:r̲ank (h, "h", 1, "h[m]")) g40:a̲t ( ((h, "h", 1, "m") g39:c̲heck (0 + g44:g̲rid 2)))))) } @)) } @) } @)
ᵉc̲orrelate< expands to
({ @ → g48:L ← t̲ally x g47:M ← t̲ally h g46:c ← (g47:M − 1) d̲iv 2 g45:p ← ((g47:M − 1) r̲eshape 0 × 1 t̲ake x) c̲at x c̲at (g47:M − 1) r̲eshape 0 × 1 t̲ake x ({ @ → g51:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g50: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 ")" } g49: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 } g52:grid ← g48:L g53:g̲rid ← { k → g52:grid r̲eshape (o̲ffsets k s̲elect g52:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g52:grid) r̲eshape 0 } g52:grid r̲eshape (({ @ → g54:grid ← g48:L c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..g47:M-1" } (1 + (g47:M − 1) − 0)) g55:g̲rid ← { k → g54:grid r̲eshape (o̲ffsets k s̲elect g54:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g54:grid) r̲eshape 0 } '+ r̲/₂ g54:grid r̲eshape (( ((g49:r̲ank (g45:p, "g45:p", 1, "g45:p[n+g46:c+m]")) g51:a̲t ( ((g45:p, "g45:p", 1, "n+g46:c+m") g50:c̲heck (((0 + g55:g̲rid 1) + g46:c) + (0 + g55:g̲rid 2))))) × ((g49:r̲ank (h, "h", 1, "h[m]")) g51:a̲t ( ((h, "h", 1, "m") g50:c̲heck (0 + g55:g̲rid 2)))))) } @)) } @) } @)
ᵉc̲orrelate< expands to
({ @ → g59:L ← t̲ally x g58:M ← t̲ally h g57:c ← (g58:M − 1) d̲iv 2 g56:p ← ((g58:M − 1) r̲eshape 0 × 1 t̲ake x) c̲at x c̲at (g58:M − 1) r̲eshape 0 × 1 t̲ake x _ ← { @ → g58:M ≤ g59:L ? @◆ "valid-mode" ⎕S̲IGNAL "valid: the kernel (" c̲at (f̲ormat g58:M) c̲at ") is longer than the signal (" c̲at (f̲ormat g59:L) c̲at ")" } @ ({ @ → g62:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g61: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 ")" } g60: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 } g63:grid ← ((g59:L − g58:M) + 1) g64:g̲rid ← { k → g63:grid r̲eshape (o̲ffsets k s̲elect g63:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g63:grid) r̲eshape 0 } g63:grid r̲eshape (({ @ → g65:grid ← ((g59:L − g58:M) + 1) c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..g58:M-1" } (1 + (g58:M − 1) − 0)) g66:g̲rid ← { k → g65:grid r̲eshape (o̲ffsets k s̲elect g65:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g65:grid) r̲eshape 0 } '+ r̲/₂ g65:grid r̲eshape (( ((g60:r̲ank (x, "x", 1, "x[n+m]")) g62:a̲t ( ((x, "x", 1, "n+m") g61:c̲heck ((0 + g66:g̲rid 1) + (0 + g66:g̲rid 2))))) × ((g60:r̲ank (h, "h", 1, "h[m]")) g62:a̲t ( ((h, "h", 1, "m") g61:c̲heck (0 + g66:g̲rid 2)))))) } @)) } @) } @)
ᵉ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}"))
ᵉ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}"))
ᵉc̲orrelate< expands to
({ @ → g84:L ← t̲ally t g83:M ← t̲ally third g82:c ← (g83:M − 1) d̲iv 2 g81:p ← ((g83:M − 1) r̲eshape 0 × 1 t̲ake t) c̲at t c̲at (g83:M − 1) r̲eshape 0 × 1 t̲ake t _ ← { @ → g83:M ≤ g84:L ? @◆ "valid-mode" ⎕S̲IGNAL "valid: the kernel (" c̲at (f̲ormat g83:M) c̲at ") is longer than the signal (" c̲at (f̲ormat g84:L) c̲at ")" } @ ({ @ → g87:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g86: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 ")" } g85: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 } g88:grid ← ((g84:L − g83:M) + 1) g89:g̲rid ← { k → g88:grid r̲eshape (o̲ffsets k s̲elect g88:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g88:grid) r̲eshape 0 } g88:grid r̲eshape (({ @ → g90:grid ← ((g84:L − g83:M) + 1) c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..g83:M-1" } (1 + (g83:M − 1) − 0)) g91:g̲rid ← { k → g90:grid r̲eshape (o̲ffsets k s̲elect g90:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g90:grid) r̲eshape 0 } '+ r̲/₂ g90:grid r̲eshape (( ((g85:r̲ank (t, "t", 1, "t[n+m]")) g87:a̲t ( ((t, "t", 1, "n+m") g86:c̲heck ((0 + g91:g̲rid 1) + (0 + g91:g̲rid 2))))) × ((g85:r̲ank (third, "third", 1, "third[m]")) g87:a̲t ( ((third, "third", 1, "m") g86:c̲heck (0 + g91:g̲rid 2)))))) } @)) } @) } @)
ᵉc̲orrelate< expands to
({ @ → g95:L ← t̲ally t g94:M ← t̲ally diff g93:c ← (g94:M − 1) d̲iv 2 g92:p ← ((g94:M − 1) r̲eshape 0 × 1 t̲ake t) c̲at t c̲at (g94:M − 1) r̲eshape 0 × 1 t̲ake t _ ← { @ → g94:M ≤ g95:L ? @◆ "valid-mode" ⎕S̲IGNAL "valid: the kernel (" c̲at (f̲ormat g94:M) c̲at ") is longer than the signal (" c̲at (f̲ormat g95:L) c̲at ")" } @ ({ @ → g98:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g97: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 ")" } g96: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 } g99:grid ← ((g95:L − g94:M) + 1) g100:g̲rid ← { k → g99:grid r̲eshape (o̲ffsets k s̲elect g99:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g99:grid) r̲eshape 0 } g99:grid r̲eshape (({ @ → g101:grid ← ((g95:L − g94:M) + 1) c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..g94:M-1" } (1 + (g94:M − 1) − 0)) g102:g̲rid ← { k → g101:grid r̲eshape (o̲ffsets k s̲elect g101:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g101:grid) r̲eshape 0 } '+ r̲/₂ g101:grid r̲eshape (( ((g96:r̲ank (t, "t", 1, "t[n+m]")) g98:a̲t ( ((t, "t", 1, "n+m") g97:c̲heck ((0 + g102:g̲rid 1) + (0 + g102:g̲rid 2))))) × ((g96:r̲ank (diff, "diff", 1, "diff[m]")) g98:a̲t ( ((diff, "diff", 1, "m") g97:c̲heck (0 + g102:g̲rid 2)))))) } @)) } @) } @)