sourcelibs/Equations/demos/catalog.xtl
1⍝!/usr/bin/env xetal
2⍝│ Thirteen equations as ML papers print them, each one call: its LaTeX (a
3⍝│ line between $$ is drawn as math in the live demo), its value, and
4⍝│ 1 when the value equals the same computation written as plain
5⍝│ X_eTaL. Run with "just demo Equations catalog".
6"e:" u̲se< "Equations"
7
8⍝━━ A matrix product: C[i,k] = sum_j A[i,j] B[j,k]
9A ← f̲loat 2 3 r̲eshape 1 2 3 4 5 6
10B ← f̲loat 3 2 r̲eshape 1 0 0 1 1 1
11(C, tex1) ← ᵉb̲oth< C_{i,k} = \sum_{j=0}^{2} A_{i,j} \, B_{j,k}
12"$$" c̲at tex1 c̲at "$$"
13C
14C m̲atch A '+ '× i̲nner B
15
16⍝━━ A dense layer with its bias: z[i] = sum_j W[i,j] x[j] + b[i]
17W ← f̲loat 2 3 r̲eshape 1 2 3 4 5 6
18x ← f̲loat 1 1 1
19b ← f̲loat 10 20
20(z, tex2) ← ᵉb̲oth< z_{i} = \left(\sum_{j=0}^{2} W_{i,j} \, x_{j}\right) + b_{i}
21"$$" c̲at tex2 c̲at "$$"
22z
23z m̲atch (W '+ '× i̲nner x) + b
24
25⍝━━ Attention scores and output: S = Q K^T / sqrt(d), O = S V
26Q ← f̲loat 2 4 r̲eshape 1 0 0 0 0 1 0 0
27K ← f̲loat 2 4 r̲eshape 2 0 0 0 0 2 0 0
28V ← f̲loat 2 3 r̲eshape 1 2 3 4 5 6
29(S, tex3) ← ᵉb̲oth< S_{i,j} = \frac{\sum_{d=0}^{3} Q_{i,d} \, K_{j,d}}{\sqrt{4}}
30"$$" c̲at tex3 c̲at "$$"
31S
32S m̲atch (Q '+ '× i̲nner 2 1 t̲ranspose K) ÷ 2.0
33(O, tex4) ← ᵉb̲oth< O_{i,k} = \sum_{j=0}^{1} S_{i,j} \, V_{j,k}
34"$$" c̲at tex4 c̲at "$$"
35O
36O m̲atch S '+ '× i̲nner V
37
38⍝━━ Softmax: p[i] = exp(z[i]) / sum_j exp(z[j])
39logits ← f̲loat 1 2 3
40(p, tex5) ← ᵉb̲oth< p_{i} = \frac{\exp(logits_{i})}{\sum_{j=0}^{2} \exp(logits_{j})}
41"$$" c̲at tex5 c̲at "$$"
42p
43p m̲atch (e̲xp logits) ÷ '+ r̲/ e̲xp logits
44
45⍝━━ Cross-entropy: L = -sum_i y[i] log(p[i])
46y ← f̲loat 0 0 1
47(ce, tex6) ← ᵉb̲oth< L = -\left(\sum_{i=0}^{2} y_{i} \, \log(p_{i})\right)
48"$$" c̲at tex6 c̲at "$$"
49ce
50ce m̲atch 0 − '+ r̲/ y × l̲og p
51
52⍝━━ Mean squared error: L = (1/n) sum_i (y[i] - p[i])^2
53yy ← f̲loat 1 2 3 4
54pp ← 1.5 2 2 4
55(mse, tex7) ← ᵉb̲oth< L = \frac{\sum_{i=0}^{3} \left(yy_{i} - pp_{i}\right)^{2}}{4}
56"$$" c̲at tex7 c̲at "$$"
57mse
58mse m̲atch ('+ r̲/ (yy − pp) ^ 2) ÷ 4.0
59
60⍝━━ Layer norm's mean and variance: mu = (1/n) sum_i x[i], var = (1/n) sum_i (x[i] - mu)^2
61h ← f̲loat 1 2 3 4
62(mu, tex8) ← ᵉb̲oth< \mu = \frac{\sum_{i=0}^{3} h_{i}}{4}
63"$$" c̲at tex8 c̲at "$$"
64(var, tex9) ← ᵉb̲oth< var = \frac{\sum_{i=0}^{3} \left(h_{i} - \mu\right)^{2}}{4}
65"$$" c̲at tex9 c̲at "$$"
66mu c̲at var
67(mu c̲at var) m̲atch 2.5 1.25
68
69⍝━━ Cosine similarity: cos = sum_i a[i] b[i] / (sqrt(sum_i a[i]^2) sqrt(sum_i b[i]^2))
70a ← f̲loat 1 0 1
71bb ← f̲loat 1 1 0
72(cosine, tex10) ← ᵉb̲oth< c = \frac{\sum_{i=0}^{2} a_{i} \, bb_{i}}{\sqrt{\sum_{i=0}^{2} a_{i}^{2}} \, \sqrt{\sum_{i=0}^{2} bb_{i}^{2}}}
73"$$" c̲at tex10 c̲at "$$"
74cosine
75cosine m̲atch ('+ r̲/ a × bb) ÷ (('+ r̲/ a ^ 2) ^ 0.5) × ('+ r̲/ bb ^ 2) ^ 0.5
76
77⍝━━ An L2 penalty: R = lambda sum_i w[i]^2
78weights ← f̲loat 1 -2 3
79(l2, tex11) ← ᵉb̲oth< R = 0.5 \, \left(\sum_{i=0}^{2} weights_{i}^{2}\right)
80"$$" c̲at tex11 c̲at "$$"
81l2
82l2 m̲atch 0.5 × '+ r̲/ weights ^ 2
83
84⍝━━ Max pooling, 2 by 2 windows: P[o,h,w] = max_r max_s X[o,2h+r,2w+s]
85X ← 1 4 4 r̲eshape r̲ange 16
86(P, tex12) ← ᵉb̲oth< P_{o,h,w} = \max_{r=0}^{1} \max_{s=0}^{1} X_{o,2\,h+r,2\,w+s}
87"$$" c̲at tex12 c̲at "$$"
88P
89P m̲atch 1 2 2 r̲eshape 6 8 14 16
90
91⍝━━ A 1-D convolution: y[n] = sum_m x[n-m] h[m], valid mode (the layer's cross-correlation is the kernel unreversed)
92xs ← 1 2 3 4
93hk ← 1 2 3
94(M, L) ← (3, 4)
95(yc, tex13) ← ᵉb̲oth< y_{n} = \sum_{m=0}^{M - 1} xs_{n+M-1-m} \, hk_{m}
96"$$" c̲at tex13 c̲at "$$"
97yc
98yc m̲atch ᵉc̲onvolve< y_{n} = \sum_{m} xs_{n - m} \, hk_{m} \quad \text{(valid)}
99yc m̲atch ((r̲ev hk) '+ '× i̲nner 3 t̲ake xs) c̲at (r̲ev hk) '+ '× i̲nner 1 d̲rop xs
100
101⍝━━ GELU: x Phi(x), Phi(x) = 1/2 + integral_0^x exp(-u^2/2) / sqrt(2 pi) du (Hendrycks and Gimpel, 2016)
102pi ← 3.141592653589793
103ᵘg̲elu ← { x → x × ᵉe̲q< \Phi = \frac{1}{2} + \left(\int_{0}^{x} \frac{\exp(-\frac{u^{2}}{2})}{\sqrt{2 \, \pi}} \, du\right) }
104tex14 ← "Phi[] | " ᵉt̲ex< "1 / 2 + (integral u:=0..x | exp(-(u^2) / 2) / sqrt(2 * pi))"
105"$$" c̲at tex14 c̲at "$$"
106gelu ← 'ᵘg̲elu e̲ach -2.0 -1.0 0.0 1.0 2.0
107(f̲loat f̲loor 0.5 + 10000.0 × gelu) ÷ 10000.0
108⍝│ The tanh approximation the frameworks compute agrees to 5e-4 (its largest error over -4..4 is 0.000473).
109ᵘt̲anh ← { z → ((e̲xp z) − e̲xp n̲eg z) ÷ (e̲xp z) + e̲xp n̲eg z }
110ᵘa̲pprox ← { x → 0.5 × x × 1.0 + ᵘt̲anh ((2.0 ÷ pi) ^ 0.5) × x + 0.044715 × x ^ 3 }
111'∧ r̲/ 0.0005 > a̲bs gelu − ᵘa̲pprox -2.0 -1.0 0.0 1.0 2.0
112
113⍝━━ KL divergence of two Gaussians: KL(p || q) = integral p(x) log(p(x) / q(x)) dx, against its closed form
114(m1, s1, m2, s2) ← (0.0, 1.0, 1.0, 2.0)
115(kl, tex15) ← ᵉb̲oth< KL = \int_{-10}^{10} \left(\frac{\exp(-\frac{\left(x - m1\right)^{2}}{2 \, s1^{2}})}{s1 \, \sqrt{2 \, \pi}}\right) \, \log(\frac{\frac{\exp(-\frac{\left(x - m1\right)^{2}}{2 \, s1^{2}})}{s1 \, \sqrt{2 \, \pi}}}{\frac{\exp(-\frac{\left(x - m2\right)^{2}}{2 \, s2^{2}})}{s2 \, \sqrt{2 \, \pi}}}) \, dx
116"$$" c̲at tex15 c̲at "$$"
117(f̲loat f̲loor 0.5 + 1000000.0 × kl) ÷ 1000000.0
118closed ← (l̲og s2 ÷ s1) + ((s1² + (m1 − m2)²) ÷ 2.0 × s2²) − 0.5
1190.000001 > a̲bs kl − closed
ᵉb̲oth< expands to
((({ @ → g3:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g2: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 ")" } g1: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 } g4:grid ← 2 c̲at 2 g5:g̲rid ← { k → g4:grid r̲eshape (o̲ffsets k s̲elect g4:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g4:grid) r̲eshape 0 } g4:grid r̲eshape (({ @ → g6:grid ← 2 c̲at 2 c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0)) g7:g̲rid ← { k → g6:grid r̲eshape (o̲ffsets k s̲elect g6:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g6:grid) r̲eshape 0 } '+ r̲/₃ g6:grid r̲eshape (( ((g1:r̲ank (A, "A", 2, "A[i,j]")) g3:a̲t (( ((A, "A", 2, "j") g2:c̲heck (0 + g7:g̲rid 3)) + (2 s̲elect s̲hape A) × ((A, "A", 1, "i") g2:c̲heck (0 + g7:g̲rid 1))))) × ((g1:r̲ank (B, "B", 2, "B[j,k]")) g3:a̲t (( ((B, "B", 2, "k") g2:c̲heck (0 + g7:g̲rid 2)) + (2 s̲elect s̲hape B) × ((B, "B", 1, "j") g2:c̲heck (0 + g7:g̲rid 3))))))) } @)) } @), "C_{i,k} = \\sum_{j=0}^{2} A_{i,j} \\, B_{j,k}"))
ᵉb̲oth< expands to
((({ @ → g10:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g9: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 ")" } g8: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 } g11:grid ← 2 g12:g̲rid ← { k → g11:grid r̲eshape (o̲ffsets k s̲elect g11:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g11:grid) r̲eshape 0 } g11:grid r̲eshape ((({ @ → g13:grid ← 2 c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0)) g14:g̲rid ← { k → g13:grid r̲eshape (o̲ffsets k s̲elect g13:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g13:grid) r̲eshape 0 } '+ r̲/₂ g13:grid r̲eshape (( ((g8:r̲ank (W, "W", 2, "W[i,j]")) g10:a̲t (( ((W, "W", 2, "j") g9:c̲heck (0 + g14:g̲rid 2)) + (2 s̲elect s̲hape W) × ((W, "W", 1, "i") g9:c̲heck (0 + g14:g̲rid 1))))) × ((g8:r̲ank (x, "x", 1, "x[j]")) g10:a̲t ( ((x, "x", 1, "j") g9:c̲heck (0 + g14:g̲rid 2)))))) } @) + ((g8:r̲ank (b, "b", 1, "b[i]")) g10:a̲t ( ((b, "b", 1, "i") g9:c̲heck (0 + g12:g̲rid 1)))))) } @), "z_{i} = \\left(\\sum_{j=0}^{2} W_{i,j} \\, x_{j}\\right) + b_{i}"))
ᵉb̲oth< expands to
((({ @ → g17:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g16: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 ")" } g15: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 } g18:grid ← 2 c̲at 2 g19:g̲rid ← { k → g18:grid r̲eshape (o̲ffsets k s̲elect g18:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g18:grid) r̲eshape 0 } g18:grid r̲eshape ((({ @ → g20:grid ← 2 c̲at 2 c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..3" } (1 + 3 − 0)) g21:g̲rid ← { k → g20:grid r̲eshape (o̲ffsets k s̲elect g20:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g20:grid) r̲eshape 0 } '+ r̲/₃ g20:grid r̲eshape (( ((g15:r̲ank (Q, "Q", 2, "Q[i,d]")) g17:a̲t (( ((Q, "Q", 2, "d") g16:c̲heck (0 + g21:g̲rid 3)) + (2 s̲elect s̲hape Q) × ((Q, "Q", 1, "i") g16:c̲heck (0 + g21:g̲rid 1))))) × ((g15:r̲ank (K, "K", 2, "K[j,d]")) g17:a̲t (( ((K, "K", 2, "d") g16:c̲heck (0 + g21:g̲rid 3)) + (2 s̲elect s̲hape K) × ((K, "K", 1, "j") g16:c̲heck (0 + g21:g̲rid 2))))))) } @) ÷ (4 ^ 0.5))) } @), "S_{i,j} = \\frac{\\sum_{d=0}^{3} Q_{i,d} \\, K_{j,d}}{\\sqrt{4}}"))
ᵉb̲oth< expands to
((({ @ → g24:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g23: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 ")" } g22: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 } g25:grid ← 2 c̲at 3 g26:g̲rid ← { k → g25:grid r̲eshape (o̲ffsets k s̲elect g25:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g25:grid) r̲eshape 0 } g25:grid r̲eshape (({ @ → g27:grid ← 2 c̲at 3 c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..1" } (1 + 1 − 0)) g28:g̲rid ← { k → g27:grid r̲eshape (o̲ffsets k s̲elect g27:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g27:grid) r̲eshape 0 } '+ r̲/₃ g27:grid r̲eshape (( ((g22:r̲ank (S, "S", 2, "S[i,j]")) g24:a̲t (( ((S, "S", 2, "j") g23:c̲heck (0 + g28:g̲rid 3)) + (2 s̲elect s̲hape S) × ((S, "S", 1, "i") g23:c̲heck (0 + g28:g̲rid 1))))) × ((g22:r̲ank (V, "V", 2, "V[j,k]")) g24:a̲t (( ((V, "V", 2, "k") g23:c̲heck (0 + g28:g̲rid 2)) + (2 s̲elect s̲hape V) × ((V, "V", 1, "j") g23:c̲heck (0 + g28:g̲rid 3))))))) } @)) } @), "O_{i,k} = \\sum_{j=0}^{1} S_{i,j} \\, V_{j,k}"))
ᵉb̲oth< expands to
((({ @ → g31:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g30: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 ")" } g29: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 } g32:grid ← 3 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 } g32:grid r̲eshape (((e̲xp ((g29:r̲ank (logits, "logits", 1, "logits[i]")) g31:a̲t ( ((logits, "logits", 1, "i") g30:c̲heck (0 + g33:g̲rid 1))))) ÷ ({ @ → g34:grid ← 3 c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0)) g35:g̲rid ← { k → g34:grid r̲eshape (o̲ffsets k s̲elect g34:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g34:grid) r̲eshape 0 } '+ r̲/₂ g34:grid r̲eshape ((e̲xp ((g29:r̲ank (logits, "logits", 1, "logits[j]")) g31:a̲t ( ((logits, "logits", 1, "j") g30:c̲heck (0 + g35:g̲rid 2)))))) } @))) } @), "p_{i} = \\frac{\\exp(logits_{i})}{\\sum_{j=0}^{2} \\exp(logits_{j})}"))
ᵉb̲oth< expands to
((({ @ → g38:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g37: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 ")" } g36: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 } ((0 − ({ @ → g41:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0)) 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 } '+ r̲/₁ g41:grid r̲eshape (( ((g36:r̲ank (y, "y", 1, "y[i]")) g38:a̲t ( ((y, "y", 1, "i") g37:c̲heck (0 + g42:g̲rid 1)))) × (l̲og ((g36:r̲ank (p, "p", 1, "p[i]")) g38:a̲t ( ((p, "p", 1, "i") g37:c̲heck (0 + g42:g̲rid 1))))))) } @))) } @), "L = -\\left(\\sum_{i=0}^{2} y_{i} \\, \\log(p_{i})\\right)"))
ᵉb̲oth< expands to
((({ @ → g45:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g44: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 ")" } g43: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 } ((({ @ → g48:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..3" } (1 + 3 − 0)) g49:g̲rid ← { k → g48:grid r̲eshape (o̲ffsets k s̲elect g48:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g48:grid) r̲eshape 0 } '+ r̲/₁ g48:grid r̲eshape (((( ((g43:r̲ank (yy, "yy", 1, "yy[i]")) g45:a̲t ( ((yy, "yy", 1, "i") g44:c̲heck (0 + g49:g̲rid 1)))) − ((g43:r̲ank (pp, "pp", 1, "pp[i]")) g45:a̲t ( ((pp, "pp", 1, "i") g44:c̲heck (0 + g49:g̲rid 1)))))) ^ 2)) } @) ÷ 4)) } @), "L = \\frac{\\sum_{i=0}^{3} \\left(yy_{i} - pp_{i}\\right)^{2}}{4}"))
ᵉb̲oth< expands to
((({ @ → g52:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g51: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 ")" } g50: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 } ((({ @ → g55:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..3" } (1 + 3 − 0)) g56:g̲rid ← { k → g55:grid r̲eshape (o̲ffsets k s̲elect g55:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g55:grid) r̲eshape 0 } '+ r̲/₁ g55:grid r̲eshape ( ((g50:r̲ank (h, "h", 1, "h[i]")) g52:a̲t ( ((h, "h", 1, "i") g51:c̲heck (0 + g56:g̲rid 1))))) } @) ÷ 4)) } @), "\\mu = \\frac{\\sum_{i=0}^{3} h_{i}}{4}"))
ᵉb̲oth< expands to
((({ @ → g59:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g58: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 ")" } g57: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 } ((({ @ → g62:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..3" } (1 + 3 − 0)) g63:g̲rid ← { k → g62:grid r̲eshape (o̲ffsets k s̲elect g62:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g62:grid) r̲eshape 0 } '+ r̲/₁ g62:grid r̲eshape (((( ((g57:r̲ank (h, "h", 1, "h[i]")) g59:a̲t ( ((h, "h", 1, "i") g58:c̲heck (0 + g63:g̲rid 1)))) − mu)) ^ 2)) } @) ÷ 4)) } @), "var = \\frac{\\sum_{i=0}^{3} \\left(h_{i} - \\mu\\right)^{2}}{4}"))
ᵉb̲oth< expands to
((({ @ → g66:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g65: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 ")" } g64: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 } ((({ @ → g69:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0)) g70:g̲rid ← { k → g69:grid r̲eshape (o̲ffsets k s̲elect g69:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g69:grid) r̲eshape 0 } '+ r̲/₁ g69:grid r̲eshape (( ((g64:r̲ank (a, "a", 1, "a[i]")) g66:a̲t ( ((a, "a", 1, "i") g65:c̲heck (0 + g70:g̲rid 1)))) × ((g64:r̲ank (bb, "bb", 1, "bb[i]")) g66:a̲t ( ((bb, "bb", 1, "i") g65:c̲heck (0 + g70:g̲rid 1)))))) } @) ÷ (((({ @ → g71:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0)) g72:g̲rid ← { k → g71:grid r̲eshape (o̲ffsets k s̲elect g71:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g71:grid) r̲eshape 0 } '+ r̲/₁ g71:grid r̲eshape (( ((g64:r̲ank (a, "a", 1, "a[i]")) g66:a̲t ( ((a, "a", 1, "i") g65:c̲heck (0 + g72:g̲rid 1)))) ^ 2)) } @) ^ 0.5) × (({ @ → g73:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0)) g74:g̲rid ← { k → g73:grid r̲eshape (o̲ffsets k s̲elect g73:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g73:grid) r̲eshape 0 } '+ r̲/₁ g73:grid r̲eshape (( ((g64:r̲ank (bb, "bb", 1, "bb[i]")) g66:a̲t ( ((bb, "bb", 1, "i") g65:c̲heck (0 + g74:g̲rid 1)))) ^ 2)) } @) ^ 0.5))))) } @), "c = \\frac{\\sum_{i=0}^{2} a_{i} \\, bb_{i}}{\\sqrt{\\sum_{i=0}^{2} a_{i}^{2}} \\, \\sqrt{\\sum_{i=0}^{2} bb_{i}^{2}}}"))
ᵉb̲oth< expands to
((({ @ → g77:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g76: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 ")" } g75: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 } ((0.5 × ({ @ → g80:grid ← ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..2" } (1 + 2 − 0)) g81:g̲rid ← { k → g80:grid r̲eshape (o̲ffsets k s̲elect g80:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g80:grid) r̲eshape 0 } '+ r̲/₁ g80:grid r̲eshape (( ((g75:r̲ank (weights, "weights", 1, "weights[i]")) g77:a̲t ( ((weights, "weights", 1, "i") g76:c̲heck (0 + g81:g̲rid 1)))) ^ 2)) } @))) } @), "R = 0.5 \\, \\left(\\sum_{i=0}^{2} weights_{i}^{2}\\right)"))
ᵉb̲oth< expands to
((({ @ → g84:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g83: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 ")" } g82: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 } g85:grid ← 1 c̲at 2 c̲at 2 g86:g̲rid ← { k → g85:grid r̲eshape (o̲ffsets k s̲elect g85:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g85:grid) r̲eshape 0 } g85:grid r̲eshape (({ @ → g87:grid ← 1 c̲at 2 c̲at 2 c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "max's range runs low..high: 0..1" } (1 + 1 − 0)) c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "max's range runs low..high: 0..1" } (1 + 1 − 0)) g88:g̲rid ← { k → g87:grid r̲eshape (o̲ffsets k s̲elect g87:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g87:grid) r̲eshape 0 } 'm̲ax r̲/₄ 'm̲ax r̲/₅ g87:grid r̲eshape ( ((g82:r̲ank (X, "X", 3, "X[o,2*h+r,2*w+s]")) g84:a̲t (( ((X, "X", 3, "2*w+s") g83:c̲heck ((2 × (0 + g88:g̲rid 3)) + (0 + g88:g̲rid 5))) + (3 s̲elect s̲hape X) × ( ((X, "X", 2, "2*h+r") g83:c̲heck ((2 × (0 + g88:g̲rid 2)) + (0 + g88:g̲rid 4))) + (2 s̲elect s̲hape X) × ((X, "X", 1, "o") g83:c̲heck (0 + g88:g̲rid 1))))))) } @)) } @), "P_{o,h,w} = \\max_{r=0}^{1} \\max_{s=0}^{1} X_{o,2\\,h+r,2\\,w+s}"))
ᵉb̲oth< expands to
((({ @ → g91:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g90: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 ")" } g89: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 } g92:grid ← ((L − M) + 1) g93:g̲rid ← { k → g92:grid r̲eshape (o̲ffsets k s̲elect g92:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g92:grid) r̲eshape 0 } g92:grid r̲eshape (({ @ → g94: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)) g95:g̲rid ← { k → g94:grid r̲eshape (o̲ffsets k s̲elect g94:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g94:grid) r̲eshape 0 } '+ r̲/₂ g94:grid r̲eshape (( ((g89:r̲ank (xs, "xs", 1, "xs[n+M-1-m]")) g91:a̲t ( ((xs, "xs", 1, "n+M-1-m") g90:c̲heck ((((0 + g95:g̲rid 1) + M) − 1) − (0 + g95:g̲rid 2))))) × ((g89:r̲ank (hk, "hk", 1, "hk[m]")) g91:a̲t ( ((hk, "hk", 1, "m") g90:c̲heck (0 + g95:g̲rid 2)))))) } @)) } @), "y_{n} = \\sum_{m=0}^{M - 1} xs_{n+M-1-m} \\, hk_{m}"))
ᵉc̲onvolve< expands to
(({ @ → g99:L ← t̲ally xs g98:M ← t̲ally hk g97:c ← (g98:M − 1) d̲iv 2 g96:p ← ((g98:M − 1) r̲eshape 0 × 1 t̲ake xs) c̲at xs c̲at (g98:M − 1) r̲eshape 0 × 1 t̲ake xs _ ← { @ → g98:M ≤ g99:L ? @◆ "valid-mode" ⎕S̲IGNAL "valid: the kernel (" c̲at (f̲ormat g98:M) c̲at ") is longer than the signal (" c̲at (f̲ormat g99:L) c̲at ")" } @ ({ @ → g102:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g101: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 ")" } g100: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 } g103:grid ← ((g99:L − g98:M) + 1) g104:g̲rid ← { k → g103:grid r̲eshape (o̲ffsets k s̲elect g103:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g103:grid) r̲eshape 0 } g103:grid r̲eshape (({ @ → g105:grid ← ((g99:L − g98:M) + 1) c̲at ({ v → (0 ≤ v) ? v◆ "bad-equation" ⎕S̲IGNAL "sum's range runs low..high: 0..g98:M-1" } (1 + (g98:M − 1) − 0)) g106:g̲rid ← { k → g105:grid r̲eshape (o̲ffsets k s̲elect g105:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g105:grid) r̲eshape 0 } '+ r̲/₂ g105:grid r̲eshape (( ((g100:r̲ank (xs, "xs", 1, "xs[n+g98:M-1-m]")) g102:a̲t ( ((xs, "xs", 1, "n+g98:M-1-m") g101:c̲heck ((((0 + g106:g̲rid 1) + g98:M) − 1) − (0 + g106:g̲rid 2))))) × ((g100:r̲ank (hk, "hk", 1, "hk[m]")) g102:a̲t ( ((hk, "hk", 1, "m") g101:c̲heck (0 + g106:g̲rid 2)))))) } @)) } @) } @))
ᵉe̲q< expands to
(({ @ → g109:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g108: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 ")" } g107: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 } (((1 ÷ 2) + ({ @ → g115:a ← f̲loat (0) g114:b ← f̲loat (x) g113:h ← (g114:b − g115:a) ÷ 32.0 g112:w ← g113:h × (1.0 c̲at (31 r̲eshape 4.0 2.0) c̲at 1.0) ÷ 3.0 g116:grid ← (33) g117:g̲rid ← { k → g116:grid r̲eshape (o̲ffsets k s̲elect g116:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g116:grid) r̲eshape 0 } '+ r̲/₁ g116:grid r̲eshape ((1 + g117:g̲rid 1) s̲elect g112:w) × (((e̲xp (0 − ((((g115:a + g113:h × f̲loat g117:g̲rid 1) ^ 2)) ÷ 2))) ÷ ((2 × pi) ^ 0.5))) } @))) } @))
ᵉt̲ex< expands to
("\\Phi = \\frac{1}{2} + \\left(\\int_{0}^{x} \\frac{\\exp(-\\frac{u^{2}}{2})}{\\sqrt{2 \\, \\pi}} \\, du\\right)")
ᵉb̲oth< expands to
((({ @ → g120:a̲t ← { A i → (1 + i) s̲elect r̲avel A } g119: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 ")" } g118: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 } (({ @ → g126:a ← f̲loat ((0 − 10)) g125:b ← f̲loat (10) g124:h ← (g125:b − g126:a) ÷ 400.0 g123:w ← g124:h × (1.0 c̲at (399 r̲eshape 4.0 2.0) c̲at 1.0) ÷ 3.0 g127:grid ← (401) g128:g̲rid ← { k → g127:grid r̲eshape (o̲ffsets k s̲elect g127:grid) 'l̲eft t̲able ('× r̲/ k d̲rop g127:grid) r̲eshape 0 } '+ r̲/₁ g127:grid r̲eshape ((1 + g128:g̲rid 1) s̲elect g123:w) × (((((e̲xp (0 − ((((((g126:a + g124:h × f̲loat g128:g̲rid 1) − m1)) ^ 2)) ÷ ((2 × (s1 ^ 2)))))) ÷ ((s1 × ((2 × pi) ^ 0.5))))) × (l̲og ((((e̲xp (0 − ((((((g126:a + g124:h × f̲loat g128:g̲rid 1) − m1)) ^ 2)) ÷ ((2 × (s1 ^ 2)))))) ÷ ((s1 × ((2 × pi) ^ 0.5))))) ÷ (((e̲xp (0 − ((((((g126:a + g124:h × f̲loat g128:g̲rid 1) − m2)) ^ 2)) ÷ ((2 × (s2 ^ 2)))))) ÷ ((s2 × ((2 × pi) ^ 0.5))))))))) } @)) } @), "KL = \\int_{-10}^{10} \\left(\\frac{\\exp(-\\frac{\\left(x - m1\\right)^{2}}{2 \\, s1^{2}})}{s1 \\, \\sqrt{2 \\, \\pi}}\\right) \\, \\log(\\frac{\\frac{\\exp(-\\frac{\\left(x - m1\\right)^{2}}{2 \\, s1^{2}})}{s1 \\, \\sqrt{2 \\, \\pi}}}{\\frac{\\exp(-\\frac{\\left(x - m2\\right)^{2}}{2 \\, s2^{2}})}{s2 \\, \\sqrt{2 \\, \\pi}}}) \\, dx"))