torch.linalg.eigvals
function eigvals<S extends Shape, D extends DType, Dev extends DeviceType>(A: Tensor<S, D, Dev>): Tensor<DynamicShape, D, Dev>Computes only the eigenvalues of a square matrix (without eigenvectors).
More efficient than eig() when only eigenvalues are needed. Essential for:
- Stability analysis (checking if |λ| < 1 for discrete dynamical systems)
- Spectral radius computation (largest |λ|)
- Characteristic polynomial evaluation
- Convergence analysis of iterative methods
- Ill-conditioning assessment
- Matrix polynomial evaluation
Note: For general (non-symmetric) matrices, eigenvalues may be complex. Currently, only real eigenvalues are supported; complex eigenvalues from general matrices are not yet fully supported.
Relationship to eig():
- eig() returns both eigenvalues and eigenvectors (O(n³) time)
- eigvals() returns only eigenvalues (still O(n³) but smaller constants)
- eigvalsh() for symmetric matrices (uses more efficient algorithm)
- More efficient than eig(): Skips eigenvector computation
- Order not guaranteed: Eigenvalues order may vary
- Complex possible: General matrices may have complex eigenvalues
- For symmetric use eigvalsh(): More efficient and always real
- Square matrix required: Input must be n × n
- Spectral radius: ||A|| ≥ |λ_max| for any natural matrix norm
- General matrices: Current implementation may not handle general matrices correctly
- Ill-conditioning: Eigenvalues can be very sensitive to perturbations
- Complex eigenvalues: Not fully supported for general non-symmetric matrices
Parameters
ATensor<S, D, Dev>- Square matrix (n × n) or batch (..., n, n)
Returns
Tensor<DynamicShape, D, Dev>– Eigenvalue vector, shape [n] or [..., n]Examples
// Compute eigenvalues only (faster than eig)
const A = torch.tensor([[4.0, 1.0], [1.0, 3.0]]);
const eigenvalues = torch.linalg.eigvals(A);
// eigenvalues ≈ [2, 5] (in some order)
// Stability check for dynamical systems
const A = torch.tensor([[0.95, 0.05], [0.1, 0.9]]);
const eigenvalues = torch.linalg.eigvals(A);
const spectral_radius = eigenvalues.abs().max(); // Largest |λ|
const is_stable = spectral_radius.item() < 1.0;
console.log(is_stable ? 'System is stable' : 'System is unstable');
// Convergence rate of iterative method
const A = torch.randn(10, 10);
const eigenvalues = torch.linalg.eigvals(A);
const convergence_rate = eigenvalues.abs().max();
// Smaller → faster convergence; threshold behavior at 1.0
// Condition number via eigenvalues (for symmetric matrices)
const A = torch.randn(5, 5);
const A_sym = A.add(A.T).mul(0.5); // Symmetrize
const eigs = torch.linalg.eigvalsh(A_sym); // Use eigvalsh for symmetric
const cond = eigs.max().div(eigs.min()); // κ(A) = λ_max / λ_min
// Batched eigenvalues
const A_batch = torch.randn(32, 5, 5);
const eigenvalues = torch.linalg.eigvals(A_batch);
// eigenvalues shape: [32, 5]See Also
- PyTorch torch.linalg.eigvals()
- eig - Eigendecomposition with eigenvectors
- eigvalsh - Eigenvalues of symmetric/Hermitian matrices (preferred)
- eigh - Symmetric eigendecomposition (with eigenvectors)
- svd - Singular values (different from eigenvalues; for rectangular matrices)