Skip to content
fn

norm

→Tensor
norm(input: Tensor, ord: int | float | str | None = None, dim: int | list[int] | tuple[int, ...] | None = None, keepdim: bool = False, dtype: object = None)

Implementing kernel

C++ engine symbols that back this Python API.

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:

∥x∥2 = (∑i∣xi∣2)1/2,∥A∥F = (∑i,j∣Aij∣2)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

inputTensor
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, tuple 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.
dtype(dtype, keyword - only)= None
If given, input is cast to dtype before computing the norm.

Returns

Tensor

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

Raises

DtypeMismatch
If input is an integer or bool tensor (a TypeError).
ValueError
If ord is given without dim and the input is neither a vector nor a single matrix — a batch of matrices leaves it ambiguous whether one norm or one per matrix was meant. Also if dim names more than two axes, or if a matrix order is asked of a vector.
NotImplementedError
If a complex input is asked for a singular-value order ("nuc" or a matrix ord of ±2\pm 2), which matrix_norm cannot take yet.

Notes

Which of the two norms runs is decided as follows:

============================ ========================================== arguments reduction ============================ ========================================== dim=None, ord=None flatten, Euclidean norm, any rank dim=None, ord given vector norm if 1-D, matrix norm if 2-D dim names one axis vector norm along it dim names two axes matrix norm over that plane ord is "fro"/"nuc" matrix norm regardless ============================ ==========================================

So ord means different things at different ranks, and the same order names two different quantities: ord=2 is the Euclidean norm of a vector but the spectral norm — the largest singular value — of a matrix, which is not its Frobenius norm. Pass an explicit dim, or call vector_norm or matrix_norm directly, wherever that distinction matters.

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.

Overflow is not a failure mode here: the input is rescaled by its largest magnitude before reducing, so a norm whose value is representable is computed even when its squares are not.

A complex input is reduced through its magnitudes ∣xi∣|x_i| by whichever of the two norms runs, and the result is real.

Examples

>>> import lucid
>>> from lucid.linalg import norm
>>> norm(lucid.tensor([3.0, 4.0]))
tensor(5.)
>>> A = lucid.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
>>> norm(A, ord=1)          # max absolute column sum
tensor(9.)
>>> norm(A, ord=1, dim=1)   # per-row vector 1-norm
tensor([6., 15.])