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;Ainvis .info == k > 0— of is exactly zero, so is singular; that matrix'sAinvis zero-filled.
Parameters
Square matrix of shape
(*, n, n).check_errors(bool, keyword - only)= FalseIf
True, raise for the first singular matrix instead of
reporting it through info.Returns
TensorInverse 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