Rate this Page

torch.linalg.solve#

torch.linalg.solve(A,B,*,left=True,out=None)Tensor#

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

LettingK\mathbb{K} beR\mathbb{R} orC\mathbb{C},this function computes the solutionXKn×kX \in \mathbb{K}^{n \times k} of thelinear system associated toAKn×n,BKn×kA \in \mathbb{K}^{n \times n}, B \in \mathbb{K}^{n \times k}, which is defined as

AX=BAX = B

Ifleft= False, this function returns the matrixXKn×kX \in \mathbb{K}^{n \times k} that solves the system

This system of linear equations has one solution if and only ifAA isinvertible.This function assumes thatAA is invertible.

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

Letting* be zero or more batch dimensions,

  • IfA has shape(*, n, n) andB has shape(*, n) (a batch of vectors) or shape(*, n, k) (a batch of matrices or “multiple right-hand sides”), this function returnsX of shape(*, n) or(*, n, k) respectively.

  • Otherwise, ifA has shape(*, n, n) andB has shape(n,) or(n, k),Bis broadcasted to have shape(*, n) or(*, n, k) respectively.This function then returns the solution of the resulting batch of systems of linear equations.

Note

This function computesX =A.inverse() @B in a faster andmore numerically stable way than performing the computations separately.

Note

It is possible to compute the solution of the systemXA=BXA = B by passing the inputsA andB transposed and transposing the output returned by this function.

Note

A is allowed to be a non-batchedtorch.sparse_csr_tensor, but only withleft=True.

Note

When inputs are on a CUDA device, this function synchronizes that device with the CPU. For a version of this function that does not synchronize, seetorch.linalg.solve_ex().

See also

torch.linalg.solve_triangular() computes the solution of a triangular system of linearequations with a unique solution.

Parameters
  • A (Tensor) – tensor of shape(*, n, n) where* is zero or more batch dimensions.

  • B (Tensor) – right-hand side tensor of shape(*, n) or(*, n, k) or(n,) or(n, k)according to the rules described above

Keyword Arguments
  • left (bool,optional) – whether to solve the systemAX=BAX=B orXA=BXA = B. Default:True.

  • out (Tensor,optional) – output tensor. Ignored ifNone. Default:None.

Raises

RuntimeError – if theA matrix is not invertible or any matrix in a batchedA is not invertible.

Examples:

>>>A=torch.randn(3,3)>>>b=torch.randn(3)>>>x=torch.linalg.solve(A,b)>>>torch.allclose(A@x,b)True>>>A=torch.randn(2,3,3)>>>B=torch.randn(2,3,4)>>>X=torch.linalg.solve(A,B)>>>X.shapetorch.Size([2, 3, 4])>>>torch.allclose(A@X,B)True>>>A=torch.randn(2,3,3)>>>b=torch.randn(3,1)>>>x=torch.linalg.solve(A,b)# b is broadcasted to size (2, 3, 1)>>>x.shapetorch.Size([2, 3, 1])>>>torch.allclose(A@x,b)True>>>b=torch.randn(3)>>>x=torch.linalg.solve(A,b)# b is broadcasted to size (2, 3)>>>x.shapetorch.Size([2, 3])>>>Ax=A@x.unsqueeze(-1)>>>torch.allclose(Ax,b.unsqueeze(-1).expand_as(Ax))True