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) – The p-value for computing the p-norm. (default: 2.0) Default is 2.

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None) 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.

Functional 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.894, 1.79 ])
>>> 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.417, -0.583,  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.0667, 0.2   ],
            [0.133 , 0.4   ]],
           [[0.2   , 0.6   ],
            [0.267 , 0.8   ],
            [0.333 , 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.131,  0.263,  0.394],
           [-0.526, -0.657, -0.788]])

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.894, 1.79]),
    b: ivy.array([0.849, 1.13, 1.41])
}
Array.clip_vector_norm(self, max_norm, /, *, p=2.0, out=None)#

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) – optional float, the p-value for computing the p-norm. (default: 2.0) Default is 2.

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None) 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)#

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 (float) – float, the maximum value of the array norm.

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

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

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

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

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

  • out (Optional[Container]) – optional output array, for writing the result to. It must (default: None) 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])
}