Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Mandelbrot Microscope

Reply
Thread Tools

Mandelbrot Microscope

 
 
Nick J Chackowsky
Guest
Posts: n/a
 
      07-08-2004
Okay, here's my naive solution to coding a Mandelbrot microscope in
Python... but it's so slow! Would anyone care to comment on how to speed
this up?

Nick.

#
#This uses the graphics.py module included with John Zelle's
#textbook. File available at http://mcsp.wartburg.edu/zelle/python/
#

from graphics import *

DEPTH = 200

def f(z):
C = z
count = 0
while count < DEPTH and abs(z) < 3.0:
z = z*z + C
count += 1
if count == DEPTH:
count -= 1
return count

def rangecolors(n):
""" Return a list of nicely blended colors """
clist = []
r, g, b = 13, 101, 137
for c in range(n):
clist.append(color_rgb(r,g,b))
r += 5
g += 7
b += 11
if r > 255: r = 0
if g > 255: g = 0
if b > 255: b = 0
#end for
return clist

def main():
llx, lly = -3.0, 3.0 #lower left
urx, ury = 3.0, -3.0 #upper right
WINDOWSZ = 200
clist = rangecolors(DEPTH)
keepgoing = True
while keepgoing:
w = GraphWin("Mandelbrot", WINDOWSZ, WINDOWSZ)
w.setCoords(llx, lly, urx, ury)
delta = abs(llx-urx)/float(WINDOWSZ)
re = llx
while re < urx:
im = ury
while im < lly:
w.plot(re, im, clist[f(complex(re, im))])
im += delta
#end while
re += delta
#end while
p1 = w.getMouse()
p2 = w.getMouse()

#for now, assume that the user input a square area correctly
llx = p1.getX()
lly = p1.getY()
urx = p2.getX()
ury = p2.getY()

w.close()
#end while

main()
 
Reply With Quote
 
 
 
 
Jeff Epler
Guest
Posts: n/a
 
      07-09-2004
IMO you won't render mandelbrots at an acceptable speed using Python.

Psyco might help, but probably not (it's unlikely to support complex
numbers)

You might speed things up a bit by doing something clever in numarray,
effectively doing an iteration of the whole window worth of points in
each loop, avoiding lots of interpreter overhead.

But to get it competitive with a native code mandelbrot calculator,
well, you'll probably have to write something that compiles to native
code.

You could also wait a few years, computers should be faster by then...

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFA7eKHJd01MZaTXX0RArlUAKCBh1t3nyHXkfyvO7NVSD 2pWVqTjACeKskd
48lvGJhqQIMTT4S8I7elAqQ=
=cWy3
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
 
Jeff Epler
Guest
Posts: n/a
 
      07-09-2004
Here's a program which calculates a similar result to yours using
numarray. It calculates a 50x50 grid to 500 iterations in 3 seconds on
a 750MHz machine. I didn't run yours so I don't know how this compares.

Hm, timeit.py says that your function runs in 2 ms per point with DEPTH=500
given the point 0+0j, and of course runs faster on points that escape.
Your program may run faster than the numarray implementation, or it
might be a wash. (2ms * 50 * 50 == 5 seconds)

This isn't a natural application for numarray.


from numarray import *

DEPTH = 500

def coords(x0, y0, x1, y1, w, h):
rx = arange(w).astype(Float32) * (x1-x0) / (w-1) + x0
ry = arange(w).astype(Float32) * (y1-y0) / (h-1) + y0
return add.outer(rx * 1j, ry)

def f(z):
C = z.copy()
d = zeros(z.shape)

for c in range(DEPTH):
z = z*z - C
zz = abs(z) > 3
#print zz; print
d += where(zz, 1, 0)
if not (c % : # keep contents of z from overflowing
z = where(zz, 10, z)
return d

# 20x20 prints nicely on the screen in numbers
c = coords(-1.5, -1.5, 1.5, 1.5, 20, 20)
print f(c)/10

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD4DBQFA7emfJd01MZaTXX0RAhxLAJddbaghpLsaap8jSaPot8 kYq9OOAJ9UqAcb
kSCcEb0lb3ZacK36KYOAGw==
=ZFA5
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Microscope. Soup Computer Support 6 02-21-2004 06:52 PM
FS: Digital camera microscope adapters Richard J Kinch Digital Photography 2 01-15-2004 04:03 PM
Coolpix 5700 on microscope CR Optiker Digital Photography 8 11-07-2003 04:01 PM
Need Help speeding up mandelbrot algo Chris C Programming 2 10-31-2003 03:17 AM
Re: 5 Megapixels vs Kodachrome & Velvia - Microscope Views Rafe B. Digital Photography 2 09-15-2003 06:42 PM



Advertisments