torch.linalg.solve_triangular — PyTorch 2.7 documentation (original) (raw)

torch.linalg.solve_triangular(A, B, *, upper, left=True, unitriangular=False, out=None) → Tensor

Computes the solution of a triangular system of linear equations with a unique solution.

Letting K\mathbb{K} be R\mathbb{R} or C\mathbb{C}, this function computes the solution X∈Kn×kX \in \mathbb{K}^{n \times k} of the linear systemassociated to the triangular matrix A∈Kn×nA \in \mathbb{K}^{n \times n} without zeros on the diagonal (that is, it is invertible) and the rectangular matrix , B∈Kn×kB \in \mathbb{K}^{n \times k}, which is defined as

AX=BAX = B

The argument upper signals whether AA is upper or lower triangular.

If left= False, this function returns the matrix X∈Kn×kX \in \mathbb{K}^{n \times k} that solves the system

XA=BA∈Kk×k,B∈Kn×k.XA = B\mathrlap{\qquad A \in \mathbb{K}^{k \times k}, B \in \mathbb{K}^{n \times k}.}

If upper= True (resp. False) just the upper (resp. lower) triangular half of Awill be accessed. The elements below the main diagonal will be considered to be zero and will not be accessed.

If unitriangular= True, the diagonal of A is assumed to be ones and will not be accessed.

The result may contain NaN s if the diagonal of A contains zeros or elements that are very close to zero and unitriangular= False (default) or if the input matrix has very small eigenvalues.

Supports inputs of float, double, cfloat and cdouble dtypes. Also supports batches of matrices, and if the inputs are batches of matrices then the output has the same batch dimensions.

See also

torch.linalg.solve() computes the solution of a general square system of linear equations with a unique solution.

Parameters

Keyword Arguments

Examples:

A = torch.randn(3, 3).triu_() B = torch.randn(3, 4) X = torch.linalg.solve_triangular(A, B, upper=True) torch.allclose(A @ X, B) True

A = torch.randn(2, 3, 3).tril_() B = torch.randn(2, 3, 4) X = torch.linalg.solve_triangular(A, B, upper=False) torch.allclose(A @ X, B) True

A = torch.randn(2, 4, 4).tril_() B = torch.randn(2, 3, 4) X = torch.linalg.solve_triangular(A, B, upper=False, left=False) torch.allclose(X @ A, B) True