-
Notifications
You must be signed in to change notification settings - Fork 0
/
fractal_qt4_mpl_lib.py
62 lines (55 loc) · 2.05 KB
/
fractal_qt4_mpl_lib.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
55
56
57
58
59
60
61
62
#!/usr/bin/python3
'''
@file fractal_qt4_mpl-lib.py
@author Philip Wiese
@date 12 Okt 2016
@brief Calculates Mandelbrot set
'''
import time
import numpy as np
class fractal_data():
def __init__(self, data, calc_t, shape=(400,400), datatype=np.int):
self.data = np.zeros(shape, dtype = datatype)
self.data = data
self.calc_time = calc_t
self.max = np.amax(data)
self.min = np.min(data[data>0])
def info(self):
print "Data Shape: " + str(self.data.shape)
print "Calculation Time: %.3fs" % self.calc_time
print "Maximum Value: %d" % self.max
print "Minimum Value >0: %d" % self.min
def mandelbrot(re_min, re_max, im_min, im_max, max_betr, max_iter, res=400, cont=False):
# Save Startime
start_t = time.time()
# ix and iy is an 2-dimensional array representing the pixels
pix_y = int(round(res / (re_max - re_min) * (im_max - im_min)))
pix_x = res
px, py = np.mgrid[0:pix_x, 0:pix_y]
# x, y are the values of the pixels
x = np.linspace(re_min, re_max, pix_x)[px]
y = np.linspace(im_min, im_max, pix_y)[py]
c = x+complex(0,1)*y
# No need of these variables bacuase we only use c
del x,y
img = np.zeros(c.shape, dtype= np.float)
px.shape = py.shape = c.shape = pix_x*pix_y
z = np.copy(c)
for i in xrange(max_iter):
if not len(z): break;
np.multiply(z, z, z)
np.add(z, c, z)
# Create boolean array with all points that are bigger than <max_betr>
rem = abs(z)>max_betr
# Saves the value of <i+i> in img array for all escaped points
if cont: img[px[rem], py[rem]] = (i+ 1) - np.log( np.log(abs(z[rem]) ) / 2 / np.log(2) ) / np.log(2)
else: img[px[rem], py[rem]] = (i+ 1)
# invert boolean array
rem = -rem
# remove all escaped points
z = z[rem]
px, py = px[rem], py[rem]
c = c[rem]
calc_t = time.time()-start_t
data = fractal_data(img, calc_t, shape=img.shape, datatype=img.dtype)
return data