fn

matrix_norm

Tensor
matrix_norm(x: Tensor, ord: int | float | str = 'fro', dim: tuple[int, int] = (-2, -1), keepdim: bool = False)
source

Compute a matrix norm.

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

  • "fro" — Frobenius norm AF=(ijAij2)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

xTensor
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.

Returns

Tensor

Matrix norm of each batch.

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.

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.0)