diff options
author | Mark Wiebe <mwiebe@enthought.com> | 2011-08-02 17:06:20 -0500 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2011-08-27 07:26:50 -0600 |
commit | 00dff152bb10c810176c7e47ad41e563df602208 (patch) | |
tree | 156498d06b040209f4581edb1b91f620c5b98afe /numpy/core/fromnumeric.py | |
parent | 0375181e807f9def0e1bd0279214f5a00ec55485 (diff) | |
download | numpy-00dff152bb10c810176c7e47ad41e563df602208.tar.gz |
ENH: umath: Make sum, prod, any, or functions use the .reduce method directly
This required some extensive changes, like:
* Making logical_or, logical_and, and logical_not on object arrays behave
like their Python equivalents instead of calling methods on the objects
* Changing the units for a fair number of the binary operations to
None, so they don't get initialized to their unit values at the start
A consequence of this is that multi-dimensional reductions like sum, prod,
any, or all no longer need to make copies, so are faster in some cases.
Diffstat (limited to 'numpy/core/fromnumeric.py')
-rw-r--r-- | numpy/core/fromnumeric.py | 40 |
1 files changed, 6 insertions, 34 deletions
diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py index 602c0ebc5..ca46e4a59 100644 --- a/numpy/core/fromnumeric.py +++ b/numpy/core/fromnumeric.py @@ -1448,12 +1448,7 @@ def sum(a, axis=None, dtype=None, out=None): out[...] = res return out return res - try: - sum = a.sum - except AttributeError: - return _wrapit(a, 'sum', axis, dtype, out) - return sum(axis, dtype, out) - + return um.add.reduce(a, axis=axis, dtype=dtype, out=out) def product (a, axis=None, dtype=None, out=None): """ @@ -1464,11 +1459,7 @@ def product (a, axis=None, dtype=None, out=None): prod : equivalent function; see for details. """ - try: - prod = a.prod - except AttributeError: - return _wrapit(a, 'prod', axis, dtype, out) - return prod(axis, dtype, out) + return um.multiply.reduce(a, axis=axis, dtype=dtype, out=out) def sometrue(a, axis=None, out=None): @@ -1482,11 +1473,7 @@ def sometrue(a, axis=None, out=None): any : equivalent function """ - try: - any = a.any - except AttributeError: - return _wrapit(a, 'any', axis, out) - return any(axis, out) + return um.logical_or.reduce(a, axis=axis, out=out) def alltrue (a, axis=None, out=None): @@ -1498,12 +1485,7 @@ def alltrue (a, axis=None, out=None): numpy.all : Equivalent function; see for details. """ - try: - all = a.all - except AttributeError: - return _wrapit(a, 'all', axis, out) - return all(axis, out) - + return um.logical_and.reduce(a, axis=axis, out=out) def any(a,axis=None, out=None): """ @@ -1569,12 +1551,7 @@ def any(a,axis=None, out=None): (191614240, 191614240) """ - try: - any = a.any - except AttributeError: - return _wrapit(a, 'any', axis, out) - return any(axis, out) - + return um.logical_or.reduce(a, axis=axis, out=out) def all(a,axis=None, out=None): """ @@ -1633,12 +1610,7 @@ def all(a,axis=None, out=None): (28293632, 28293632, array([ True], dtype=bool)) """ - try: - all = a.all - except AttributeError: - return _wrapit(a, 'all', axis, out) - return all(axis, out) - + return um.logical_and.reduce(a, axis=axis, out=out) def cumsum (a, axis=None, dtype=None, out=None): """ |