norm(x: Tensor, ord: int | float | str | None = None, dim: int | list[int] | tuple[int, ...] | None = None, keepdim: bool = False)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 () norm of a vector input:
Parameters
(n,), a matrix (m, n), or
higher-rank with explicit dim.ord(int, float, str or None)= None0, 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= NoneNone
reduces over all elements.keepdimbool= FalseTrue, retains reduced dimensions with size 1.Returns
TensorNorm value(s); shape depends on dim / keepdim.
Raises
ValueErrorord 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.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 ; 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.
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.])