Skip to content
fn

vector_norm

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

Compute a vector pp-norm along an axis.

Reduces along dim (or all elements if dim is None) using

∥x∥p = (∑i∣xi∣ p)1/p\|x\|_p \,=\, \Big(\sum_i |x_i|^{\,p}\Big)^{1/p}

for any positive real pp. Special-cased values:

  • p=0p = 0 — count of non-zero entries (not a true norm).
  • p=1p = 1 — ∑i∣xi∣\sum_i |x_i|.
  • p=2p = 2 — Euclidean norm.
  • p=+∞p = +\infty — max⁡i∣xi∣\max_i |x_i|.
  • p=−∞p = -\infty — min⁡i∣xi∣\min_i |x_i|.

Parameters

inputTensor
Input tensor.
ordint or float= 2
Order of the norm. Default 2.
dimint, list of int, tuple of int or None= None
Axis or axes to reduce. None reduces over every axis — the rank is retained, so keepdim=True yields one size-1 axis per input axis rather than a single one.
keepdimbool= False
If True, reduced dimensions are retained with size 1.
dtype(dtype, keyword - only)= None
Cast input to this dtype before reducing, so the whole computation — and the result — is in it; a complex dtype gives its real counterpart. It must be complex for a complex input and real otherwise, and must not narrow input. None (default) reduces in input's own dtype.

Returns

Tensor

Norm along the specified axes — real, also for a complex input.

Raises

TypeError
If input is an integer or bool tensor (a DtypeMismatch), or if dtype would narrow it or its kind (real / complex) differs.
ValueError
If dim names one axis twice.

Notes

Gradients flow for non-integer pp too. A slice whose pp-th powers leave the float range keeps its norm and its derivatives (float32 near 1e-20 gives 2e-20): they are taken relative to its largest entry. A zero norm has a zero gradient — the subgradient the reference picks — rather than the nan there would give; so is a zero entry's for p<1p < 1.

A float16 or bfloat16 input is reduced in float32 and the result cast back, so a norm that fits the input dtype is not lost to an intermediate sum that does not: the 2-norm of 65 536 half-precision ones is 256, not inf.

A complex input is reduced through its magnitudes ∣xi∣|x_i|, as the definition says. Squaring the entries themselves gave a complex "norm" for ord=2 on Metal and an error on the CPU.

Examples

>>> import lucid
>>> from lucid.linalg import vector_norm
>>> vector_norm(lucid.tensor([3.0, 4.0]))
tensor(5.)
>>> vector_norm(lucid.tensor([1.0, -2.0, 3.0]), ord=1)
tensor(6.)
>>> vector_norm(lucid.tensor([3.0, 4.0]), dtype=lucid.float64)
tensor(5., dtype=lucid.float64)
>>> vector_norm(lucid.tensor([3 + 4j, 0j]))
tensor(5.)

Used by 3