-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathmandelbrot.py
More file actions
67 lines (51 loc) · 1.87 KB
/
Copy pathmandelbrot.py
File metadata and controls
67 lines (51 loc) · 1.87 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
from numba import jit, prange
import numpy as np
from PIL import Image
def compute_point(c, max_iter=200):
i = -1
z = complex(0, 0)
while abs(z) < 2:
i += 1
if i == max_iter:
break
z = z**2 + c
return 255 - (255 * i) // max_iter
compute_point_numba = jit()(compute_point)
compute_point_numba_forceobj = jit(forceobj=True)(compute_point)
compute_point_numba(complex(4, 4))
compute_point_numba_forceobj(complex(4, 4))
size = 2000
start = -1.5, -1.3
end = 0.5, 1.3
img_array = np.empty((size, size), dtype=np.uint8)
def do_all(size, start, end, img_array, compute_fun):
startx, starty = start
endx, endy = end
for xp in range(size):
x = (endx - startx)*(xp/size) + startx # precision issues
# x = (xp - size/2) / (size/4) # precision issues
# print(x)
for yp in range(size):
y = (endy - starty)*(yp/size) + starty # precision issues
img_array[yp, xp] = compute_fun(complex(x,y))
do_all(size, start, end, img_array, compute_point_numba)
img = Image.fromarray(img_array, mode="P")
img.save("mandelbrot.png")
# img.putpalette(ImagePalette.sepia())
# compute_point_typed = jit(compute_point, "uint8(complex128)", nopython=True)
@jit(nopython=True, parallel=True, nogil=True)
def pdo_all(size, start, end, img_array, compute_fun):
startx, starty = start
endx, endy = end
for xp in prange(size):
x = (endx - startx)*(xp/size) + startx # precision issues
# x = (xp - size/2) / (size/4) # precision issues
# print(x)
for yp in range(size): # put prange here?
# Loops are fine with Numba
y = (endy - starty)*(yp/size) + starty # precision issues
b = complex(0, 0)
b = compute_fun(complex(x, y))
img_array[yp, xp] = b
# parallel_diagnostics - just note
# print(threading_layer())