NumPy vs. iterative vs. ???

Huaiyu Zhu hzhu at yahoo.com
Wed Aug 30 20:16:52 EDT 2000


On Wed, 30 Aug 2000 19:14:47 GMT, Tim Lavoie <tim.lavoie at mts.net> wrote:
>I've been tinkering (no, not TkInter-ing, at least yet) with fractals in
>Python, but am not sure if my approach is sensible. By the way, I mean
>iterative-by-nature, Mandelbrot set sort of fractals.
>
>I like the idea of NumPy, in that it does a lot for me and makes for more
>elegant code. However, I'm not sure how well it fits this application, since
>it prefers to apply each operation to every element in the array. Also,
>keeping several arrays (some complex-type) for the whole works is very
>RAM-hungry as well, at an element per pixel per array.
[snip]

I played with your interesting code.  It could be speeded up by approx
factor of 2.  On my machine, it takes 2.8 (real) seconds to finish one
calculation and 0.3 second to draw.  Calculating and drawing 12 images takes
38 seconds, using a total of 19MB memory.  Neither time nor memory would be
a problem on today's machines.

Huaiyu


#-------------------- %<---------------------------------
#!/usr/bin/env python
#
# Mandelbrot ASCII-art using Numeric Python 1.0beta1
#
# Rob Hooft, 1996. Distribute freely.
#
# Additional kluges and hackery, Tim Lavoie, 2000
# Huaiyu Zhu, 2000. Change to class, grayscale view, some ckeanup.

from Numeric import arange, NewAxis, zeros, where, greater
from NumTut import view
from time import time

class Mandelbrot:
    fraccode = 0
    def __init__(self, frac='z*z+c'):
        self._time = time()
        self.fraccode = compile(frac, '<string>', 'eval')

    def set_size(self, x0=-2.1, x1=0.7, y0=-1.2, y1=1.2, nx=20, ny=20):
        self.xx = arange(x0, x1, (x1-x0)/nx)
        self.yy = arange(y1, y0, (y0-y1)/ny)*1j
    
    def calc(self, c0, z0, maxiter=30):
        print time() - self._time
        c = self.xx + self.yy[:,NewAxis]
        z = zeros(c.shape)
        finished = zeros(c.shape)
        print z.shape, c.shape, time() - self._time
    
        fraccode = self.fraccode
        for iter in range(maxiter):
            z = eval(fraccode)
            finished = greater(abs(z), 2)
            #c = where(finished, c0, c) # external bands
            z = where(finished, z0, z)
        self.result = z.real
        #self.result = finished         # black and white
        print z.shape, c.shape, time() - self._time

    def view(self):
        view(self.result)
        print time() - self._time

if __name__ == "__main__":
    f = Mandelbrot()
    f.set_size(nx=250, ny=250)
    for c0 in [0.0, 0.15, 0.25]:
        for z0 in [1.0, 1.1, 1.25, 1.4]:
            f.calc(c0, z0, maxiter=30)
            f.view()
    raw_input("Press Enter ...")



More information about the Python-list mailing list