summaryrefslogtreecommitdiff
path: root/numpy/core/fromnumeric.py
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/core/fromnumeric.py')
-rw-r--r--numpy/core/fromnumeric.py960
1 files changed, 652 insertions, 308 deletions
diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py
index 00086207f..59a820d53 100644
--- a/numpy/core/fromnumeric.py
+++ b/numpy/core/fromnumeric.py
@@ -3,18 +3,19 @@
"""
from __future__ import division, absolute_import, print_function
+import functools
import types
import warnings
import numpy as np
from .. import VisibleDeprecationWarning
from . import multiarray as mu
+from . import overrides
from . import umath as um
from . import numerictypes as nt
from .numeric import asarray, array, asanyarray, concatenate
from . import _methods
-
_dt_ = nt.sctype2char
# functions that are methods
@@ -26,17 +27,15 @@ __all__ = [
'rank', 'ravel', 'repeat', 'reshape', 'resize', 'round_',
'searchsorted', 'shape', 'size', 'sometrue', 'sort', 'squeeze',
'std', 'sum', 'swapaxes', 'take', 'trace', 'transpose', 'var',
- ]
-
-
-try:
- _gentype = types.GeneratorType
-except AttributeError:
- _gentype = type(None)
+]
+_gentype = types.GeneratorType
# save away Python sum
_sum_ = sum
+array_function_dispatch = functools.partial(
+ overrides.array_function_dispatch, module='numpy')
+
# functions that are now methods
def _wrapit(obj, method, *args, **kwds):
@@ -67,19 +66,57 @@ def _wrapfunc(obj, method, *args, **kwds):
return _wrapit(obj, method, *args, **kwds)
+def _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs):
+ passkwargs = {k: v for k, v in kwargs.items()
+ if v is not np._NoValue}
+
+ if type(obj) is not mu.ndarray:
+ try:
+ reduction = getattr(obj, method)
+ except AttributeError:
+ pass
+ else:
+ # This branch is needed for reductions like any which don't
+ # support a dtype.
+ if dtype is not None:
+ return reduction(axis=axis, dtype=dtype, out=out, **passkwargs)
+ else:
+ return reduction(axis=axis, out=out, **passkwargs)
+
+ return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
+
+
+def _take_dispatcher(a, indices, axis=None, out=None, mode=None):
+ return (a, out)
+
+
+@array_function_dispatch(_take_dispatcher)
def take(a, indices, axis=None, out=None, mode='raise'):
"""
Take elements from an array along an axis.
- This function does the same thing as "fancy" indexing (indexing arrays
- using arrays); however, it can be easier to use if you need elements
- along a given axis.
+ When axis is not None, this function does the same thing as "fancy"
+ indexing (indexing arrays using arrays); however, it can be easier to use
+ if you need elements along a given axis. A call such as
+ ``np.take(arr, indices, axis=3)`` is equivalent to
+ ``arr[:,:,:,indices,...]``.
+
+ Explained without fancy indexing, this is equivalent to the following use
+ of `ndindex`, which sets each of ``ii``, ``jj``, and ``kk`` to a tuple of
+ indices::
+
+ Ni, Nk = a.shape[:axis], a.shape[axis+1:]
+ Nj = indices.shape
+ for ii in ndindex(Ni):
+ for jj in ndindex(Nj):
+ for kk in ndindex(Nk):
+ out[ii + jj + kk] = a[ii + (indices[jj],) + kk]
Parameters
----------
- a : array_like
+ a : array_like (Ni..., M, Nk...)
The source array.
- indices : array_like
+ indices : array_like (Nj...)
The indices of the values to extract.
.. versionadded:: 1.8.0
@@ -88,7 +125,7 @@ def take(a, indices, axis=None, out=None, mode='raise'):
axis : int, optional
The axis over which to select values. By default, the flattened
input array is used.
- out : ndarray, optional
+ out : ndarray, optional (Ni..., Nj..., Nk...)
If provided, the result will be placed in this array. It should
be of the appropriate shape and dtype.
mode : {'raise', 'wrap', 'clip'}, optional
@@ -104,13 +141,31 @@ def take(a, indices, axis=None, out=None, mode='raise'):
Returns
-------
- subarray : ndarray
+ out : ndarray (Ni..., Nj..., Nk...)
The returned array has the same type as `a`.
See Also
--------
compress : Take elements using a boolean mask
ndarray.take : equivalent method
+ take_along_axis : Take elements by matching the array and the index arrays
+
+ Notes
+ -----
+
+ By eliminating the inner loop in the description above, and using `s_` to
+ build simple slice objects, `take` can be expressed in terms of applying
+ fancy indexing to each 1-d slice::
+
+ Ni, Nk = a.shape[:axis], a.shape[axis+1:]
+ for ii in ndindex(Ni):
+ for kk in ndindex(Nj):
+ out[ii + s_[...,] + kk] = a[ii + s_[:,] + kk][indices]
+
+ For this reason, it is equivalent to (but faster than) the following use
+ of `apply_along_axis`::
+
+ out = np.apply_along_axis(lambda a_1d: a_1d[indices], axis, a)
Examples
--------
@@ -134,7 +189,12 @@ def take(a, indices, axis=None, out=None, mode='raise'):
return _wrapfunc(a, 'take', indices, axis=axis, out=out, mode=mode)
+def _reshape_dispatcher(a, newshape, order=None):
+ return (a,)
+
+
# not deprecated --- copy if necessary, view otherwise
+@array_function_dispatch(_reshape_dispatcher)
def reshape(a, newshape, order='C'):
"""
Gives a new shape to an array without changing its data.
@@ -176,11 +236,11 @@ def reshape(a, newshape, order='C'):
Notes
-----
It is not always possible to change the shape of an array without
- copying the data. If you want an error to be raise if the data is copied,
+ copying the data. If you want an error to be raised when the data is copied,
you should assign the new shape to the shape attribute of the array::
>>> a = np.zeros((10, 2))
- # A transpose make the array non-contiguous
+ # A transpose makes the array non-contiguous
>>> b = a.T
# Taking a view makes it possible to modify the shape without modifying
# the initial object.
@@ -232,6 +292,14 @@ def reshape(a, newshape, order='C'):
return _wrapfunc(a, 'reshape', newshape, order=order)
+def _choose_dispatcher(a, choices, out=None, mode=None):
+ yield a
+ for c in choices:
+ yield c
+ yield out
+
+
+@array_function_dispatch(_choose_dispatcher)
def choose(a, choices, out=None, mode='raise'):
"""
Construct an array from an index array and a set of arrays to choose from.
@@ -354,6 +422,11 @@ def choose(a, choices, out=None, mode='raise'):
return _wrapfunc(a, 'choose', choices, out=out, mode=mode)
+def _repeat_dispatcher(a, repeats, axis=None):
+ return (a,)
+
+
+@array_function_dispatch(_repeat_dispatcher)
def repeat(a, repeats, axis=None):
"""
Repeat elements of an array.
@@ -381,6 +454,8 @@ def repeat(a, repeats, axis=None):
Examples
--------
+ >>> np.repeat(3, 4)
+ array([3, 3, 3, 3])
>>> x = np.array([[1,2],[3,4]])
>>> np.repeat(x, 2)
array([1, 1, 2, 2, 3, 3, 4, 4])
@@ -396,6 +471,11 @@ def repeat(a, repeats, axis=None):
return _wrapfunc(a, 'repeat', repeats, axis=axis)
+def _put_dispatcher(a, ind, v, mode=None):
+ return (a, ind, v)
+
+
+@array_function_dispatch(_put_dispatcher)
def put(a, ind, v, mode='raise'):
"""
Replaces specified elements of an array with given values.
@@ -430,6 +510,7 @@ def put(a, ind, v, mode='raise'):
See Also
--------
putmask, place
+ put_along_axis : Put elements by matching the array and the index arrays
Examples
--------
@@ -453,6 +534,11 @@ def put(a, ind, v, mode='raise'):
return put(ind, v, mode=mode)
+def _swapaxes_dispatcher(a, axis1, axis2):
+ return (a,)
+
+
+@array_function_dispatch(_swapaxes_dispatcher)
def swapaxes(a, axis1, axis2):
"""
Interchange two axes of an array.
@@ -469,8 +555,8 @@ def swapaxes(a, axis1, axis2):
Returns
-------
a_swapped : ndarray
- For Numpy >= 1.10, if `a` is an ndarray, then a view of `a` is
- returned; otherwise a new array is created. For earlier Numpy
+ For NumPy >= 1.10.0, if `a` is an ndarray, then a view of `a` is
+ returned; otherwise a new array is created. For earlier NumPy
versions a view of `a` is returned only if the order of the
axes is changed, otherwise the input array is returned.
@@ -499,6 +585,11 @@ def swapaxes(a, axis1, axis2):
return _wrapfunc(a, 'swapaxes', axis1, axis2)
+def _transpose_dispatcher(a, axes=None):
+ return (a,)
+
+
+@array_function_dispatch(_transpose_dispatcher)
def transpose(a, axes=None):
"""
Permute the dimensions of an array.
@@ -548,6 +639,11 @@ def transpose(a, axes=None):
return _wrapfunc(a, 'transpose', axes)
+def _partition_dispatcher(a, kth, axis=None, kind=None, order=None):
+ return (a,)
+
+
+@array_function_dispatch(_partition_dispatcher)
def partition(a, kth, axis=-1, kind='introselect', order=None):
"""
Return a partitioned copy of an array.
@@ -569,7 +665,7 @@ def partition(a, kth, axis=-1, kind='introselect', order=None):
Element index to partition by. The k-th value of the element
will be in its final sorted position and all smaller elements
will be moved before it and all equal or greater elements behind
- it. The order all elements in the partitions is undefined. If
+ it. The order of all elements in the partitions is undefined. If
provided with a sequence of k-th it will partition all elements
indexed by k-th of them into their sorted position at once.
axis : int or None, optional
@@ -630,14 +726,20 @@ def partition(a, kth, axis=-1, kind='introselect', order=None):
"""
if axis is None:
+ # flatten returns (1, N) for np.matrix, so always use the last axis
a = asanyarray(a).flatten()
- axis = 0
+ axis = -1
else:
a = asanyarray(a).copy(order="K")
a.partition(kth, axis=axis, kind=kind, order=order)
return a
+def _argpartition_dispatcher(a, kth, axis=None, kind=None, order=None):
+ return (a,)
+
+
+@array_function_dispatch(_argpartition_dispatcher)
def argpartition(a, kth, axis=-1, kind='introselect', order=None):
"""
Perform an indirect partition along the given axis using the
@@ -674,7 +776,9 @@ def argpartition(a, kth, axis=-1, kind='introselect', order=None):
-------
index_array : ndarray, int
Array of indices that partition `a` along the specified axis.
- In other words, ``a[index_array]`` yields a partitioned `a`.
+ If `a` is one-dimensional, ``a[index_array]`` yields a partitioned `a`.
+ More generally, ``np.take_along_axis(a, index_array, axis=a)`` always
+ yields the partitioned `a`, irrespective of dimensionality.
See Also
--------
@@ -704,6 +808,11 @@ def argpartition(a, kth, axis=-1, kind='introselect', order=None):
return _wrapfunc(a, 'argpartition', kth, axis=axis, kind=kind, order=order)
+def _sort_dispatcher(a, axis=None, kind=None, order=None):
+ return (a,)
+
+
+@array_function_dispatch(_sort_dispatcher)
def sort(a, axis=-1, kind='quicksort', order=None):
"""
Return a sorted copy of an array.
@@ -715,7 +824,7 @@ def sort(a, axis=-1, kind='quicksort', order=None):
axis : int or None, optional
Axis along which to sort. If None, the array is flattened before
sorting. The default is -1, which sorts along the last axis.
- kind : {'quicksort', 'mergesort', 'heapsort'}, optional
+ kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, optional
Sorting algorithm. Default is 'quicksort'.
order : str or list of str, optional
When `a` is an array with fields defined, this argument specifies
@@ -745,13 +854,13 @@ def sort(a, axis=-1, kind='quicksort', order=None):
order. The three available algorithms have the following
properties:
- =========== ======= ============= ============ =======
- kind speed worst case work space stable
- =========== ======= ============= ============ =======
+ =========== ======= ============= ============ ========
+ kind speed worst case work space stable
+ =========== ======= ============= ============ ========
'quicksort' 1 O(n^2) 0 no
'mergesort' 2 O(n*log(n)) ~n/2 yes
'heapsort' 3 O(n*log(n)) 0 no
- =========== ======= ============= ============ =======
+ =========== ======= ============= ============ ========
All the sort algorithms make temporary copies of the data when
sorting along any but the last axis. Consequently, sorting along
@@ -774,6 +883,16 @@ def sort(a, axis=-1, kind='quicksort', order=None):
placements are sorted according to the non-nan part if it exists.
Non-nan values are sorted as before.
+ .. versionadded:: 1.12.0
+
+ quicksort has been changed to an introsort which will switch
+ heapsort when it does not make enough progress. This makes its
+ worst case O(n*log(n)).
+
+ 'stable' automatically choses the best stable sorting algorithm
+ for the data type being sorted. It is currently mapped to
+ merge sort.
+
Examples
--------
>>> a = np.array([[1,4],[3,1]])
@@ -807,14 +926,20 @@ def sort(a, axis=-1, kind='quicksort', order=None):
"""
if axis is None:
+ # flatten returns (1, N) for np.matrix, so always use the last axis
a = asanyarray(a).flatten()
- axis = 0
+ axis = -1
else:
a = asanyarray(a).copy(order="K")
a.sort(axis=axis, kind=kind, order=order)
return a
+def _argsort_dispatcher(a, axis=None, kind=None, order=None):
+ return (a,)
+
+
+@array_function_dispatch(_argsort_dispatcher)
def argsort(a, axis=-1, kind='quicksort', order=None):
"""
Returns the indices that would sort an array.
@@ -830,7 +955,7 @@ def argsort(a, axis=-1, kind='quicksort', order=None):
axis : int or None, optional
Axis along which to sort. The default is -1 (the last axis). If None,
the flattened array is used.
- kind : {'quicksort', 'mergesort', 'heapsort'}, optional
+ kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, optional
Sorting algorithm.
order : str or list of str, optional
When `a` is an array with fields defined, this argument specifies
@@ -844,6 +969,8 @@ def argsort(a, axis=-1, kind='quicksort', order=None):
index_array : ndarray, int
Array of indices that sort `a` along the specified axis.
If `a` is one-dimensional, ``a[index_array]`` yields a sorted `a`.
+ More generally, ``np.take_along_axis(a, index_array, axis=a)`` always
+ yields the sorted `a`, irrespective of dimensionality.
See Also
--------
@@ -874,14 +1001,22 @@ def argsort(a, axis=-1, kind='quicksort', order=None):
array([[0, 3],
[2, 2]])
- >>> np.argsort(x, axis=0)
+ >>> np.argsort(x, axis=0) # sorts along first axis (down)
array([[0, 1],
[1, 0]])
- >>> np.argsort(x, axis=1)
+ >>> np.argsort(x, axis=1) # sorts along last axis (across)
array([[0, 1],
[0, 1]])
+ Indices of the sorted elements of a N-dimensional array:
+
+ >>> ind = np.unravel_index(np.argsort(x, axis=None), x.shape)
+ >>> ind
+ (array([0, 1, 1, 0]), array([0, 0, 1, 1]))
+ >>> x[ind] # same as np.sort(x, axis=None)
+ array([0, 2, 2, 3])
+
Sorting with keys:
>>> x = np.array([(1, 0), (0, 1)], dtype=[('x', '<i4'), ('y', '<i4')])
@@ -899,6 +1034,11 @@ def argsort(a, axis=-1, kind='quicksort', order=None):
return _wrapfunc(a, 'argsort', axis=axis, kind=kind, order=order)
+def _argmax_dispatcher(a, axis=None, out=None):
+ return (a, out)
+
+
+@array_function_dispatch(_argmax_dispatcher)
def argmax(a, axis=None, out=None):
"""
Returns the indices of the maximum values along an axis.
@@ -933,10 +1073,10 @@ def argmax(a, axis=None, out=None):
Examples
--------
- >>> a = np.arange(6).reshape(2,3)
+ >>> a = np.arange(6).reshape(2,3) + 10
>>> a
- array([[0, 1, 2],
- [3, 4, 5]])
+ array([[10, 11, 12],
+ [13, 14, 15]])
>>> np.argmax(a)
5
>>> np.argmax(a, axis=0)
@@ -944,17 +1084,30 @@ def argmax(a, axis=None, out=None):
>>> np.argmax(a, axis=1)
array([2, 2])
+ Indexes of the maximal elements of a N-dimensional array:
+
+ >>> ind = np.unravel_index(np.argmax(a, axis=None), a.shape)
+ >>> ind
+ (1, 2)
+ >>> a[ind]
+ 15
+
>>> b = np.arange(6)
>>> b[1] = 5
>>> b
array([0, 5, 2, 3, 4, 5])
- >>> np.argmax(b) # Only the first occurrence is returned.
+ >>> np.argmax(b) # Only the first occurrence is returned.
1
"""
return _wrapfunc(a, 'argmax', axis=axis, out=out)
+def _argmin_dispatcher(a, axis=None, out=None):
+ return (a, out)
+
+
+@array_function_dispatch(_argmin_dispatcher)
def argmin(a, axis=None, out=None):
"""
Returns the indices of the minimum values along an axis.
@@ -989,10 +1142,10 @@ def argmin(a, axis=None, out=None):
Examples
--------
- >>> a = np.arange(6).reshape(2,3)
+ >>> a = np.arange(6).reshape(2,3) + 10
>>> a
- array([[0, 1, 2],
- [3, 4, 5]])
+ array([[10, 11, 12],
+ [13, 14, 15]])
>>> np.argmin(a)
0
>>> np.argmin(a, axis=0)
@@ -1000,17 +1153,30 @@ def argmin(a, axis=None, out=None):
>>> np.argmin(a, axis=1)
array([0, 0])
- >>> b = np.arange(6)
- >>> b[4] = 0
+ Indices of the minimum elements of a N-dimensional array:
+
+ >>> ind = np.unravel_index(np.argmin(a, axis=None), a.shape)
+ >>> ind
+ (0, 0)
+ >>> a[ind]
+ 10
+
+ >>> b = np.arange(6) + 10
+ >>> b[4] = 10
>>> b
- array([0, 1, 2, 3, 0, 5])
- >>> np.argmin(b) # Only the first occurrence is returned.
+ array([10, 11, 12, 13, 10, 15])
+ >>> np.argmin(b) # Only the first occurrence is returned.
0
"""
return _wrapfunc(a, 'argmin', axis=axis, out=out)
+def _searchsorted_dispatcher(a, v, side=None, sorter=None):
+ return (a, v, sorter)
+
+
+@array_function_dispatch(_searchsorted_dispatcher)
def searchsorted(a, v, side='left', sorter=None):
"""
Find indices where elements should be inserted to maintain order.
@@ -1019,6 +1185,15 @@ def searchsorted(a, v, side='left', sorter=None):
corresponding elements in `v` were inserted before the indices, the
order of `a` would be preserved.
+ Assuming that `a` is sorted:
+
+ ====== ============================
+ `side` returned index `i` satisfies
+ ====== ============================
+ left ``a[i-1] < v <= a[i]``
+ right ``a[i-1] <= v < a[i]``
+ ====== ============================
+
Parameters
----------
a : 1-D array_like
@@ -1051,9 +1226,13 @@ def searchsorted(a, v, side='left', sorter=None):
-----
Binary search is used to find the required insertion points.
- As of Numpy 1.4.0 `searchsorted` works with real/complex arrays containing
+ As of NumPy 1.4.0 `searchsorted` works with real/complex arrays containing
`nan` values. The enhanced sort order is documented in `sort`.
+ This function is a faster version of the builtin python `bisect.bisect_left`
+ (``side='left'``) and `bisect.bisect_right` (``side='right'``) functions,
+ which is also vectorized in the `v` argument.
+
Examples
--------
>>> np.searchsorted([1,2,3,4,5], 3)
@@ -1067,6 +1246,11 @@ def searchsorted(a, v, side='left', sorter=None):
return _wrapfunc(a, 'searchsorted', v, side=side, sorter=sorter)
+def _resize_dispatcher(a, new_shape):
+ return (a,)
+
+
+@array_function_dispatch(_resize_dispatcher)
def resize(a, new_shape):
"""
Return a new array with the specified shape.
@@ -1095,6 +1279,16 @@ def resize(a, new_shape):
--------
ndarray.resize : resize an array in-place.
+ Notes
+ -----
+ Warning: This functionality does **not** consider axes separately,
+ i.e. it does not apply interpolation/extrapolation.
+ It fills the return array with the required number of elements, taken
+ from `a` as they are laid out in memory, disregarding strides and axes.
+ (This is in case the new shape is smaller. For larger, see above.)
+ This functionality is therefore not suitable to resize images,
+ or data where each axis represents a separate and distinct entity.
+
Examples
--------
>>> a=np.array([[0,1],[2,3]])
@@ -1112,26 +1306,29 @@ def resize(a, new_shape):
new_shape = (new_shape,)
a = ravel(a)
Na = len(a)
- if not Na:
- return mu.zeros(new_shape, a.dtype)
total_size = um.multiply.reduce(new_shape)
+ if Na == 0 or total_size == 0:
+ return mu.zeros(new_shape, a.dtype)
+
n_copies = int(total_size / Na)
extra = total_size % Na
- if total_size == 0:
- return a[:0]
-
if extra != 0:
- n_copies = n_copies+1
- extra = Na-extra
+ n_copies = n_copies + 1
+ extra = Na - extra
- a = concatenate((a,)*n_copies)
+ a = concatenate((a,) * n_copies)
if extra > 0:
a = a[:-extra]
return reshape(a, new_shape)
+def _squeeze_dispatcher(a, axis=None):
+ return (a,)
+
+
+@array_function_dispatch(_squeeze_dispatcher)
def squeeze(a, axis=None):
"""
Remove single-dimensional entries from the shape of an array.
@@ -1154,6 +1351,16 @@ def squeeze(a, axis=None):
dimensions of length 1 removed. This is always `a` itself
or a view into `a`.
+ Raises
+ ------
+ ValueError
+ If `axis` is not `None`, and an axis being squeezed is not of length 1
+
+ See Also
+ --------
+ expand_dims : The inverse operation, adding singleton dimensions
+ reshape : Insert, remove, and combine dimensions, and resize existing ones
+
Examples
--------
>>> x = np.array([[[0], [1], [2]]])
@@ -1161,7 +1368,13 @@ def squeeze(a, axis=None):
(1, 3, 1)
>>> np.squeeze(x).shape
(3,)
- >>> np.squeeze(x, axis=(2,)).shape
+ >>> np.squeeze(x, axis=0).shape
+ (3, 1)
+ >>> np.squeeze(x, axis=1).shape
+ Traceback (most recent call last):
+ ...
+ ValueError: cannot select an axis to squeeze out which has size not equal to one
+ >>> np.squeeze(x, axis=2).shape
(1, 3)
"""
@@ -1169,14 +1382,17 @@ def squeeze(a, axis=None):
squeeze = a.squeeze
except AttributeError:
return _wrapit(a, 'squeeze')
- try:
- # First try to use the new axis= parameter
- return squeeze(axis=axis)
- except TypeError:
- # For backwards compatibility
+ if axis is None:
return squeeze()
+ else:
+ return squeeze(axis=axis)
+
+
+def _diagonal_dispatcher(a, offset=None, axis1=None, axis2=None):
+ return (a,)
+@array_function_dispatch(_diagonal_dispatcher)
def diagonal(a, offset=0, axis1=0, axis2=1):
"""
Return specified diagonals.
@@ -1228,13 +1444,14 @@ def diagonal(a, offset=0, axis1=0, axis2=1):
Returns
-------
array_of_diagonals : ndarray
- If `a` is 2-D and not a matrix, a 1-D array of the same type as `a`
- containing the diagonal is returned. If `a` is a matrix, a 1-D
- array containing the diagonal is returned in order to maintain
- backward compatibility. If the dimension of `a` is greater than
- two, then an array of diagonals is returned, "packed" from
- left-most dimension to right-most (e.g., if `a` is 3-D, then the
- diagonals are "packed" along rows).
+ If `a` is 2-D, then a 1-D array containing the diagonal and of the
+ same type as `a` is returned unless `a` is a `matrix`, in which case
+ a 1-D array rather than a (2-D) `matrix` is returned in order to
+ maintain backward compatibility.
+
+ If ``a.ndim > 2``, then the dimensions specified by `axis1` and `axis2`
+ are removed, and a new axis inserted at the end corresponding to the
+ diagonal.
Raises
------
@@ -1290,6 +1507,12 @@ def diagonal(a, offset=0, axis1=0, axis2=1):
return asanyarray(a).diagonal(offset=offset, axis1=axis1, axis2=axis2)
+def _trace_dispatcher(
+ a, offset=None, axis1=None, axis2=None, dtype=None, out=None):
+ return (a, out)
+
+
+@array_function_dispatch(_trace_dispatcher)
def trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None):
"""
Return the sum along diagonals of the array.
@@ -1353,6 +1576,11 @@ def trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None):
return asanyarray(a).trace(offset=offset, axis1=axis1, axis2=axis2, dtype=dtype, out=out)
+def _ravel_dispatcher(a, order=None):
+ return (a,)
+
+
+@array_function_dispatch(_ravel_dispatcher)
def ravel(a, order='C'):
"""Return a contiguous flattened array.
@@ -1388,10 +1616,9 @@ def ravel(a, order='C'):
Returns
-------
y : array_like
- If `a` is a matrix, y is a 1-D ndarray, otherwise y is an array of
- the same subtype as `a`. The shape of the returned array is
- ``(a.size,)``. Matrices are special cased for backward
- compatibility.
+ y is an array of the same subtype as `a`, with shape ``(a.size,)``.
+ Note that matrices are special cased for backward compatibility, if `a`
+ is a matrix, then y is a 1-D ndarray.
See Also
--------
@@ -1460,6 +1687,11 @@ def ravel(a, order='C'):
return asanyarray(a).ravel(order=order)
+def _nonzero_dispatcher(a):
+ return (a,)
+
+
+@array_function_dispatch(_nonzero_dispatcher)
def nonzero(a):
"""
Return the indices of the elements that are non-zero.
@@ -1501,35 +1733,43 @@ def nonzero(a):
Examples
--------
- >>> x = np.eye(3)
+ >>> x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])
>>> x
- array([[ 1., 0., 0.],
- [ 0., 1., 0.],
- [ 0., 0., 1.]])
+ array([[3, 0, 0],
+ [0, 4, 0],
+ [5, 6, 0]])
>>> np.nonzero(x)
- (array([0, 1, 2]), array([0, 1, 2]))
+ (array([0, 1, 2, 2]), array([0, 1, 0, 1]))
>>> x[np.nonzero(x)]
- array([ 1., 1., 1.])
+ array([3, 4, 5, 6])
>>> np.transpose(np.nonzero(x))
array([[0, 0],
[1, 1],
- [2, 2]])
+ [2, 0],
+ [2, 1])
A common use for ``nonzero`` is to find the indices of an array, where
a condition is True. Given an array `a`, the condition `a` > 3 is a
boolean array and since False is interpreted as 0, np.nonzero(a > 3)
yields the indices of the `a` where the condition is true.
- >>> a = np.array([[1,2,3],[4,5,6],[7,8,9]])
+ >>> a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> a > 3
array([[False, False, False],
[ True, True, True],
- [ True, True, True]], dtype=bool)
+ [ True, True, True]])
>>> np.nonzero(a > 3)
(array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))
- The ``nonzero`` method of the boolean array can also be called.
+ Using this result to index `a` is equivalent to using the mask directly:
+
+ >>> a[np.nonzero(a > 3)]
+ array([4, 5, 6, 7, 8, 9])
+ >>> a[a > 3] # prefer this spelling
+ array([4, 5, 6, 7, 8, 9])
+
+ ``nonzero`` can also be called as a method of the array.
>>> (a > 3).nonzero()
(array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))
@@ -1538,6 +1778,11 @@ def nonzero(a):
return _wrapfunc(a, 'nonzero')
+def _shape_dispatcher(a):
+ return (a,)
+
+
+@array_function_dispatch(_shape_dispatcher)
def shape(a):
"""
Return the shape of an array.
@@ -1583,6 +1828,11 @@ def shape(a):
return result
+def _compress_dispatcher(condition, a, axis=None, out=None):
+ return (condition, a, out)
+
+
+@array_function_dispatch(_compress_dispatcher)
def compress(condition, a, axis=None, out=None):
"""
Return selected slices of an array along given axis.
@@ -1646,6 +1896,11 @@ def compress(condition, a, axis=None, out=None):
return _wrapfunc(a, 'compress', condition, axis=axis, out=out)
+def _clip_dispatcher(a, a_min, a_max, out=None):
+ return (a, a_min, a_max)
+
+
+@array_function_dispatch(_clip_dispatcher)
def clip(a, a_min, a_max, out=None):
"""
Clip (limit) the values in an array.
@@ -1659,11 +1914,15 @@ def clip(a, a_min, a_max, out=None):
----------
a : array_like
Array containing elements to clip.
- a_min : scalar or array_like
- Minimum value.
- a_max : scalar or array_like
- Maximum value. If `a_min` or `a_max` are array_like, then they will
- be broadcasted to the shape of `a`.
+ a_min : scalar or array_like or `None`
+ Minimum value. If `None`, clipping is not performed on lower
+ interval edge. Not more than one of `a_min` and `a_max` may be
+ `None`.
+ a_max : scalar or array_like or `None`
+ Maximum value. If `None`, clipping is not performed on upper
+ interval edge. Not more than one of `a_min` and `a_max` may be
+ `None`. If `a_min` or `a_max` are array_like, then the three
+ arrays will be broadcasted to match their shapes.
out : ndarray, optional
The results will be placed in this array. It may be the input
array for in-place clipping. `out` must be of the right shape
@@ -1692,14 +1951,20 @@ def clip(a, a_min, a_max, out=None):
>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
- >>> np.clip(a, [3,4,1,1,1,4,4,4,4,4], 8)
+ >>> np.clip(a, [3, 4, 1, 1, 1, 4, 4, 4, 4, 4], 8)
array([3, 4, 2, 3, 4, 5, 6, 7, 8, 8])
"""
return _wrapfunc(a, 'clip', a_min, a_max, out=out)
-def sum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
+def _sum_dispatcher(a, axis=None, dtype=None, out=None, keepdims=None,
+ initial=None):
+ return (a, out)
+
+
+@array_function_dispatch(_sum_dispatcher)
+def sum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue, initial=np._NoValue):
"""
Sum of array elements over a given axis.
@@ -1731,13 +1996,17 @@ def sum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
keepdims : bool, optional
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
- the result will broadcast correctly against the original `arr`.
+ the result will broadcast correctly against the input array.
If the default value is passed, then `keepdims` will not be
passed through to the `sum` method of sub-classes of
`ndarray`, however any non-default value will be. If the
- sub-classes `sum` method does not implement `keepdims` any
+ sub-class' method does not implement `keepdims` any
exceptions will be raised.
+ initial : scalar, optional
+ Starting value for the sum. See `~numpy.ufunc.reduce` for details.
+
+ .. versionadded:: 1.15.0
Returns
-------
@@ -1785,76 +2054,33 @@ def sum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
>>> np.ones(128, dtype=np.int8).sum(dtype=np.int8)
-128
+ You can also start the sum with a value other than zero:
+
+ >>> np.sum([10], initial=5)
+ 15
"""
- kwargs = {}
- if keepdims is not np._NoValue:
- kwargs['keepdims'] = keepdims
if isinstance(a, _gentype):
+ # 2018-02-25, 1.15.0
+ warnings.warn(
+ "Calling np.sum(generator) is deprecated, and in the future will give a different result. "
+ "Use np.sum(np.fromiter(generator)) or the python sum builtin instead.",
+ DeprecationWarning, stacklevel=2)
+
res = _sum_(a)
if out is not None:
out[...] = res
return out
return res
- if type(a) is not mu.ndarray:
- try:
- sum = a.sum
- except AttributeError:
- pass
- else:
- return sum(axis=axis, dtype=dtype, out=out, **kwargs)
- return _methods._sum(a, axis=axis, dtype=dtype,
- out=out, **kwargs)
-
-def product(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
- """
- Return the product of array elements over a given axis.
-
- See Also
- --------
- prod : equivalent function; see for details.
-
- """
- kwargs = {}
- if keepdims is not np._NoValue:
- kwargs['keepdims'] = keepdims
- return um.multiply.reduce(a, axis=axis, dtype=dtype, out=out, **kwargs)
+ return _wrapreduction(a, np.add, 'sum', axis, dtype, out, keepdims=keepdims,
+ initial=initial)
-def sometrue(a, axis=None, out=None, keepdims=np._NoValue):
- """
- Check whether some values are true.
-
- Refer to `any` for full documentation.
-
- See Also
- --------
- any : equivalent function
-
- """
- arr = asanyarray(a)
- kwargs = {}
- if keepdims is not np._NoValue:
- kwargs['keepdims'] = keepdims
- return arr.any(axis=axis, out=out, **kwargs)
-
-
-def alltrue(a, axis=None, out=None, keepdims=np._NoValue):
- """
- Check if all elements of input array are true.
-
- See Also
- --------
- numpy.all : Equivalent function; see for details.
-
- """
- arr = asanyarray(a)
- kwargs = {}
- if keepdims is not np._NoValue:
- kwargs['keepdims'] = keepdims
- return arr.all(axis=axis, out=out, **kwargs)
+def _any_dispatcher(a, axis=None, out=None, keepdims=None):
+ return (a, out)
+@array_function_dispatch(_any_dispatcher)
def any(a, axis=None, out=None, keepdims=np._NoValue):
"""
Test whether any array element along a given axis evaluates to True.
@@ -1885,12 +2111,12 @@ def any(a, axis=None, out=None, keepdims=np._NoValue):
keepdims : bool, optional
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
- the result will broadcast correctly against the original `arr`.
+ the result will broadcast correctly against the input array.
If the default value is passed, then `keepdims` will not be
passed through to the `any` method of sub-classes of
`ndarray`, however any non-default value will be. If the
- sub-classes `sum` method does not implement `keepdims` any
+ sub-class' method does not implement `keepdims` any
exceptions will be raised.
Returns
@@ -1916,7 +2142,7 @@ def any(a, axis=None, out=None, keepdims=np._NoValue):
True
>>> np.any([[True, False], [False, False]], axis=0)
- array([ True, False], dtype=bool)
+ array([ True, False])
>>> np.any([-1, 0, 5])
True
@@ -1927,7 +2153,7 @@ def any(a, axis=None, out=None, keepdims=np._NoValue):
>>> o=np.array([False])
>>> z=np.any([-1, 4, 5], out=o)
>>> z, o
- (array([ True], dtype=bool), array([ True], dtype=bool))
+ (array([ True]), array([ True]))
>>> # Check now that z is a reference to o
>>> z is o
True
@@ -1935,13 +2161,14 @@ def any(a, axis=None, out=None, keepdims=np._NoValue):
(191614240, 191614240)
"""
- arr = asanyarray(a)
- kwargs = {}
- if keepdims is not np._NoValue:
- kwargs['keepdims'] = keepdims
- return arr.any(axis=axis, out=out, **kwargs)
+ return _wrapreduction(a, np.logical_or, 'any', axis, None, out, keepdims=keepdims)
+
+
+def _all_dispatcher(a, axis=None, out=None, keepdims=None):
+ return (a, out)
+@array_function_dispatch(_all_dispatcher)
def all(a, axis=None, out=None, keepdims=np._NoValue):
"""
Test whether all array elements along a given axis evaluate to True.
@@ -1970,12 +2197,12 @@ def all(a, axis=None, out=None, keepdims=np._NoValue):
keepdims : bool, optional
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
- the result will broadcast correctly against the original `arr`.
+ the result will broadcast correctly against the input array.
If the default value is passed, then `keepdims` will not be
passed through to the `all` method of sub-classes of
`ndarray`, however any non-default value will be. If the
- sub-classes `sum` method does not implement `keepdims` any
+ sub-class' method does not implement `keepdims` any
exceptions will be raised.
Returns
@@ -2001,7 +2228,7 @@ def all(a, axis=None, out=None, keepdims=np._NoValue):
False
>>> np.all([[True,False],[True,True]], axis=0)
- array([ True, False], dtype=bool)
+ array([ True, False])
>>> np.all([-1, 4, 5])
True
@@ -2012,16 +2239,17 @@ def all(a, axis=None, out=None, keepdims=np._NoValue):
>>> o=np.array([False])
>>> z=np.all([-1, 4, 5], out=o)
>>> id(z), id(o), z # doctest: +SKIP
- (28293632, 28293632, array([ True], dtype=bool))
+ (28293632, 28293632, array([ True]))
"""
- arr = asanyarray(a)
- kwargs = {}
- if keepdims is not np._NoValue:
- kwargs['keepdims'] = keepdims
- return arr.all(axis=axis, out=out, **kwargs)
+ return _wrapreduction(a, np.logical_and, 'all', axis, None, out, keepdims=keepdims)
+def _cumsum_dispatcher(a, axis=None, dtype=None, out=None):
+ return (a, out)
+
+
+@array_function_dispatch(_cumsum_dispatcher)
def cumsum(a, axis=None, dtype=None, out=None):
"""
Return the cumulative sum of the elements along a given axis.
@@ -2089,20 +2317,12 @@ def cumsum(a, axis=None, dtype=None, out=None):
return _wrapfunc(a, 'cumsum', axis=axis, dtype=dtype, out=out)
-def cumproduct(a, axis=None, dtype=None, out=None):
- """
- Return the cumulative product over the given axis.
-
-
- See Also
- --------
- cumprod : equivalent function; see for details.
-
- """
- return _wrapfunc(a, 'cumprod', axis=axis, dtype=dtype, out=out)
+def _ptp_dispatcher(a, axis=None, out=None, keepdims=None):
+ return (a, out)
-def ptp(a, axis=None, out=None):
+@array_function_dispatch(_ptp_dispatcher)
+def ptp(a, axis=None, out=None, keepdims=np._NoValue):
"""
Range of values (maximum - minimum) along an axis.
@@ -2112,14 +2332,31 @@ def ptp(a, axis=None, out=None):
----------
a : array_like
Input values.
- axis : int, optional
+ axis : None or int or tuple of ints, optional
Axis along which to find the peaks. By default, flatten the
- array.
+ array. `axis` may be negative, in
+ which case it counts from the last to the first axis.
+
+ .. versionadded:: 1.15.0
+
+ If this is a tuple of ints, a reduction is performed on multiple
+ axes, instead of a single axis or all the axes as before.
out : array_like
Alternative output array in which to place the result. It must
have the same shape and buffer length as the expected output,
but the type of the output values will be cast if necessary.
+ keepdims : bool, optional
+ If this is set to True, the axes which are reduced are left
+ in the result as dimensions with size one. With this option,
+ the result will broadcast correctly against the input array.
+
+ If the default value is passed, then `keepdims` will not be
+ passed through to the `ptp` method of sub-classes of
+ `ndarray`, however any non-default value will be. If the
+ sub-class' method does not implement `keepdims` any
+ exceptions will be raised.
+
Returns
-------
ptp : ndarray
@@ -2140,10 +2377,25 @@ def ptp(a, axis=None, out=None):
array([1, 1])
"""
- return _wrapfunc(a, 'ptp', axis=axis, out=out)
+ kwargs = {}
+ if keepdims is not np._NoValue:
+ kwargs['keepdims'] = keepdims
+ if type(a) is not mu.ndarray:
+ try:
+ ptp = a.ptp
+ except AttributeError:
+ pass
+ else:
+ return ptp(axis=axis, out=out, **kwargs)
+ return _methods._ptp(a, axis=axis, out=out, **kwargs)
+
+def _amax_dispatcher(a, axis=None, out=None, keepdims=None, initial=None):
+ return (a, out)
-def amax(a, axis=None, out=None, keepdims=np._NoValue):
+
+@array_function_dispatch(_amax_dispatcher)
+def amax(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue):
"""
Return the maximum of an array or maximum along an axis.
@@ -2155,7 +2407,7 @@ def amax(a, axis=None, out=None, keepdims=np._NoValue):
Axis or axes along which to operate. By default, flattened input is
used.
- .. versionadded: 1.7.0
+ .. versionadded:: 1.7.0
If this is a tuple of ints, the maximum is selected over multiple axes,
instead of a single axis or all the axes as before.
@@ -2167,14 +2419,21 @@ def amax(a, axis=None, out=None, keepdims=np._NoValue):
keepdims : bool, optional
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
- the result will broadcast correctly against the original `arr`.
+ the result will broadcast correctly against the input array.
If the default value is passed, then `keepdims` will not be
passed through to the `amax` method of sub-classes of
`ndarray`, however any non-default value will be. If the
- sub-classes `sum` method does not implement `keepdims` any
+ sub-class' method does not implement `keepdims` any
exceptions will be raised.
+ initial : scalar, optional
+ The minimum value of an output element. Must be present to allow
+ computation on empty slice. See `~numpy.ufunc.reduce` for details.
+
+ .. versionadded:: 1.15.0
+
+
Returns
-------
amax : ndarray or scalar
@@ -2220,31 +2479,38 @@ def amax(a, axis=None, out=None, keepdims=np._NoValue):
>>> np.amax(a, axis=1) # Maxima along the second axis
array([1, 3])
- >>> b = np.arange(5, dtype=np.float)
+ >>> b = np.arange(5, dtype=float)
>>> b[2] = np.NaN
>>> np.amax(b)
nan
>>> np.nanmax(b)
4.0
+ You can use an initial value to compute the maximum of an empty slice, or
+ to initialize it to a different value:
+
+ >>> np.max([[-50], [10]], axis=-1, initial=0)
+ array([ 0, 10])
+
+ Notice that the initial value is used as one of the elements for which the
+ maximum is determined, unlike for the default argument Python's max
+ function, which is only used for empty iterables.
+
+ >>> np.max([5], initial=6)
+ 6
+ >>> max([5], default=6)
+ 5
"""
- kwargs = {}
- if keepdims is not np._NoValue:
- kwargs['keepdims'] = keepdims
+ return _wrapreduction(a, np.maximum, 'max', axis, None, out, keepdims=keepdims,
+ initial=initial)
- if type(a) is not mu.ndarray:
- try:
- amax = a.max
- except AttributeError:
- pass
- else:
- return amax(axis=axis, out=out, **kwargs)
- return _methods._amax(a, axis=axis,
- out=out, **kwargs)
+def _amin_dispatcher(a, axis=None, out=None, keepdims=None, initial=None):
+ return (a, out)
-def amin(a, axis=None, out=None, keepdims=np._NoValue):
+@array_function_dispatch(_amin_dispatcher)
+def amin(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue):
"""
Return the minimum of an array or minimum along an axis.
@@ -2256,7 +2522,7 @@ def amin(a, axis=None, out=None, keepdims=np._NoValue):
Axis or axes along which to operate. By default, flattened input is
used.
- .. versionadded: 1.7.0
+ .. versionadded:: 1.7.0
If this is a tuple of ints, the minimum is selected over multiple axes,
instead of a single axis or all the axes as before.
@@ -2268,14 +2534,20 @@ def amin(a, axis=None, out=None, keepdims=np._NoValue):
keepdims : bool, optional
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
- the result will broadcast correctly against the original `arr`.
+ the result will broadcast correctly against the input array.
If the default value is passed, then `keepdims` will not be
passed through to the `amin` method of sub-classes of
`ndarray`, however any non-default value will be. If the
- sub-classes `sum` method does not implement `keepdims` any
+ sub-class' method does not implement `keepdims` any
exceptions will be raised.
+ initial : scalar, optional
+ The maximum value of an output element. Must be present to allow
+ computation on empty slice. See `~numpy.ufunc.reduce` for details.
+
+ .. versionadded:: 1.15.0
+
Returns
-------
amin : ndarray or scalar
@@ -2321,29 +2593,36 @@ def amin(a, axis=None, out=None, keepdims=np._NoValue):
>>> np.amin(a, axis=1) # Minima along the second axis
array([0, 2])
- >>> b = np.arange(5, dtype=np.float)
+ >>> b = np.arange(5, dtype=float)
>>> b[2] = np.NaN
>>> np.amin(b)
nan
>>> np.nanmin(b)
0.0
+ >>> np.min([[-50], [10]], axis=-1, initial=0)
+ array([-50, 0])
+
+ Notice that the initial value is used as one of the elements for which the
+ minimum is determined, unlike for the default argument Python's max
+ function, which is only used for empty iterables.
+
+ Notice that this isn't the same as Python's ``default`` argument.
+
+ >>> np.min([6], initial=5)
+ 5
+ >>> min([6], default=5)
+ 6
"""
- kwargs = {}
- if keepdims is not np._NoValue:
- kwargs['keepdims'] = keepdims
- if type(a) is not mu.ndarray:
- try:
- amin = a.min
- except AttributeError:
- pass
- else:
- return amin(axis=axis, out=out, **kwargs)
+ return _wrapreduction(a, np.minimum, 'min', axis, None, out, keepdims=keepdims,
+ initial=initial)
- return _methods._amin(a, axis=axis,
- out=out, **kwargs)
+def _alen_dispathcer(a):
+ return (a,)
+
+@array_function_dispatch(_alen_dispathcer)
def alen(a):
"""
Return the length of the first dimension of the input array.
@@ -2377,7 +2656,13 @@ def alen(a):
return len(array(a, ndmin=1))
-def prod(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
+def _prod_dispatcher(
+ a, axis=None, dtype=None, out=None, keepdims=None, initial=None):
+ return (a, out)
+
+
+@array_function_dispatch(_prod_dispatcher)
+def prod(a, axis=None, dtype=None, out=None, keepdims=np._NoValue, initial=np._NoValue):
"""
Return the product of array elements over a given axis.
@@ -2415,8 +2700,12 @@ def prod(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
If the default value is passed, then `keepdims` will not be
passed through to the `prod` method of sub-classes of
`ndarray`, however any non-default value will be. If the
- sub-classes `sum` method does not implement `keepdims` any
+ sub-class' method does not implement `keepdims` any
exceptions will be raised.
+ initial : scalar, optional
+ The starting value for this product. See `~numpy.ufunc.reduce` for details.
+
+ .. versionadded:: 1.15.0
Returns
-------
@@ -2435,7 +2724,7 @@ def prod(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
raised on overflow. That means that, on a 32-bit platform:
>>> x = np.array([536870910, 536870910, 536870910, 536870910])
- >>> np.prod(x) #random
+ >>> np.prod(x) # random
16
The product of an empty array is the neutral element 1:
@@ -2471,25 +2760,23 @@ def prod(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
is the default platform integer:
>>> x = np.array([1, 2, 3], dtype=np.int8)
- >>> np.prod(x).dtype == np.int
+ >>> np.prod(x).dtype == int
True
+ You can also start the product with a value other than one:
+
+ >>> np.prod([1, 2], initial=5)
+ 10
"""
- kwargs = {}
- if keepdims is not np._NoValue:
- kwargs['keepdims'] = keepdims
- if type(a) is not mu.ndarray:
- try:
- prod = a.prod
- except AttributeError:
- pass
- else:
- return prod(axis=axis, dtype=dtype, out=out, **kwargs)
+ return _wrapreduction(a, np.multiply, 'prod', axis, dtype, out, keepdims=keepdims,
+ initial=initial)
- return _methods._prod(a, axis=axis, dtype=dtype,
- out=out, **kwargs)
+
+def _cumprod_dispatcher(a, axis=None, dtype=None, out=None):
+ return (a, out)
+@array_function_dispatch(_cumprod_dispatcher)
def cumprod(a, axis=None, dtype=None, out=None):
"""
Return the cumulative product of elements along a given axis.
@@ -2553,6 +2840,11 @@ def cumprod(a, axis=None, dtype=None, out=None):
return _wrapfunc(a, 'cumprod', axis=axis, dtype=dtype, out=out)
+def _ndim_dispatcher(a):
+ return (a,)
+
+
+@array_function_dispatch(_ndim_dispatcher)
def ndim(a):
"""
Return the number of dimensions of an array.
@@ -2590,62 +2882,11 @@ def ndim(a):
return asarray(a).ndim
-def rank(a):
- """
- Return the number of dimensions of an array.
-
- If `a` is not already an array, a conversion is attempted.
- Scalars are zero dimensional.
-
- .. note::
- This function is deprecated in NumPy 1.9 to avoid confusion with
- `numpy.linalg.matrix_rank`. The ``ndim`` attribute or function
- should be used instead.
-
- Parameters
- ----------
- a : array_like
- Array whose number of dimensions is desired. If `a` is not an array,
- a conversion is attempted.
-
- Returns
- -------
- number_of_dimensions : int
- The number of dimensions in the array.
-
- See Also
- --------
- ndim : equivalent function
- ndarray.ndim : equivalent property
- shape : dimensions of array
- ndarray.shape : dimensions of array
-
- Notes
- -----
- In the old Numeric package, `rank` was the term used for the number of
- dimensions, but in Numpy `ndim` is used instead.
-
- Examples
- --------
- >>> np.rank([1,2,3])
- 1
- >>> np.rank(np.array([[1,2,3],[4,5,6]]))
- 2
- >>> np.rank(1)
- 0
-
- """
- # 2014-04-12, 1.9
- warnings.warn(
- "`rank` is deprecated; use the `ndim` attribute or function instead. "
- "To find the rank of a matrix see `numpy.linalg.matrix_rank`.",
- VisibleDeprecationWarning)
- try:
- return a.ndim
- except AttributeError:
- return asarray(a).ndim
+def _size_dispatcher(a, axis=None):
+ return (a,)
+@array_function_dispatch(_size_dispatcher)
def size(a, axis=None):
"""
Return the number of elements along a given axis.
@@ -2692,6 +2933,11 @@ def size(a, axis=None):
return asarray(a).shape[axis]
+def _around_dispatcher(a, decimals=None, out=None):
+ return (a, out)
+
+
+@array_function_dispatch(_around_dispatcher)
def around(a, decimals=0, out=None):
"""
Evenly round to the given number of decimals.
@@ -2729,7 +2975,7 @@ def around(a, decimals=0, out=None):
Notes
-----
- For values exactly halfway between rounded decimal values, Numpy
+ For values exactly halfway between rounded decimal values, NumPy
rounds to the nearest even value. Thus 1.5 and 2.5 round to 2.0,
-0.5 and 0.5 round to 0.0, etc. Results may also be surprising due
to the inexact representation of decimal fractions in the IEEE
@@ -2738,11 +2984,11 @@ def around(a, decimals=0, out=None):
References
----------
- .. [1] "Lecture Notes on the Status of IEEE 754", William Kahan,
- http://www.cs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF
+ .. [1] "Lecture Notes on the Status of IEEE 754", William Kahan,
+ https://people.eecs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF
.. [2] "How Futile are Mindless Assessments of
Roundoff in Floating-Point Computation?", William Kahan,
- http://www.cs.berkeley.edu/~wkahan/Mindless.pdf
+ https://people.eecs.berkeley.edu/~wkahan/Mindless.pdf
Examples
--------
@@ -2761,20 +3007,11 @@ def around(a, decimals=0, out=None):
return _wrapfunc(a, 'round', decimals=decimals, out=out)
-def round_(a, decimals=0, out=None):
- """
- Round an array to the given number of decimals.
-
- Refer to `around` for full documentation.
-
- See Also
- --------
- around : equivalent function
-
- """
- return around(a, decimals=decimals, out=out)
+def _mean_dispatcher(a, axis=None, dtype=None, out=None, keepdims=None):
+ return (a, out)
+@array_function_dispatch(_mean_dispatcher)
def mean(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
"""
Compute the arithmetic mean along the specified axis.
@@ -2792,7 +3029,7 @@ def mean(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
Axis or axes along which the means are computed. The default is to
compute the mean of the flattened array.
- .. versionadded: 1.7.0
+ .. versionadded:: 1.7.0
If this is a tuple of ints, a mean is performed over multiple axes,
instead of a single axis or all the axes as before.
@@ -2809,12 +3046,12 @@ def mean(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
keepdims : bool, optional
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
- the result will broadcast correctly against the original `arr`.
+ the result will broadcast correctly against the input array.
If the default value is passed, then `keepdims` will not be
passed through to the `mean` method of sub-classes of
`ndarray`, however any non-default value will be. If the
- sub-classes `sum` method does not implement `keepdims` any
+ sub-class' method does not implement `keepdims` any
exceptions will be raised.
Returns
@@ -2839,6 +3076,9 @@ def mean(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
example below). Specifying a higher-precision accumulator using the
`dtype` keyword can alleviate this issue.
+ By default, `float16` results are computed using `float32` intermediates
+ for extra precision.
+
Examples
--------
>>> a = np.array([[1, 2], [3, 4]])
@@ -2855,7 +3095,7 @@ def mean(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
>>> a[0, :] = 1.0
>>> a[1, :] = 0.1
>>> np.mean(a)
- 0.546875
+ 0.54999924
Computing the mean in float64 is more accurate:
@@ -2878,6 +3118,12 @@ def mean(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
out=out, **kwargs)
+def _std_dispatcher(
+ a, axis=None, dtype=None, out=None, ddof=None, keepdims=None):
+ return (a, out)
+
+
+@array_function_dispatch(_std_dispatcher)
def std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue):
"""
Compute the standard deviation along the specified axis.
@@ -2894,7 +3140,7 @@ def std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue):
Axis or axes along which the standard deviation is computed. The
default is to compute the standard deviation of the flattened array.
- .. versionadded: 1.7.0
+ .. versionadded:: 1.7.0
If this is a tuple of ints, a standard deviation is performed over
multiple axes, instead of a single axis or all the axes as before.
@@ -2913,12 +3159,12 @@ def std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue):
keepdims : bool, optional
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
- the result will broadcast correctly against the original `arr`.
+ the result will broadcast correctly against the input array.
If the default value is passed, then `keepdims` will not be
passed through to the `std` method of sub-classes of
`ndarray`, however any non-default value will be. If the
- sub-classes `sum` method does not implement `keepdims` any
+ sub-class' method does not implement `keepdims` any
exceptions will be raised.
Returns
@@ -2996,6 +3242,12 @@ def std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue):
**kwargs)
+def _var_dispatcher(
+ a, axis=None, dtype=None, out=None, ddof=None, keepdims=None):
+ return (a, out)
+
+
+@array_function_dispatch(_var_dispatcher)
def var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue):
"""
Compute the variance along the specified axis.
@@ -3013,7 +3265,7 @@ def var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue):
Axis or axes along which the variance is computed. The default is to
compute the variance of the flattened array.
- .. versionadded: 1.7.0
+ .. versionadded:: 1.7.0
If this is a tuple of ints, a variance is performed over multiple axes,
instead of a single axis or all the axes as before.
@@ -3032,12 +3284,12 @@ def var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue):
keepdims : bool, optional
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
- the result will broadcast correctly against the original `arr`.
+ the result will broadcast correctly against the input array.
If the default value is passed, then `keepdims` will not be
passed through to the `var` method of sub-classes of
`ndarray`, however any non-default value will be. If the
- sub-classes `sum` method does not implement `keepdims` any
+ sub-class' method does not implement `keepdims` any
exceptions will be raised.
Returns
@@ -3113,3 +3365,95 @@ def var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue):
return _methods._var(a, axis=axis, dtype=dtype, out=out, ddof=ddof,
**kwargs)
+
+
+# Aliases of other functions. These have their own definitions only so that
+# they can have unique docstrings.
+
+@array_function_dispatch(_around_dispatcher)
+def round_(a, decimals=0, out=None):
+ """
+ Round an array to the given number of decimals.
+
+ See Also
+ --------
+ around : equivalent function; see for details.
+ """
+ return around(a, decimals=decimals, out=out)
+
+
+@array_function_dispatch(_prod_dispatcher, verify=False)
+def product(*args, **kwargs):
+ """
+ Return the product of array elements over a given axis.
+
+ See Also
+ --------
+ prod : equivalent function; see for details.
+ """
+ return prod(*args, **kwargs)
+
+
+@array_function_dispatch(_cumprod_dispatcher, verify=False)
+def cumproduct(*args, **kwargs):
+ """
+ Return the cumulative product over the given axis.
+
+ See Also
+ --------
+ cumprod : equivalent function; see for details.
+ """
+ return cumprod(*args, **kwargs)
+
+
+@array_function_dispatch(_any_dispatcher, verify=False)
+def sometrue(*args, **kwargs):
+ """
+ Check whether some values are true.
+
+ Refer to `any` for full documentation.
+
+ See Also
+ --------
+ any : equivalent function; see for details.
+ """
+ return any(*args, **kwargs)
+
+
+@array_function_dispatch(_all_dispatcher, verify=False)
+def alltrue(*args, **kwargs):
+ """
+ Check if all elements of input array are true.
+
+ See Also
+ --------
+ numpy.all : Equivalent function; see for details.
+ """
+ return all(*args, **kwargs)
+
+
+@array_function_dispatch(_ndim_dispatcher)
+def rank(a):
+ """
+ Return the number of dimensions of an array.
+
+ .. note::
+ This function is deprecated in NumPy 1.9 to avoid confusion with
+ `numpy.linalg.matrix_rank`. The ``ndim`` attribute or function
+ should be used instead.
+
+ See Also
+ --------
+ ndim : equivalent non-deprecated function
+
+ Notes
+ -----
+ In the old Numeric package, `rank` was the term used for the number of
+ dimensions, but in NumPy `ndim` is used instead.
+ """
+ # 2014-04-12, 1.9
+ warnings.warn(
+ "`rank` is deprecated; use the `ndim` attribute or function instead. "
+ "To find the rank of a matrix see `numpy.linalg.matrix_rank`.",
+ VisibleDeprecationWarning, stacklevel=2)
+ return ndim(a)