Engee documentation
Notebook

The Mandelbrot Set

Imagine an infinitely complex mathematical pattern where each point contains a whole universe of self-similar structures. This is the Mandelbrot set — one of the most famous fractals in the world, a mathematical object of incredible complexity, born from a simple formula. The Mandelbrot set can be compared to the digital fingerprint of mathematics itself — unique, infinitely detailed and mysterious.

Historical context

Although the formal discovery of the Mandelbrot set is associated with the name of the French-American mathematician Benoit Mandelbrot in the 1970s, its roots go much deeper.:

Background (1918-1920)

  • French mathematicians Pierre Fatou and Gaston Julia independently studied iterations of complex functions
  • Their work laid the mathematical foundation, but without computer visualization they could not see all the beauty.

The Mandelbrot Revolution (1975)

  • Benoit Mandelbrot, working at IBM, used computers for visualization
  • He coined the term "fractal" to describe objects with an infinite self-similar structure.
  • His book Fractal Geometry of Nature (1982) popularized these concepts.

Digital Age (1980-present)

  • With the development of computing technology, fractals have become available to a wide audience
  • The Mandelbrot set has become a symbol of "mathematical art"

How the code works

Imagine that you are checking every point on the map (the complex plane) for "stability". For each point, we repeatedly apply a simple mathematical operation and see if it "runs away" to infinity.

The point verification process:

  1. Starting from scratch
  2. Repeatedly square and add the coordinates of the point
  3. If the value remains limited after many iterations, the point belongs to the set.
  4. If it "runs away", we remember how quickly it happened.

Optimizations in the code

Smart checks:
The code uses mathematical tricks to quickly identify the points that exactly belong to the set, without a full calculation. This is similar to how an experienced chess player immediately sees winning positions without calculating all the moves.

Efficient computing:

  • The square of the distance is used instead of extracting the root (faster for the computer)
  • Vectorization allows you to process many points at the same time
  • Special checks for specific areas save time
In [ ]:
function mandelbrot_optimized(c, max_iter=50)
    z = 0.0 + 0.0im  # Explicit creation of a complex number
    # Checking whether you belong to the main cardioid or circles
    re, im_val = real(c), imag(c)  # Renamed the variable to avoid a conflict
    q = (re - 0.25)^2 + im_val^2
    if q*(q + (re - 0.25)) < 0.25*im_val^2 || (re + 1)^2 + im_val^2 < 0.0625
        return max_iter
    end
    # Standard iterations
    for n in 1:max_iter
        z = z^2 + c
        if abs2(z) > 4  # We use the square of the module for optimization
            return n
        end
    end
    return max_iter
end
# Vectorized version for broadcast use
function mandelbrot_set_vectorized(xs, ys, max_iter=50)
    f(x, y) = mandelbrot_optimized(x + y*im, max_iter)  # The im is available here
    return f.(xs', ys)  # Транспонирование для корректной формы матрицы
end
Out[0]:
mandelbrot_set_vectorized (generic function with 2 methods)

Visualization

Color as a storyteller:

  • Black color: points belonging to a set (stable)
  • Colored areas: points that "run away" at different speeds
  • Bright colors: quick escape
  • Dark colors: slow "escape"

Two display styles:

  • contourf: creates smooth color transitions that emphasize the structure
  • heatmap: sharper borders, shows details better
In [ ]:
# Parameters of the construction area
xs = range(-2.0, 1.0, length=400)  # Reduced the resolution for speed
ys = range(-1.2, 1.2, length=400)

# Plotting using contourf for smooth gradients
matrix = mandelbrot_set_vectorized(xs, ys, 100)
Out[0]:
400×400 Matrix{Int64}:
 1  1  1  1  1  1  1  1  1  1  1  1  1  …  2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1     2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1     2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1     2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1     2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1  …  2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1     2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1     2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1     2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1     2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1  …  2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1     2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1     2  2  2  2  2  2  2  2  2  2  2  2
 ⋮              ⋮              ⋮        ⋱        ⋮              ⋮           
 1  1  1  1  1  1  1  1  1  1  1  1  1     2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1     2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1  …  2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1     2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1     2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1     2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1     2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1  …  2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1     2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1     2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1     2  2  2  2  2  2  2  2  2  2  2  2
 1  1  1  1  1  1  1  1  1  1  1  1  1     2  2  2  2  2  2  2  2  2  2  2  2
In [ ]:
contourf(xs, ys, matrix, 
    color=:deep,
    aspect_ratio=:equal,
    xlabel="Re", 
    ylabel="Im",
    title="The optimized Mandelbrot set",
    fill=true
)
Out[0]:
In [ ]:
heatmap(xs, ys, matrix, 
    color=:viridis,
    aspect_ratio=:equal,
    xlabel="Re", 
    ylabel="Im",
    title="The optimized Mandelbrot set"
)
Out[0]:

Philosophical significance

The Mandelbrot set is not just a beautiful picture. It demonstrates several profound ideas.:

** Simplicity breeds complexity**
An incredibly complex structure arises from the elementary z2 operation. This is a reminder of how simple laws of nature can generate complex ecosystems.

Infinity in the finite
Each zoom-in reveals new details. Fractal nature means that patterns repeat on different scales, but they are never completely the same.

The border as a source of beauty
The most interesting part of the set is its boundary. It is there that an infinite variety of shapes and structures arise.

Practical significance

Although fractals were originally studied as pure mathematics, they have found application in:

  • Image compression
  • Modeling of natural objects (mountains, clouds, coastlines)
  • Computer graphics and special effects
  • Analysis of financial markets

Conclusion

This code is not just a program, but a window into a wonderful world of mathematical beauty. It allows us to see the visual embodiment of abstract mathematical ideas that once existed only in the imagination of mathematicians. Each run of this code is a new journey to the limits of infinite complexity, born of amazing simplicity.

Mandelbrot's set reminds us that even in the world of pure mathematics, one can find inexhaustible sources of beauty and inspiration, and that sometimes the deepest truths are hidden behind the simplest formulas.