Skip to content
fn

solve

→Tensor
solve(A: Tensor, B: Tensor, left: bool = True)

Implementing kernel

C++ engine symbols that back this Python API.

Solve a square linear system AX=BAX = B (or XA=BXA = B).

Returns the unique solution XX of the system

AX=BA X = B

where AA is a non-singular square matrix. The system is solved by LU decomposition with partial pivoting, which is both faster and more accurate than forming A−1A^{-1} explicitly.

Parameters

Square coefficient matrix of shape (*, n, n).
Right-hand side of shape (*, n, k) (or (*, k, n) when left=False) or (*, n) (single RHS vector).
left(bool, keyword - only)= True
If True (default) solves AX=BAX = B; if False solves XA=BXA = B via transposition.

Returns

Tensor

Solution XX with the same shape as B.

Raises

DtypeMismatch
If A is bool or integer (judged first) or A and B have different dtypes (a TypeError), as the reference does; so do solve_ex and tensorsolve.

Notes

Algorithm: factor PA=LUPA = LU, then perform forward-substitution LY=PBL Y = P B followed by back-substitution UX=YU X = Y. Total cost is O(n3+kn2)O(n^3 + k n^2).

Examples

>>> import lucid
>>> from lucid.linalg import solve
>>> A = lucid.tensor([[3.0, 1.0], [1.0, 2.0]])
>>> b = lucid.tensor([9.0, 8.0])
>>> solve(A, b)
tensor([2., 3.])

Used by 2