Skip to content
fn

inv_ex

→Tensor
inv_ex(A: Tensor, check_errors: bool = False)

Matrix inverse with an explicit success flag.

Variant of inv that returns an info code instead of raising on a singular input.

  • info == 0 — success; Ainv is A−1A^{-1}.
  • info == k > 0 — UkkU_{kk} of PA=LUPA = LU is exactly zero, so AA is singular; that matrix's Ainv is zero-filled.

Parameters

Square matrix of shape (*, n, n).
check_errors(bool, keyword - only)= False
If True, raise for the first singular matrix instead of reporting it through info.

Returns

Tensor

Inverse of shape (*, n, n) — zeros for the singular matrices.

Notes

Useful in algorithms that occasionally probe near-singular matrices (e.g., iterative refinement, regularisation grid searches) without wanting to wrap every call in a try.

Each matrix of a batch is judged on its own: a singular one leaves the other inverses, and their gradients, intact. The batch is inverted directly; only when that fails is it factorised again to find which matrices failed and at which pivot.

Examples

>>> import lucid
>>> from lucid.linalg import inv_ex
>>> Ainv, info = inv_ex(lucid.tensor([[1.0, 2.0], [3.0, 4.0]]))
>>> int(info)
0
>>> _, info = inv_ex(lucid.tensor([[1.0, 2.0], [2.0, 4.0]]))
>>> int(info)
2

Used by 1