[Scipy-svn] r4669 - branches/fast_vectorize/examples

scipy-svn@scip... scipy-svn@scip...
Sat Aug 23 13:44:24 CDT 2008


Author: ilan
Date: 2008-08-23 13:44:21 -0500 (Sat, 23 Aug 2008)
New Revision: 4669

Added:
   branches/fast_vectorize/examples/README.txt
Removed:
   branches/fast_vectorize/examples/mandel_mkImage.py
   branches/fast_vectorize/examples/mandel_py.py
Modified:
   branches/fast_vectorize/examples/benchmark.py
   branches/fast_vectorize/examples/compdec.py
   branches/fast_vectorize/examples/primes.py
Log:
Improved documentation of the examples.

Added: branches/fast_vectorize/examples/README.txt
===================================================================
--- branches/fast_vectorize/examples/README.txt	2008-08-23 18:10:44 UTC (rev 4668)
+++ branches/fast_vectorize/examples/README.txt	2008-08-23 18:44:21 UTC (rev 4669)
@@ -0,0 +1,21 @@
+
+
+benchmark.py
+    Some speed comparisons between dirrerent ways of vectorizing
+    and calculating a simple polynom.
+
+compdec.py
+    A small example illustrates how PyPy's translator package can
+    be used to write compile decorator.
+
+mandel.py
+    A program which uses fast_vectorize to calulate an image file
+    of the well-known Mandelbort set.
+
+mandel_c.py
+    A program which illustrates how to create a Ufunc by writing
+    C code into weave.
+
+primes.py
+    A Python function for counting primes is first executed as a
+    regular function, as well as after "fast_vectorization".


Property changes on: branches/fast_vectorize/examples/benchmark.py
___________________________________________________________________
Name: svn:executable
   - *

Modified: branches/fast_vectorize/examples/compdec.py
===================================================================
--- branches/fast_vectorize/examples/compdec.py	2008-08-23 18:10:44 UTC (rev 4668)
+++ branches/fast_vectorize/examples/compdec.py	2008-08-23 18:44:21 UTC (rev 4669)
@@ -1,9 +1,10 @@
 """
-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.  
+This small example illustrates how PyPy's translator package can
+be used to write compile decorator.  This is done by 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 very simple.
 """
 from pypy.translator.interactive import Translation
 
@@ -22,13 +23,14 @@
 
         return self.cfunc(*args)
 
-@compdec
-def is_prime(n):
-    if n < 2:
-        return False
-    for i in xrange(2, n):
-        if n%i == 0:
+if __name__ == '__main__':
+    @compdec
+    def is_prime(n):
+        if n < 2:
             return False
-    return True
+        for i in xrange(2, n):
+            if n%i == 0:
+                return False
+        return True
 
-print sum(is_prime(n) for n in xrange(100000))
+    print sum(is_prime(n) for n in xrange(100000))

Deleted: branches/fast_vectorize/examples/mandel_mkImage.py
===================================================================
--- branches/fast_vectorize/examples/mandel_mkImage.py	2008-08-23 18:10:44 UTC (rev 4668)
+++ branches/fast_vectorize/examples/mandel_mkImage.py	2008-08-23 18:44:21 UTC (rev 4669)
@@ -1,47 +0,0 @@
-#!/usr/bin/env python
-
-# Before running this be sure to apply Travis Oliphant's patch to PIL:
-# http://www.scipy.org/Cookbook/PIL
-
-from numpy import asarray, concatenate, ogrid, uint8
-from PIL import Image
-
-
-import sys
-sys.path.append('../mkufunc')
-from fast_vectorize import fast_vectorize
-
-
-from mandel_c import mandel
-
-
-@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')

Deleted: branches/fast_vectorize/examples/mandel_py.py
===================================================================
--- branches/fast_vectorize/examples/mandel_py.py	2008-08-23 18:10:44 UTC (rev 4668)
+++ branches/fast_vectorize/examples/mandel_py.py	2008-08-23 18:44:21 UTC (rev 4669)
@@ -1,36 +0,0 @@
-#!/usr/bin/env python
-from numpy import array
-
-import sys
-sys.path.append('../mkufunc')
-from fast_vectorize import fast_vectorize
-
-
-D = 1000
-
-
-@fast_vectorize([(float, float, int)])
-def mandel(cr, ci):
-    d = 1
-    zr = cr
-    zi = ci
-    for d in xrange(1, D):
-        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
-
-
-
-if __name__ == '__main__':
-
-    assert mandel(-1, .3) == 36
-    assert mandel(0, 0) == -1
-    assert mandel(10, 10) == 1
-    assert (mandel(array([-1, 0, 10]), array([.3, 0, 10])) ==
-            array([36, -1,  1])).all()
-    


Property changes on: branches/fast_vectorize/examples/primes.py
___________________________________________________________________
Name: svn:executable
   - *



More information about the Scipy-svn mailing list