summaryrefslogtreecommitdiff
path: root/numpy/core/numeric.py
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/core/numeric.py')
-rw-r--r--numpy/core/numeric.py114
1 files changed, 61 insertions, 53 deletions
diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py
index 123bff2ec..1108d4667 100644
--- a/numpy/core/numeric.py
+++ b/numpy/core/numeric.py
@@ -1,6 +1,11 @@
from __future__ import division, absolute_import, print_function
-import collections
+try:
+ # Accessing collections abstract classes from collections
+ # has been deprecated since Python 3.3
+ import collections.abc as collections_abc
+except ImportError:
+ import collections as collections_abc
import itertools
import operator
import sys
@@ -69,7 +74,7 @@ __all__ = [
'False_', 'True_', 'bitwise_not', 'CLIP', 'RAISE', 'WRAP', 'MAXDIMS',
'BUFSIZE', 'ALLOW_THREADS', 'ComplexWarning', 'full', 'full_like',
'matmul', 'shares_memory', 'may_share_memory', 'MAY_SHARE_BOUNDS',
- 'MAY_SHARE_EXACT', 'TooHardError', 'AxisError' ]
+ 'MAY_SHARE_EXACT', 'TooHardError', 'AxisError']
if sys.version_info[0] < 3:
__all__.extend(['getbuffer', 'newbuffer'])
@@ -118,11 +123,10 @@ def zeros_like(a, dtype=None, order='K', subok=True):
See Also
--------
- ones_like : Return an array of ones with shape and type of input.
empty_like : Return an empty array with shape and type of input.
+ ones_like : Return an array of ones with shape and type of input.
+ full_like : Return a new array with shape of input filled with value.
zeros : Return a new array setting values to zero.
- ones : Return a new array setting values to one.
- empty : Return a new uninitialized array.
Examples
--------
@@ -160,9 +164,10 @@ def ones(shape, dtype=None, order='C'):
dtype : data-type, optional
The desired data-type for the array, e.g., `numpy.int8`. Default is
`numpy.float64`.
- order : {'C', 'F'}, optional
- Whether to store multidimensional data in C- or Fortran-contiguous
- (row- or column-wise) order in memory.
+ order : {'C', 'F'}, optional, default: C
+ Whether to store multi-dimensional data in row-major
+ (C-style) or column-major (Fortran-style) order in
+ memory.
Returns
-------
@@ -171,7 +176,11 @@ def ones(shape, dtype=None, order='C'):
See Also
--------
- zeros, ones_like
+ ones_like : Return an array of ones with shape and type of input.
+ empty : Return a new uninitialized array.
+ zeros : Return a new array setting values to zero.
+ full : Return a new array of given shape filled with value.
+
Examples
--------
@@ -228,11 +237,10 @@ def ones_like(a, dtype=None, order='K', subok=True):
See Also
--------
- zeros_like : Return an array of zeros with shape and type of input.
empty_like : Return an empty array with shape and type of input.
- zeros : Return a new array setting values to zero.
+ zeros_like : Return an array of zeros with shape and type of input.
+ full_like : Return a new array with shape of input filled with value.
ones : Return a new array setting values to one.
- empty : Return a new uninitialized array.
Examples
--------
@@ -281,13 +289,10 @@ def full(shape, fill_value, dtype=None, order='C'):
See Also
--------
- zeros_like : Return an array of zeros with shape and type of input.
- ones_like : Return an array of ones with shape and type of input.
- empty_like : Return an empty array with shape and type of input.
- full_like : Fill an array with shape and type of input.
- zeros : Return a new array setting values to zero.
- ones : Return a new array setting values to one.
+ full_like : Return a new array with shape of input filled with value.
empty : Return a new uninitialized array.
+ ones : Return a new array setting values to one.
+ zeros : Return a new array setting values to zero.
Examples
--------
@@ -336,13 +341,10 @@ def full_like(a, fill_value, dtype=None, order='K', subok=True):
See Also
--------
- zeros_like : Return an array of zeros with shape and type of input.
- ones_like : Return an array of ones with shape and type of input.
empty_like : Return an empty array with shape and type of input.
- zeros : Return a new array setting values to zero.
- ones : Return a new array setting values to one.
- empty : Return a new uninitialized array.
- full : Fill a new array.
+ ones_like : Return an array of ones with shape and type of input.
+ zeros_like : Return an array of zeros with shape and type of input.
+ full : Return a new array of given shape filled with value.
Examples
--------
@@ -365,6 +367,7 @@ def full_like(a, fill_value, dtype=None, order='K', subok=True):
multiarray.copyto(res, fill_value, casting='unsafe')
return res
+
def count_nonzero(a, axis=None):
"""
Counts the number of non-zero values in the array ``a``.
@@ -686,12 +689,12 @@ def require(a, dtype=None, requirements=None):
UPDATEIFCOPY : False
"""
- possible_flags = {'C':'C', 'C_CONTIGUOUS':'C', 'CONTIGUOUS':'C',
- 'F':'F', 'F_CONTIGUOUS':'F', 'FORTRAN':'F',
- 'A':'A', 'ALIGNED':'A',
- 'W':'W', 'WRITEABLE':'W',
- 'O':'O', 'OWNDATA':'O',
- 'E':'E', 'ENSUREARRAY':'E'}
+ possible_flags = {'C': 'C', 'C_CONTIGUOUS': 'C', 'CONTIGUOUS': 'C',
+ 'F': 'F', 'F_CONTIGUOUS': 'F', 'FORTRAN': 'F',
+ 'A': 'A', 'ALIGNED': 'A',
+ 'W': 'W', 'WRITEABLE': 'W',
+ 'O': 'O', 'OWNDATA': 'O',
+ 'E': 'E', 'ENSUREARRAY': 'E'}
if not requirements:
return asanyarray(a, dtype=dtype)
else:
@@ -829,12 +832,12 @@ def flatnonzero(a):
"""
Return indices that are non-zero in the flattened version of a.
- This is equivalent to a.ravel().nonzero()[0].
+ This is equivalent to np.nonzero(np.ravel(a))[0].
Parameters
----------
- a : ndarray
- Input array.
+ a : array_like
+ Input data.
Returns
-------
@@ -862,7 +865,7 @@ def flatnonzero(a):
array([-2, -1, 1, 2])
"""
- return a.ravel().nonzero()[0]
+ return np.nonzero(np.ravel(a))[0]
_mode_from_name_dict = {'v': 0,
@@ -1082,7 +1085,7 @@ def outer(a, b, out=None):
References
----------
- .. [1] : G. H. Golub and C. F. van Loan, *Matrix Computations*, 3rd
+ .. [1] : G. H. Golub and C. F. Van Loan, *Matrix Computations*, 3rd
ed., Baltimore, MD, Johns Hopkins University Press, 1996,
pg. 8.
@@ -1123,7 +1126,7 @@ def outer(a, b, out=None):
"""
a = asarray(a)
b = asarray(b)
- return multiply(a.ravel()[:, newaxis], b.ravel()[newaxis,:], out)
+ return multiply(a.ravel()[:, newaxis], b.ravel()[newaxis, :], out)
def tensordot(a, b, axes=2):
@@ -1790,6 +1793,7 @@ def cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None):
return moveaxis(cp, -1, axisc)
+
little_endian = (sys.byteorder == 'little')
@@ -2313,12 +2317,12 @@ def isclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False):
absolute(`a` - `b`) <= (`atol` + `rtol` * absolute(`b`))
Unlike the built-in `math.isclose`, the above equation is not symmetric
- in `a` and `b` -- it assumes `b` is the reference value -- so that
+ in `a` and `b` -- it assumes `b` is the reference value -- so that
`isclose(a, b)` might be different from `isclose(b, a)`. Furthermore,
the default value of atol is not zero, and is used to determine what
small values should be considered close to zero. The default value is
appropriate for expected values of order unity: if the expected values
- are significantly smaller than one, it can result in false positives.
+ are significantly smaller than one, it can result in false positives.
`atol` should be carefully selected for the use case at hand. A zero value
for `atol` will result in `False` if either `a` or `b` is zero.
@@ -2471,12 +2475,12 @@ def array_equiv(a1, a2):
return bool(asarray(a1 == a2).all())
-_errdict = {"ignore":ERR_IGNORE,
- "warn":ERR_WARN,
- "raise":ERR_RAISE,
- "call":ERR_CALL,
- "print":ERR_PRINT,
- "log":ERR_LOG}
+_errdict = {"ignore": ERR_IGNORE,
+ "warn": ERR_WARN,
+ "raise": ERR_RAISE,
+ "call": ERR_CALL,
+ "print": ERR_PRINT,
+ "log": ERR_LOG}
_errdict_rev = {}
for key in _errdict.keys():
@@ -2525,7 +2529,7 @@ def seterr(all=None, divide=None, over=None, under=None, invalid=None):
Notes
-----
- The floating-point exceptions are defined in the IEEE 754 standard [1]:
+ The floating-point exceptions are defined in the IEEE 754 standard [1]_:
- Division by zero: infinite result obtained from finite numbers.
- Overflow: result too large to be expressed.
@@ -2543,7 +2547,8 @@ def seterr(all=None, divide=None, over=None, under=None, invalid=None):
{'over': 'ignore', 'divide': 'ignore', 'invalid': 'ignore',
'under': 'ignore'}
>>> np.seterr(**old_settings) # reset to default
- {'over': 'raise', 'divide': 'ignore', 'invalid': 'ignore', 'under': 'ignore'}
+ {'over': 'raise', 'divide': 'ignore', 'invalid': 'ignore',
+ 'under': 'ignore'}
>>> np.int16(32000) * np.int16(3)
30464
@@ -2690,11 +2695,11 @@ def seterrcall(func):
Function to call upon floating-point errors ('call'-mode) or
object whose 'write' method is used to log such message ('log'-mode).
- The call function takes two arguments. The first is a string describing the
- type of error (such as "divide by zero", "overflow", "underflow", or "invalid value"),
- and the second is the status flag. The flag is a byte, whose four
- least-significant bits indicate the type of error, one of "divide", "over",
- "under", "invalid"::
+ The call function takes two arguments. The first is a string describing
+ the type of error (such as "divide by zero", "overflow", "underflow",
+ or "invalid value"), and the second is the status flag. The flag is a
+ byte, whose four least-significant bits indicate the type of error, one
+ of "divide", "over", "under", "invalid"::
[0 0 0 0 divide over under invalid]
@@ -2754,8 +2759,8 @@ def seterrcall(func):
{'over': 'log', 'divide': 'log', 'invalid': 'log', 'under': 'log'}
"""
- if func is not None and not isinstance(func, collections.Callable):
- if not hasattr(func, 'write') or not isinstance(func.write, collections.Callable):
+ if func is not None and not isinstance(func, collections_abc.Callable):
+ if not hasattr(func, 'write') or not isinstance(func.write, collections_abc.Callable):
raise ValueError("Only callable can be used as callback")
pyvals = umath.geterrobj()
old = geterrcall()
@@ -2811,6 +2816,8 @@ def geterrcall():
class _unspecified(object):
pass
+
+
_Unspecified = _unspecified()
@@ -2918,6 +2925,7 @@ def extend_all(module):
if a not in adict:
__all__.append(a)
+
from .umath import *
from .numerictypes import *
from . import fromnumeric