fn

lu_solve

Tensor
lu_solve(LU: Tensor, pivots: Tensor, B: Tensor)
source

Solve a linear system from a precomputed LU factorization.

Given the packed factorization PA=LUPA = LU returned by lu_factor, solves

AX=BA\,X \,=\, B

by applying the permutation and performing two triangular solves (forward + back substitution).

Parameters

LUTensor
Packed LU factor of shape (*, n, n) from lu_factor.
pivotsTensor
Pivot indices of shape (*, n) from lu_factor (1-based, LAPACK convention).
BTensor
Right-hand side of shape (*, n, k) (or (*, n)).

Returns

Tensor

Solution XX, same shape as B.

Notes

Backed by LAPACK getrs. Cost per solve is O(n2k)O(n^2 k) — much cheaper than a fresh solve (O(n3)O(n^3)) when the same AA is reused.

Examples

>>> import lucid
>>> from lucid.linalg import lu_factor, lu_solve
>>> A = lucid.tensor([[3.0, 1.0], [1.0, 2.0]])
>>> LU, piv = lu_factor(A)
>>> b = lucid.tensor([[9.0], [8.0]])
>>> lu_solve(LU, piv, b)
Tensor([[2.0000],
        [3.0000]])