Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/python/bench/src/mandel.py
1067 views
1
# Benchmark based on what is discussed here: https://news.ycombinator.com/item?id=28814141
2
3
from bench import register, all
4
5
# Want this to run on any Python without numpy.
6
def arange(a, b, step):
7
eps = 0.00000001
8
v = []
9
x = a
10
while x + eps < b:
11
v.append(x)
12
x += step
13
return v
14
15
16
def mandelbrot_iter(c):
17
z = complex(0, 0)
18
for iters in range(200):
19
if abs(z) >= 2:
20
return iters
21
z = z * z + c
22
return iters
23
24
25
def mandelbrot(size=200):
26
pix = list(range(size * size))
27
xPts = arange(-1.5, 0.5, 2.0 / size)
28
yPts = arange(-1, 1, 2.0 / size)
29
30
for xx, x in enumerate(xPts):
31
for yy, y in enumerate(yPts):
32
pix[xx + size * yy] = mandelbrot_iter(complex(x, y))
33
return pix
34
35
36
register('mandelbrot', mandelbrot)
37
38
# quick consistency check:
39
assert mandelbrot(5) == [
40
2, 3, 3, 5, 4, 3, 4, 6, 199, 199, 5, 199, 199, 199, 199, 5, 199, 199, 199,
41
199, 3, 4, 6, 199, 199
42
]
43
44
if __name__ == '__main__':
45
all()
46
47