PyTorch torch.cholesky_solve Function
Pytorch torch Reference Manual
torch.cholesky_solveThis is a function in PyTorch that solves linear equations using Cholesky decomposition. It utilizes Cholesky decomposition to efficiently solve AX = B.
Function Definition
torch.cholesky_solve(B, L, upper=False, out=None)
Parameters:
B(Tensor): The right-hand side matrix or vector of the system of linear equations.L(Tensor): The upper or lower triangular matrix obtained from Cholesky decomposition.upper(bool, optional): If True, L is an upper triangular matrix; otherwise, it is a lower triangular matrix. Default is False.out(Tensor, optional): Output tensor.
Return Value:
torch.Tensor: Returns the solution X of the system of linear equations.
Usage Example
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)
# Use Cholesky to solve AX = B
X = torch.cholesky_solve(B, L)
print(Matrix A:)
print(A)
print(nRight-hand side vector B:)
print(B)
print(nSolution X:)
print(X)
print(nVerification: A @ X =)
print(A @ X)
# 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)
# Use Cholesky to solve AX = B
X = torch.cholesky_solve(B, L)
print(Matrix A:)
print(A)
print(nRight-hand side vector B:)
print(B)
print(nSolution X:)
print(X)
print(nVerification: A @ X =)
print(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)
Other Extensions