Shortcuts

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 K\mathbb{K} be R\mathbb{R} or C\mathbb{C}, the condition number κ\kappa of a matrix AKn×nA \in \mathbb{K}^{n \times n} is defined as

κ(A)=ApA1p\kappa(A) = \|A\|_p\|A^{-1}\|_p

The condition number of A measures the numerical stability of the linear system AX = B with respect to a matrix norm.

Supports input of float, double, cfloat and cdouble dtypes. Also supports batches of matrices, and if A is a batch of matrices then the output has the same batch dimensions.

p defines the matrix norm that is computed. The following norms are supported:

p

matrix norm

None

2-norm (largest singular value)

‘fro’

Frobenius norm

‘nuc’

nuclear norm

inf

max(sum(abs(x), dim=1))

-inf

min(sum(abs(x), dim=1))

1

max(sum(abs(x), dim=0))

-1

min(sum(abs(x), dim=0))

2

largest singular value

-2

smallest singular value

where inf refers to float(‘inf’), NumPy’s inf object, or any equivalent object.

For p is one of (‘fro’, ‘nuc’, inf, -inf, 1, -1), this function uses torch.linalg.norm() and torch.linalg.inv(). As such, in this case, the matrix (or every matrix in the batch) A has to be square and invertible.

For p in (2, -2), this function can be computed in terms of the singular values σ1σn\sigma_1 \geq \ldots \geq \sigma_n

κ2(A)=σ1σnκ2(A)=σnσ1\kappa_2(A) = \frac{\sigma_1}{\sigma_n}\qquad \kappa_{-2}(A) = \frac{\sigma_n}{\sigma_1}

In these cases, it is computed using torch.linalg.svd(). For these norms, the matrix (or every matrix in the batch) A may have any shape.

Note

When inputs are on a CUDA device, this function synchronizes that device with the CPU if if p is 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 for p in (2, -2), and of shape (*, n, n) where every matrix is invertible for p in (‘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

Keyword Arguments

out (Tensor, optional) – output tensor. Ignored if None. Default: None.

Returns

A real-valued tensor, even when A is complex.

Raises

RuntimeError – if p is one of (‘fro’, ‘nuc’, inf, -inf, 1, -1) and the A matrix or any matrix in the batch A is 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)
>>> a
tensor([[[-0.9204,  1.1140,  1.2055],
        [ 0.3988, -0.2395, -0.7441],
        [-0.5160,  0.3115,  0.2619]],

        [[-2.2128,  0.9241,  2.1492],
        [-1.1277,  2.7604, -0.8760],
        [ 1.2159,  0.5960,  0.0498]]])
>>> torch.linalg.cond(a)
tensor([[9.5917],
        [3.2538]])

>>> a = torch.randn(2, 3, 3, dtype=torch.complex64)
>>> a
tensor([[[-0.4671-0.2137j, -0.1334-0.9508j,  0.6252+0.1759j],
        [-0.3486-0.2991j, -0.1317+0.1252j,  0.3025-0.1604j],
        [-0.5634+0.8582j,  0.1118-0.4677j, -0.1121+0.7574j]],

        [[ 0.3964+0.2533j,  0.9385-0.6417j, -0.0283-0.8673j],
        [ 0.2635+0.2323j, -0.8929-1.1269j,  0.3332+0.0733j],
        [ 0.1151+0.1644j, -1.1163+0.3471j, -0.5870+0.1629j]]])
>>> torch.linalg.cond(a)
tensor([[4.6245],
        [4.5671]])
>>> torch.linalg.cond(a, 1)
tensor([9.2589, 9.3486])

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources