fn

solve_triangular

Tensor
solve_triangular(A: Tensor, B: Tensor, upper: bool = True, left: bool = True, unitriangular: bool = False)
source

Solve a triangular linear system by back/forward substitution.

Solves the system AX=BAX = B (or XA=BXA = B) in which the coefficient matrix AA is triangular. For upper-triangular AA the system is solved by back-substitution starting from the last row; for lower-triangular AA by forward substitution from the first row. Either direction runs in O(n2k)O(n^2 k) time and is numerically stable when the diagonal of AA is well-conditioned.

Parameters

ATensor
Triangular coefficient matrix of shape (*, n, n). Only the relevant triangle is read; the other half is ignored.
BTensor
Right-hand side of shape (*, n, k) (or (*, n)).
upper(bool, keyword - only)= True
If True (default) AA is upper-triangular; if False lower-triangular.
left(bool, keyword - only)= True
If True (default) solves AX=BAX = B; if False solves XA=BXA = B via transposition.
unitriangular(bool, keyword - only)= False
If True the diagonal of AA is treated as all-ones regardless of its stored values (LAPACK's "unit-diagonal" mode).

Returns

Tensor

Solution XX shaped like BB.

Notes

Backed by LAPACK trsm. Triangular solves are the workhorse used inside Cholesky, LU, and QR back-substitution paths.

Examples

>>> import lucid
>>> from lucid.linalg import solve_triangular
>>> A = lucid.tensor([[2.0, 1.0], [0.0, 3.0]])  # upper
>>> b = lucid.tensor([[5.0], [9.0]])
>>> solve_triangular(A, b, upper=True)
Tensor([[1.0000],
        [3.0000]])