DEV Community

Cover image for Mandelbrot set with Numerical Elixir and Zigler
NDREAN
NDREAN

Posted on • Edited on

1

Mandelbrot set with Numerical Elixir and Zigler

We build a Mandelbrot set explorer with a Livebook. It is an Elixir code runner that lets you write Elixir in the browser.

Algorithm

An explanation can be found here

Given an image with dimensions (eg w x h of 1500 x 1000), max iteration (eg 100), we want to assign to each pixel a colour which represents the "stability" of the orbit of the underlying point.

Iterate over each pixel (i,j):

  • map it into the 2D plane: compute its "complex coordinates"
  • compute the iteration number
  • compute a colour
  • Sum-up and draw.

Explorer

We have to parts:

  • one Livebook to visualise the orbits of a point, the iterates
  • one Livebook to visualise the Mandelbrot set and zoom into any point.

This is done with the Numerical Elixir library Nx. A solution is also proposed using embedded Zig within the Livebook.

The Zig code can also be compiled to WebAssembly. You can view the WeAssembly version with the link below:

https://ndrean.github.io/zig-assembly-test/

The Kino.JS.Live library makes it possible to interact with the browser.

Exploring orbits

Image description

In this Livebook, you click on a point c of the 2D-plane and it computes the succession of points, the orbit, under the iterative map z(n+1) = z(n) * z(n) + c and z(0)=c.

Run in Livebook

Code source: https://github.com/ndrean/mandelbrot/blob/main/livebook/orbits.livemd

Explore the Mandelbrot set

Image description

In this Livebook, you can click into the 2D-plane to zoom into the area of interest.

We propose two solutions:

  • one using only Elixir for the computations,
  • one using Zig for the heavy computations (you need to have Zig installed).

Although Zig gives you a huge boost, the performance of the Elixir code is good. This is because we use the numerical library Nx with the EXLA backend.

Run in Livebook

Code source: https://github.com/ndrean/mandelbrot/blob/main/livebook/mandelbrot.livemd

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay