Skip to content
fn

solve_ex

→Tensor
solve_ex(A: Tensor, B: Tensor, left: bool = True, check_errors: bool = False)

Solve AX=BAX = B with an explicit success flag.

Variant of solve that returns an info code instead of raising on a singular coefficient matrix.

  • info == 0 — success; XX is the unique solution.
  • info == k > 0 — UkkU_{kk} of PA=LUPA = LU is exactly zero, so AA is singular; that system's XX is zero-filled.

Parameters

Coefficient matrix of shape (*, n, n).
Right-hand side of shape (*, n, k) (or (*, n)).
left(bool, keyword - only)= True
If True (default), solve AX=BA X = B; False solves XA=BX A = B.
check_errors(bool, keyword - only)= False
If True, raise for the first singular matrix instead of reporting it through info.

Returns

Tensor

Solution shaped like solve's — zeros for the systems whose A was singular.

Notes

This is the recommended form when calling from inside a batched / jit-compiled / vmapped routine where raising would break control flow; manually inspect info afterwards and decide whether to recover.

Each system of a batch is judged on its own: a singular A leaves the other solutions, and their gradients, intact. The batch is solved directly; only when that fails is A factorised again to find which systems failed and at which pivot.

Examples

>>> import lucid
>>> from lucid.linalg import solve_ex
>>> A = lucid.tensor([[3.0, 1.0], [1.0, 2.0]])
>>> b = lucid.tensor([9.0, 8.0])
>>> X, info = solve_ex(A, b)
>>> int(info)
0

Used by 1