norm#
- class saiunit.linalg.norm(x, ord=None, axis=None, keepdims=False, **kwargs)#
Compute the norm of a matrix or vector.
Computes a variety of vector and matrix norms, preserving the physical unit of the input.
- Parameters:
x (
Array|ndarray|bool|number|bool|int|float|complex| saiunit.Quantity) – N-dimensional input for which the norm will be computed.ord (
int|str|None) – Order of the norm. Default is Frobenius norm for matrices and the 2-norm for vectors. See Notes for details.axis (
None|tuple[int,...] |int) – Axes over which the norm is computed. Defaults to all axes.keepdims (
bool) – IfTrue, reduced axes are kept as size-1 dimensions (default:False).
- Returns:
out – Norm of x. Carries the same unit as x.
- Return type:
Array| saiunit.Quantity
Notes
The flavor of norm computed depends on the value of ord and the number of axes being reduced.
For vector norms (single-axis reduction):
ord=None(default) – 2-normord=inf–max(abs(x))ord=-inf–min(abs(x))ord=0–sum(x != 0)other numeric –
sum(abs(x)**ord)**(1/ord)
For matrix norms (two-axis reduction):
ord='fro'orNone(default) – Frobenius normord='nuc'– nuclear norm (sum of singular values)ord=1–max(abs(x).sum(axis=0))ord=-1–min(abs(x).sum(axis=0))ord=2– largest singular valueord=-2– smallest singular value
Examples
>>> import saiunit as u >>> import jax.numpy as jnp Vector 2-norm: >>> x = jnp.array([3., 4., 12.]) * u.meter >>> u.linalg.norm(x) 13. * meter L1 vector norm: >>> u.linalg.norm(x, ord=1) 19. * meter Frobenius matrix norm: >>> m = jnp.array([[1., 2., 3.], ... [4., 5., 7.]]) * u.meter >>> u.linalg.norm(m) 10.198039 * meter