PyTorch torch.lu_solve function
PyTorch torch reference manual
torch.lu_solve is a function in PyTorch used to solve systems of linear equations using LU decomposition. It uses an already computed LU decomposition to solve AX = B efficiently.
Function definition
torch.lu_solve(B, LU, pivots, out=None)
Parameters:
B(Tensor): Right-hand-side matrix or vector.LU(Tensor): Matrix obtained from the LU decomposition.pivots(Tensor): Pivot indices from the LU decomposition.out(Tensor, optional): the output tensor.
Return Value:
torch.Tensor: Returns the solution to the system of linear equations.
Usage examples
Example
import
torch
Create a matrix and a right-hand-side vector
A
=
torch.
tensor
(
[
[
1.0
,
2.0
,
3.0
]
,
[
4.0
,
5.0
,
6.0
]
,
[
7.0
,
8.0
,
9.0
]
]
,
dtype
=
torch.
float64
)
B
=
torch.
tensor
(
[
14.0
,
32.0
,
50.0
]
,
dtype
=
torch.
float64
)
LU decomposition
LU
,
pivots
=
torch.
lu
(
A
)
Solve using LU decomposition
X
=
torch.
lu_solve
(
B
,
LU
,
pivots
)
(
"Matrix A:"
)
(
A
)
(
"nRight-hand-side vector B:"
)
(
B
)
(
"nSolution X:"
)
(
X
)
(
"nVerify: A @ X ="
)
(
A
@
X
)
The output is:
矩阵 A:
tensor([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]], dtype=torch.float64)
右侧向量 B:
tensor([14., 32., 50.], dtype=torch.float64)
解 X:
tensor([1., 2., 3.], dtype=torch.float64)
验证: A @ X =
tensor([14., 32., 50.], dtype=torch.float64)