vector_norm(x: Tensor, ord: int | float = 2, dim: int | list[int] | tuple[int, ...] | None = None, keepdim: bool = False, dtype: object = None)Compute a vector -norm along an axis.
Reduces along dim (or all elements if dim is None) using
for any positive real . Special-cased values:
- — count of non-zero entries (not a true norm).
- — .
- — Euclidean norm.
- — .
- — .
Parameters
ordint or float= 22.dimint, list of int, tuple of int or None= NoneNone 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= FalseTrue, reduced dimensions are retained with size 1.dtypedtype= Nonex 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 x and real
otherwise, and must not narrow x. None (default) reduces
in x's own dtype.Returns
TensorNorm along the specified axes — real, also for a complex x.
Raises
TypeErrorx is an integer or bool tensor (a DtypeMismatch), or if
dtype would narrow it or its kind (real / complex) differs.ValueErrordim names one axis twice.Notes
All operations are routed through autograd-aware engine kernels, so
gradients flow naturally even for non-integer (via
-power and root). Where the norm is zero the gradient is
zero — the subgradient the reference picks — rather than the nan
the root's infinite slope there would give; so is a zero entry's for
.
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 x is reduced through its magnitudes , 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.)