diff options
author | Stephan Hoyer <shoyer@google.com> | 2019-05-13 08:09:26 -0700 |
---|---|---|
committer | Stephan Hoyer <shoyer@google.com> | 2019-05-13 08:09:26 -0700 |
commit | 07452c86c78a1cf213a764d17f060b4887ec3681 (patch) | |
tree | 12c7ae7b14552bd28334277e7e244b603ca4300a /numpy/core/numeric.py | |
parent | 804f71bb42fef775a85a52366dc4bd1a22c130a8 (diff) | |
parent | 4ad33d21b1a30f931e23307e9f9355b70f633bed (diff) | |
download | numpy-07452c86c78a1cf213a764d17f060b4887ec3681.tar.gz |
Merge branch 'master' into implement-numpy-implementation
Diffstat (limited to 'numpy/core/numeric.py')
-rw-r--r-- | numpy/core/numeric.py | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 34705efc7..e72ab3012 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -90,12 +90,12 @@ class ComplexWarning(RuntimeWarning): pass -def _zeros_like_dispatcher(a, dtype=None, order=None, subok=None): +def _zeros_like_dispatcher(a, dtype=None, order=None, subok=None, shape=None): return (a,) @array_function_dispatch(_zeros_like_dispatcher) -def zeros_like(a, dtype=None, order='K', subok=True): +def zeros_like(a, dtype=None, order='K', subok=True, shape=None): """ Return an array of zeros with the same shape and type as a given array. @@ -119,6 +119,12 @@ def zeros_like(a, dtype=None, order='K', subok=True): If True, then the newly created array will use the sub-class type of 'a', otherwise it will be a base-class array. Defaults to True. + shape : int or sequence of ints, optional. + Overrides the shape of the result. If order='K' and the number of + dimensions is unchanged, will try to keep order, otherwise, + order='C' is implied. + + .. versionadded:: 1.17.0 Returns ------- @@ -150,7 +156,7 @@ def zeros_like(a, dtype=None, order='K', subok=True): array([0., 0., 0.]) """ - res = empty_like(a, dtype=dtype, order=order, subok=subok) + res = empty_like(a, dtype=dtype, order=order, subok=subok, shape=shape) # needed instead of a 0 to get same result as zeros for for string dtypes z = zeros(1, dtype=res.dtype) multiarray.copyto(res, z, casting='unsafe') @@ -210,12 +216,12 @@ def ones(shape, dtype=None, order='C'): return a -def _ones_like_dispatcher(a, dtype=None, order=None, subok=None): +def _ones_like_dispatcher(a, dtype=None, order=None, subok=None, shape=None): return (a,) @array_function_dispatch(_ones_like_dispatcher) -def ones_like(a, dtype=None, order='K', subok=True): +def ones_like(a, dtype=None, order='K', subok=True, shape=None): """ Return an array of ones with the same shape and type as a given array. @@ -239,6 +245,12 @@ def ones_like(a, dtype=None, order='K', subok=True): If True, then the newly created array will use the sub-class type of 'a', otherwise it will be a base-class array. Defaults to True. + shape : int or sequence of ints, optional. + Overrides the shape of the result. If order='K' and the number of + dimensions is unchanged, will try to keep order, otherwise, + order='C' is implied. + + .. versionadded:: 1.17.0 Returns ------- @@ -270,7 +282,7 @@ def ones_like(a, dtype=None, order='K', subok=True): array([1., 1., 1.]) """ - res = empty_like(a, dtype=dtype, order=order, subok=subok) + res = empty_like(a, dtype=dtype, order=order, subok=subok, shape=shape) multiarray.copyto(res, 1, casting='unsafe') return res @@ -322,12 +334,12 @@ def full(shape, fill_value, dtype=None, order='C'): return a -def _full_like_dispatcher(a, fill_value, dtype=None, order=None, subok=None): +def _full_like_dispatcher(a, fill_value, dtype=None, order=None, subok=None, shape=None): return (a,) @array_function_dispatch(_full_like_dispatcher) -def full_like(a, fill_value, dtype=None, order='K', subok=True): +def full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None): """ Return a full array with the same shape and type as a given array. @@ -349,6 +361,12 @@ def full_like(a, fill_value, dtype=None, order='K', subok=True): If True, then the newly created array will use the sub-class type of 'a', otherwise it will be a base-class array. Defaults to True. + shape : int or sequence of ints, optional. + Overrides the shape of the result. If order='K' and the number of + dimensions is unchanged, will try to keep order, otherwise, + order='C' is implied. + + .. versionadded:: 1.17.0 Returns ------- @@ -379,7 +397,7 @@ def full_like(a, fill_value, dtype=None, order='K', subok=True): array([0.1, 0.1, 0.1, 0.1, 0.1, 0.1]) """ - res = empty_like(a, dtype=dtype, order=order, subok=subok) + res = empty_like(a, dtype=dtype, order=order, subok=subok, shape=shape) multiarray.copyto(res, fill_value, casting='unsafe') return res @@ -1984,7 +2002,8 @@ def load(file): "np.core.numeric.load is deprecated, use pickle.load instead", DeprecationWarning, stacklevel=2) if isinstance(file, type("")): - file = open(file, "rb") + with open(file, "rb") as file_pointer: + return pickle.load(file_pointer) return pickle.load(file) |