torch.Tensor.Tensor.triangular_solve
Tensor.triangular_solve(A: Tensor, options?: TriangularSolveOptions): { solution: Tensor; cloned_coefficient: Tensor }Tensor.triangular_solve(A: Tensor, upper: boolean, transpose: boolean, unitriangular: boolean, options?: TriangularSolveOptions): { solution: Tensor; cloned_coefficient: Tensor }Solves triangular linear systems efficiently using forward/backward substitution.
Solves AX = B where A is triangular (upper or lower). Much faster than general solve() because it uses triangular structure. Used internally by many decomposition solvers (LU, QR, Cholesky). Self is the B (right-hand side), A is the triangular matrix.
Use Cases:
- Back-substitution after LU/QR decomposition
- Solving triangular systems in iterative algorithms
- Efficient implementation of decomposition-based solvers
- Performance-critical linear algebra code
- Lower triangular: upper=false means A is lower (most operations default)
- Upper triangular: upper=true means A is upper (from QR decomposition)
- Unit assumption: unitriangular=true skips division - assumes diagonal is 1
- Transpose handling: transpose=true solves A^T X = B efficiently
- Forward substitution: Used for lower triangular
- Back-substitution: Used for upper triangular
- Efficiency: O(n²) for solving, much faster than O(n³) for general solve
- Batch support: Works with batched matrices
- Triangularity requirement: A must actually be triangular (method doesn't check)
- Zero diagonal: Non-unit triangular with zeros on diagonal causes division by zero
- Upper/lower confusion: Specifying wrong triangle type gives wrong results
Parameters
ATensor- Triangular coefficient matrix (lower if upper=false, upper if upper=true)
optionsTriangularSolveOptionsoptional
Returns
{ solution: Tensor; cloned_coefficient: Tensor }– Object with: - solution: X where AX = self (or A^TX = self if transpose=true) - cloned_coefficient: Copy of the coefficient matrix AExamples
// Solve lower triangular system
const L = torch.tensor([[2, 0], [3, 4]]); // Lower triangular
const b = torch.ones(2);
const { solution } = b.triangular_solve(L, false); // Solve L x = b
// Solve upper triangular system (from QR decomposition)
const { Q, R } = A.qr();
const b = torch.randn(5);
const y = Q.T.matmul(b);
const { solution: x } = y.triangular_solve(R, true); // Solve R x = y
// Unit triangular (assumes 1s on diagonal)
const U = torch.tensor([[1, 2, 3], [0, 1, 4], [0, 0, 1]]);
const b = torch.tensor([1, 2, 3]);
const { solution } = b.triangular_solve(U, true, false, true);
// Skips division by diagonal elements (all 1)
// Solve transposed system
const A = torch.tensor([[2, 0], [3, 4]]);
const b = torch.ones(2);
const { solution } = b.triangular_solve(A, false, true); // Solve A^T x = bSee Also
- PyTorch torch.triangular_solve() (or tensor.triangular_solve())
- solve - General linear solver
- cholesky_solve - For Cholesky factors (which are triangular)
- lu - LU decomposition (produces triangular factors)
- qr - QR decomposition (produces triangular factor)