clip_vector_norm#

ivy.clip_vector_norm(x, max_norm, /, *, p=2.0, out=None)[source]#

Clips (limits) the vector p-norm of an array.

Parameters:
  • x (Union[Array, NativeArray]) – Input array containing elements to clip.

  • max_norm (float) – The maximum value of the array norm.

  • p (float, default: 2.0) – The p-value for computing the p-norm. Default is 2.

  • 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 vector norm downscaled to the max norm if needed.

Examples

With ivy.Array input:

>>> x = ivy.array([0., 1., 2.])
>>> y = ivy.clip_vector_norm(x, 2.0)
>>> print(y)
ivy.array([0.        , 0.89442718, 1.78885436])
>>> x = ivy.array([0.5, -0.7, 2.4])
>>> y = ivy.clip_vector_norm(x, 3.0, p=1.0)
>>> print(y)
ivy.array([ 0.41666666, -0.58333331,  2.        ])
>>> x = ivy.array([[[0., 0.], [1., 3.], [2., 6.]],
...                [[3., 9.], [4., 12.], [5., 15.]]])
>>> y = ivy.zeros(((2, 3, 2)))
>>> ivy.clip_vector_norm(x, 4.0, p=1.0, out=y)
>>> print(y)
ivy.array([[[0.        , 0.        ],
    [0.06666667, 0.20000002],
    [0.13333334, 0.40000004]],
[[0.20000002, 0.60000002],

[0.26666668, 0.80000007], [0.33333334, 1. ]]]))

>>> x = ivy.array([[1.1, 2.2, 3.3],
...                [-4.4, -5.5, -6.6]])
>>> ivy.clip_vector_norm(x, 1.0, p=3.0, out=x)
>>> print(x)
ivy.array([[ 0.13137734,  0.26275468,  0.39413199],
   [-0.52550936, -0.6568867 , -0.78826398]])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([0., 1., 2.]),
...                   b=ivy.array([3., 4., 5.]))
>>> y = ivy.clip_vector_norm(x, 2.0)
>>> print(y)
{
    a: ivy.array([0., 0.89442718, 1.78885436]),
    b: ivy.array([0.84852815, 1.1313709, 1.41421366])
}

With multiple ivy.Container inputs:

>>> x = ivy.Container(a=ivy.array([0., 1., 2.]),
...                   b=ivy.array([3., 4., 5.]))
>>> max_norm = ivy.Container(a=2, b=3)
>>> y = ivy.clip_vector_norm(x, max_norm)
>>> print(y)
{
    a: ivy.array([0., 0.89442718, 1.78885436]),
    b: ivy.array([1.27279221, 1.69705628, 2.12132034])
}
Array.clip_vector_norm(self, max_norm, /, *, p=2.0, out=None)[source]#

ivy.Array instance method variant of ivy.clip_vector_norm. This method simply wraps the function, and so the docstring for ivy.clip_vector_norm also applies to this method with minimal changes.

Parameters:
  • self (Array) – input array

  • max_norm (float) – float, the maximum value of the array norm.

  • p (float, default: 2.0) – optional float, the p-value for computing the p-norm. Default is 2.

  • 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 vector norm downscaled to the max norm if needed.

Examples

With ivy.Array instance method:

>>> x = ivy.array([0., 1., 2.])
>>> y = x.clip_vector_norm(2.0)
>>> print(y)
ivy.array([0., 0.894, 1.79])
Container.clip_vector_norm(self, max_norm, /, *, p=2.0, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container instance method variant of ivy.clip_vector_norm. This method simply wraps the function, and so the docstring for ivy.clip_vector_norm also applies to this method with minimal changes.

Parameters:
  • self (Container) – input array

  • max_norm (Union[float, Container]) – float, the maximum value of the array norm.

  • p (Union[float, Container], default: 2.0) – optional float, the p-value for computing the p-norm. Default is 2.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

  • out (Optional[Container], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Container

Returns:

ret – An array with the vector norm downscaled to the max norm if needed.

Examples

With ivy.Container instance method:

>>> x = ivy.Container(a=ivy.array([0., 1., 2.]),
...                   b=ivy.array([3., 4., 5.]))
>>> y = x.clip_vector_norm(2.0, p=1.0)
>>> print(y)
{
    a: ivy.array([0., 0.667, 1.33]),
    b: ivy.array([0.5, 0.667, 0.833])
}