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 ."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
inputTensorInput 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.dtype(dtype, keyword - only)= NoneCast
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
TensorMatrix norm of each batch — real, also for a complex input.
Raises
DtypeMismatchIf
input is an integer or bool tensor (a TypeError).ValueErrorIf
input 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 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 . Entry-wise norms reduce in a single pass.
The entry-wise orders ("fro", , )
are taken through vector_norm, so a complex input 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.)