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/

source

The view

rows : Int

value · line 19

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
Used in: im, cr

cols : Int

value · line 20
cols ← 64
Used in: re, im, ci

cx : Float

value · line 21
cx ← -0.6
Used in: re

cy : Float

value · line 22
cy ← 0.0
Used in: im

w : Float

value · line 23
w ← 3.0
Used in: re, im

k : Int

value · line 24
k ← 30

aspect : Float

value · line 25
aspect ← 2.0
Used in: im

ᵘc̲entred : Int -> Float

function · line 29

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 }
Used in: re, im

c for every point, by broadcasting

re : Float

value · line 38

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
Used in: cr

im : Float

value · line 39
im ← cy − aspect × w × (ᵘc̲entred rows) ÷ f̲loat cols
Used in: ci

cr : Float

value · line 40
cr ← (o̲ffsets rows) 'r̲ight t̲able re

ci : Float

value · line 41
ci ← im 'l̲eft t̲able o̲ffsets cols
Used in: ᵘs̲tep

One step for every point at once

ᵘs̲tep : (Float, Float, Float) -> (Float, Float, Float)

function · line 52

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)
}
Used in: %2

k steps, from z = 0

%2 : Float

value · line 65

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

value · line 65

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

value · line 65

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)
Used in: ᵘs̲tep, %2, m2

counts : Float

value · line 65

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)

m2

value · line 66
m2 ← (zr × zr) + zi × zi

One point's orbit

cr0 : Float

value · line 83

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
Used in: ᵘo̲rbit

ci0 : Float -> Float

value · line 84
ci0 ← 0.1
Used in: ᵘo̲rbit

ᵘo̲rbit

function · line 85
ᵘo̲rbit ← { s → (cr0 + ((1 s̲elect s) × 1 s̲elect s) − (2 s̲elect s) × 2 s̲elect s) c̲at ci0 + 2 × (1 s̲elect s) × 2 s̲elect s }