sourcelibs/Equations/demos/symbols.xtl

1⍝!/usr/bin/env xetal 2⍝ Equations: an equation whose symbols a page can find. e:e_q< computes 3⍝ a dense layer, z = W x + b; e:t_exSymbols< gives its LaTeX with each 4⍝ symbol (z, W, x, b) in a class of its own, so a page can light up W 5⍝ in the equation beside the matrix W itself. In the live demo, hover 6⍝ over a symbol. Run with "just demo Equations symbols". 7"e:" u̲se< "Equations" 8W ← 2 3 r̲eshape 1 2 3 4 5 6 ⍝ weights: 2 outputs, 3 inputs 9x ← 1 0 -1 ⍝ the input 10b ← 10 20 ⍝ the bias 11z ← ᵉe̲q< z_{i} = \left(\sum_{j=0}^{2} W_{i,j} \, x_{j}\right) + b_{i}
ᵉe̲q< 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
  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 ({ 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 (W, "W", 2, "W[i,j]")) g3:a̲t ((
      ((W, "W", 2, "j") g2:c̲heck (0 + g7:g̲rid 2)) + (2 s̲elect s̲hape W) × 
      ((W, "W", 1, "i") g2:c̲heck (0 + g7:g̲rid 1))))) × 
    ((g1:r̲ank (x, "x", 1, "x[j]")) g3:a̲t (
      ((x, "x", 1, "j") g2:c̲heck (0 + g7:g̲rid 2))))))
} @) + 
    ((g1:r̲ank (b, "b", 1, "b[i]")) g3:a̲t (
      ((b, "b", 1, "i") g2:c̲heck (0 + g5:g̲rid 1))))))
} @))
12z ⍝ 8 18 13"$$" c̲at ("z[i] | 2" ᵉt̲exSymbols< "(sum j:=0..2 | W[i,j] * x[j]) + b[i]") c̲at "$$"
ᵉt̲exSymbols< expands to
("\\htmlClass{sym sym-z}{z_{i}} = \\left(\\sum_{j=0}^{2} \\htmlClass{sym sym-W}{W_{i,j}} \\, \\htmlClass{sym sym-x}{x_{j}}\\right) + \\htmlClass{sym sym-b}{b_{i}}")