sourcelibs/Integrals/demos/convolution.xtl

1⍝!/usr/bin/env xetal 2⍝ Integrals: the continuous convolution of two unit boxes, as a paper 3⍝ writes it, (f * g)(t) = integral over tau of f(tau) g(t - tau): the 4⍝ triangle, drawn as a strip of its values; then one integral and its 5⍝ LaTeX. Run with "just demo Integrals"; "just expand-lib Integrals 6⍝ convolution" shows what each call became. 7"integ:" u̲se< "Integrals" 8⍝ A unit box: 1 on 0..1, 0 elsewhere. 9ᵘb̲ox ← { x → f̲loat (x ≥ 0.0) ∧ x ≤ 1.0 } 10⍝ The convolution at one t: the integral over tau, 128 steps. 11ᵘc̲onv ← { t → @ ⁱⁿᵗᵉᵍi̲ntegral< "integral tau:=0..2 by trapezoid 128 | (u:b_ox tau) * u:b_ox t - tau" }
ⁱⁿᵗᵉᵍi̲ntegral< expands to
{ @ →
  g1:a ← f̲loat 0
  g2:b ← f̲loat 2
  g3:h ← (g2:b − g1:a) ÷ 128.0
  tau ← g1:a + g3:h × f̲loat o̲ffsets 129
  g4:w ← 0.5 c̲at (127 r̲eshape 1.0) c̲at 0.5
  g3:h × '+ r̲/ g4:w × (ᵘb̲ox tau) × ᵘb̲ox t − tau
} @
12ts ← 0.125 × f̲loat o̲ffsets 17 13(f̲loat f̲loor 0.5 + 1000.0 × 'ᵘc̲onv e̲ach ts) ÷ 1000.0 14⍝ The triangle it converges to: t up to 1, then 2 - t. 15(ts × f̲loat ts ≤ 1.0) + (2.0 − ts) × f̲loat ts > 1.0 16⍝ What the paper writes, for a page that draws math. 17@ ⁱⁿᵗᵉᵍt̲ex< "integral tau:=0..2 | (u:b_ox tau) * u:b_ox t - tau"
ⁱⁿᵗᵉᵍt̲ex< expands to
"\\int_{0}^{2} (u:b_ox tau) * u:b_ox t - tau \\, d\\tau"
18⍝ The area under e^x from 0 to 1 is e - 1, by simpson on 16 steps and exactly. 19@ ⁱⁿᵗᵉᵍi̲ntegral< "integral x:=0..1 by simpson 16 | e_xp x"
ⁱⁿᵗᵉᵍi̲ntegral< expands to
{ @ →
  g5:a ← f̲loat 0
  g6:b ← f̲loat 1
  g7:h ← (g6:b − g5:a) ÷ 16.0
  x ← g5:a + g7:h × f̲loat o̲ffsets 17
  g8:w ← (1.0 c̲at (15 r̲eshape 4.0 2.0) c̲at 1.0) ÷ 3.0
  g7:h × '+ r̲/ g8:w × e̲xp x
} @
20(e̲xp 1.0) − 1.0 21@ ⁱⁿᵗᵉᵍt̲ex< "integral x:=0..1 by simpson 16 | e_xp x"
ⁱⁿᵗᵉᵍt̲ex< expands to
"\\int_{0}^{1} e_xp x \\, dx"