fractals with Python (was: against Mathematica [sci.fractals])

Tim Lavoie tim.lavoie at mts.net
Mon Aug 28 16:56:06 EDT 2000


In sci.fractals, Louis M. Pecora wrote:

>This I will take exception to.  I have been using Mathematica for
>years, actually since it's first incarnation as SMP.  Debuggging is not
>easy in Mathematica.  It's a great package, but it's very power and
>generality enables you to make some horrible mistakes and it will not
>catch them.  You want easy debugging?  Try Python.  Compared to any
>other language/package I've used (Basic, C, C++, Mathematica, MACSYMA,
>Fortran, Applescript, Unix-shell-scripts) it's a Dream.


Hi there,

I've been tinkering for a little while with generating fractals in Python,
so that I can tinker to my heart's content without being restricted by an
application's structure or assumptions.

One avenue I've been using started with Rob Hooft's ASCII Mandelbrot
program, which accompanies the NumTut package (attached to the end of this
message). 

My problem is, I'd like to use functionality which is both elegant to write
and quick to execute; however, this example processes the entire array every
time, including parts which have already finished. (And I'd like to preserve
the z and c arrays, so I can use their contents for different drawing
techniques; the method below mangles them when finished). In a sense, I
guess I want to take a "deep" rather than "wide" approach, processing a
pixel's worth at a time instead of the whole works. I've tried re-writing
this with iterative, nested-loop code; not only is it *slower*, it strikes
me as too, well, un-Pythonic.

Am I on the right path here, or should I try Something Completely Different?

   - Tim



#!/usr/bin/env python
#
# Mandelbrot ASCII-art using Numeric Python 1.0beta1
#
# Rob Hooft, 1996. Distribute freely.

from Numeric import *
import profile

def draw(LowX, HighX, LowY, HighY, stepx=80, stepy=50, maxiter=30):
	xx=arange(LowX,HighX,(HighX-LowX)/stepx)
	yy=arange(HighY,LowY,(LowY-HighY)/stepy)*1j
	c=ravel(xx+yy[:,NewAxis])
	z=zeros(c.shape,Complex)
	output=resize(array(['_'],'c'),c.shape)
	for iter in range(maxiter):
		z=z*z+c
		finished=greater(abs(z),2.0)
		c=where(finished,0+0j,c)
		z=where(finished,0+0j,z)
		output=where(finished,chr(66+iter),output)
	return output.tostring()


if __name__ == "__main__":
	print draw(-2.1, 0.7, -1.2, 1.2)





More information about the Python-list mailing list