[PYTHON MATRIX-SIG] 2 Bugs? and: Mandelbrot: another nice useless NumPy script?!
Rob.Hooft@embl-heidelberg.de
Rob.Hooft@embl-heidelberg.de
Sat, 7 Sep 1996 19:26:50 +0200 (MET DST)
I guess a lot of you have seen the nice posting on c.l.python where
Guido explains the fact that "lambda" can be used to obfuscate python
code beyond belief. I decided to rewrite the mandelbrot script he
sent there. First I did it in plain python (will post to c.l.python
later). My unobfuscated version was about 2 times faster than the
original :-)
But: if one wants to make a really fast python script with vector
code: why not use Numeric python. The result (3 times faster than the
fastest plain python version I could get) is appended to this mail.
To make this message at least a bit useful: are the following two
problems (which I ran in to while coding this) effect of a known bug
in NumPy1.0a2?
Python 1.4b3 (Aug 31 1996) [GCC 2.7.2p snapshot 960816]
Copyright 1991-1996 Stichting Mathematisch Centrum, Amsterdam
>>> from Numeric import *
>>> z=array(6*[0j])
>>> z**2 # v v Why are these not zero?
(0.+0.j) (0.+0.j) (0.+0.j) (1.+0.j) (1.+0.j) (0.+0.j)
>>> `z`
"array([0j, 0j, 0j, 0j, 0j, 0j], 'D')"
>>> z=array(6*[0j],Complex)
>>> z
0. 0. 0. 0. 0. 0. ### <==== Hey, these are reals!!!!
>>> `z`
"array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 'd')"
Regards,
Rob Hooft.
--------------------------------------------------------
#!/bin/env python
#
# Mandelbrot ASCII-art using Numeric Python 1.0alpha2
#
# Rob Hooft, 1996. Distribute freely.
from Numeric import *
def draw(LowX, HighX, LowY, HighY, stepx=80, stepy=24, 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 add.reduce(output)
if __name__ == "__main__":
print draw(-2.1, 0.7, -1.2, 1.2)
--
=== Rob.Hooft@EMBL-Heidelberg.DE http://www.Sander.EMBL-Heidelberg.DE/rob/ ==
==== In need of protein modeling? http://www.Sander.EMBL-Heidelberg.DE/whatif/
Validation of protein structures? http://biotech.EMBL-Heidelberg.DE:8400/ ====
== PGPid 0xFA19277D == Use Linux! Free Software Rules The World! =============
=================
MATRIX-SIG - SIG on Matrix Math for Python
send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================