Table of Contents
Shortcuts

# torch.linalg.solve_triangular

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}K be R\mathbb{R}R or C\mathbb{C}C, this function computes the solution X∈K n×k X \in \mathbb{K}^{n \times k}X∈K n×k of the linear system associated to the triangular matrix A∈K n×n A \in \mathbb{K}^{n \times n}A∈K n×n without zeros on the diagonal (that is, it is invertible) and the rectangular matrix , B∈K n×k B \in \mathbb{K}^{n \times k}B∈K n×k, which is defined as

  A X=B AX = B A X=B

  The argument `upper` signals whether A A A is upper or lower triangular.

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

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

  If `upper`= True (resp. False) just the upper (resp. lower) triangular half of `A` will 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:

    - A (Tensor) – tensor of shape (*, n, n) (or (*, k, k) if `left`= True) where * is zero or more batch dimensions.

    - B (Tensor) – right-hand side tensor of shape (*, n, k).

  Keyword Arguments:

    - upper (bool) – whether `A` is an upper or lower triangular matrix.

    - left (bool, optional) – whether to solve the system A X=B AX=B A X=B or X A=B XA = B X A=B. Default: True.

    - unitriangular (bool, optional) – if True, the diagonal elements of `A` are assumed to be all equal to 1. Default: False.

    - out (Tensor, optional) – output tensor. B may be passed as out and the result is computed in-place on B. Ignored if None. Default: None.

  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

## Docs

Access comprehensive developer documentation for PyTorch

View Docs

## Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

## Resources

Find development resources and get your questions answered

View Resources

To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.