[Scipy-svn] r4661 - branches/fast_vectorize/examples
scipy-svn@scip...
scipy-svn@scip...
Wed Aug 20 19:35:53 CDT 2008
Author: ilan
Date: 2008-08-20 19:35:44 -0500 (Wed, 20 Aug 2008)
New Revision: 4661
Added:
branches/fast_vectorize/examples/compdec.py
branches/fast_vectorize/examples/mandel.py
Modified:
branches/fast_vectorize/examples/benchmark.py
Log:
Added examples, and decreased array size for benchmark.
Modified: branches/fast_vectorize/examples/benchmark.py
===================================================================
--- branches/fast_vectorize/examples/benchmark.py 2008-08-21 00:01:08 UTC (rev 4660)
+++ branches/fast_vectorize/examples/benchmark.py 2008-08-21 00:35:44 UTC (rev 4661)
@@ -75,7 +75,7 @@
#############################################################
-x = linspace(0, 1, 10000*1000)
+x = linspace(0, 1, 1000*1000)
start_time = time.time()
b_y = empty_like(x)
Added: branches/fast_vectorize/examples/compdec.py
===================================================================
--- branches/fast_vectorize/examples/compdec.py 2008-08-21 00:01:08 UTC (rev 4660)
+++ branches/fast_vectorize/examples/compdec.py 2008-08-21 00:35:44 UTC (rev 4661)
@@ -0,0 +1,34 @@
+"""
+I have written a class which allows using the compiled version of a
+Python functions simply by adding a decorator to the function.
+The nice thing about doing things this way is that all the code is pure
+Python code, and switching between the compiled and uncompiled version
+of the function is as simple as possible.
+"""
+from pypy.translator.interactive import Translation
+
+class compdec:
+ def __init__(self, func):
+ self.func = func
+ self.argtypes = None
+
+ def __call__(self, *args):
+ argtypes = tuple(type(arg) for arg in args)
+ if argtypes != self.argtypes:
+ self.argtypes = argtypes
+ t = Translation(self.func)
+ t.annotate(argtypes)
+ self.cfunc = t.compile_c()
+
+ return self.cfunc(*args)
+
+@compdec
+def is_prime(n):
+ if n < 2:
+ return False
+ for i in xrange(2, n):
+ if n%i == 0:
+ return False
+ return True
+
+print sum(is_prime(n) for n in xrange(100000))
Added: branches/fast_vectorize/examples/mandel.py
===================================================================
--- branches/fast_vectorize/examples/mandel.py 2008-08-21 00:01:08 UTC (rev 4660)
+++ branches/fast_vectorize/examples/mandel.py 2008-08-21 00:35:44 UTC (rev 4661)
@@ -0,0 +1,54 @@
+#!/usr/bin/env python
+from numpy import asarray, concatenate, ogrid, uint8
+from PIL import Image
+
+import sys
+sys.path.append('../mkufunc')
+from fast_vectorize import fast_vectorize
+
+
+@fast_vectorize([(float, float, int)])
+def mandel(cr, ci):
+ d = 1
+ zr = cr
+ zi = ci
+ for d in xrange(1, 1000):
+ zr2 = zr * zr
+ zi2 = zi * zi
+ if zr2 + zi2 > 16:
+ return d
+ zi = 2.0 * zr * zi + ci
+ zr = zr2 - zi2 + cr
+ else:
+ return -1
+
+@fast_vectorize(int)
+def red(i):
+ if i == -1: return 0
+ return (i * 5) % 256
+
+@fast_vectorize(int)
+def green(i):
+ if i == -1: return 0
+ return (i % 16) * 15
+
+@fast_vectorize(int)
+def blue(i):
+ if i == -1: return 0
+ return 255
+
+
+w, h = 1200, 900
+
+y, x = ogrid[-1.5:+1.5:h*1j, -2.75:+1.15:w*1j]
+
+mand = mandel(x, y)
+
+r = asarray(red(mand), dtype=uint8).reshape(h, w, 1)
+g = asarray(green(mand), dtype=uint8).reshape(h, w, 1)
+b = asarray(blue(mand), dtype=uint8).reshape(h, w, 1)
+
+a = concatenate((r, g, b), axis=2).reshape(h, w, 3)
+
+im = Image.fromarray(a)
+im.save('mandel.png')
More information about the Scipy-svn
mailing list