Skip to content
fn

cond

→Tensor
cond(input: Tensor, p: int | float | str | None = None)

Compute the condition number of a matrix.

The condition number under norm ∥⋅∥p\|\cdot\|_p is

κp(A) = ∥A∥p ∥A−1∥p.\kappa_p(A) \,=\, \|A\|_p \,\|A^{-1}\|_p.

For the spectral (p=2p = 2) norm this simplifies to the ratio of the largest to smallest singular value:

κ2(A) = σmax⁡(A) / σmin⁡(A).\kappa_2(A) \,=\, \sigma_{\max}(A) \,/\, \sigma_{\min}(A).

The condition number quantifies how sensitive the solution of Ax=bAx = b is to perturbations in AA or bb.

Parameters

inputTensor
Input matrix of shape (*, m, n).
p(int, float, str or None)= None
Norm order: None (default) or 2 for the spectral norm, -2 for its reciprocal σmin⁡/σmax⁡\sigma_{\min} / \sigma_{\max}, "nuc", or one of the matrix_norm orders "fro", 1, -1, inf, -inf.

Returns

Tensor

Condition number, shape (*,).

Raises

ValueError
For an unknown p, an input with fewer than two dimensions, or a non-square input under any order but ±2\pm 2.
NotImplementedError
For a complex input: every order needs its singular values or its inverse, and neither has a complex kernel yet.

Notes

A condition number near 1/εmach1/\varepsilon_{\mathrm{mach}} indicates numerical singularity. Non-spectral orders require an explicit inverse, so prefer p = 2 for rank-deficient or rectangular matrices.

The spectral orders and "nuc" are read off the singular values; the others are ∥A∥p∥A−1∥p\|A\|_p \|A^{-1}\|_p with the inverse from inv_ex, so a singular matrix has condition number ∞\infty rather than an error, each matrix of a batch on its own. A matrix with no entries has condition number 0.

Examples

>>> import lucid
>>> from lucid.linalg import cond
>>> cond(lucid.tensor([[1.0, 0.0], [0.0, 1e-6]]))
tensor(1e+06)
>>> cond(lucid.tensor([[1.0, 2.0], [2.0, 4.0]]), "fro")
tensor(inf)