[Scipy-svn] r4522 - trunk/scipy/sandbox/mkufunc/mkufunc

scipy-svn at scipy.org scipy-svn at scipy.org
Wed Jul 2 14:21:39 EDT 2008


Author: ilan
Date: 2008-07-02 13:21:39 -0500 (Wed, 02 Jul 2008)
New Revision: 4522

Modified:
   trunk/scipy/sandbox/mkufunc/mkufunc/api.py
   trunk/scipy/sandbox/mkufunc/mkufunc/test_mkufunc.py
Log:
Added tests. src keyword to mkufunc to show translated C source

Modified: trunk/scipy/sandbox/mkufunc/mkufunc/api.py
===================================================================
--- trunk/scipy/sandbox/mkufunc/mkufunc/api.py	2008-07-02 13:24:46 UTC (rev 4521)
+++ trunk/scipy/sandbox/mkufunc/mkufunc/api.py	2008-07-02 18:21:39 UTC (rev 4522)
@@ -15,6 +15,7 @@
 
 
 verbose = 0
+showC = 0
 
 def func_hash(f, salt=None):
     """ Return a MD5 hash for a function object as string.
@@ -112,7 +113,7 @@
             \s*                  # possibly whitespace
             \([^)]*\)            # argument types
             \s*                  # possibly whitespace
-            \{.*?[\n\r]\}[\n\r]  # function body ending with } in single line
+            \{.*?\n\}\n          # function body ending with } in single line
             ''' % self.cname,
             re.DOTALL | re.MULTILINE | re.VERBOSE)
         
@@ -120,7 +121,12 @@
         assert len(found) == 1
         res = found[0]
         res = res.replace(self._prefix + 'pypy_g_ll_math_ll_math_', '')
-        return 'inline ' + res + '\n'
+        if showC:
+            print '------------------'
+            print res
+            print '------------------'
+        
+        return 'inline %s\n' % res
     
     def ufunc_support_code(self):
         # Unfortunately the code in here is very hard to read.
@@ -288,7 +294,7 @@
                         customize=ufunc_info)
 
 
-def mkufunc(arg0=[float]):
+def mkufunc(arg0=[float], src=0):
     """ Python decorator which returns compiled UFunc of the function given.
     
     >>> from numpy import arange
@@ -330,7 +336,10 @@
             
         def __call__(self, *args):
             return self.ufunc(*args)
-        
+
+    global showC
+    showC = src
+    
     if isinstance(arg0, FunctionType):
         f = arg0
         signatures = [float]

Modified: trunk/scipy/sandbox/mkufunc/mkufunc/test_mkufunc.py
===================================================================
--- trunk/scipy/sandbox/mkufunc/mkufunc/test_mkufunc.py	2008-07-02 13:24:46 UTC (rev 4521)
+++ trunk/scipy/sandbox/mkufunc/mkufunc/test_mkufunc.py	2008-07-02 18:21:39 UTC (rev 4522)
@@ -123,10 +123,10 @@
         self.assertFuncsEqual(f, lambda x: math.pi * x)
 
     def test_e(self):
-        @mkufunc#(show=1)
+        @mkufunc
         def f(x): return math.e * x
         self.assertFuncsEqual(f, lambda x: math.e * x)
-   
+        
     def test_exp(self):
         @mkufunc
         def f(x): return math.exp(x)
@@ -217,6 +217,19 @@
         a = f(xx, yy)
         b = [math.pow(x, y) for x, y in zip(xx, yy)]
         self.assertClose(a, b)
+        
+    def test_hypot(self):
+        @mkufunc
+        def f(x, y):
+            return math.hypot(x, y)
+        
+        self.assertClose(f(3, 4), 5)
+        
+        xx = array([3.0, 2.4, -2.4,  3.1, -2.3, -1.0])
+        yy = array([4.0, 7.5,  7.5, -8.7,  0.0, -3.2])
+        a = f(xx, yy)
+        b = [math.hypot(x, y) for x, y in zip(xx, yy)]
+        self.assertClose(a, b)
        
     def test_arithmetic(self):
         def f(x):




More information about the Scipy-svn mailing list