Skip to content
fn

matrix_norm

→Tensor
matrix_norm(input: Tensor, ord: int | float | str = 'fro', dim: tuple[int, int] = (-2, -1), keepdim: bool = False, dtype: object = None)

Compute a matrix norm.

Reduces the trailing two axes of an input to a scalar matrix norm. Supported orders:

  • "fro" — Frobenius norm ∥A∥F=(∑ij∣Aij∣2)1/2\|A\|_F = \big(\sum_{ij} |A_{ij}|^2\big)^{1/2}.
  • "nuc" — nuclear norm ∥A∥∗=∑iσi(A)\|A\|_* = \sum_i \sigma_i(A) (sum of singular values).
  • 1 / -1 — max / min absolute column sum.
  • inf / -inf — max / min absolute row sum.
  • 2 / -2 — largest / smallest singular value (spectral norm and its reciprocal).

Parameters

inputTensor
Input of shape (*, m, n).
ord(int, float or str)= 'fro'
Norm order. Default "fro".
dimtuple of two ints= (-2, -1)
Axis pair identifying the matrix dimensions. Default (-2, -1).
keepdimbool= False
If True, reduced dims are retained with size 1.
dtype(dtype, keyword - only)= None
Cast input to this dtype before computing the norm; must match the real/complex kind of input and must not narrow it. None (default) uses input's own dtype.

Returns

Tensor

Matrix norm of each batch — real, also for a complex input.

Raises

DtypeMismatch
If input is an integer or bool tensor (a TypeError).
ValueError
If input has fewer than two dimensions, dim does not name two distinct axes, or ord is not one of the orders above.
NotImplementedError
If ord is "nuc", 2 or -2 and input is complex: those orders are read off the singular values, and svd has no complex kernel yet.

Notes

Spectral and nuclear norms require an SVD and so cost O(min⁡(m,n)2max⁡(m,n))O(\min(m,n)^2 \max(m,n)). Entry-wise norms reduce in a single pass.

The entry-wise orders ("fro", ±1\pm 1, ±∞\pm\infty) are taken through vector_norm, so a complex input is reduced through its magnitudes ∣Aij∣|A_{ij}| exactly as a vector would be.

Examples

>>> import lucid
>>> from lucid.linalg import matrix_norm
>>> A = lucid.tensor([[3.0, 4.0], [0.0, 0.0]])
>>> matrix_norm(A, ord="fro")
tensor(5.)
>>> matrix_norm(lucid.tensor([[3 + 4j, 0j], [0j, 0j]]), ord="fro")
tensor(5.)