Table of Contents
Shortcuts

# torch.linalg.lu_solve

torch.linalg.lu_solve(LU, pivots, B, *, left=True, adjoint=False, out=None) → Tensor

  Computes the solution of a square system of linear equations with a unique solution given an LU decomposition.

  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 A∈K n×n,B∈K n×k A \in \mathbb{K}^{n \times n}, B \in \mathbb{K}^{n \times k}A∈K n×n,B∈K n×k, which is defined as

  A X=B AX = B A X=B

  where A A A is given factorized as returned by `lu_factor()`.

  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 `adjoint`= True (and `left`= True), given an LU factorization of :math:`A this function function returns the X∈K n×k X \in \mathbb{K}^{n \times k}X∈K n×k that solves the system

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

  where A H A^{\text{H}}A H is the conjugate transpose when A A A is complex, and the transpose when A A A is real-valued. The `left`= False case is analogous.

  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.

  Parameters:

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

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

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

  Keyword Arguments:

    - 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.

    - adjoint (bool, optional) – whether to solve the system A X=B AX=B A X=B or A H X=B A^{\text{H}}X = B A H X=B. Default: False.

    - out (Tensor, optional) – output tensor. Ignored if None. Default: None.

  Examples:

      >>> A = torch.randn(3, 3)
      >>> LU, pivots = torch.linalg.lu_factor(A)
      >>> B = torch.randn(3, 2)
      >>> X = torch.linalg.lu_solve(LU, pivots, B)
      >>> torch.allclose(A @ X, B)
      True

      >>> B = torch.randn(3, 3, 2)   # Broadcasting rules apply: A is broadcasted
      >>> X = torch.linalg.lu_solve(LU, pivots, B)
      >>> torch.allclose(A @ X, B)
      True

      >>> B = torch.randn(3, 5, 3)
      >>> X = torch.linalg.lu_solve(LU, pivots, B, left=False)
      >>> torch.allclose(X @ A, B)
      True

      >>> B = torch.randn(3, 3, 4)   # Now solve for A^T
      >>> X = torch.linalg.lu_solve(LU, pivots, B, adjoint=True)
      >>> torch.allclose(A.mT @ X, 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.