eigh#
- scipy.linalg.eigh(a, b=None, *, lower=True, eigvals_only=False, overwrite_a=False, overwrite_b=False, type=1, check_finite=True, subset_by_index=None, subset_by_value=None, driver=None)[source]#
Solve a standard or generalized eigenvalue problem for a complex Hermitian or real symmetric matrix.
Find eigenvalues array
w
and optionally eigenvectors arrayv
of arraya
, whereb
is positive definite such that for every eigenvalue λ (i-th entry of w) and its eigenvectorvi
(i-th column ofv
) satisfies:a @ vi = λ * b @ vi vi.conj().T @ a @ vi = λ vi.conj().T @ b @ vi = 1
In the standard problem,
b
is assumed to be the identity matrix.- Parameters:
- a(M, M) array_like
A complex Hermitian or real symmetric matrix whose eigenvalues and eigenvectors will be computed.
- b(M, M) array_like, optional
A complex Hermitian or real symmetric definite positive matrix in. If omitted, identity matrix is assumed.
- lowerbool, optional
Whether the pertinent array data is taken from the lower or upper triangle of
a
and, if applicable,b
. (Default: lower)- eigvals_onlybool, optional
Whether to calculate only eigenvalues and no eigenvectors. (Default: both are calculated)
- subset_by_indexiterable, optional
If provided, this two-element iterable defines the start and the end indices of the desired eigenvalues (ascending order and 0-indexed). To return only the second smallest to fifth smallest eigenvalues,
[1, 4]
is used.[n-3, n-1]
returns the largest three. Only available with “evr”, “evx”, and “gvx” drivers. The entries are directly converted to integers viaint()
.- subset_by_valueiterable, optional
If provided, this two-element iterable defines the half-open interval
(a, b]
that, if any, only the eigenvalues between these values are returned. Only available with “evr”, “evx”, and “gvx” drivers. Usenp.inf
for the unconstrained ends.- driverstr, optional
Defines which LAPACK driver should be used. Valid options are “ev”, “evd”, “evr”, “evx” for standard problems and “gv”, “gvd”, “gvx” for generalized (where b is not None) problems. See the Notes section. The default for standard problems is “evr”. For generalized problems, “gvd” is used for full set, and “gvx” for subset requested cases.
- typeint, optional
For the generalized problems, this keyword specifies the problem type to be solved for
w
andv
(only takes 1, 2, 3 as possible inputs):1 => a @ v = w @ b @ v 2 => a @ b @ v = w @ v 3 => b @ a @ v = w @ v
This keyword is ignored for standard problems.
- overwrite_abool, optional
Whether to overwrite data in
a
(may improve performance). Default is False.- overwrite_bbool, optional
Whether to overwrite data in
b
(may improve performance). Default is False.- check_finitebool, optional
Whether to check that the input matrices contain only finite numbers. Disabling may give a performance gain, but may result in problems (crashes, non-termination) if the inputs do contain infinities or NaNs.
- Returns:
- w(N,) ndarray
The N (N<=M) selected eigenvalues, in ascending order, each repeated according to its multiplicity.
- v(M, N) ndarray
The normalized eigenvector corresponding to the eigenvalue
w[i]
is the columnv[:,i]
. Only returned ifeigvals_only=False
.
- Raises:
- LinAlgError
If eigenvalue computation does not converge, an error occurred, or b matrix is not definite positive. Note that if input matrices are not symmetric or Hermitian, no error will be reported but results will be wrong.
See also
eigvalsh
eigenvalues of symmetric or Hermitian arrays
eig
eigenvalues and right eigenvectors for non-symmetric arrays
eigh_tridiagonal
eigenvalues and right eiegenvectors for symmetric/Hermitian tridiagonal matrices
Notes
This function does not check the input array for being Hermitian/symmetric in order to allow for representing arrays with only their upper/lower triangular parts. Also, note that even though not taken into account, finiteness check applies to the whole array and unaffected by “lower” keyword.
This function uses LAPACK drivers for computations in all possible keyword combinations, prefixed with
sy
if arrays are real andhe
if complex, e.g., a float array with “evr” driver is solved via “syevr”, complex arrays with “gvx” driver problem is solved via “hegvx” etc.As a brief summary, the slowest and the most robust driver is the classical
<sy/he>ev
which uses symmetric QR.<sy/he>evr
is seen as the optimal choice for the most general cases. However, there are certain occasions that<sy/he>evd
computes faster at the expense of more memory usage.<sy/he>evx
, while still being faster than<sy/he>ev
, often performs worse than the rest except when very few eigenvalues are requested for large arrays though there is still no performance guarantee.Note that the underlying LAPACK algorithms are different depending on whether eigvals_only is True or False — thus the eigenvalues may differ depending on whether eigenvectors are requested or not. The difference is generally of the order of machine epsilon times the largest eigenvalue, so is likely only visible for zero or nearly zero eigenvalues.
For the generalized problem, normalization with respect to the given type argument:
type 1 and 3 : v.conj().T @ a @ v = w type 2 : inv(v).conj().T @ a @ inv(v) = w type 1 or 2 : v.conj().T @ b @ v = I type 3 : v.conj().T @ inv(b) @ v = I
Examples
>>> import numpy as np >>> from scipy.linalg import eigh >>> A = np.array([[6, 3, 1, 5], [3, 0, 5, 1], [1, 5, 6, 2], [5, 1, 2, 2]]) >>> w, v = eigh(A) >>> np.allclose(A @ v - v @ np.diag(w), np.zeros((4, 4))) True
Request only the eigenvalues
>>> w = eigh(A, eigvals_only=True)
Request eigenvalues that are less than 10.
>>> A = np.array([[34, -4, -10, -7, 2], ... [-4, 7, 2, 12, 0], ... [-10, 2, 44, 2, -19], ... [-7, 12, 2, 79, -34], ... [2, 0, -19, -34, 29]]) >>> eigh(A, eigvals_only=True, subset_by_value=[-np.inf, 10]) array([6.69199443e-07, 9.11938152e+00])
Request the second smallest eigenvalue and its eigenvector
>>> w, v = eigh(A, subset_by_index=[1, 1]) >>> w array([9.11938152]) >>> v.shape # only a single column is returned (5, 1)