clip¶
- clip(x: array, /, min: int | float | array | None = None, max: int | float | array | None = None) array¶
Clamps each element
x_iof the input arrayxto the range[min, max].- Parameters:
x (array) – input array. Should have a real-valued data type.
min (Optional[Union[int, float, array]]) – lower-bound of the range to which to clamp. If
None, no lower bound must be applied. Must be compatible withxandmax(see Broadcasting). Should have the same data type asx. Default:None.max (Optional[Union[int, float, array]]) – upper-bound of the range to which to clamp. If
None, no upper bound must be applied. Must be compatible withxandmin(see Broadcasting). Should have the same data type asx. Default:None.
- Returns:
out (array) – an array containing element-wise results. The returned array should have the same data type as
x.
Notes
This function is conceptually equivalent to
maximum(minimum(x, max), min)whenx,min, andmaxhave the same data type.If both
minandmaxareNone, the elements of the returned array must equal the respective elements inx.If a broadcasted element in
minis greater than a corresponding broadcasted element inmax, behavior is unspecified and thus implementation-dependent.For scalar
minand/ormax, the scalar values should follow type promotion rules for operations involving arrays and scalar operands (see Type Promotion Rules).If
xhas an integral data type and a broadcasted element inminormaxis outside the bounds of the data type ofx, behavior is unspecified and thus implementation-dependent.If either
minormaxis an array having a different data type thanx, behavior is unspecified and thus implementation-dependent.
Special cases
If
x_iisNaN, the result isNaN.If
min_iisNaN, the result isNaN.If
max_iisNaN, the result isNaN.
New in version 2023.12.
Changed in version 2024.12: Added special case behavior when one of the operands is
NaN.Changed in version 2024.12: Clarified that behavior is only defined when
x,min, andmaxresolve to arrays having the same data type.Changed in version 2024.12: Clarified that behavior is only defined when elements of
minandmaxare inside the bounds of the input array data type.