1 | """Fundamental routines for computing the Mandelbrot set |
---|
2 | |
---|
3 | Ole Nielsen, SUT 2003 |
---|
4 | """ |
---|
5 | |
---|
6 | def balance(N, P, p): |
---|
7 | """Compute p'th interval when N is distributed over P bins. |
---|
8 | """ |
---|
9 | |
---|
10 | from math import floor |
---|
11 | |
---|
12 | L = int(floor(float(N)/P)) |
---|
13 | K = N - P*L |
---|
14 | if p < K: |
---|
15 | Nlo = p*L + p |
---|
16 | Nhi = Nlo + L + 1 |
---|
17 | else: |
---|
18 | Nlo = p*L + K |
---|
19 | Nhi = Nlo + L |
---|
20 | |
---|
21 | return Nlo, Nhi |
---|
22 | |
---|
23 | |
---|
24 | def calculate_point(c, kmax): |
---|
25 | """Python version for calculating on point of the set |
---|
26 | This is slow and for reference purposes only. |
---|
27 | Use version from mandel_ext instead. |
---|
28 | """ |
---|
29 | |
---|
30 | z = complex(0,0) |
---|
31 | |
---|
32 | count = 0 |
---|
33 | while count < kmax and abs(z) <= 2: |
---|
34 | z = z*z + c |
---|
35 | count += 1 |
---|
36 | |
---|
37 | return count |
---|
38 | |
---|
39 | |
---|
40 | def calculate_region(real_min, real_max, imag_min, imag_max, kmax, M, N, |
---|
41 | Mlo = 0, Mhi = None, Nlo = 0, Nhi = None): |
---|
42 | """Calculate the mandelbrot set in the given region with resolution M by N |
---|
43 | If Mlo, Mhi or Nlo, Nhi are specified computed only given subinterval. |
---|
44 | """ |
---|
45 | |
---|
46 | from numpy import zeros |
---|
47 | from mandel_ext import calculate_point #Fast C implementation |
---|
48 | |
---|
49 | if Mhi is None: Mhi = M |
---|
50 | if Nhi is None: Nhi = N |
---|
51 | |
---|
52 | real_step = (real_max-real_min)/M |
---|
53 | imag_step = (imag_max-imag_min)/N |
---|
54 | |
---|
55 | A = zeros((M, N), dtype='i') # Create M x N matrix |
---|
56 | |
---|
57 | for i in range(Mlo, Mhi): |
---|
58 | for j in range(Nlo, Nhi): |
---|
59 | c = complex(real_min + i*real_step, imag_min + j*imag_step) |
---|
60 | A[i,j] = calculate_point(c, kmax) |
---|
61 | |
---|
62 | return A |
---|
63 | |
---|
64 | |
---|
65 | |
---|
66 | def calculate_region_cyclic(real_min, real_max, imag_min, imag_max, kmax, |
---|
67 | M, N, p=0, P=1, row = 1): |
---|
68 | """Calculate rows p+nP, n in N of the mandelbrot set in the given region |
---|
69 | with resolution M by N |
---|
70 | |
---|
71 | This is the most efficient way of partitioning the work. |
---|
72 | """ |
---|
73 | |
---|
74 | |
---|
75 | from numpy import zeros |
---|
76 | from mandel_ext import calculate_point #Fast C implementation |
---|
77 | |
---|
78 | real_step = (real_max-real_min)/M |
---|
79 | imag_step = (imag_max-imag_min)/N |
---|
80 | |
---|
81 | A = zeros((M, N), dtype='i') # Create M x N matrix |
---|
82 | |
---|
83 | if row: |
---|
84 | for i in range(M): |
---|
85 | if i%P == p: |
---|
86 | for j in range(N): |
---|
87 | c = complex(real_min + i*real_step, imag_min + j*imag_step) |
---|
88 | A[i,j] = calculate_point(c, kmax) |
---|
89 | else: |
---|
90 | for j in range(N): |
---|
91 | if j%P == p: |
---|
92 | for i in range(M): |
---|
93 | c = complex(real_min + i*real_step, imag_min + j*imag_step) |
---|
94 | A[i,j] = calculate_point(c, kmax) |
---|
95 | return A |
---|
96 | |
---|
97 | |
---|