fn

solve

Tensor
solve(A: Tensor, b: Tensor)
source

Solve a square linear system AX=BAX = 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 A1A^{-1} explicitly.

Parameters

ATensor
Square coefficient matrix of shape (*, n, n).
bTensor
Right-hand side of shape (*, n, k) (multiple RHS columns) or (*, n) (single RHS vector).

Returns

Tensor

Solution XX with the same shape as b.

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.0000, 3.0000])