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
|
import numpy as np
import time
tic = time.time()
x1 = -2.0
x2 = 1.0
y1 = -1.0
y2 = 1.0
w = 150
h = 100
maxit = 127
def mandelbrot(x, y, maxit):
c = x + y*1j
z = 0 + 0j
it = 0
while abs(z) < 2 and it < maxit:
z = z**2 + c
it += 1
return it
dx = (x2 - x1) / w
dy = (y2 - y1) / h
C = np.empty([h, w], dtype='i')
for k in np.arange(h):
y = y1 + k * dy
for j in np.arange(w):
x = x1 + j * dx
C[k, j] = mandelbrot(x, y, maxit)
M = C
toc = time.time()
print('wall clock time: %8.2f seconds' % (toc-tic))
# eye candy (requires matplotlib)
if 1:
try:
from matplotlib import pyplot as plt
plt.imshow(M, aspect='equal')
try:
plt.nipy_spectral()
except AttributeError:
plt.spectral()
plt.pause(2)
except:
pass
|