PyTorch torch.cholesky_solve function
PyTorch torch reference manual
torch.cholesky_solve is a function in PyTorch used to solve systems of linear equations using Cholesky decomposition. It uses the Cholesky decomposition to solve AX = B efficiently.
Function definition
torch.cholesky_solve(B, L, upper=False, out=None)
Parameters:
B(Tensor): The right-hand-side matrix or vector of the linear system.L(Tensor): The upper or lower triangular matrix obtained from the Cholesky decomposition.upper(bool, optional): If True, L is an upper triangular matrix; otherwise it is a lower triangular matrix. Defaults to False.out(Tensor, optional): the output tensor.
Return Value:
torch.Tensor: Returns the solution X of the linear system.
Usage examples
Example
import
torch
Create a symmetric positive-definite matrix A
A
=
torch.
tensor
(
[
[
4.0
,
2.0
,
2.0
]
,
[
2.0
,
5.0
,
3.0
]
,
[
2.0
,
3.0
,
6.0
]
]
,
dtype
=
torch.
float64
)
Create the right-hand-side vector B
B
=
torch.
tensor
(
[
3
,
4
,
5
]
,
dtype
=
torch.
float64
)
Cholesky decomposition
L
=
torch.
cholesky
(
A
)
Solve AX = B using Cholesky
X
=
torch.
cholesky_solve
(
B
,
L
)
(
"Matrix A:"
)
(
A
)
(
"nRight-hand-side vector B:"
)
(
B
)
(
"nSolution X:"
)
(
X
)
(
"nVerify: A @ X ="
)
(
A
@
X
)
The output is:
矩阵 A:
tensor([[4., 2., 2.],
[2., 5., 3.],
[2., 3., 6.]], dtype=torch.float64)
右侧向量 B:
tensor([3., 4., 5.], dtype=torch.float64)
解 X:
tensor([-2.5000, 5.0000, -2.0000], dtype=torch.float64)
验证: A @ X =
tensor([3., 4., 5.], dtype=torch.float64)