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

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

AX=BAX = B

where AA is given factorized as returned by lu_factor().

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

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

where AHA^{\text{H}} is the conjugate transpose when AA is complex, and the transpose when AA 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

Keyword Arguments

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