On this page
torch.linalg.cond
torch.linalg.cond(A, p=None, *, out=None) → Tensor-
Computes the condition number of a matrix with respect to a matrix norm.
Letting be or , the condition number of a matrix is defined as
The condition number of
Ameasures the numerical stability of the linear systemAX = Bwith respect to a matrix norm.Supports input of float, double, cfloat and cdouble dtypes. Also supports batches of matrices, and if
Ais a batch of matrices then the output has the same batch dimensions.pdefines the matrix norm that is computed. The following norms are supported:pmatrix norm
None2-norm (largest singular value)‘fro’Frobenius norm
‘nuc’nuclear norm
infmax(sum(abs(x), dim=1))-infmin(sum(abs(x), dim=1))1max(sum(abs(x), dim=0))-1min(sum(abs(x), dim=0))2largest singular value
-2smallest singular value
where
infrefers tofloat(‘inf’), NumPy’sinfobject, or any equivalent object.For
pis one of(‘fro’, ‘nuc’, inf, -inf, 1, -1), this function usestorch.linalg.norm()andtorch.linalg.inv(). As such, in this case, the matrix (or every matrix in the batch)Ahas to be square and invertible.For
pin(2, -2), this function can be computed in terms of the singular valuesIn these cases, it is computed using
torch.linalg.svdvals(). For these norms, the matrix (or every matrix in the batch)Amay have any shape.Note
When inputs are on a CUDA device, this function synchronizes that device with the CPU if
pis one of(‘fro’, ‘nuc’, inf, -inf, 1, -1).See also
torch.linalg.solve()for a function that solves linear systems of square matrices.torch.linalg.lstsq()for a function that solves linear systems of general matrices.- Parameters
-
- A (Tensor) – tensor of shape
(*, m, n)where*is zero or more batch dimensions forpin(2, -2), and of shape(*, n, n)where every matrix is invertible forpin(‘fro’, ‘nuc’, inf, -inf, 1, -1). - p (int, inf, -inf, 'fro', 'nuc', optional) – the type of the matrix norm to use in the computations (see above). Default:
None
- A (Tensor) – tensor of shape
- Keyword Arguments
-
out (Tensor, optional) – output tensor. Ignored if
None. Default:None. - Returns
-
A real-valued tensor, even when
Ais complex. - Raises
-
RuntimeError – if
pis one of(‘fro’, ‘nuc’, inf, -inf, 1, -1)and theAmatrix or any matrix in the batchAis not square or invertible.
Examples:
>>> A = torch.randn(3, 4, 4, dtype=torch.complex64) >>> torch.linalg.cond(A) >>> A = torch.tensor([[1., 0, -1], [0, 1, 0], [1, 0, 1]]) >>> torch.linalg.cond(A) tensor([1.4142]) >>> torch.linalg.cond(A, 'fro') tensor(3.1623) >>> torch.linalg.cond(A, 'nuc') tensor(9.2426) >>> torch.linalg.cond(A, float('inf')) tensor(2.) >>> torch.linalg.cond(A, float('-inf')) tensor(1.) >>> torch.linalg.cond(A, 1) tensor(2.) >>> torch.linalg.cond(A, -1) tensor(1.) >>> torch.linalg.cond(A, 2) tensor([1.4142]) >>> torch.linalg.cond(A, -2) tensor([0.7071]) >>> A = torch.randn(2, 3, 3) >>> torch.linalg.cond(A) tensor([[9.5917], [3.2538]]) >>> A = torch.randn(2, 3, 3, dtype=torch.complex64) >>> torch.linalg.cond(A) tensor([[4.6245], [4.5671]])
© 2024, PyTorch Contributors
PyTorch has a BSD-style license, as found in the LICENSE file.
https://pytorch.org/docs/2.1/generated/torch.linalg.cond.html