norm

Contents

norm#

class brainunit.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) – If True, 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-norm

  • ord=infmax(abs(x))

  • ord=-infmin(abs(x))

  • ord=0sum(x != 0)

  • other numeric – sum(abs(x)**ord)**(1/ord)

For matrix norms (two-axis reduction):

  • ord='fro' or None (default) – Frobenius norm

  • ord='nuc' – nuclear norm (sum of singular values)

  • ord=1max(abs(x).sum(axis=0))

  • ord=-1min(abs(x).sum(axis=0))

  • ord=2 – largest singular value

  • ord=-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