PyTorch torch.solve function
PyTorch torch reference manual
torch.solve is a function in PyTorch used to solve systems of linear equations. It solves AX = B, where A is the coefficient matrix and B is the right-hand side matrix or vector.
Function definition
torch.solve(B, A, out=None)
Parameters:
B(Tensor): Right-hand-side matrix or vector.A(Tensor): Coefficient matrix.out(tuple, optional): Output tuple.
Return Value:
tuple: Returns a tuple of (X, LU), where X is the solution and LU is the factorization.
Usage examples
Example
import
torch
Create the coefficient matrix and the right-hand-side vector
A
=
torch.
tensor
(
[
[
1.0
,
2.0
]
,
[
3.0
,
4.0
]
]
)
B
=
torch.
tensor
(
[
5.0
,
11.0
]
)
Solve AX = B
X
,
_
=
torch.
solve
(
B
,
A
)
(
"Coefficient 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.]])
右侧向量 B:
tensor([ 5., 11.])
解 X:
tensor([1., 2.])
验证: A @ X =
tensor([ 5., 11.])