Thanks to visit codestin.com
Credit goes to github.com

Skip to content

WIP: MAINT: Made clip into an ufunc #7876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions numpy/core/code_generators/generate_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,12 @@ def english_upper(s):
TypeDescription('d', None, 'd', 'di'),
TypeDescription('g', None, 'g', 'gi'),
],
),
'clip' :
Ufunc(3, 1, None,
docstrings.get('numpy.core.umath.clip'),
'PyUFunc_ClipTypeResolver',
TD(noobj),
)
}

Expand Down Expand Up @@ -910,6 +916,17 @@ def indent(st, spaces):
'G': 'GG_G',
'O': 'OO_O',
'P': 'OO_O_method'}

chartotype3 = {'e': 'eee_e',
'f': 'fff_f',
'd': 'ddd_d',
'g': 'ggg_g',
'F': 'FFF_F',
'D': 'DDD_D',
'G': 'GGG_G',
'O': 'OOO_O',
'P': 'OOO_O_method'}

#for each name
# 1) create functions, data, and signature
# 2) fill in functions and data in InitOperators
Expand All @@ -930,9 +947,11 @@ def make_arrays(funcdict):
k = 0
sub = 0

if uf.nin > 1:
assert uf.nin == 2
thedict = chartotype2 # two inputs and one output
if uf.nin > 2:
assert uf.nin == 3
thedict = chartotype3 # three inputs and one output
elif uf.nin == 2:
thedict = chartotype2
else:
thedict = chartotype1 # one input and one output

Expand Down
51 changes: 51 additions & 0 deletions numpy/core/code_generators/ufunc_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3669,3 +3669,54 @@ def add_newdoc(place, name, doc):
array([ 0., 1., 2., 3., 4., 5.])

""")

add_newdoc('numpy.core.umath', 'clip',
"""
Clip (limit) the values in an array.

Given an interval, values outside the interval are clipped to
the interval edges. For example, if an interval of ``[0, 1]``
is specified, values smaller than 0 become 0, and values larger
than 1 become 1.

Parameters
----------
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 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
to hold the output. Its type is preserved.

Returns
-------
clipped_array : ndarray
An array with the elements of `a`, but where values
< `a_min` are replaced with `a_min`, and those > `a_max`
with `a_max`.

See Also
--------
numpy.doc.ufuncs : Section "Output arguments"

Examples
--------
>>> a = np.arange(10)
>>> np.clip(a, 1, 8)
array([1, 1, 2, 3, 4, 5, 6, 7, 8, 8])
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.clip(a, 3, 6, out=a)
array([3, 3, 3, 3, 4, 5, 6, 6, 6, 6])
>>> 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)
array([3, 4, 2, 3, 4, 5, 6, 7, 8, 8])

""")
59 changes: 1 addition & 58 deletions numpy/core/fromnumeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# functions that are methods
__all__ = [
'alen', 'all', 'alltrue', 'amax', 'amin', 'any', 'argmax',
'argmin', 'argpartition', 'argsort', 'around', 'choose', 'clip',
'argmin', 'argpartition', 'argsort', 'around', 'choose',
'compress', 'cumprod', 'cumproduct', 'cumsum', 'diagonal', 'mean',
'ndim', 'nonzero', 'partition', 'prod', 'product', 'ptp', 'put',
'rank', 'ravel', 'repeat', 'reshape', 'resize', 'round_',
Expand Down Expand Up @@ -1670,63 +1670,6 @@ def compress(condition, a, axis=None, out=None):
return _wrapfunc(a, 'compress', condition, axis=axis, out=out)


def clip(a, a_min, a_max, out=None):
"""
Clip (limit) the values in an array.

Given an interval, values outside the interval are clipped to
the interval edges. For example, if an interval of ``[0, 1]``
is specified, values smaller than 0 become 0, and values larger
than 1 become 1.

Parameters
----------
a : array_like
Array containing elements to clip.
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
to hold the output. Its type is preserved.

Returns
-------
clipped_array : ndarray
An array with the elements of `a`, but where values
< `a_min` are replaced with `a_min`, and those > `a_max`
with `a_max`.

See Also
--------
numpy.doc.ufuncs : Section "Output arguments"

Examples
--------
>>> a = np.arange(10)
>>> np.clip(a, 1, 8)
array([1, 1, 2, 3, 4, 5, 6, 7, 8, 8])
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.clip(a, 3, 6, out=a)
array([3, 3, 3, 3, 4, 5, 6, 6, 6, 6])
>>> 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)
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):
"""
Sum of array elements over a given axis.
Expand Down
Loading