[pypy-commit] pypy numpypy-out: never create intermediates, add tests to verify

mattip noreply at buildbot.pypy.org
Mon Feb 13 00:57:52 CET 2012


Author: mattip
Branch: numpypy-out
Changeset: r52403:4dad485d2e24
Date: 2012-02-12 23:20 +0200
http://bitbucket.org/pypy/pypy/changeset/4dad485d2e24/

Log:	never create intermediates, add tests to verify

diff --git a/pypy/module/micronumpy/interp_ufuncs.py b/pypy/module/micronumpy/interp_ufuncs.py
--- a/pypy/module/micronumpy/interp_ufuncs.py
+++ b/pypy/module/micronumpy/interp_ufuncs.py
@@ -168,22 +168,23 @@
                         'output parameter shape mismatch, expecting %s' +
                         ' , got %s', str(shape), str(out.shape))
                 #Test for dtype agreement, perhaps create an itermediate
-                if out.dtype != dtype
-                    raise OperationError(space.w_TypeError, space.wrap(
-                        "mismatched  dtypes"))
-                return self.do_axis_reduce(obj, dtype, axis, out)
+                #if out.dtype != dtype:
+                #    raise OperationError(space.w_TypeError, space.wrap(
+                #        "mismatched  dtypes"))
+                return self.do_axis_reduce(obj, out.find_dtype(), axis, out)
             else:
                 result = W_NDimArray(support.product(shape), shape, dtype)
                 return self.do_axis_reduce(obj, dtype, axis, result)
-        arr = ReduceArray(self.func, self.name, self.identity, obj, dtype)
-        val = loop.compute(arr)
         if out:
             if len(out.shape)>0:
                 raise operationerrfmt(space.w_ValueError, "output parameter "
                               "for reduction operation %s has too many"
                               " dimensions",self.name)
-            out.value = out.dtype.coerce(space, val)
-            return out
+            arr = ReduceArray(self.func, self.name, self.identity, obj,
+                                                            out.find_dtype())
+        else:
+            arr = ReduceArray(self.func, self.name, self.identity, obj, dtype)
+        val = loop.compute(arr)
         return val 
 
     def do_axis_reduce(self, obj, dtype, axis, result):
diff --git a/pypy/module/micronumpy/signature.py b/pypy/module/micronumpy/signature.py
--- a/pypy/module/micronumpy/signature.py
+++ b/pypy/module/micronumpy/signature.py
@@ -441,6 +441,5 @@
             cur = arr.left.getitem(iterator.offset)
             value = self.binfunc(self.calc_dtype, cur, v)
         arr.left.setitem(iterator.offset, value)
-    
     def debug_repr(self):
         return 'AxisReduceSig(%s, %s)' % (self.name, self.right.debug_repr())
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
@@ -896,7 +896,7 @@
         assert (array([[1,2],[3,4]]).prod(1) == [2, 12]).all()
 
     def test_reduce_out(self):
-        from numpypy import arange, array
+        from numpypy import arange, zeros, array
         a = arange(15).reshape(5, 3)
         b = arange(12).reshape(4,3)
         c = a.sum(0, out=b[1])
@@ -906,6 +906,16 @@
         a=arange(12).reshape(3,2,2)
         raises(ValueError, 'a.sum(0, out=arange(12).reshape(3,2,2))')
         raises(ValueError, 'a.sum(0, out=arange(3))')
+        c = array([-1, 0, 1]).sum(out=zeros([], dtype=bool))
+        #You could argue that this should product False, but
+        # that would require an itermediate result. Cpython numpy
+        # gives True.
+        assert c == True
+        a = array([[-1, 0, 1], [1, 0, -1]])
+        c = a.sum(0, out=zeros((3,), dtype=bool))
+        assert (c == [True, False, True]).all()
+        c = a.sum(1, out=zeros((2,), dtype=bool))
+        assert (c == [True, True]).all()
 
     def test_reduce_intermediary(self):
         from numpypy import arange, array


More information about the pypy-commit mailing list