-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandelbrot_datatypes.py
55 lines (40 loc) · 1.92 KB
/
mandelbrot_datatypes.py
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
import numpy
import time
pRE = 10000
pIM = 10000
iterations = 100
threshold = 2
def mandelbrot(float_data_type, complex_data_type):
start_time = time.time()
# Generates linear spaces with pRE and pIM elements respectively around the plane of the Mandelbrot set
x_space = numpy.linspace(-2.3, 0.8, pRE, dtype=float_data_type).reshape((1, pRE))
y_space = numpy.linspace(-1.2, 1.2, pIM, dtype=float_data_type).reshape((pIM, 1))
# Generate a 2D array for each dimension of the complex plane
complete_space = x_space + y_space * 1j
# Generate a 2D array of zeroes, which is then converted to a boolean data type array
mandelbrot_mask = numpy.ones_like(complete_space, dtype=bool)
z = numpy.zeros_like(complete_space, dtype=complex_data_type)
divergence_time = numpy.zeros(complete_space.shape, dtype=float_data_type)
# Iterate over the complex plane
for i in range(iterations):
# Apply the Mandelbrot formula
z[mandelbrot_mask] = z[mandelbrot_mask] * z[mandelbrot_mask] + complete_space[mandelbrot_mask]
# Check each element of the array for divergence
diverged = mandelbrot_mask & (numpy.abs(z) > threshold)
# Update the divergence time
divergence_time[diverged] = i
# Check if the absolute value of z is greater than the threshold
mandelbrot_mask[numpy.abs(z) > threshold] = False
end_time = time.time()
print("Float data-type:", float_data_type, "Complex data-type:", complex_data_type,
"Computation time:", round(end_time - start_time, 5), "s")
return divergence_time
def main(show_figure=False):
mandelbrot(numpy.float64, numpy.complex64)
mandelbrot(numpy.float64, numpy.complex128)
mandelbrot(numpy.float32, numpy.complex64)
mandelbrot(numpy.float32, numpy.complex128)
mandelbrot(numpy.float16, numpy.complex64)
mandelbrot(numpy.float16, numpy.complex128)
if __name__ == '__main__':
main()