PyTorch torch.lu function
PyTorch torch reference manual
torch.lu is a function in PyTorch used to compute the LU decomposition of a matrix. LU decomposition factors matrix A as A = P * L * U, where L is a lower triangular matrix, U is an upper triangular matrix, and P is a permutation matrix.
Function definition
torch.lu(A, pivot=True, get_infos=False, out=None)
Parameters:
A(Tensor): Input matrix.pivot(bool, optional): Whether to perform LU decomposition with column pivoting. Defaults to True.get_infos(bool, optional): If True, return information. Defaults to False.out(tuple, optional): Output tuple.
Return Value:
tuple: Returns a tuple (pivot, L, U).
Usage examples
Example
import
torch
Create a matrix
A
=
torch.
tensor
(
[
[
1.0
,
2.0
,
3.0
]
,
[
4.0
,
5.0
,
6.0
]
,
[
7.0
,
8.0
,
9.0
]
]
)
LU decomposition
LU
,
pivots
=
torch.
lu
(
A
,
pivot
=
True
)
(
"Matrix A:"
)
(
A
)
(
"nLU matrix:"
)
(
LU
)
(
"nPivot indices:"
)
(
pivots
)
The output is:
矩阵 A:
tensor([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]])
LU 矩阵:
tensor([[7., 8., 9.],
[0.2, 0.4, 0.6],
[0.6, 0.8, 0.0]])
Primary index:
tensor([3, 3, 3], dtype=torch.int32)
Example - using get_infos
import
torch
A
=
torch.
tensor
(
[
[
1.0
,
2.0
,
3.0
]
,
[
4.0
,
5.0
,
6.0
]
,
[
7.0
,
8.0
,
9.0
]
]
)
LU decomposition with information
LU
,
pivots
,
info
=
torch.
lu
(
A
,
pivot
=
True
,
get_infos
=
True
)
(
"Information:"
,
info
)