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.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])
}

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.894, 1.79]),
    b: ivy.array([2.449, 2.65, 2.83])
}
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])
}