Compute the condition number of a matrix.
The condition number under norm is
For the spectral () norm this simplifies to the ratio of the largest to smallest singular value:
The condition number quantifies how sensitive the solution of is to perturbations in or .
Parameters
Input matrix of shape
(*, m, n).p(int, float, str or None)= NoneNorm order:
None (default) or 2 for the spectral norm,
-2 for its reciprocal ,
"nuc", or one of the matrix_norm orders "fro",
1, -1, inf, -inf.Returns
TensorCondition number, shape (*,).
Raises
ValueErrorFor an unknown
p, an input with fewer than two dimensions, or a
non-square input under any order but .NotImplementedErrorFor a complex
A: every order needs its singular values or its
inverse, and neither has a complex kernel yet.Notes
A condition number near
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 with the inverse from
inv_ex, so a singular matrix has condition number
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)