Skip to content
fn

solve_triangular

→Tensor
solve_triangular(input: Tensor, B: Tensor, upper: bool, left: bool = True, unitriangular: bool = False)

Implementing kernel

C++ engine symbols that back this Python API.

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

inputTensor
Triangular coefficient matrix of shape (*, n, n). Only the relevant triangle is read; the other half is ignored.
Right-hand side (*, n, k) ((*, k, n) if left=False); batch dims broadcast with input. A vector is refused: b.unsqueeze(-1) gives it a column.
upper(bool, keyword - only)
If True 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 — the broadcast batch, then (n, k).

Raises

ShapeMismatch
An operand under two dimensions, a non-square input, or shapes that do not fit or broadcast: a RuntimeError with the reference's message.

Notes

A B of another dtype is read in input's, and the answer has it too, as the reference does.

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

A singular AA (an exact zero on the diagonal, with unitriangular=False) is not refused: substitution divides by the zero, so the solution holds ±∞\pm\infty, or NaN where it meets 0/00 / 0, as the reference framework does.

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.], [3.]])

Used by 3