clip¶
- clip(x: array, /, min: int | float | array | None = None, max: int | float | array | None = None) array ¶
Clamps each element
x_i
of the input arrayx
to 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 withx
andmax
(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 withx
andmin
(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
, andmax
have the same data type.If both
min
andmax
areNone
, the elements of the returned array must equal the respective elements inx
.If a broadcasted element in
min
is greater than a corresponding broadcasted element inmax
, behavior is unspecified and thus implementation-dependent.For scalar
min
and/ormax
, the scalar values should follow type promotion rules for operations involving arrays and scalar operands (see Type Promotion Rules).If
x
has an integral data type and a broadcasted element inmin
ormax
is outside the bounds of the data type ofx
, behavior is unspecified and thus implementation-dependent.If either
min
ormax
is an array having a different data type thanx
, behavior is unspecified and thus implementation-dependent.
Special cases
If
x_i
isNaN
, the result isNaN
.If
min_i
isNaN
, the result isNaN
.If
max_i
isNaN
, 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
, andmax
resolve to arrays having the same data type.Changed in version 2024.12: Clarified that behavior is only defined when elements of
min
andmax
are inside the bounds of the input array data type.