PyTorch torch.linalg.solve function
PyTorch torch reference manual
torch.linalg.solve is a function in PyTorch's linear algebra module used to solve systems of linear equations. It solves AX = B.
Function definition
torch.linalg.solve(A, B, left=True, out=None)
Parameters:
A(Tensor): Coefficient matrix.B(Tensor): Right-hand-side matrix or vector.left(bool, optional): If True, solve AX = B; otherwise solve XA = B. Defaults to True.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 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.
linalg
.
solve
(
A
,
B
)
(
"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.])