sourcelibs/Integrals/src/Integrals.xtlm
1⍝│ Integrals: a definite integral written as a paper prints it, computed by a quadrature rule -- "integral tau:=a..b by trapezoid 64 | body"
2⍝│
3⍝│ Import it with an alias of your choice: "integ:" u_se< "Integrals".
4⍝│ Put libs/Integrals/src on XETAL_PATH ("just path"); the reference is libs/Integrals/docs.
5⍝│ Names with m: are macros; those under h: are private to this file.
6⍝│
7⍝│ A macro is a function from the source text written left and right of
8⍝│ its call to the source that replaces the call, before the program is
9⍝│ compiled (X_eTaL MC10); xetal expand FILE shows the result. The call
10⍝│ is X_eTaL's Saga 42 shape (its decisions 2 to 4, 2026-10-17): integral
11⍝│ read like sum, the rule and the grid as option words with a documented
12⍝│ default, improper integrals refused. Its reader uses all of its text
13⍝│ or refuses it. Decision 1 puts integral in X_eTaL-libraries' Equations
14⍝│ in the end; this library is the same call standing alone, lifted
15⍝│ whole when that lands (the body here is X_eTaL, there the equation
16⍝│ grammar).
17
18"t:" u̲se< "Strings"
19
20⍝━━ The macros
21
22⍝│ The value of a definite integral by a quadrature rule: "integral
23⍝│ tau:=a..b by trapezoid 64 | body". The variable and its bounds (any
24⍝│ X_eTaL giving a Float; a whole-number literal is made Float), then
25⍝│ the rule (trapezoid or simpson) and the grid (how many steps; simpson
26⍝│ needs an even number), then | and the body: X_eTaL over the variable,
27⍝│ which the macro binds to the grid's points as one vector, a, a + h,
28⍝│ ..., b, so the body is evaluated once for the whole grid. Without
29⍝│ "by" the rule is trapezoid 64. A bound of inf is refused: write a
30⍝│ truncation (0..20) and show the tail is small, or change variables.
31⍝│ Trapezoid is exact on lines, simpson on cubics; at a jump of the
32⍝│ body the sample takes the body's value at that point as written.
33⍝│ >> "integ:" u_se< "Integrals"
34⍝│ >> @ integ:i_ntegral< "integral x:=0..1 by simpson 2 | x ^ 2"
35⍝│ 0.3333333333333333
36ᵐi̲ntegral< ← { @ text → ʰw̲rite ʰr̲ead text }
37
38⍝│ The LaTeX of the same call, a string: \int_{a}^{b} body \, dx, the
39⍝│ bounds and the body as written (a Greek letter's name is \tau).
40⍝│ >> "integ:" u_se< "Integrals"
41⍝│ >> @ integ:t_ex< "integral tau:=0..1 | e_xp tau"
42⍝│ \int_{0}^{1} e_xp tau \, d\tau
43ᵐt̲ex< ← { @ text → "\"" c̲at (ʰl̲atex ʰr̲ead text) c̲at "\"" }
44
45⍝━━ Reading the call
46
47⍝│ The call's parts: (variable, low, high, rule, grid, body), each text,
48⍝│ every rule checked.
49ʰr̲ead ← { text →
50 t ← ᵗt̲rim text
51 _ ← (0 < t̲ally t) ʰr̲equire "an empty integral: write integral tau:=a..b | body"
52 bar ← ʰf̲irst "|" ʰo̲utside t
53 _ ← (0 < bar) ʰr̲equire "the integral, then | and the body: " c̲at t
54 head ← ᵗt̲rim (bar − 1) t̲ake t
55 body ← ᵗt̲rim bar d̲rop t
56 _ ← (0 < t̲ally body) ʰr̲equire "an empty body after |: " c̲at t
57 _ ← ("integral " ᵗp̲refix? head) ʰr̲equire "an integral starts with the word integral: " c̲at head
58 rest ← ᵗt̲rim 8 d̲rop head
59 at ← ʰf̲irst ":=" ʰo̲utside rest
60 _ ← (0 < at) ʰr̲equire "the variable and its bounds, tau:=a..b, not " c̲at rest
61 var ← ᵗt̲rim (at − 1) t̲ake rest
62 _ ← (ʰn̲ame? var) ʰr̲equire "the variable is a lowercase name: " c̲at var
63 after ← ᵗt̲rim (at + 1) d̲rop rest
64 by ← ʰf̲irst " by " ʰo̲utside after
65 range ← ᵗt̲rim by ʰu̲pTo after
66 opt ← ᵗt̲rim (by, 4) ʰp̲ast after
67 dots ← ʰf̲irst ".." ʰo̲utside range
68 _ ← (0 < dots) ʰr̲equire "the bounds are low..high: " c̲at range
69 low ← ᵗt̲rim (dots − 1) t̲ake range
70 high ← ᵗt̲rim (dots + 1) d̲rop range
71 _ ← ((0 < t̲ally low) ∧ 0 < t̲ally high) ʰr̲equire "a bound is missing: " c̲at range
72 _ ← (n̲ot (ʰi̲nfinite? low) ∨ ʰi̲nfinite? high) ʰr̲equire "an improper integral (a bound of inf): write a truncation, 0..20 say, and show the tail is small, or change variables so the bounds are finite: " c̲at range
73 (rule, n) ← ʰo̲ption opt
74 (var, ʰf̲loat low, ʰf̲loat high, rule, n, body)
75}
76
77⍝│ The rule and the grid from the text after "by" ("" for none, the
78⍝│ default trapezoid 64): exactly the two words, a rule the library has,
79⍝│ a whole number of steps, even for simpson.
80ʰo̲ption ← { opt →
81 0 = t̲ally opt ? ("trapezoid", "64")
82 ws ← ᵗw̲ords opt
83 _ ← (2 = t̲ally ws) ʰr̲equire "after by: the rule and the grid, trapezoid 64 or simpson 32, not " c̲at opt
84 rule ← d̲isclose f̲irst ws
85 n ← d̲isclose 2 s̲elect ws
86 _ ← ((rule m̲atch "trapezoid") ∨ rule m̲atch "simpson") ʰr̲equire "the rule is trapezoid or simpson, not " c̲at rule
87 _ ← ((0 < t̲ally n) ∧ '∧ r̲/ n m̲ember? ⎕D) ʰr̲equire "the grid is a whole number of steps: " c̲at n
88 k ← f̲irst f̲loor n̲umbers n
89 _ ← (0 < k) ʰr̲equire "the grid needs at least one step: " c̲at n
90 _ ← (n̲ot (rule m̲atch "simpson") ∧ 1 = k m̲od 2) ʰr̲equire "simpson needs an even number of steps, not " c̲at n
91 (rule, n)
92}
93
94⍝│ Whether a bound is written infinite (inf, -inf, infinity, oo).
95ʰi̲nfinite? ← { b → '∨ r̲/ '{ w → (d̲isclose w) m̲atch b } e̲ach ᵗw̲ords "inf -inf +inf infinity -infinity oo -oo" }
96
97⍝│ A bound as Float code: a whole-number literal gets f_loat, any other
98⍝│ expression stands as written.
99ʰf̲loat ← { b →
100 '∧ r̲/ b m̲ember? ⎕D c̲at "-" ? "f_loat " c̲at b
101 b
102}
103
104⍝━━ Writing the code
105
106⍝│ The expansion: the bounds, the step h, the variable bound to the
107⍝│ grid (n + 1 points), the rule's weights, and h times the weighted
108⍝│ sum of the body over the grid.
109ʰw̲rite ← { (var, low, high, rule, n, body) →
110 a ← ⎕G̲ENSYM "a"
111 b ← ⎕G̲ENSYM "b"
112 h ← ⎕G̲ENSYM "h"
113 w ← ⎕G̲ENSYM "w"
114 k ← f̲irst f̲loor n̲umbers n
115 "{ @ ->\n " c̲at a c̲at " := " c̲at low c̲at "\n " c̲at b c̲at " := " c̲at high c̲at "\n " c̲at h c̲at " := (" c̲at b c̲at " - " c̲at a c̲at ") / " c̲at n c̲at ".0\n " c̲at var c̲at " := " c̲at a c̲at " + " c̲at h c̲at " * f_loat o_ffsets " c̲at (f̲ormat k + 1) c̲at "\n " c̲at w c̲at " := " c̲at (rule ʰw̲eights k) c̲at "\n " c̲at h c̲at " * '+ r_/ " c̲at w c̲at " * " c̲at body c̲at "\n} @"
116}
117
118⍝│ The rule's weights over k steps (k + 1 points), as code: trapezoid
119⍝│ 1/2, 1, ..., 1, 1/2; simpson (1, 4, 2, 4, ..., 4, 1) / 3.
120ʰw̲eights ← { rule k →
121 rule m̲atch "simpson" ? "(1.0 c_at (" c̲at (f̲ormat k − 1) c̲at " r_eshape 4.0 2.0) c_at 1.0) / 3.0"
122 "0.5 c_at (" c̲at (f̲ormat k − 1) c̲at " r_eshape 1.0) c_at 0.5"
123}
124
125⍝━━ The same call as LaTeX
126
127⍝│ \int_{a}^{b} body \, dvar, the parts as written; a Greek letter's
128⍝│ name (tau, theta, ...) becomes its command.
129ʰl̲atex ← { (var, low, high, rule, n, body) → "\\\\int_{" c̲at (ʰp̲lain low) c̲at "}^{" c̲at (ʰp̲lain high) c̲at "} " c̲at body c̲at " \\\\, d" c̲at ʰg̲reek var }
130
131⍝│ A bound without the f_loat the reader added to a literal.
132ʰp̲lain ← { b →
133 "f_loat " ᵗp̲refix? b ? 7 d̲rop b
134 b
135}
136
137⍝│ A Greek letter's name as its LaTeX command; any other name as is.
138ʰg̲reek ← { v →
139 '∨ r̲/ '{ w → (d̲isclose w) m̲atch v } e̲ach ᵗw̲ords greek ? "\\\\" c̲at v
140 v
141}
142greek ← "alpha beta gamma delta epsilon theta lambda mu nu xi pi rho sigma tau phi chi psi omega"
143
144⍝━━ Text helpers
145
146⍝│ Nothing when ok holds; else the call is refused with message, at
147⍝│ its text (the right side).
148ʰr̲equire ← { ok message →
149 ok ? ""
150 "bad-integral right" ⎕R̲EJECT message
151}
152
153⍝│ Whether v is a lowercase name: a lowercase letter, then letters or digits.
154ʰn̲ame? ← { v →
155 0 = t̲ally v ? 0 = 1
156 ((f̲irst v) m̲ember? "abcdefghijklmnopqrstuvwxyz") ∧ '∧ r̲/ v m̲ember? "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
157}
158
159⍝│ 1 at each character of s inside a string literal (its quotes included).
160ʰq̲uoted ← { s → ('+ s̲\ 0 + s m̲ember? "\"") m̲od 2 }
161
162⍝│ The positions in s where sep starts outside brackets, braces,
163⍝│ parentheses and strings.
164ʰo̲utside ← { sep s →
165 q ← ʰq̲uoted s
166 d ← '+ s̲\ ((0 + s m̲ember? "([{") − 0 + s m̲ember? ")]}") × 1 − q
167 ps ← sep ᵗf̲ind s
168 (((ps s̲elect d) = 0) ∧ 0 = ps s̲elect q) r̲eplicate ps
169}
170
171⍝│ The first of the positions ps, or 0 for none.
172ʰf̲irst ← { ps →
173 0 = t̲ally ps ? 0
174 f̲irst ps
175}
176
177⍝│ s before position k (all of s when k is 0).
178ʰu̲pTo ← { k s →
179 0 = k ? s
180 (k − 1) t̲ake s
181}
182
183⍝│ s after a separator of length m at position k ("" when k is 0).
184ʰp̲ast ← { (k, m) s →
185 0 = k ? ""
186 ((k + m) − 1) d̲rop s
187}