PyTorch torch.linalg.eig function
PyTorch torch reference manual
torch.linalg.eig is a function in PyTorch's linear algebra module used to compute the eigendecomposition of a square matrix. It returns all eigenvalues and right eigenvectors of the matrix.
Function definition
torch.linalg.eig(A, out=None)
Parameters:
A(Tensor): Input square matrix.out(tuple, optional): Output tuple.
Return Value:
tuple: Returns a tuple of (eigenvalues, eigenvectors).
Usage examples
Example
import
torch
Create a square matrix
A
=
torch.
tensor
(
[
[
1.0
,
2.0
]
,
[
3.0
,
4.0
]
]
,
dtype
=
torch.
complex128
)
Eigendecomposition
eigenvalues
,
eigenvectors
=
torch.
linalg
.
eig
(
A
)
(
"Matrix A:"
)
(
A
)
(
"nEigenvalues:"
)
(
eigenvalues
)
(
"nEigenvectors:"
)
(
eigenvectors
)
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)
Eigenvector:
tensor([[-0.8246+0.j, -0.4159+0.j],
[ 0.5658+0.j, -0.9094+0.j]], dtype=torch.complex128)