Files
rose-ash/lib/apl/tests/programs/mandelbrot.apl
giles 49eb22243a
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 1m5s
apl: mandelbrot real-axis batched z=z²+c (+9 tests, 296/296)
2026-05-07 05:07:25 +00:00

30 lines
1.3 KiB
APL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
⍝ Mandelbrot — real-axis subset
⍝ For complex c, the Mandelbrot set is { c : |z_n| stays bounded } where
⍝ z_0 = 0, z_{n+1} = z_n² + c.
⍝ Restricting c (and z) to gives the segment c ∈ [-2, 1/4]
⍝ where the iteration stays bounded.
⍝ Rank-polymorphic batched-iteration form:
⍝ mandelbrot ← {⍵ ⍵⍵ +,( × ) }
⍝ Pseudocode (as we don't have ⎕ system fns yet):
⍝ z ← 0×c ⍝ start at zero
⍝ alive ← 1+0×c ⍝ all "still in"
⍝ for k iterations:
⍝ alive ← alive ∧ 4 ≥ z×z ⍝ still bounded?
⍝ z ← alive × c + z×z ⍝ freeze escaped via mask
⍝ count ← count + alive ⍝ tally surviving iters
⍝ Examples (count after 100 iterations):
⍝ c=0 : 100 (z stays at 0)
⍝ c=-1 : 100 (cycles 0,-1,0,-1,...)
⍝ c=-2 : 100 (settles at 2 — boundary)
⍝ c=0.25 : 100 (boundary — converges to 0.5)
⍝ c=0.5 : 5 (escapes by iteration 6)
⍝ c=1 : 3 (escapes quickly)
⍝ Real-axis Mandelbrot set: bounded for c ∈ [-2, 0.25].
mandelbrot {zalivecount0× {alivealive4z×z zalive×+z×z count+alive}}