clip#
- ivy.clip(x, /, x_min=None, x_max=None, *, out=None)[source]#
Clips (limits) the values in an array.
Given an interval, values outside the interval are clipped to the interval edges (element-wise). For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1. Minimum value needs to smaller or equal to maximum value to return correct results.
- Parameters:
x (
Union[Array,NativeArray]) – Input array containing elements to clip.x_min (
Optional[Union[Number,Array,NativeArray]], default:None) – Minimum value.x_max (
Optional[Union[Number,Array,NativeArray]], default:None) – Maximum value.out (
Optional[Array], default:None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
- Returns:
ret – An array with the elements of x, but where values < x_min are replaced with x_min, and those > x_max with x_max.
Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts
ivy.Containerinstances in place of any of the arguments.Examples
With
ivy.Arrayinput:>>> x = ivy.array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]) >>> y = ivy.clip(x, 1., 5.) >>> print(y) ivy.array([1., 1., 2., 3., 4., 5., 5., 5., 5., 5.])
>>> x = ivy.array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]) >>> y = ivy.zeros_like(x) >>> ivy.clip(x, 2., 7., out=y) >>> print(y) ivy.array([2., 2., 2., 3., 4., 5., 6., 7., 7., 7.])
>>> x = ivy.array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]) >>> x_min = ivy.array([3., 3., 1., 0., 2., 3., 4., 0., 4., 4.]) >>> x_max = ivy.array([5., 4., 3., 3., 5., 7., 8., 3., 8., 8.]) >>> y = ivy.clip(x, x_min, x_max) >>> print(y) ivy.array([3., 3., 2., 3., 4., 5., 6., 3., 8., 8.])
With
ivy.NativeArrayinput:>>> x = ivy.native_array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]) >>> x_min = ivy.native_array([3., 3., 1., 0., 2., 3., 4., 2., 4., 4.]) >>> x_max = ivy.native_array([5., 4., 3., 3., 5., 7., 8., 3., 8., 8.]) >>> y = ivy.clip(x, x_min, x_max) >>> print(y) ivy.array([3., 3., 2., 3., 4., 5., 6., 3., 8., 8.])
With a mix of
ivy.Arrayandivy.NativeArrayinputs:>>> x = ivy.array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]) >>> x_min = ivy.native_array([3., 3., 1., 0., 2., 3., 4., 2., 4., 4.]) >>> x_max = ivy.native_array([5., 4., 3., 3., 5., 7., 8., 3., 8., 8.]) >>> y = ivy.clip(x, x_min, x_max) >>> print(y) ivy.array([3., 3., 2., 3., 4., 5., 6., 3., 8., 8.])
With
ivy.Containerinput:>>> x = ivy.Container(a=ivy.array([0., 1., 2.]), ... b=ivy.array([3., 4., 5.])) >>> y = ivy.clip(x, 1., 5.) >>> print(y) { a: ivy.array([1., 1., 2.]), b: ivy.array([3., 4., 5.]) }
With multiple
ivy.Containerinputs:>>> x = ivy.Container(a=ivy.array([0., 1., 2.]), ... b=ivy.array([3., 4., 5.])) >>> x_min = ivy.Container(a=0, b=-3) >>> x_max = ivy.Container(a=1, b=-1) >>> y = ivy.clip(x, x_min,x_max) >>> print(y) { a: ivy.array([0., 1., 1.]), b: ivy.array([-1., -1., -1.]) }
With a mix of
ivy.Arrayandivy.Containerinputs:>>> x = ivy.array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]) >>> x_min = ivy.array([3., 0., 1]) >>> x_max = ivy.array([5., 4., 3.]) >>> y = ivy.Container(a=ivy.array([0., 1., 2.]), ... b=ivy.array([3., 4., 5.])) >>> z = ivy.clip(y, x_min, x_max) >>> print(z) { a: ivy.array([3., 1., 2.]), b: ivy.array([3., 4., 3.]) }
- Array.clip(self, /, x_min=None, x_max=None, *, out=None)[source]#
ivy.Array instance method variant of ivy.clip. This method simply wraps the function, and so the docstring for ivy.clip also applies to this method with minimal changes.
- Parameters:
self (
Array) – Input array containing elements to clip.x_min (
Optional[Union[Number,Array,NativeArray]], default:None) – Minimum value.x_max (
Optional[Union[Number,Array,NativeArray]], default:None) – Maximum value.out (
Optional[Array], default:None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array- Returns:
ret – An array with the elements of self, but where values < x_min are replaced with x_min, and those > x_max with x_max.
Examples
>>> x = ivy.array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]) >>> y = x.clip(1., 5.) >>> print(y) ivy.array([1., 1., 2., 3., 4., 5., 5., 5., 5., 5.])
- Container.clip(self, /, x_min=None, x_max=None, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#
ivy.Container instance method variant of ivy.clip. This method simply wraps the function, and so the docstring for ivy.clip also applies to this method with minimal changes.
- Parameters:
self (
Container) – Input container containing elements to clip.x_min (
Optional[Union[Number,Array,NativeArray,Container]], default:None) – Minimum value.x_max (
Optional[Union[Number,Array,NativeArray,Container]], default:None) – Maximum value.key_chains (
Optional[Union[List[str],Dict[str,str],Container]], default:None) – The key-chains to apply or not apply the method to. Default isNone.to_apply (
Union[bool,Container], default:True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default isTrue.prune_unapplied (
Union[bool,Container], default:False) – Whether to prune key_chains for which the function was not applied. Default isFalse.map_sequences (
Union[bool,Container], default:False) – Whether to also map method to sequences (lists, tuples). Default isFalse.out (
Optional[Container], default:None) – optional output container, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Container- Returns:
ret – A container with the elements of x, but where values < x_min are replaced with x_min, and those > x_max with x_max.
Examples
>>> x = ivy.Container(a=ivy.array([0., 1., 2.]), b=ivy.array([3., 4., 5.])) >>> y = x.clip(1,2) >>> print(y) { a: ivy.array([1., 1., 2.]), b: ivy.array([2., 2., 2.]) }