PyTorch torch.triangular_solve function
PyTorch torch reference manual
torch.triangular_solve is a function in PyTorch used to solve triangular systems of linear equations. It solves AX = B, where A is a triangular matrix.
Function definition
torch.triangular_solve(A, B, upper, transpose, unitriangular)
Parameter Description
A: The coefficient matrix (must be square)B: The right-hand side matrix or vectorupper: Whether A is upper triangular (defaults to True)transpose: Whether to transpose A (defaults to False)unitriangular: Whether to use a unit triangular matrix (defaults to False)
Usage examples
Example
import
torch
Create an upper triangular coefficient matrix
A
=
torch.
tensor
(
[
[
3.0
,
1.0
,
2.0
]
,
[
0.0
,
2.0
,
1.0
]
,
[
0.0
,
0.0
,
1.0
]
]
)
Right-hand side vector
B
=
torch.
tensor
(
[
9.0
,
5.0
,
2.0
]
)
Solve AX = B
X
=
torch.
triangular_solve
(
B.
unsqueeze
(
1
)
,
A
,
upper
=
True
)
(
"Solution X:"
)
(
X.
solution
)
The output is:
解 X:
tensor([[1.],
[2.],
[2.]])