PyTorch torch.linalg.eigvals function
PyTorch torch reference manual
torch.linalg.eigvals is a function in PyTorch's linear algebra module used to compute the eigenvalues of a square matrix. It returns only the eigenvalues and does not compute eigenvectors, so it is faster than torch.linalg.eig .
Function definition
torch.linalg.eigvals(A, out=None)
Parameters:
A(Tensor): Input square matrix.out(Tensor, optional): the output tensor.
Return Value:
torch.Tensor: Returns the eigenvalues.
Usage examples
Example
import
torch
Create a square matrix
A
=
torch.
tensor
(
[
[
1.0
,
2.0
]
,
[
3.0
,
4.0
]
]
,
dtype
=
torch.
complex128
)
Compute eigenvalues
eigenvalues
=
torch.
linalg
.
eigvals
(
A
)
(
"Matrix A:"
)
(
A
)
(
"nEigenvalues:"
)
(
eigenvalues
)
The output is:
矩阵 A:
tensor([[1., 2.],
[3., 4.]], dtype=torch.complex128)
Eigenvalues:
tensor([-0.3723+0.j, 5.3723+0.j], dtype=torch.complex128)