fn

norm

Tensor
norm(x: Tensor, ord: int | float | str | None = None, dim: int | list[int] | None = None, keepdim: bool = False)
source

Compute a vector or matrix norm.

Generic norm dispatcher that delegates to vector_norm or matrix_norm based on the input rank and reduction axes. The default behaviour computes the Frobenius norm of a matrix input and the Euclidean (2\ell_2) norm of a vector input:

x2=(ixi2)1/2,AF=(i,jAij2)1/2.\|x\|_2 \,=\, \Big(\sum_i |x_i|^2\Big)^{1/2}, \qquad \|A\|_F \,=\, \Big(\sum_{i,j} |A_{ij}|^2\Big)^{1/2}.

Parameters

xTensor
Input tensor. May be a vector (n,), a matrix (m, n), or higher-rank with explicit dim.
ord(int, float, str or None)= None
Order of the norm. Vector orders: 0, 1, 2 (default when None), inf, -inf, or any positive real. Matrix orders: "fro", "nuc", 1, -1, 2, -2, inf, -inf.
dimint, list of int or None= None
Reduction axis (or pair of axes for matrix norms). None reduces over all elements.
keepdimbool= False
If True, retains reduced dimensions with size 1.

Returns

Tensor

Norm value(s); shape depends on dim / keepdim.

Notes

Many norm orders (spectral, nuclear) require an SVD and therefore cost O(min(m,n)mn)O(\min(m,n) \cdot mn); entry-wise norms reduce in a single pass.

Examples

>>> import lucid
>>> from lucid.linalg import norm
>>> norm(lucid.tensor([3.0, 4.0]))
Tensor(5.0)