clip_matrix_norm#

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

Clips (limits) the matrix 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 matrix norm downscaled to the max norm if needed.

Examples

With ivy.Array input:

>>> x = ivy.array([[0., 1., 2.]])
>>> y = ivy.clip_matrix_norm(x, 2.0)
>>> print(y)
ivy.array([[0.   , 0.894, 1.79 ]])
>>> x = ivy.array([[0.1, -1.2, 3.7], [0., 7.3, -0.5]])
>>> y = ivy.clip_matrix_norm(x, 3.0, p=1.0)
>>> print(y)
ivy.array([[ 0.0353, -0.424 ,  1.31  ],
           [ 0.    ,  2.58  , -0.176 ]])
>>> x = ivy.array([[[5., 4.], [-2., 6.]],
...                [[3., 7.], [0., -5.]]])
>>> y = ivy.empty((2, 2, 2))
>>> y = ivy.clip_matrix_norm(x, 0.5, p=2.0)
>>> print(y)
ivy.array([[[ 0.339,  0.271],
            [-0.135,  0.406]],
           [[ 0.168,  0.391],
            [ 0.   , -0.279]]])
>>> x = ivy.array([[0., 1.],
...                [2., 3.]])
>>> ivy.clip_matrix_norm(x, 5.0, p=1.0, out=x)
>>> print(x)
ivy.array([[0., 1.],
           [2., 3.]])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([[0., 1., 2.]]),
...                   b=ivy.array([[3., 4., 5.]]))
>>> y = ivy.clip_matrix_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_matrix_norm(self, max_norm, /, *, p=2.0, out=None)[source]#

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

Parameters:
  • self (Array) – input array

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

Examples

With ivy.Array instance method:

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

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

Parameters:
  • self (Container) – Input array containing elements to clip.

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

  • p (Union[float, Container], default: 2.0) – 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 matrix 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_matrix_norm(2.0, p=1.0)
>>> print(y)
{
    a: ivy.array([[0., 1., 2.]]),
    b: ivy.array([[1.2, 1.6, 2.]])
}