torch.linalg.lu
- torch.linalg.lu(A, *, pivot=True, out=None)
Computes the LU decomposition with partial pivoting of a matrix.
Letting be or , the of a matrix is defined as
Computes the LU decomposition with partial pivoting of a matrix.
Letting be or , the of a matrix is defined as
where k = min(m,n), is a permutation matrix, is lower triangular with ones on the diagonal and is upper triangular.
If pivot= False and A is on GPU, then the LU decomposition without pivoting is computed
When pivot= False, the returned matrix P will be empty.
The LU decomposition without pivoting may not exist if any of the principal minors of A is singular.
In this case, the output matrix may contain inf or NaN.
Supports input of float, double, cfloat and cdouble dtypes.
Also supports batches of matrices, and if A is a batch of matrices then
the output has the same batch dimensions.
out (tuple, optional) – output tuple of three tensors. Ignored if None. Default: None.
A named tuple (P, L, U).
Examples:
>>> A = torch.randn(3, 2)
>>> P, L, U = torch.linalg.lu(A)
>>> P
tensor([[0., 1., 0.],
[0., 0., 1.],
[1., 0., 0.]])
>>> L
tensor([[1.0000, 0.0000],
[0.5007, 1.0000],
[0.0633, 0.9755]])
>>> U
tensor([[0.3771, 0.0489],
[0.0000, 0.9644]])
>>> torch.dist(A, P @ L @ U)
tensor(5.9605e-08)
>>> A = torch.randn(2, 5, 7, device="cuda")
>>> P, L, U = torch.linalg.lu(A, pivot=False)
>>> P
tensor([], device='cuda:0')
>>> torch.dist(A, L @ U)
tensor(1.0376e-06, device='cuda:0')