diff options
Diffstat (limited to 'numpy/core/numeric.py')
-rw-r--r-- | numpy/core/numeric.py | 108 |
1 files changed, 15 insertions, 93 deletions
diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index abfa8fd24..f33a2b664 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -40,9 +40,7 @@ __all__ = ['newaxis', 'ndarray', 'flatiter', 'nditer', 'nested_iters', 'ufunc', 'Inf', 'inf', 'infty', 'Infinity', 'nan', 'NaN', 'False_', 'True_', 'bitwise_not', 'CLIP', 'RAISE', 'WRAP', 'MAXDIMS', 'BUFSIZE', 'ALLOW_THREADS', - 'ComplexWarning', 'may_share_memory', - 'nans', 'nans_like', 'infs', 'infs_like'] - + 'ComplexWarning', 'may_share_memory', 'filled', 'filled_like'] if sys.version_info[0] < 3: __all__.extend(['getbuffer', 'newbuffer']) @@ -231,41 +229,38 @@ def _check_dtype_nan(dtype): 'can represent non-numbers as defined in ' 'IEEE 754-1985.') -def nans(shape, dtype=None, order='C'): +def filled(shape, val, dtype=None, order='C'): """ - Return a new array of given shape and type, filled with nans. - - Note that only floating point numbers can represent non-numbers as defined - in IEEE 754-1985. + Return a new array of given shape and type, filled with `val`. Please refer to the documentation for `zeros` for further details. + Other parameters + ---------------- + val : scalar + Fill value. + 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. - nans_like : Return an array of nans with shape and type of input. - infs_like : Return an array of infs with shape and type of input. + filled_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. empty : Return a new uninitialized array. - infs : Return a new array setting values to inf. """ _check_dtype_nan(dtype) a = empty(shape, dtype, order) - multiarray.copyto(a, nan, casting='unsafe') + multiarray.copyto(a, val, casting='unsafe') return a -def nans_like(a, dtype=None, order='K', subok=True): +def filled_like(a, val, dtype=None, order='K', subok=True): """ Return an array of nans with the same shape and type as a given array. - Note that only floating point numbers can represent non-numbers as defined - in IEEE 754-1985. - With default parameters, is equivalent to ``a.copy().fill(np.nan)``. Parameters @@ -273,6 +268,8 @@ def nans_like(a, dtype=None, order='K', subok=True): a : array_like The shape and data-type of `a` define these same attributes of the returned array. + val : scalar + Fill value. dtype : data-type, optional Overrides the data type of the result. order : {'C', 'F', 'A', or 'K'}, optional @@ -291,91 +288,16 @@ def nans_like(a, dtype=None, order='K', subok=True): 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. - infs_like : Return an array of infs 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. - nans : Return a new array setting values to nan. - infs : Return a new array setting values to inf. - - """ - - _check_dtype_nan(dtype) - res = empty_like(a, dtype=dtype, order=order, subok=subok) - multiarray.copyto(res, nan, casting='unsafe') - return res - -def infs(shape, dtype=None, order='C'): - """ - Return a new array of given shape and type, filled with infs. - - Note that only floating point numbers can represent non-numbers as defined - in IEEE 754-1985. - - Please refer to the documentation for `zeros` for further details. - - 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. - nans_like : Return an array of nans with shape and type of input. - infs_like : Return an array of infs 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. - nans : Return a new array setting values to nan. - - """ - - _check_dtype_nan(dtype) - a = empty(shape, dtype, order) - multiarray.copyto(a, inf, casting='unsafe') - return a - -def infs_like(a, dtype=None, order='K', subok=True): - """ - Return an array of infs with the same shape and type as a given array. - - Note that only floating point numbers can represent non-numbers as defined - in IEEE 754-1985. - - With default parameters, is equivalent to ``a.copy().fill(np.inf)``. - - Parameters - ---------- - a : array_like - The shape and data-type of `a` define these same attributes of - the returned array. - dtype : data-type, optional - Overrides the data type of the result. - order : {'C', 'F', 'A', or 'K'}, optional - Overrides the memory layout of the result. 'C' means C-order, - 'F' means F-order, 'A' means 'F' if `a` is Fortran contiguous, - 'C' otherwise. 'K' means match the layout of `a` as closely - as possible. - - Returns - ------- - out : ndarray - Array of infs with the same shape and type as `a`. - - 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. - nans : Return a new array setting values to nan. - infs : Return a new array setting values to infs. + filled : Fill a new array. """ _check_dtype_nan(dtype) res = empty_like(a, dtype=dtype, order=order, subok=subok) - multiarray.copyto(res, inf, casting='unsafe') + multiarray.copyto(res, val, casting='unsafe') return res |