Advertisement
Guest User

Untitled

a guest
Jan 18th, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.50 KB | None | 0 0
  1. from numpy import *
  2.  
  3. def mandel(n, m, itermax, xmin, xmax, ymin, ymax):
  4.     '''
  5.    Fast mandelbrot computation using numpy.
  6.  
  7.    (n, m) are the output image dimensions
  8.    itermax is the maximum number of iterations to do
  9.    xmin, xmax, ymin, ymax specify the region of the
  10.    set to compute.
  11.    '''
  12.     # The point of ix and iy is that they are 2D arrays
  13.     # giving the x-coord and y-coord at each point in
  14.     # the array. The reason for doing this will become
  15.     # clear below...
  16.     ix, iy = mgrid[0:n, 0:m]
  17.     # Now x and y are the x-values and y-values at each
  18.     # point in the array, linspace(start, end, n)
  19.     # is an array of n linearly spaced points between
  20.     # start and end, and we then index this array using
  21.     # numpy fancy indexing. If A is an array and I is
  22.     # an array of indices, then A[I] has the same shape
  23.     # as I and at each place i in I has the value A[i].
  24.     x = linspace(xmin, xmax, n)[ix]
  25.     y = linspace(ymin, ymax, m)[iy]
  26.     # c is the complex number with the given x, y coords
  27.     c = x+complex(0,1)*y
  28.     del x, y # save a bit of memory, we only need z
  29.     # the output image coloured according to the number
  30.     # of iterations it takes to get to the boundary
  31.     # abs(z)>2
  32.     img = zeros(c.shape, dtype=int)
  33.     # Here is where the improvement over the standard
  34.     # algorithm for drawing fractals in numpy comes in.
  35.     # We flatten all the arrays ix, iy and c. This
  36.     # flattening doesn't use any more memory because
  37.     # we are just changing the shape of the array, the
  38.     # data in memory stays the same. It also affects
  39.     # each array in the same way, so that index i in
  40.     # array c has x, y coords ix[i], iy[i]. The way the
  41.     # algorithm works is that whenever abs(z)>2 we
  42.     # remove the corresponding index from each of the
  43.     # arrays ix, iy and c. Since we do the same thing
  44.     # to each array, the correspondence between c and
  45.     # the x, y coords stored in ix and iy is kept.
  46.     ix.shape = n*m
  47.     iy.shape = n*m
  48.     c.shape = n*m
  49.     # we iterate z->z^2+c with z starting at 0, but the
  50.     # first iteration makes z=c so we just start there.
  51.     # We need to copy c because otherwise the operation
  52.     # z->z^2 will send c->c^2.
  53.     z = copy(c)
  54.     for i in xrange(itermax):
  55.         if not len(z): break # all points have escaped
  56.         # equivalent to z = z*z+c but quicker and uses
  57.         # less memory
  58.         multiply(z, z, z)
  59.         add(z, c, z)
  60.         # these are the points that have escaped
  61.         rem = abs(z)>2.0
  62.         # colour them with the iteration number, we
  63.         # add one so that points which haven't
  64.         # escaped have 0 as their iteration number,
  65.         # this is why we keep the arrays ix and iy
  66.         # because we need to know which point in img
  67.         # to colour
  68.         img[ix[rem], iy[rem]] = i+1
  69.         # -rem is the array of points which haven't
  70.         # escaped, in numpy -A for a boolean array A
  71.         # is the NOT operation.
  72.         rem = -rem
  73.         # So we select out the points in
  74.         # z, ix, iy and c which are still to be
  75.         # iterated on in the next step
  76.         z = z[rem]
  77.         ix, iy = ix[rem], iy[rem]
  78.         c = c[rem]
  79.     return img
  80.  
  81. if __name__=='__main__':
  82.     from pylab import *
  83.     import time
  84.     start = time.time()
  85.     I = mandel(1000, 1000, 100, 0.0, 1, .3, .38)
  86.     print 'Time taken:', time.time()-start
  87.     I[I==0] = 101
  88.     img = imshow(I.T, origin='lower left')
  89.     img.write_png('mandel3.png', noscale=True)
  90.     show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement