[pypy-commit] pypy numpy-dtype-alt: bunch of ufuncs/methods for non-float arrays

alex_gaynor noreply at buildbot.pypy.org
Thu Aug 25 06:44:11 CEST 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: numpy-dtype-alt
Changeset: r46765:f7a681246467
Date: 2011-08-25 00:17 -0400
http://bitbucket.org/pypy/pypy/changeset/f7a681246467/

Log:	bunch of ufuncs/methods for non-float arrays

diff --git a/pypy/module/micronumpy/interp_dtype.py b/pypy/module/micronumpy/interp_dtype.py
--- a/pypy/module/micronumpy/interp_dtype.py
+++ b/pypy/module/micronumpy/interp_dtype.py
@@ -124,7 +124,7 @@
 def unaryop(func):
     @functools.wraps(func)
     def impl(self, v):
-        return self.box(func(self, self.unbox(v)))
+        return self.adapt_val(func(self, self.for_computation(self.unbox(v))))
     return impl
 
 class ArithmaticTypeMixin(object):
@@ -143,9 +143,22 @@
     def div(self, v1, v2):
         return v1 / v2
 
+    @unaryop
+    def pos(self, v):
+        return +v
+    @unaryop
+    def neg(self, v):
+        return -v
+    @unaryop
+    def abs(self, v):
+        return abs(v)
+
     @binop
     def max(self, v1, v2):
         return max(v1, v2)
+    @binop
+    def min(self, v1, v2):
+        return min(v1, v2)
 
     def bool(self, v):
         return bool(self.for_computation(self.unbox(v)))
@@ -167,15 +180,6 @@
         return math.pow(v1, v2)
 
     @unaryop
-    def pos(self, v):
-        return +v
-    @unaryop
-    def neg(self, v):
-        return -v
-    @unaryop
-    def abs(self, v):
-        return abs(v)
-    @unaryop
     def sign(self, v):
         if v == 0.0:
             return 0.0
@@ -193,9 +197,6 @@
         return math.floor(v)
 
     @binop
-    def min(self, v1, v2):
-        return min(v1, v2)
-    @binop
     def copysign(self, v1, v2):
         return math.copysign(v1, v2)
     @unaryop
@@ -237,6 +238,10 @@
     def mod(self, v1, v2):
         return v1 % v2
 
+    @unaryop
+    def sign(self, v):
+        return cmp(v, 0)
+
     def str_format(self, item):
         return str(widen(self.unbox(item)))
 
diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -325,6 +325,10 @@
         for i in range(5):
             assert b[i] == a[i]
 
+        a = +array(range(5))
+        for i in range(5):
+            assert a[i] == i
+
     def test_neg(self):
         from numpy import array
         a = array([1.,-2.,3.,-4.,-5.])
@@ -332,6 +336,10 @@
         for i in range(5):
             assert b[i] == -a[i]
 
+        a = -array(range(5), dtype="int8")
+        for i in range(5):
+            assert a[i] == -i
+
     def test_abs(self):
         from numpy import array
         a = array([1.,-2.,3.,-4.,-5.])
@@ -339,6 +347,10 @@
         for i in range(5):
             assert b[i] == abs(a[i])
 
+        a = abs(array(range(-5, 5), dtype="int8"))
+        for i in range(-5, 5):
+            assert a[i + 5] == abs(i)
+
     def test_auto_force(self):
         from numpy import array
         a = array(range(5))
diff --git a/pypy/module/micronumpy/test/test_ufuncs.py b/pypy/module/micronumpy/test/test_ufuncs.py
--- a/pypy/module/micronumpy/test/test_ufuncs.py
+++ b/pypy/module/micronumpy/test/test_ufuncs.py
@@ -132,6 +132,11 @@
         for i in range(4):
             assert b[i] == reference[i]
 
+        a = sign(array(range(-5, 5)))
+        ref = [-1, -1, -1, -1, -1, 0, 1, 1, 1, 1]
+        for i in range(10):
+            assert a[i] == ref[i]
+
     def test_reciporocal(self):
         from numpy import array, reciprocal
 


More information about the pypy-commit mailing list