import numpy as np def mandel(xrange, yrange, maxiter): if xrange.ndim != 1 or yrange.ndim != 1: raise ValueError("need 1D ranges for x and y") ny = yrange.size nx = xrange.size niter = np.zeros([ny, nx], dtype=np.int) for i in range(ny): y0 = yrange[i] for j in range(nx): x0 = xrange[j] # we do not manipulate python complex numbers, but write # the complex operations explicitly # initial value x = x0 y = y0 for k in range(maxiter): x2 = x * x y2 = y * y if x2 + y2 > 4: # check absolute value break # update series y = 2 * x * y + y0 x = x2 - y2 + x0 # maxiter or last iteration niter[i, j] = k return niter