programdemos/julia/julia.xtl

Julia sets and the Mandelbrot set from one function, u:i_terate. Both iterate z becoming z * z + c and count the steps z stays within 2 of the origin. For the Mandelbrot set, c is the grid of points and z starts at 0; for a Julia set, z starts at the grid and c is one number, which scalar extension spreads over the whole grid: the step is written once and works both ways. X_eTaL has no complex numbers yet, so z and c are each a real and an imaginary part (see the demo's README Workarounds). Run it with "xetal run demos/julia/julia.xtl"; it prints the grid's shape, a Julia set (c = -0.8 + 0.156i) drawn with characters, and the Mandelbrot set from the same function. The live page lets you pick c on the Mandelbrot map and watch the Julia set change: https://softwarewrighter.github.io/X_eTaL-demos/julia/

source

The view

rows : Int

value · line 21

rows, cols: the picture's size. cx, cy, w: the view's center and width. k: how many steps. aspect: how tall a cell is for its width (2.0 for terminal characters).

rows ← 22
Used in: im, grid

cols : Int

value · line 22
cols ← 64
Used in: re, im, grid

cx : Float

value · line 23
cx ← 0.0
Used in: re

cy : Float

value · line 24
cy ← 0.0
Used in: im

w : Float

value · line 25
w ← 3.2
Used in: re, im

k : Int

value · line 26
k ← 40

aspect : Float

value · line 27
aspect ← 2.0
Used in: im

ᵘc̲entred : Int -> Float

function · line 31

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

The grid of points, by broadcasting

re : Float

value · line 39

re, im: one row of real parts and one column of imaginary parts. grid: both parts spread over the whole grid with t_able, as a pair (gr, gi): every point's c, for the Mandelbrot set, or every starting z0, for a Julia set.

re ← cx + w × (ᵘc̲entred cols) ÷ f̲loat cols
Used in: grid

im : Float

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

grid : (Float, Float)

value · line 41
grid ← ((o̲ffsets rows) 'r̲ight t̲able re, im 'l̲eft t̲able o̲ffsets cols)
Used in: %1, %5, %6

%1 : Float

value · line 42
(gr, gi) ← grid

gr : Float

value · line 42
(gr, gi) ← grid

gi : (Float, Float) -> (Float, Float) -> (Float, Float, Float)

value · line 42
(gr, gi) ← grid
Used in: %1, %6

c u:i_terate z0: k steps of z * z + c from z0

ᵘi̲terate

function · line 50

c and z0 are each a pair (real, imaginary): two numbers (scalar extension) or two planes of the grid, taken apart by the patterns on both sides (D7, landed). The state adds a count of steps spent inside; a point outside keeps its z.

ᵘi̲terate ← { (cr, ci) (zr0, zi0) →
  ⍝ One step, as a local function (s_tep, bare and scoped to this
  ⍝ lambda, PN6): the same arithmetic as mandelbrot.xtl's u:s_tep.
  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 's̲tep p̲ower (zr0, zi0, 0.0 × zr0)
}
Used in: %5, %6

%5 : Float

value · line 67

julia: a Julia set, c = -0.8 + 0.156i the same for every point, z0 the grid.

(jzr, jzi, julia) ← (-0.8, 0.156) ᵘi̲terate grid

jzr : Float

value · line 67

julia: a Julia set, c = -0.8 + 0.156i the same for every point, z0 the grid.

(jzr, jzi, julia) ← (-0.8, 0.156) ᵘi̲terate grid
Used in: %5

jzi

value · line 67

julia: a Julia set, c = -0.8 + 0.156i the same for every point, z0 the grid.

(jzr, jzi, julia) ← (-0.8, 0.156) ᵘi̲terate grid
Used in: %5

julia

value · line 67

julia: a Julia set, c = -0.8 + 0.156i the same for every point, z0 the grid.

(jzr, jzi, julia) ← (-0.8, 0.156) ᵘi̲terate grid

%6 : Float

value · line 73

mandel: the Mandelbrot set from the same function: c the grid, z0 = 0.

(mzr, mzi, mandel) ← grid ᵘi̲terate (0.0 × gr, 0.0 × gi)

mzr

value · line 73

mandel: the Mandelbrot set from the same function: c the grid, z0 = 0.

(mzr, mzi, mandel) ← grid ᵘi̲terate (0.0 × gr, 0.0 × gi)
Used in: %6