matrix_norm(x: Tensor, ord: int | float | str = 'fro', dim: tuple[int, int] = (-2, -1), keepdim: bool = False)Compute a matrix norm.
Reduces the trailing two axes of an input to a scalar matrix norm. Supported orders:
"fro"— Frobenius norm ."nuc"— nuclear norm (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
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= FalseIf
True, reduced dims are retained with size 1.Returns
TensorMatrix norm of each batch — real, also for a complex x.
Raises
DtypeMismatchIf
x is an integer or bool tensor (a TypeError).ValueErrorIf
x has fewer than two dimensions, dim does not name two
distinct axes, or ord is not one of the orders above.NotImplementedErrorIf
ord is "nuc", 2 or -2 and x 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 . Entry-wise norms reduce in a single pass.
The entry-wise orders ("fro", , )
are taken through vector_norm, so a complex x is reduced
through its magnitudes 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.)