xp_vector_norm

xp_vector_norm(x: Any, /, *, axis: int | tuple[int] | None = None, keepdims: bool = False, ord: int | float = 2, xp: ModuleType | None = None) Any[source][source]

Compute the vector norm of an array.

Parameters:
  • x (Array) – The input array.

  • axis (int or tuple of ints, optional) – The axis or axes along which to compute the norm. If None, the norm is computed over all elements in the array.

  • keepdims (bool, optional) –

    If True, the axes which are reduced are left in the result as
    dimensions with size one. With this option, the result will broadcast

    correctly against the original array.

  • ord (int or float, optional) – The order of the norm. Default is 2 (Euclidean norm).

  • xp (module, optional) –

    The array API namespace to use. If not provided, the namespace is

    inferred from the array.

Returns:

The vector norm of the input array.

Return type:

Array

Notes

This function attempts to use the linalg.vector_norm function from the
array API if available. If not, it falls back to a simple implementation
for the Euclidean norm (ord=2). For backends not implementing the

linalg extension, only the Euclidean norm is supported.

Examples

>>> import numpy as np
>>> x = np.array([3.0, 4.0])
>>> float(xp_vector_norm(x, xp=np))  # Euclidean norm (default)
5.0
>>> float(xp_vector_norm(x, ord=1, xp=np))  # L1 norm
7.0
>>> x = np.array([[1, 2], [3, 4]])
>>> xp_vector_norm(x, axis=0, xp=np)  # Norm along first axis
array([3.16227766, 4.47213595])
>>> xp_vector_norm(x, axis=1, xp=np)  # Norm along second axis
array([2.23606798, 5.        ])