[Scipy-svn] r4554 - trunk/scipy/sandbox/mkufunc/examples

scipy-svn at scipy.org scipy-svn at scipy.org
Sun Jul 20 23:17:18 EDT 2008


Author: ilan
Date: 2008-07-20 22:17:18 -0500 (Sun, 20 Jul 2008)
New Revision: 4554

Added:
   trunk/scipy/sandbox/mkufunc/examples/mandel_c.py
   trunk/scipy/sandbox/mkufunc/examples/mandel_mkImage.py
   trunk/scipy/sandbox/mkufunc/examples/mandel_py.py
Removed:
   trunk/scipy/sandbox/mkufunc/examples/mandel-c.py
   trunk/scipy/sandbox/mkufunc/examples/mandel-py.py
Log:
Improved Mandelbrot set example

Deleted: trunk/scipy/sandbox/mkufunc/examples/mandel-c.py
===================================================================
--- trunk/scipy/sandbox/mkufunc/examples/mandel-c.py	2008-07-20 01:00:07 UTC (rev 4553)
+++ trunk/scipy/sandbox/mkufunc/examples/mandel-c.py	2008-07-21 03:17:18 UTC (rev 4554)
@@ -1,88 +0,0 @@
-#!/usr/bin/env python
-"""
->>> mandel(-1, .3)
-36
->>> mandel(0, 0)
--1
->>> mandel(10, 10)
-1
->>> mandel(array([-1, 0, 10]), array([.3, 0, 10]))
-array([36, -1,  1])
-"""
-import hashlib
-
-from numpy import array
-from scipy import weave
-
-support_code = '''
-#define D 1000
-
-long iterations(double cr, double ci)
-{
-        long d = 1;
-        double zr=cr, zi=ci, zr2, zi2;
-        for(;;) {
-                zr2 = zr * zr;
-                zi2 = zi * zi;
-                if( zr2+zi2 > 16.0 ) return d;
-                if( ++d == D ) return -1;
-                zi = 2.0 * zr * zi + ci;
-                zr = zr2 - zi2 + cr;
-        }
-}
-
-static void
-PyUFunc_0(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
-{
-        npy_intp i, n;
-        npy_intp is0 = steps[0];
-        npy_intp is1 = steps[1];
-        npy_intp os = steps[2];
-        char *ip0 = args[0];
-        char *ip1 = args[1];
-        char *op = args[2];
-        n = dimensions[0];
-        
-        for(i = 0; i < n; i++) {
-                *(long *)op = iterations(*(double *)ip0,
-                                         *(double *)ip1);
-                ip0 += is0;
-                ip1 += is1;
-                op += os;
-        }
-}
-
-static PyUFuncGenericFunction f_functions[] = {
-        PyUFunc_0,
-};
-
-static char f_types[] = {
-        NPY_DOUBLE, NPY_DOUBLE, NPY_LONG,
-};
-'''
-ufunc_info = weave.base_info.custom_info()
-ufunc_info.add_header('"numpy/ufuncobject.h"')
-
-mandel = weave.inline('/*' + hashlib.md5(support_code).hexdigest() + '''*/
-import_ufunc();
-
-return_val = PyUFunc_FromFuncAndData(
-            f_functions,
-            NULL,
-            f_types,
-            1,             /* ntypes */
-            2,             /* nin */
-            1,             /* nout */
-            PyUFunc_None,  /* identity */
-            "mandel",      /* name */
-            "returns number of iterations from cr, ci",    /* doc */
-            0);
-            ''',
-                     support_code=support_code,
-                     verbose=0,
-                     customize=ufunc_info)
-
-
-if __name__ == '__main__':
-    import doctest
-    doctest.testmod()

Deleted: trunk/scipy/sandbox/mkufunc/examples/mandel-py.py
===================================================================
--- trunk/scipy/sandbox/mkufunc/examples/mandel-py.py	2008-07-20 01:00:07 UTC (rev 4553)
+++ trunk/scipy/sandbox/mkufunc/examples/mandel-py.py	2008-07-21 03:17:18 UTC (rev 4554)
@@ -1,37 +0,0 @@
-#!/usr/bin/env python
-"""
->>> mandel(-1, .3)
-36
->>> mandel(0, 0)
--1
->>> mandel(10, 10)
-1
->>> mandel(array([-1, 0, 10]), array([.3, 0, 10]))
-array([36, -1,  1])
-"""
-from numpy import array
-from mkufunc.api import mkufunc
-
-D = 1000
-
-
- at mkufunc([(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__':
-    import doctest
-    doctest.testmod()

Added: trunk/scipy/sandbox/mkufunc/examples/mandel_c.py
===================================================================
--- trunk/scipy/sandbox/mkufunc/examples/mandel_c.py	2008-07-20 01:00:07 UTC (rev 4553)
+++ trunk/scipy/sandbox/mkufunc/examples/mandel_c.py	2008-07-21 03:17:18 UTC (rev 4554)
@@ -0,0 +1,82 @@
+#!/usr/bin/env python
+import hashlib
+
+from numpy import array
+from scipy import weave
+
+support_code = '''
+#define D 1000
+
+long iterations(double cr, double ci)
+{
+        long d = 1;
+        double zr=cr, zi=ci, zr2, zi2;
+        for(;;) {
+                zr2 = zr * zr;
+                zi2 = zi * zi;
+                if( zr2+zi2 > 16.0 ) return d;
+                if( ++d == D ) return -1;
+                zi = 2.0 * zr * zi + ci;
+                zr = zr2 - zi2 + cr;
+        }
+}
+
+static void
+PyUFunc_0(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
+{
+        npy_intp i, n;
+        npy_intp is0 = steps[0];
+        npy_intp is1 = steps[1];
+        npy_intp os = steps[2];
+        char *ip0 = args[0];
+        char *ip1 = args[1];
+        char *op = args[2];
+        n = dimensions[0];
+        
+        for(i = 0; i < n; i++) {
+                *(long *)op = iterations(*(double *)ip0,
+                                         *(double *)ip1);
+                ip0 += is0;
+                ip1 += is1;
+                op += os;
+        }
+}
+
+static PyUFuncGenericFunction f_functions[] = {
+        PyUFunc_0,
+};
+
+static char f_types[] = {
+        NPY_DOUBLE, NPY_DOUBLE, NPY_LONG,
+};
+'''
+ufunc_info = weave.base_info.custom_info()
+ufunc_info.add_header('"numpy/ufuncobject.h"')
+
+mandel = weave.inline('/*' + hashlib.md5(support_code).hexdigest() + '''*/
+import_ufunc();
+
+return_val = PyUFunc_FromFuncAndData(
+            f_functions,
+            NULL,
+            f_types,
+            1,             /* ntypes */
+            2,             /* nin */
+            1,             /* nout */
+            PyUFunc_None,  /* identity */
+            "mandel",      /* name */
+            "returns number of iterations from cr, ci",    /* doc */
+            0);
+            ''',
+                     support_code=support_code,
+                     verbose=0,
+                     customize=ufunc_info)
+
+
+if __name__ == '__main__':
+
+    assert mandel(-1, .3) == 36
+    assert mandel(0, 0) == -1
+    assert mandel(10, 10) == 1
+    assert all(mandel(array([-1, 0, 10]), array([.3, 0, 10])) ==
+               array([36, -1,  1]))

Added: trunk/scipy/sandbox/mkufunc/examples/mandel_mkImage.py
===================================================================
--- trunk/scipy/sandbox/mkufunc/examples/mandel_mkImage.py	2008-07-20 01:00:07 UTC (rev 4553)
+++ trunk/scipy/sandbox/mkufunc/examples/mandel_mkImage.py	2008-07-21 03:17:18 UTC (rev 4554)
@@ -0,0 +1,47 @@
+#!/usr/bin/env python
+
+# Before running this be sure to apply Travis Oliphant's patch to PIL:
+# http://www.scipy.org/Cookbook/PIL
+
+import numpy
+from PIL import Image
+
+from mkufunc.api import mkufunc
+
+from mandel_c import mandel
+
+
+ at mkufunc(int)
+def color(i):
+    return (i * 10) % 256
+    
+    n = i % 3
+    if n == 0:
+        c = (255, 127, 128)
+    elif n == 1:
+        c = (128, 255, 0)
+    else:
+        c = (0, 128, 255)
+
+    return c[0] + (c[1] + c[2]*256)*256
+
+
+
+w, h = 1024, 768
+
+x, y = numpy.ogrid[-2.5:+1.5:w*1j, -1.5:+1.5:h*1j]
+
+img = mandel(x, y)
+
+print img.dtype
+print img.shape
+
+img = color(img)
+img.dtype = numpy.uint8
+img = img.reshape(h, w, 4)
+    
+print img.dtype
+print img.shape
+
+pilImage = Image.fromarray(img)
+pilImage.save('mandel.png')


Property changes on: trunk/scipy/sandbox/mkufunc/examples/mandel_mkImage.py
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/scipy/sandbox/mkufunc/examples/mandel_py.py
===================================================================
--- trunk/scipy/sandbox/mkufunc/examples/mandel_py.py	2008-07-20 01:00:07 UTC (rev 4553)
+++ trunk/scipy/sandbox/mkufunc/examples/mandel_py.py	2008-07-21 03:17:18 UTC (rev 4554)
@@ -0,0 +1,31 @@
+#!/usr/bin/env python
+from numpy import array
+from mkufunc.api import mkufunc
+
+D = 1000
+
+
+ at mkufunc([(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 all(mandel(array([-1, 0, 10]), array([.3, 0, 10])) ==
+               array([36, -1,  1]))




More information about the Scipy-svn mailing list