Commits

Brian Kearns  committed df3dbf3

consolidate duplicate numpypy implementations of sum/min/max

  • Participants
  • Parent commits 84511eb

Comments (0)

Files changed (9)

File lib_pypy/numpypy/__init__.py

 from _numpypy import *
 from .core import *
+
 import _numpypy
-
 __all__ = _numpypy.__all__
 
 import sys

File lib_pypy/numpypy/core/__init__.py

 from .numeric import *
 from .shape_base import *
 
-from _numpypy import abs, max, min
+from _numpypy import abs
+from .fromnumeric import amax as max, amin as min

File lib_pypy/numpypy/core/fromnumeric.py

 
     """
     assert dtype is None
-    assert out is None
     if not hasattr(a, "sum"):
         a = numpypy.array(a)
-    return a.sum(axis=axis)
+    return a.sum(axis=axis, out=out)
 
 
 def product (a, axis=None, dtype=None, out=None):
     4.0
 
     """
-    assert axis is None
-    assert out is None
     if not hasattr(a, "max"):
         a = numpypy.array(a)
-    return a.max()
+    if a.size < 1:
+        return numpypy.array([])
+    return a.max(axis=axis, out=out)
 
 
 def amin(a, axis=None, out=None):
     0.0
 
     """
-    # amin() is equivalent to min()
-    assert axis is None
-    assert out is None
     if not hasattr(a, 'min'):
         a = numpypy.array(a)
-    return a.min()
+    if a.size < 1:
+        return numpypy.array([])
+    return a.min(axis=axis, out=out)
 
 def alen(a):
     """

File pypy/module/micronumpy/__init__.py

 
     appleveldefs = {
         'average': 'app_numpy.average',
-        'sum': 'app_numpy.sum',
-        'min': 'app_numpy.min',
         'identity': 'app_numpy.identity',
         'eye': 'app_numpy.eye',
-        'max': 'app_numpy.max',
         'arange': 'app_numpy.arange',
     }
     def setup_after_space_initialization(self):
         space = self.space
-        alllist = sorted(Module.interpleveldefs.keys() + \
+        all_list = sorted(Module.interpleveldefs.keys() + \
                                 Module.appleveldefs.keys())
         # found by set(numpypy.__all__) - set(numpy.__all__)
-        alllist.remove('min')
-        alllist.remove('max') 
-        alllist.remove('bool') 
-        alllist.remove('int') 
-        alllist.remove('abs') 
-        alllist.remove('typeinfo') 
-        w_all = space.wrap(alllist)
+        all_list.remove('bool')
+        all_list.remove('int')
+        all_list.remove('abs')
+        all_list.remove('typeinfo')
+        w_all = space.wrap(all_list)
         space.setitem(self.w_dict, space.new_interned_str('__all__'), w_all)
 
 if long_double_size == 16:

File pypy/module/micronumpy/app_numpy.py

         mi += 1
     return a
 
-def sum(a,axis=None, out=None):
-    '''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(axis=axis, out=out)
-
-def min(a, axis=None, out=None):
-    if not hasattr(a, "min"):
-        a = _numpypy.array(a)
-    if a.size < 1:
-        return _numpypy.array([])
-    return a.min(axis=axis, out=out)
-
-def max(a, axis=None, out=None):
-    if not hasattr(a, "max"):
-        a = _numpypy.array(a)
-    if a.size < 1:
-        return _numpypy.array([])
-    return a.max(axis=axis, out=out)
-
 def arange(start, stop=None, step=1, dtype=None):
     '''arange([start], stop[, step], dtype=None)
     Generate values in the half-interval [start, stop).

File pypy/module/micronumpy/test/test_module.py

         from _numpypy import array, average
         assert average(range(10)) == 4.5
         assert average(array(range(10))) == 4.5
-
-    def test_sum(self):
-        from _numpypy import array, sum
-        assert sum(range(10)) == 45
-        assert sum(array(range(10))) == 45
-
-    def test_min(self):
-        from _numpypy import array, min, zeros
-        assert min(range(10)) == 0
-        assert min(array(range(10))) == 0
-        assert list(min(zeros((0, 2)), axis=1)) == []
-
-    def test_max(self):
-        from _numpypy import array, max, zeros
-        assert max(range(10)) == 9
-        assert max(array(range(10))) == 9
-        assert list(max(zeros((0, 2)), axis=1)) == []

File pypy/module/micronumpy/test/test_outarg.py

         b = add(10, 10, out=out)
         assert b==out
         assert b.dtype == out.dtype
-        
-    def test_applevel(self):
-        from _numpypy import array, sum, max, min
-        a = array([[1, 2], [3, 4]])
-        out = array([[0, 0], [0, 0]])
-        c = sum(a, axis=0, out=out[0])
-        assert (c == [4, 6]).all()
-        assert (c == out[0]).all()
-        assert (c != out[1]).all()
-        c = max(a, axis=1, out=out[0])
-        assert (c == [2, 4]).all()
-        assert (c == out[0]).all()
-        assert (c != out[1]).all()
-        
+
     def test_ufunc_cast(self):
-        from _numpypy import array, negative, add, sum
+        from _numpypy import array, negative, add
         a = array(16, dtype = int)
         c = array(0, dtype = float)
         b = negative(a, out=c)
         b = add(a, a, out=c)
         assert b == c
         d = array([16, 16], dtype=int)
-        b = sum(d, out=c)
+        b = d.sum(out=c)
         assert b == c
         #cast_error = raises(TypeError, negative, c, a)
         #assert str(cast_error.value) == \

File pypy/module/test_lib_pypy/numpypy/core/test_fromnumeric.py

         # If the accumulator is too small, overflow occurs:
         # assert ones(128, dtype=int8).sum(dtype=int8) == -128
 
+        assert sum(range(10)) == 45
+        assert sum(array(range(10))) == 45
+
+        a = array([[1, 2], [3, 4]])
+        out = array([[0, 0], [0, 0]])
+        c = sum(a, axis=0, out=out[0])
+        assert (c == [4, 6]).all()
+        assert (c == out[0]).all()
+        assert (c != out[1]).all()
+
     def test_amin(self):
         # tests taken from numpy/core/fromnumeric.py docstring
-        from numpypy import array, arange, amin
+        from numpypy import array, arange, amin, zeros
         a = arange(4).reshape((2,2))
         assert amin(a) == 0
         # # Minima along the first axis
         # assert amin(b) == nan
         # assert nanmin(b) == 0.0
 
+        assert amin(range(10)) == 0
+        assert amin(array(range(10))) == 0
+        assert list(amin(zeros((0, 2)), axis=1)) == []
+
+        a = array([[1, 2], [3, 4]])
+        out = array([[0, 0], [0, 0]])
+        c = amin(a, axis=1, out=out[0])
+        assert (c == [1, 3]).all()
+        assert (c == out[0]).all()
+        assert (c != out[1]).all()
+
     def test_amax(self):
         # tests taken from numpy/core/fromnumeric.py docstring
-        from numpypy import array, arange, amax
+        from numpypy import array, arange, amax, zeros
         a = arange(4).reshape((2,2))
         assert amax(a) == 3
         # assert (amax(a, axis=0) == array([2, 3])).all()
         # assert amax(b) == nan
         # assert nanmax(b) == 4.0
 
+        assert amax(range(10)) == 9
+        assert amax(array(range(10))) == 9
+        assert list(amax(zeros((0, 2)), axis=1)) == []
+
+        a = array([[1, 2], [3, 4]])
+        out = array([[0, 0], [0, 0]])
+        c = amax(a, axis=1, out=out[0])
+        assert (c == [2, 4]).all()
+        assert (c == out[0]).all()
+        assert (c != out[1]).all()
+
     def test_alen(self):
         # tests taken from numpy/core/fromnumeric.py docstring
         from numpypy import array, zeros, alen

File pypy/module/test_lib_pypy/numpypy/test_numpy.py

         assert min(4, 3, 2, 1) == 1
         assert max(1, 2, 3, 4) == 4
 
-        from numpypy import min, max
+        from numpypy import min, max, amin, amax
         assert min is not __builtin__.min
         assert max is not __builtin__.max
+        assert min is amin
+        assert max is amax