[pypy-commit] pypy numpypy-axisops: added dim arg to sum, but unused

mattip noreply at buildbot.pypy.org
Sun Dec 25 01:02:07 CET 2011


Author: mattip
Branch: numpypy-axisops
Changeset: r50846:a4ff50ca85e8
Date: 2011-12-25 00:58 +0200
http://bitbucket.org/pypy/pypy/changeset/a4ff50ca85e8/

Log:	added dim arg to sum, but unused

diff --git a/pypy/module/micronumpy/app_numpy.py b/pypy/module/micronumpy/app_numpy.py
--- a/pypy/module/micronumpy/app_numpy.py
+++ b/pypy/module/micronumpy/app_numpy.py
@@ -25,6 +25,30 @@
     return a.mean()
 
 def sum(a):
+    '''sum(a, axis=None)
+    Sum of array elements over a given axis.
+    
+    Parameters
+    ----------
+    a : array_like
+        Elements to sum.
+    axis : integer, optional
+        Axis over which the sum is taken. By default `axis` is None,
+        and all elements are summed.
+    
+    Returns
+    -------
+    sum_along_axis : ndarray
+        An array with the same shape as `a`, with the specified
+        axis removed.   If `a` is a 0-d array, or if `axis` is None, a scalar
+        is returned.  If an output array is specified, a reference to
+        `out` is returned.
+    
+    See Also
+    --------
+    ndarray.sum : Equivalent method.
+    '''
+    # TODO: add to doc (once it's implemented): cumsum : Cumulative sum of array elements.
     if not hasattr(a, "sum"):
         a = numpypy.array(a)
     return a.sum()
diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -279,8 +279,8 @@
     descr_rmod = _binop_right_impl("mod")
 
     def _reduce_ufunc_impl(ufunc_name):
-        def impl(self, space):
-            return getattr(interp_ufuncs.get(space), ufunc_name).reduce(space, self, multidim=True)
+        def impl(self, space, args_w):
+            return getattr(interp_ufuncs.get(space), ufunc_name).reduce(space, self, True, args_w)
         return func_with_new_name(impl, "reduce_%s_impl" % ufunc_name)
 
     descr_sum = _reduce_ufunc_impl("add")
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
@@ -47,15 +47,19 @@
         return self.call(space, __args__.arguments_w)
 
     def descr_reduce(self, space, w_obj):
-        return self.reduce(space, w_obj, multidim=False)
+        return self.reduce(space, w_obj, False)
 
-    def reduce(self, space, w_obj, multidim):
+    def reduce(self, space, w_obj, multidim, args_w):
         from pypy.module.micronumpy.interp_numarray import convert_to_array, Scalar
-        
         if self.argcount != 2:
             raise OperationError(space.w_ValueError, space.wrap("reduce only "
                 "supported for binary functions"))
-
+        dim = -1
+        if multidim and len(args_w)>0:
+            dim = space.int_w(args_w[0])
+        if len(args_w)>1:
+            raise OperationError(space.w_TypeError, space.wrap(
+                 self.name + " recieved extra arguments"))
         assert isinstance(self, W_Ufunc2)
         obj = convert_to_array(space, w_obj)
         if isinstance(obj, Scalar):
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
@@ -711,13 +711,18 @@
         assert a[:4].mean() == 1.5
 
     def test_sum(self):
-        from numpypy import array
+        from numpypy import array, arange
         a = array(range(5))
         assert a.sum() == 10.0
         assert a[:4].sum() == 6.0
 
         a = array([True] * 5, bool)
         assert a.sum() == 5
+        
+        raises(TypeError, 'a.sum(2, 3)')
+        a = arange(15).reshape(5, 3)
+        assert (a.sum(0) == [30, 35, 40]).all()
+        assert (a.sum(1) == [3, 12, 21, 30, 39]).all()
 
     def test_identity(self):
         from numpypy import identity, array


More information about the pypy-commit mailing list