Challenging Claude's creativity with IFS fractals and OCaml

Hello,

Back in 2010, during a functional programming course, I wrote a small OCaml program that draws fractals with iterated function systems (IFS), using the graphics library and the chaos game. It is one of my oldest repositories still alive, and it had barely changed since:

Each fractal is just a record: a list of affine transforms with cumulative weights, plus a viewport. The chaos game does the rest. The Barnsley fern is twenty-four coefficients.

Last weekend I dusted it off, and as an experiment I asked Claude Fable 5 to invent new IFS fractals rather than fix or refactor anything. I wrote up the full story on my blog, but the parts that might interest people here:

  • The repo went from eleven predefined fractals to sixteen. The nicest newcomer in my opinion is lace, Queen Anne’s lace (wild carrot): an umbel of umbels, where five transforms place shrunken rotated copies of the whole plant on the rim of a dome and a sixth squashes everything into the stem. Thirty-six numbers total:

let lace =
{ po = {x= -0.90 ; y= -0.55};
sz = {x= 1.80 ; y= 2.00};
lt = [{pb= 0.18; kf= [| 0.2649; 0.1408; -0.7048; -0.1408; 0.2649; 0.38|]};
{pb= 0.36; kf= [| 0.3493; 0.0871; -0.4302; -0.0871; 0.3493; 0.5013|]};
{pb= 0.54; kf= [| 0.38; 0.00; 0.00; 0.00; 0.38; 0.55|]};
{pb= 0.72; kf= [| 0.3493; -0.0871; 0.4302; 0.0871; 0.3493; 0.5013|]};
{pb= 0.90; kf= [| 0.2649; -0.1408; 0.7048; 0.1408; 0.2649; 0.38|]};
{pb= 1.00; kf= [| 0.02; 0.00; 0.00; 0.00; 0.55; 0.00|]}]};;

  • A sunflower built on phyllotaxis with only two transforms: one rotates by the golden angle (137.508°) while contracting toward the center, the other plants a bud at the rim. Because the golden angle is the “most irrational” angle, the buds fill the disk evenly, just like real seed heads.
  • A fun negative result: I asked for the vegvísir, the Icelandic stave. Strictly speaking an IFS cannot draw it, since the attractor is a single self-similar set while the real symbol has a different rune on each of its eight arms. The workaround was a non-contracting map, a pure 45° rotation at scale 1.0 that draws nothing itself and only distributes points across the eight arms, so the other transforms only need to describe one arm. I had never thought of using a measure-preserving map as a “symmetry map” like that.
  • The takeaway I keep coming back to: plants make good IFS subjects because they grow by iterating simple local rules, so their shape literally is an attractor. Designed symbols resist because nobody grew them.

The write-up with all the images is here:

If you want to play with it: opam install graphics ocamlfind, then in the toplevel #use “ifs_fractals.ml”;; and draw lace 300000;;.

I am curious whether others have used non-contracting symmetry maps in IFS before, or have favorite attractors worth adding. And if anyone remembers writing this kind of toy in an OCaml course, I would love to hear about it. Pull-requests are of course welcome!

Sixteen years after the first commit I finally created a pull-request in order to make this work available via opam: Package ifs-fractals.1.0.0 by cedricbonhomme · Pull Request #30346 · ocaml/opam-repository · GitHub :smiling_face_with_three_hearts:

Not in OCaml, but I remember writing these in Pascal, the first language I seriously taught myself after copycat dabblings in BASIC. It was for a math high-school semester project I had chosen to do on fractals.

They would be atrociously slow to render on the computer we had at home which was inconvenient for exploring them and have good quality images for the handout.

But I had learned there were more powerful machines available for staff at the EPFL in Lausanne without access control except for the room. So I’d wait in front of the door until someone would badge in and innocently slip in behind with my floppy disks. There they would only take a dozen of minutes to render satisfyingly – and the machines had access to the mysterious and still mostly academic world wide web…

My father would launch various fractal renders on a different machine and the home’s computer would be off-limits for a day or two at a time.

Once I had learned to code in OCaml, I coded a renderer for a few fractals with the Graphics display backend. I don’t think I saved the code anywhere though, it was a fairly simple thing.

Children screen time management techniques from the past :–)

Thank you for your comments!

The project is already available on opam. Submitted yesterday evening, merged early this morning, with a kind word from the maintainer.

The OCaml community is a lovely place!