programdemos/mandelbrot/mandelbrot.xtl
The Mandelbrot set as arrays: every point c of a view iterated at once, z becoming z * z + c, counting the steps each point stays within 2 of the origin. X_eTaL has no complex numbers yet (an ask: see the demo's README Workarounds), so z and c are each two Float arrays, their real and imaginary parts. Run it with "xetal run demos/mandelbrot/mandelbrot.xtl"; it prints the shapes of c and of the stepped state, the set drawn with characters, and one point's orbit (its real parts, then its imaginary parts). The live page steps k and lets you click a point for its orbit: https://softwarewrighter.github.io/X_eTaL-demos/mandelbrot/
The view
rows : Int
rows, cols: the picture's size. cx, cy, w: the view's center and width (in the plane, not pixels). k: how many steps. aspect: how tall a cell is for its width (2.0, since a character is about twice as tall as it is wide; a page of square pixels uses 1.0).
rows ← 24
ᵘc̲entred : Int -> Float
Numbers centered on 0 (u:c_entred n): 0 1 2 3 becomes -1.5 -0.5 0.5 1.5.
ᵘc̲entred ← { n → (f̲loat o̲ffsets n) − (f̲loat n − 1) ÷ 2 }
c for every point, by broadcasting
re : Float
re, im: one row of real parts and one column of imaginary parts, centered on (cx, cy) and w wide. cr, ci: c's real and imaginary parts spread over the whole grid: t_able spreads the row down and the column across, no loop over points.
re ← cx + w × (ᵘc̲entred cols) ÷ f̲loat cols
One step for every point at once
ᵘs̲tep : (Float, Float, Float) -> (Float, Float, Float)
u:s_tep (zr, zi, counts): the state is a tuple of three same-shaped arrays (z's real part, its imaginary part, and the count of steps spent inside), taken apart by the pattern and given back as a new tuple; no more packing them into one array (D7, landed). One step computes z * z + c everywhere, keeps z where the point has already left the disk |z|^2 > 4 (the mask inside), and adds inside to the count.
ᵘs̲tep ← { (zr, zi, counts) → inside ← f̲loat 4 ≥ (zr × zr) + zi × zi a ← (inside × cr + (zr × zr) − zi × zi) + (1 − inside) × zr b ← (inside × ci + 2 × zr × zi) + (1 − inside) × zi (a, b, inside + counts) }
k steps, from z = 0
%2 : Float
zr, zi, counts: the state after k steps (function power, from a zero state), taken apart by the same pattern as the binding reads it in. m2: |z|^2. counts: how many steps each point stayed inside; the points still inside after k steps are the set, so far.
(zr, zi, counts) ← k 'ᵘs̲tep p̲ower (0.0 × cr, 0.0 × cr, 0.0 × cr)
zr : Float
zr, zi, counts: the state after k steps (function power, from a zero state), taken apart by the same pattern as the binding reads it in. m2: |z|^2. counts: how many steps each point stayed inside; the points still inside after k steps are the set, so far.
(zr, zi, counts) ← k 'ᵘs̲tep p̲ower (0.0 × cr, 0.0 × cr, 0.0 × cr)
zi : Float
zr, zi, counts: the state after k steps (function power, from a zero state), taken apart by the same pattern as the binding reads it in. m2: |z|^2. counts: how many steps each point stayed inside; the points still inside after k steps are the set, so far.
(zr, zi, counts) ← k 'ᵘs̲tep p̲ower (0.0 × cr, 0.0 × cr, 0.0 × cr)
counts : Float
zr, zi, counts: the state after k steps (function power, from a zero state), taken apart by the same pattern as the binding reads it in. m2: |z|^2. counts: how many steps each point stayed inside; the points still inside after k steps are the set, so far.
(zr, zi, counts) ← k 'ᵘs̲tep p̲ower (0.0 × cr, 0.0 × cr, 0.0 × cr)
One point's orbit
cr0 : Float
cr0, ci0: a point near the set's edge, c = -0.75 + 0.1i, which circles slowly rather than running away or settling. u:o_rbit s: the next z (z * z + c) for this one point, s its previous z (real part, imaginary part), no mask needed (a single point, always run the full k steps).
cr0 ← -0.75