October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

How to Compute Eigenvalues in Python with SciPy

Choose the right SciPy eigenvalue function for a general dense matrix, a symmetric or Hermitian matrix, or a sparse problem where you need only a few eigenpairs.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a general dense matrix, use scipy.linalg.eigvals to get eigenvalues only, or scipy.linalg.eig if you also need eigenvectors. For a real symmetric or complex Hermitian matrix, use scipy.linalg.eigvalsh or scipy.linalg.eigh. If a sparse symmetric or Hermitian matrix has many dimensions and you need only a few eigenpairs, consider scipy.sparse.linalg.eigsh.

Choose the SciPy routine that matches your matrix

The main decision is whether the matrix has a structure-specific property and whether you need eigenvectors. SciPy’s linear algebra reference lists these and other specialized solvers.

As an Amazon Associate I earn from qualifying purchases.

Task Routine Key behavior
Eigenvalues of a general dense matrix scipy.linalg.eigvals(a) Eigenvalue 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 symmetry or Hermitian structure; returns values in ascending order.
Eigenvalues and eigenvectors of a dense real-symmetric or complex-Hermitian matrix scipy.linalg.eigh(a) Assumes symmetry or Hermitian structure; can select part of the spectrum.
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.

Compute eigenvalues of a general dense matrix

Use eigvals when the matrix may be nonsymmetric or non-Hermitian and you do not need eigenvectors. Use eig when you need eigenvectors as well. These general routines support ordinary and generalized eigenvalue problems; SciPy describes eigvals as computing eigenvalues from either kind of problem.

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, eig

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

values = eigvals(A)
values, right_vectors = eig(A)

The returned eigenvalue order from eigvals is not specified, so sort the values yourself if your application needs a particular ordering. eig returns right eigenvectors by default; it can also return left eigenvectors. The routine normalizes right eigenvectors, while its documentation says left eigenvectors are not normalized.

For generalized problems, eigvals(a, b) computes eigenvalues for the pair of matrices. Its homogeneous_eigvals=True option returns numerator/denominator coordinates rather than direct ratios, which the API identifies as useful when a ratio could overflow or underflow.

Use the symmetric or Hermitian routines only when the structure is valid

A real symmetric matrix equals its transpose; a complex Hermitian matrix equals its conjugate transpose. For these matrices, eigvalsh returns eigenvalues only, while eigh returns eigenvalues and eigenvectors by default. The eigvalsh reference describes it as the eigenvalue-only shorthand for eigh(..., eigvals_only=True).

from scipy.linalg import eigvalsh, eigh

# A must actually be symmetric (or Hermitian for complex data).
values = eigvalsh(A)
values, vectors = eigh(A)

These routines assume the required structure; they do not verify that the input satisfies it. If the matrix is not symmetric or Hermitian, they can produce incorrect results without raising an error. Choose eig or eigvals when that assumption is not justified. The symmetric/Hermitian eigenvalues are returned in ascending order.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

Generalized symmetric/Hermitian problems

eigh(a, b) solves the default generalized problem a @ v = w * b @ v. For this routine, b must be positive definite. Its type parameter selects among three supported formulations. The API also supports selecting a subset of eigenvalues by index or value; for a generalized subset problem, its documented compatible driver is gvx.

Get a few eigenpairs from a sparse matrix

For a sparse real-symmetric or complex-Hermitian matrix, scipy.sparse.linalg.eigsh is intended for finding a limited number of eigenvalues and eigenvectors rather than computing the full spectrum. Set k to the number of eigenpairs requested; it must be less than the matrix dimension N. Consult the eigsh API reference for its iterative options and parameter details.

There is a documented nuance for complex Hermitian input: eigsh routes the computation through eigs and returns the real parts of the resulting eigenvalues. Do not treat this sparse routine as interchangeable with the dense solvers or assume it computes all eigenvalues.

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

Handle input checks and convergence deliberately

The dense routines expose check_finite. Setting it to False may improve performance, but SciPy warns that NaN or infinity in the inputs can then lead to crashes or non-termination. Leave the check enabled unless the data has already been validated and the trade-off is acceptable.

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

Eigenvalue calculations can fail to converge; the dense API documents LinAlgError for such failures, and sparse iterative solvers have their own algorithmic parameters and behavior. No routine guarantees convergence for every input. If a calculation fails, inspect the matrix and the chosen method rather than assuming a different function name alone will resolve it.

The dense eig, eigvals, eigh, and eigvalsh references also describe support for arrays with leading batch dimensions. The eigh and eigvalsh references note that zero-size batches are unsupported.

When a specialized solver is a better fit

If your matrix is banded or tridiagonal, SciPy’s linear algebra index lists specialized eigenvalue solvers for those structures. Use them when the matrix genuinely has that form; for an ordinary dense general matrix, use the general routines above.

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.

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

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.