PyTorch torch.linalg.eigh function
PyTorch torch reference manual
torch.linalg.eigh is a function in PyTorch's linear algebra module used to compute the eigendecomposition of a Hermitian matrix (symmetric or complex conjugate matrix). Compared with ordinary eigendecomposition, it is more efficient and numerically more stable.
Function definition
torch.linalg.eigh(A, UPLO='L', out=None)
Parameters:
A(Tensor): Input Hermitian matrix.UPLO(str, optional): 'L' means lower triangle, 'U' means upper triangle. Defaults to 'L'.out(tuple, optional): Output tuple.
Return Value:
tuple: Returns a tuple of (eigenvalues, eigenvectors).
Usage examples
Example
import
torch
Create a symmetric matrix
A
=
torch.
tensor
(
[
[
2.0
,
1.0
]
,
[
1.0
,
2.0
]
]
)
Hermitian eigendecomposition
eigenvalues
,
eigenvectors
=
torch.
linalg
.
eigh
(
A
)
(
"Matrix A:"
)
(
A
)
(
"nEigenvalues:"
)
(
eigenvalues
)
(
"nEigenvectors:"
)
(
eigenvectors
)
The output is:
矩阵 A:
tensor([[2., 1.],
[1., 2.]])
Eigenvalues:
tensor([1., 3.])
Eigenvector:
tensor([[-0.7071, 0.7071],
[ 0.7071, 0.7071]])