fn

cond

Tensor
cond(A: Tensor, p: int | float | str | None = None)
source

Compute the condition number of a matrix.

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

κp(A)=ApA1p.\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

ATensor
Input matrix of shape (*, m, n).
p(int, float, str or None)= None
Norm order. None (default) and 2 use the spectral norm via SVD; -2 returns the reciprocal σmin/σmax\sigma_{\min} / \sigma_{\max}. Other orders dispatch to norm.

Returns

Tensor

Condition number, shape (*,).

Notes

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

Examples

>>> import lucid
>>> from lucid.linalg import cond
>>> cond(lucid.tensor([[1.0, 0.0], [0.0, 1e-6]]))
Tensor(1000000.0)