Rate this Page

torch.linalg.lu_factor#

torch.linalg.lu_factor(A,*,boolpivot=True,out=None)->(Tensor,Tensor)#

Computes a compact representation of the LU factorization with partial pivoting of a matrix.

This function computes a compact representation of the decomposition given bytorch.linalg.lu().If the matrix is square, this representation may be used intorch.linalg.lu_solve()to solve system of linear equations that share the matrixA.

The returned decomposition is represented as a named tuple(LU, pivots).TheLU matrix has the same shape as the input matrixA. Its upper and lower triangularparts encode the non-constant elements ofL andU of the LU decomposition ofA.

The returned permutation matrix is represented by a 1-indexed vector.pivots[i] == j representsthat in thei-th step of the algorithm, thei-th row was permuted with thej-1-th row.

On CUDA, one may usepivot= False. In this case, this function returns the LUdecomposition without pivoting if it exists.

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.

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.lu_factor_ex().

Warning

The LU decomposition is almost never unique, as often there are different permutationmatrices that can yield different LU decompositions.As such, different platforms, like SciPy, or inputs on different devices,may produce different valid decompositions.

Gradient computations are only supported if the input matrix is full-rank.If this condition is not met, no error will be thrown, but the gradient may not be finite.This is because the LU decomposition with pivoting is not differentiable at these points.

See also

torch.linalg.lu_solve() solves a system of linear equations given the output of thisfunction provided the input matrix was square and invertible.

torch.lu_unpack() unpacks the tensors returned bylu_factor() into the threematricesP, L, U that form the decomposition.

torch.linalg.lu() computes the LU decomposition with partial pivoting of a possiblynon-square matrix. It is a composition oflu_factor() andtorch.lu_unpack().

torch.linalg.solve() solves a system of linear equations. It is a compositionoflu_factor() andlu_solve().

Parameters

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

Keyword Arguments
  • pivot (bool,optional) – Whether to compute the LU decomposition with partial pivoting, or the regular LUdecomposition.pivot= False not supported on CPU. Default:True.

  • out (tuple,optional) – tuple of two tensors to write the output to. Ignored ifNone. Default:None.

Returns

A named tuple(LU, pivots).

Raises

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

Examples:

>>>A=torch.randn(2,3,3)>>>B1=torch.randn(2,3,4)>>>B2=torch.randn(2,3,7)>>>LU,pivots=torch.linalg.lu_factor(A)>>>X1=torch.linalg.lu_solve(LU,pivots,B1)>>>X2=torch.linalg.lu_solve(LU,pivots,B2)>>>torch.allclose(A@X1,B1)True>>>torch.allclose(A@X2,B2)True