Skip to content

sparsh-karna/MandelbrotSet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Mandelbrot Set Visualization Project

Mandelbrot Set

Overview

This project provides two distinct implementations for visualizing the famous Mandelbrot Set, one of the most celebrated fractals in mathematics. The project includes both a static high-resolution image generator and an interactive real-time renderer using OpenGL shaders.

Significance of the Mandelbrot Set

The Mandelbrot set is a famous fractal named after the mathematician Benoît B. Mandelbrot (1924-2010), who first visualized and studied it in detail using computer graphics in 1980. It represents one of the most iconic examples of mathematical beauty emerging from computational exploration and has become a symbol of the deep connection between mathematics, computation, and visual art.

Historical Context

The Mandelbrot set emerged from the study of complex dynamics, a field that investigates the behavior of iterative functions in the complex plane. While the mathematical foundations were laid by Pierre Fatou and Gaston Julia in the early 20th century, it wasn't until the advent of computer graphics that the full visual complexity of these sets could be appreciated.

Mathematical Significance

The set demonstrates several profound mathematical concepts:

Complex Dynamics: The Mandelbrot set serves as a parameter space for the family of quadratic polynomials:

$$f_c(z) = z^2 + c$$

where $c$ is a complex parameter and $z$ is a complex variable.

Universality: The Mandelbrot set exhibits universal properties found across many dynamical systems, making it a fundamental object in chaos theory and complex dynamics.

Computational Mathematics: It bridges pure mathematics with computational science, demonstrating how numerical methods can reveal deep mathematical structures.

Connection to Julia Sets

Each point $c$ in the complex plane corresponds to a Julia set $J_c$, defined as the boundary of the set of points whose orbits remain bounded under iteration of $f_c(z) = z^2 + c$. The Mandelbrot set encodes the topological properties of all Julia sets:

  • If $c \in M$ (Mandelbrot set), then $J_c$ is connected
  • If $c \notin M$, then $J_c$ is a Cantor dust (totally disconnected)

This relationship is expressed mathematically as:

$$M = {c \in \mathbb{C} : J_c \text{ is connected}}$$

Key Properties

Self-Similarity: The boundary $\partial M$ exhibits quasi-self-similarity at different scales. While not exactly self-similar like the Koch snowflake, it contains approximate copies of itself at various magnifications.

Complexity from Simplicity: Despite originating from the simple quadratic recurrence relation $z_{n+1} = z_n^2 + c$, the set produces infinitely intricate boundary structures.

Fractal Dimension: The boundary has a fractal dimension of 2, meaning it has infinite length but finite area.

Universality in Chaos: The set demonstrates period-doubling bifurcations and routes to chaos that appear in many physical and biological systems.

Mathematical Foundation

The Mandelbrot Set Definition

The Mandelbrot set $M$ is defined as the set of complex numbers $c$ for which the dynamical system:

$$z_0 = 0$$ $$z_{n+1} = z_n^2 + c$$

remains bounded as $n \to \infty$.

Formally, the Mandelbrot set is expressed as:

$$M = {c \in \mathbb{C} : \lim_{n \to \infty} |z_n| < \infty}$$

Equivalently, using the escape radius theorem:

$$M = {c \in \mathbb{C} : |z_n| \leq 2 \text{ for all } n \in \mathbb{N}}$$

Where:

  • $c$ is a complex number in the form $c = a + bi$ where $a, b \in \mathbb{R}$
  • $z$ is initialized to $0$
  • $|z|$ represents the modulus (absolute value) of the complex number
  • $\mathbb{C}$ denotes the set of complex numbers
  • $\mathbb{N}$ denotes the set of natural numbers

Mathematical Calculation Process

To determine if a complex number $c$ is in the Mandelbrot set, we implement the following algorithm:

  1. Initialize: $z_0 = 0$
  2. Iterate: $z_{n+1} = z_n^2 + c$ for $n = 0, 1, 2, \ldots$
  3. Test Escape Condition: If $|z_n| &gt; 2$, then $c \notin M$
  4. Bounded Check: If $|z_n| \leq 2$ after maximum iterations, then $c \in M$

The escape time $T(c)$ is defined as:

$$T(c) = \min{n \in \mathbb{N} : |z_n| > 2}$$

If no such $n$ exists within our computational limit, we assume $c \in M$.

Complex Number Arithmetic

For complex numbers $z = x + yi$ and $c = a + bi$, the iteration formula $z_{n+1} = z_n^2 + c$ expands to:

$$(x + yi)^2 + (a + bi) = (x^2 - y^2) + 2xyi + a + bi$$

This gives us the component-wise recurrence relations:

  • Real part: $x_{n+1} = x_n^2 - y_n^2 + a$
  • Imaginary part: $y_{n+1} = 2x_n y_n + b$

Escape Condition Theorem

Theorem: If $|z_n| &gt; 2$ for any iteration $n$, then $\lim_{k \to \infty} |z_k| = \infty$.

Proof Sketch: For $|z_n| &gt; 2$, we have: $$|z_{n+1}| = |z_n^2 + c| \geq |z_n|^2 - |c|$$

When $|z_n|$ is sufficiently large, $|z_n|^2$ dominates $|c|$, ensuring the sequence diverges.

The modulus of a complex number $z = x + yi$ is calculated as:

$$|z| = \sqrt{x^2 + y^2}$$

For computational efficiency, we often use $|z|^2 = x^2 + y^2$ and compare against $4$ instead of $2$.

Coordinate Mapping

To visualize the Mandelbrot set, we map pixel coordinates $(i, j)$ to the complex plane:

$$a = \text{map}(i, 0, \text{width}, x_{\min}, x_{\max})$$ $$b = \text{map}(j, 0, \text{height}, y_{\min}, y_{\max})$$

Where the linear mapping function is defined as:

$$\text{map}(v, i_{\min}, i_{\max}, o_{\min}, o_{\max}) = \frac{(v - i_{\min})(o_{\max} - o_{\min})}{i_{\max} - i_{\min}} + o_{\min}$$

The standard viewing window for the Mandelbrot set is typically $[-2, 2] \times [-2, 2]$ in the complex plane, which encompasses the entire set with some surrounding area.

Analytical Properties

Cardioid Main Body: The main body of the Mandelbrot set is approximately a cardioid, parametrized by:

$$c(\theta) = \frac{1}{4}e^{i\theta}\left(1 - \frac{e^{i\theta}}{2}\right) \quad \text{for } \theta \in [0, 2\pi]$$

Circular Bulb: The large circular bulb to the left of the main cardioid is centered at $c = -1$ with radius $\frac{1}{4}$.

Period-2 Bulb: Points in this bulb have orbits with period 2, satisfying:

$$c \in \{ z \in \mathbb{C} : |z + 1| \leq \frac{1}{4} \}$$

Project Structure

MandelbrotSet/
├── static/
│   ├── mandelbrot.py          # High-resolution static generator
│   └── mandelbrot_set.png     # Generated fractal image
├── dynamic/
│   ├── main.py               # Real-time interactive renderer
│   ├── fragmentShader.glsl   # GPU fragment shader
│   └── vertexShader.glsl     # GPU vertex shader
├── README.md
└── requirements.txt

Implementation Details

Static Implementation (static/mandelbrot.py)

The static implementation generates high-resolution images (36,000 × 36,000 pixels) using pure Python:

  1. Pixel Iteration: Each pixel represents a complex number c
  2. Escape Time Algorithm: Iterates the formula zₙ₊₁ = zₙ² + c up to 100 times
  3. Color Mapping: Maps the number of iterations to grayscale values
  4. Boundary Detection: Points that don't escape (n = 99) are colored black (part of the set)

Time Complexity: O(width × height × max_iterations) = O(n³)

Dynamic Implementation (dynamic/)

The dynamic implementation uses GPU-accelerated rendering for real-time interaction:

Fragment Shader (fragmentShader.glsl)

The fragment shader implements the Mandelbrot iteration on the GPU:

float real = ((gl_FragCoord.x / 1080.0 - offsetX) * ZoomScale + CenterX) * 4.0;
float imag = ((gl_FragCoord.y / 1080.0 - offsetY) * ZoomScale + CenterY) * 4.0;

while (iterations < MAX_ITERATIONS) {
    float tmp_real = real;
    real = (pow(real, 2.0) - pow(imag, 2.0)) + real_number;
    imag = (2.0 * tmp_real * imag) + imaginary;
    
    if (pow(real, 2.0) + pow(imag, 2.0) > 4.0) break;
    ++iterations;
}

Color Scheme

The dynamic renderer employs a sophisticated coloring algorithm with four color ranges:

  • color_0: Black (points in the set)
  • color_1: Dynamic blue-purple gradient
  • color_2: Green-based gradient
  • color_3: Full RGB spectrum based on time

Performance Optimization

  • Parallel Processing: Each pixel is calculated simultaneously on GPU cores
  • Maximum Iterations: Limited to 1000 for real-time performance
  • Escape Radius: Uses |z|² > 4 instead of |z| > 2 to avoid expensive square root calculations

Controls (Dynamic Version)

Key Action
Arrow Keys Navigate (Pan)
W Zoom In
S Zoom Out
ESC Exit

Mathematical Properties

Fractal Dimension

The boundary $\partial M$ of the Mandelbrot set has a Hausdorff dimension $d_H = 2$, making it a true fractal boundary. This means:

$$\mathcal{H}^{d_H}(\partial M) \in (0, \infty)$$

where $\mathcal{H}^{d_H}$ denotes the $d_H$-dimensional Hausdorff measure.

Self-Similarity and Scaling

The Mandelbrot set exhibits quasi-self-similarity with scaling properties governed by:

$$\lim_{n \to \infty} M_n = M$$

where $M_n$ represents the set at finite iteration depth $n$.

Connectedness

Theorem (Douady-Hubbard): The Mandelbrot set is simply connected - it forms a single, connected shape without holes.

Mathematically: $M$ is connected and $\mathbb{C} \setminus M$ (the complement) is connected.

Topological Properties

Area: The Mandelbrot set has finite area: $$\text{Area}(M) = \iint_{M} dx , dy &lt; \infty$$

Perimeter: The boundary has infinite length: $$\text{Perimeter}(\partial M) = \infty$$

Cardioid and Bulb Analysis

The main body components can be characterized analytically:

Main Cardioid: For $c$ in the main cardioid, the orbit converges to a fixed point $z^$ satisfying: $$z^ = (z^*)^2 + c$$

Solving: $z^* = \frac{1 \pm \sqrt{1-4c}}{2}$

Period-2 Bulb: For $c$ in the period-2 bulb, the orbit has period 2, cycling between two values.

Bifurcation Theory

The Mandelbrot set serves as a bifurcation locus for the family $f_c(z) = z^2 + c$. Key bifurcation phenomena include:

Period-doubling cascade: As parameters vary along certain paths, periodic orbits undergo period-doubling bifurcations following Feigenbaum's universal constants:

$$\delta = \lim_{n \to \infty} \frac{a_n - a_{n-1}}{a_{n+1} - a_n} \approx 4.669...$$

where $a_n$ are bifurcation parameter values.

Renormalization Theory

The fine structure of the Mandelbrot set can be understood through renormalization, which reveals universal scaling laws and explains the appearance of smaller copies of the set at different scales.

Installation and Usage

Prerequisites

pip install -r requirements.txt

Running the Static Generator

cd static
python mandelbrot.py

This generates a high-resolution mandelbrot_set.png image.

Running the Interactive Renderer

cd dynamic
python main.py

This opens a real-time, interactive Mandelbrot set explorer.

Technical Specifications

Static Version

  • Resolution: 36,000 × 36,000 pixels (1.296 gigapixels)
  • Color Depth: RGBA (32-bit)
  • Iterations: 100 maximum
  • Coordinate Range: [-2, 2] × [-2, 2]

Dynamic Version

  • Resolution: 1920 × 1080 pixels
  • Frame Rate: Up to 60 FPS
  • Iterations: 1000 maximum
  • Interactive: Real-time zoom and pan
  • Rendering: GPU-accelerated via OpenGL

Mathematical Extensions

Julia Sets

The code can be easily modified to generate Julia Sets $J_c$ by fixing $c$ and varying the initial condition $z_0$:

$$J_c = {z_0 \in \mathbb{C} : |z_n| \leq 2 \text{ for all } n \in \mathbb{N}}$$

The filled Julia set $K_c$ is defined as: $$K_c = {z_0 \in \mathbb{C} : \lim_{n \to \infty} |f_c^n(z_0)| &lt; \infty}$$

where $f_c^n$ denotes the $n$-fold composition of $f_c$.

Burning Ship Fractal

The Burning Ship fractal uses a modified iteration: $$z_{n+1} = (|\text{Re}(z_n)| + i|\text{Im}(z_n)|)^2 + c$$

This creates a fractal with different symmetry properties and interesting "ship-like" structures.

Multibrot Sets

Generalize to Multibrot sets of degree $d$: $$z_{n+1} = z_n^d + c \quad \text{where } d \in \mathbb{R}^+$$

For integer values:

  • $d = 2$: Classical Mandelbrot set
  • $d = 3$: Tricorn (with complex conjugate)
  • $d = 4, 5, 6, ...$: Higher-order Multibrot sets

Rational Maps

Extend to rational functions: $$f_c(z) = \frac{P(z)}{Q(z)} + c$$

where $P(z)$ and $Q(z)$ are polynomials, leading to rational Mandelbrot sets.

Newton Fractals

Apply Newton's method to polynomial equations: $$z_{n+1} = z_n - \frac{p(z_n)}{p'(z_n)}$$

The basins of attraction for different roots create Newton fractals.

Escape Time Algorithms

For general iteration functions $f_c(z)$, the escape time algorithm computes:

$$T_R(z_0, c) = \inf{n \geq 0 : |f_c^n(z_0)| > R}$$

where $R$ is the escape radius (typically $R = 2$ for quadratic polynomials).

Performance Analysis

Computational Complexity

For an image of size $W \times H$ with maximum $I$ iterations:

Time Complexity: $O(W \times H \times I)$

Space Complexity: $O(W \times H)$

The average case complexity is often better due to early escape, leading to:

$$\mathbb{E}[T] = O(W \times H \times \bar{I})$$

where $\bar{I} &lt; I$ is the average escape time.

Algorithmic Optimizations

Symmetry Exploitation: The Mandelbrot set has symmetry about the real axis: $$c \in M \iff \bar{c} \in M$$

where $\bar{c}$ denotes the complex conjugate.

Boundary Tracing: For high-resolution images, implement boundary following algorithms to avoid redundant calculations in large uniform regions.

Perturbation Theory: For deep zooms, use series approximation and perturbation methods to maintain numerical precision:

$$z_{n+1} = f'(Z_n)\delta_n + f(Z_n) + \varepsilon_n$$

where $Z_n$ is a reference orbit and $\delta_n$ is the perturbation.

Numerical Precision

For zoom levels beyond $10^{12}$, standard double precision fails. Solutions include:

Extended Precision: Use arbitrary precision arithmetic libraries.

Scaled Coordinates: Implement DeepZoom algorithms with reference point techniques.

Series Approximation: Use polynomial approximations for high-iteration counts.

GPU vs CPU Performance

The GPU implementation provides approximately 100-1000× speedup over CPU implementation due to:

Parallel Processing: Thousands of cores executing simultaneously: $$\text{Speedup} \approx \min\left(\frac{N_{\text{GPU cores}}}{N_{\text{CPU cores}}}, \frac{W \times H}{N_{\text{threads}}}\right)$$

SIMD Operations: Single Instruction, Multiple Data execution patterns.

Memory Bandwidth: Higher throughput for data-intensive operations: $$\text{Bandwidth}{\text{GPU}} \gg \text{Bandwidth}{\text{CPU}}$$

Cache Efficiency: Better cache utilization for regular access patterns.

References

  1. Mandelbrot, B. B. (1982). The Fractal Geometry of Nature. W.H. Freeman and Company.
  2. Douady, A., & Hubbard, J. H. (1985). Étude dynamique des polynômes complexes. Publications Mathématiques d'Orsay.
  3. Peitgen, H.-O., Jürgens, H., & Saupe, D. (2004). Chaos and Fractals: New Frontiers of Science. Springer.
  4. Milnor, J. (2006). Dynamics in One Complex Variable. Princeton University Press.
  5. Falconer, K. (2003). Fractal Geometry: Mathematical Foundations and Applications. John Wiley & Sons.
  6. Devaney, R. L. (2003). An Introduction to Chaotic Dynamical Systems. Westview Press.
  7. Barnsley, M. (2014). Fractals Everywhere. Dover Publications.
  8. McMullen, C. (1994). Complex Dynamics and Renormalization. Princeton University Press.

Mathematical Papers

  • Douady, A., & Hubbard, J. H. (1984). "On the dynamics of polynomial-like mappings." Annales scientifiques de l'École Normale Supérieure, 18(2), 287-343.
  • Shishikura, M. (1998). "The Hausdorff dimension of the boundary of the Mandelbrot set and Julia sets." Annals of Mathematics, 147(2), 225-267.
  • Lyubich, M. (1999). "Feigenbaum-Coullet-Tresser universality and Milnor's hairiness conjecture." Annals of Mathematics, 149(2), 319-420.

License

This project is open source and available under the MIT License.


Appendix: Advanced Topics

Escape Time Coloring Algorithms

Linear Interpolation: $$\text{color}(c) = \frac{T(c)}{\text{MAX_ITER}} \times 255$$

Logarithmic Smoothing: $$\text{smooth_iter} = T(c) + 1 - \frac{\log(\log(|z_T|))}{\log(2)}$$

Histogram Coloring: Normalize iteration counts based on their frequency distribution.

Deep Zoom Techniques

For zooms beyond machine precision, implement:

Reference Point Method: Choose reference $c_0$ and compute: $$\delta c = c - c_0$$ $$\delta z_n = z_n - Z_n$$

where $Z_n$ is the reference orbit.

Series Approximation: Use Taylor series expansion: $$z_n \approx Z_n + A_n \delta c + B_n (\delta c)^2 + \cdots$$

Perturbation Theory

For computing deep zooms efficiently: $$z_{n+1} = z_n^2 + c = (Z_n + \delta z_n)^2 + (C + \delta c)$$ $$= Z_n^2 + C + 2Z_n\delta z_n + (\delta z_n)^2 + \delta c$$

This allows reuse of reference orbit computations across nearby points.

About

This repo implements the algorithm of Mandelbrot set to create a static as well as a dynamic representation of the set on a complex plane

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors