Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

For a dense matrix, use scipy.linalg.eigvals to get eigenvalues only, or scipy.linalg.eig when you also need eigenvectors. If the matrix is real symmetric or complex Hermitian, use eigvalsh or eigh instead. For a sparse symmetric or Hermitian matrix when you need only a limited number of eigenpairs, use scipy.sparse.linalg.eigsh.

Choose the SciPy eigenvalue function that fits your matrix

Need Function Important behavior
Eigenvalues of a general dense matrix scipy.linalg.eigvals(a) Supports ordinary and generalized eigenvalue problems. Output order is unspecified.
Eigenvalues and eigenvectors of a general dense matrix scipy.linalg.eig(a) Returns right eigenvectors by default; left eigenvectors are optional.
Eigenvalues of a dense real-symmetric or complex-Hermitian matrix scipy.linalg.eigvalsh(a) Assumes the matrix has that structure and returns values in ascending order.
Eigenvalues and eigenvectors of a dense real-symmetric or complex-Hermitian matrix scipy.linalg.eigh(a) Assumes the matrix has that structure; also supports generalized problems with a positive-definite second matrix.
A limited number of eigenpairs from a sparse symmetric or Hermitian matrix scipy.sparse.linalg.eigsh(A, k=...) Requires k < N, where N is the matrix dimension.

These distinctions follow the SciPy v1.18.0 linear algebra reference and the individual API pages for eigvals, eig, eigvalsh, eigh, and eigsh.

Use the general dense routines for matrices without guaranteed symmetry

Eigenvalues only: eigvals

Call scipy.linalg.eigvals(a) when the matrix may be nonsymmetric or non-Hermitian and you do not need eigenvectors. SciPy describes this function as computing eigenvalues from an ordinary or generalized eigenvalue problem. For example:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import numpy as np
from scipy.linalg import eigvals

A = np.array([[1.0, 2.0],
              [3.0, 4.0]])
values = eigvals(A)
print(values)

Do not rely on the returned order: the general eigvals documentation does not specify an ordering. If an application needs a particular order, sort explicitly, taking care to define how complex values should be ordered.

Eigenvalues and eigenvectors: eig

Use scipy.linalg.eig(a) when you need eigenvectors as well as eigenvalues. It returns the eigenvalues and right eigenvectors by default. The columns of the returned right-eigenvector array correspond to the eigenvalues. The routine can also return left eigenvectors; SciPy documents right eigenvectors as normalized and left eigenvectors as not normalized.

Generalized problems with a and b

The general dense functions also accept a second matrix b for a generalized eigenvalue problem. In addition, eigvals offers homogeneous_eigvals=True, which returns numerator-and-denominator coordinates rather than directly forming ratios. SciPy identifies this form as useful when a direct ratio could overflow or underflow. See the API documentation for eigvals for the return shape and details.

Use eigh or eigvalsh only when the matrix structure is valid

For a real symmetric or complex Hermitian matrix, the structure-specific routines are the appropriate choices: eigvalsh returns eigenvalues alone, while eigh also returns eigenvectors. They exploit the documented structure assumption, but do not check that your input actually satisfies it. If the matrix is not symmetric or Hermitian, results can be wrong without an error. Do not use these functions merely because the matrix is expected to be close to symmetric.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Eigenvalues alone

scipy.linalg.eigvalsh(a) is the eigenvalue-only shorthand for eigh(a, eigvals_only=True). Its results are specified in ascending order, including when selecting a subset of the spectrum. The SciPy eigvalsh reference documents its structure assumption and options.

Eigenvectors or a selected part of the spectrum

Use scipy.linalg.eigh(a) when eigenvectors are needed. It also supports selecting eigenvalues by index or value with compatible LAPACK drivers. This can be useful when a dense matrix is manageable but you need only part of its spectrum.

Generalized Hermitian problem

For eigh(a, b), the default problem is a @ v = w * b @ v, and b must be positive definite. The type parameter selects among three generalized formulations. When requesting a subset for the generalized case, the documented driver is gvx. These constraints differ from the general eig/eigvals generalized interfaces. Consult the SciPy eigh reference for compatible options.

For sparse matrices, use an iterative solver for a partial spectrum

scipy.sparse.linalg.eigsh(A, k=...) is intended for a limited number of eigenpairs of a real symmetric or complex Hermitian sparse matrix or operator. The requested count must satisfy k < N. Its API includes controls for the iterative computation; consult the SciPy eigsh reference for selection and convergence-related parameters.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

There is a specific caveat for complex Hermitian input: SciPy routes eigsh through eigs and returns the real parts of the resulting eigenvalues. Do not assume the sparse routine behaves identically to the dense Hermitian routines in every detail.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Handle numerical and input risks deliberately

  • Check the matrix assumptions. Use a general routine unless symmetry or Hermitian structure is established; eigh and eigvalsh do not validate that assumption.
  • Expect convergence errors to be possible. The dense API references document LinAlgError if computation fails to converge. Sparse iteration has additional algorithmic parameters and behavior; no routine should be treated as guaranteeing convergence for every input.
  • Use check_finite=False only when inputs are known to be finite. The dense API documentation warns that disabling finite checks may improve performance, but NaN or infinity inputs can lead to crashes or non-termination.
  • Account for batching where applicable. The dense eig/eigvals and eigh/eigvalsh APIs document leading batch dimensions. The latter references note that zero-size batches are unsupported.

Further learning

For a broader treatment of SciPy linear algebra and related scientific-computing workflows, Packt describes a chapter on eigenvalues and eigenvectors in Learning SciPy for Numerical and Scientific Computing. The official function references remain the source for current API details.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.