-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpybrot.py
More file actions
100 lines (75 loc) · 2.78 KB
/
Copy pathpybrot.py
File metadata and controls
100 lines (75 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
PyBrot - Pure Python Mandelbrot Set Computation
This is a reference implementation in pure Python (no shortcuts like NumPy)
to compare against the SQL-based DuckBrot benchmark. It computes the same
Mandelbrot set using traditional procedural code.
Author: Thomas Zeutschler
License: MIT
GitHub: https://github.com/Zeutschler/sql-mandelbrot-benchmark
"""
from utils import save_mandelbrot_image
def mandelbrot_iteration(cx, cy, max_iterations):
"""
Calculate the number of iterations for a single point in the complex plane.
Args:
cx: Real part of complex number c
cy: Imaginary part of complex number c
max_iterations: Maximum number of iterations to test
Returns:
Number of iterations before escape (or max_iterations if bounded)
"""
zx = 0.0
zy = 0.0
iteration = 0
while iteration < max_iterations:
# Check if point has escaped (magnitude > 2, or magnitude² > 4)
if (zx * zx + zy * zy) > 4.0:
break
# Compute next iteration: z = z² + c
zx_new = zx * zx - zy * zy + cx
zy_new = 2.0 * zx * zy + cy
zx = zx_new
zy = zy_new
iteration += 1
return iteration
def compute_mandelbrot(width, height, max_iterations):
"""
Compute the Mandelbrot set for the entire image.
Args:
width: Image width in pixels
height: Image height in pixels
max_iterations: Maximum iterations per pixel
Returns:
2D list of iteration counts (height x width)
"""
# Initialize result array
mandelbrot = [[0 for _ in range(width)] for _ in range(height)]
# Compute complex coordinates for each pixel
for y in range(height):
for x in range(width):
# Map pixel coordinates to complex plane
# Standard Mandelbrot view: real [-2.5, 1.0], imaginary [-1.0, 1.0]
cx = -2.5 + (x * 3.5 / (width - 1))
cy = -1.0 + (y * 2.0 / (height - 1))
# Compute iterations for this point
mandelbrot[y][x] = mandelbrot_iteration(cx, cy, max_iterations)
return mandelbrot
def run_pybrot(width, height, max_iterations):
"""
Compute Mandelbrot set using pure Python (no shortcuts).
Args:
width: Image width in pixels
height: Image height in pixels
max_iterations: Maximum iterations per pixel
Returns:
2D list of iteration counts
"""
return compute_mandelbrot(width, height, max_iterations)
if __name__ == "__main__":
# Standalone execution
WIDTH = 1400
HEIGHT = 800
MAX_ITERATIONS = 256
print(f"Computing Mandelbrot set ({WIDTH}x{HEIGHT}, max {MAX_ITERATIONS} iterations)...")
result = run_pybrot(WIDTH, HEIGHT, MAX_ITERATIONS)
save_mandelbrot_image(result, MAX_ITERATIONS, 'pybrot.png')