Author: Brian Kearns <[email protected]>
Branch:
Changeset: r61635:df3dbf3213d3
Date: 2013-02-22 21:25 -0500
http://bitbucket.org/pypy/pypy/changeset/df3dbf3213d3/
Log: consolidate duplicate numpypy implementations of sum/min/max
diff --git a/lib_pypy/numpypy/__init__.py b/lib_pypy/numpypy/__init__.py
--- a/lib_pypy/numpypy/__init__.py
+++ b/lib_pypy/numpypy/__init__.py
@@ -1,7 +1,7 @@
from _numpypy import *
from .core import *
+
import _numpypy
-
__all__ = _numpypy.__all__
import sys
diff --git a/lib_pypy/numpypy/core/__init__.py
b/lib_pypy/numpypy/core/__init__.py
--- a/lib_pypy/numpypy/core/__init__.py
+++ b/lib_pypy/numpypy/core/__init__.py
@@ -2,4 +2,5 @@
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
diff --git a/lib_pypy/numpypy/core/fromnumeric.py
b/lib_pypy/numpypy/core/fromnumeric.py
--- a/lib_pypy/numpypy/core/fromnumeric.py
+++ b/lib_pypy/numpypy/core/fromnumeric.py
@@ -1360,10 +1360,9 @@
"""
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):
@@ -1720,11 +1719,11 @@
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):
@@ -1782,12 +1781,11 @@
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):
"""
diff --git a/pypy/module/micronumpy/__init__.py
b/pypy/module/micronumpy/__init__.py
--- a/pypy/module/micronumpy/__init__.py
+++ b/pypy/module/micronumpy/__init__.py
@@ -164,25 +164,20 @@
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:
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
@@ -35,49 +35,6 @@
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).
diff --git a/pypy/module/micronumpy/test/test_module.py
b/pypy/module/micronumpy/test/test_module.py
--- a/pypy/module/micronumpy/test/test_module.py
+++ b/pypy/module/micronumpy/test/test_module.py
@@ -6,20 +6,3 @@
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)) == []
diff --git a/pypy/module/micronumpy/test/test_outarg.py
b/pypy/module/micronumpy/test/test_outarg.py
--- a/pypy/module/micronumpy/test/test_outarg.py
+++ b/pypy/module/micronumpy/test/test_outarg.py
@@ -83,22 +83,9 @@
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)
@@ -106,7 +93,7 @@
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) == \
diff --git a/pypy/module/test_lib_pypy/numpypy/core/test_fromnumeric.py
b/pypy/module/test_lib_pypy/numpypy/core/test_fromnumeric.py
--- a/pypy/module/test_lib_pypy/numpypy/core/test_fromnumeric.py
+++ b/pypy/module/test_lib_pypy/numpypy/core/test_fromnumeric.py
@@ -45,9 +45,19 @@
# 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
@@ -60,9 +70,20 @@
# 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()
@@ -73,6 +94,17 @@
# 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
diff --git a/pypy/module/test_lib_pypy/numpypy/test_numpy.py
b/pypy/module/test_lib_pypy/numpypy/test_numpy.py
--- a/pypy/module/test_lib_pypy/numpypy/test_numpy.py
+++ b/pypy/module/test_lib_pypy/numpypy/test_numpy.py
@@ -26,6 +26,8 @@
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
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit