bundles / scipy 1.17.1 / scipy / linalg / _decomp / eigh
function
scipy.linalg._decomp:eigh
source: /scipy/linalg/_decomp.py :284
Signature
def 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 ) Summary
Solve a standard or generalized eigenvalue problem for a complex Hermitian or real symmetric matrix.
Extended Summary
Find eigenvalues array w and optionally eigenvectors array v of array a, where b is positive definite such that for every eigenvalue λ (i-th entry of w) and its eigenvector vi (i-th column of v) 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.
The documentation is written assuming array arguments are of specified "core" shapes. However, array argument(s) of this function may have additional "batch" dimensions prepended to the core shape. In this case, the array is treated as a batch of lower-dimensional slices; see linalg_batch for details. Note that calls with zero-size batches are unsupported and will raise a ValueError.
Parameters
a: (M, M) array_likeA complex Hermitian or real symmetric matrix whose eigenvalues and eigenvectors will be computed.
b: (M, M) array_like, optionalA complex Hermitian or real symmetric definite positive matrix in. If omitted, identity matrix is assumed.
lower: bool, optionalWhether the pertinent array data is taken from the lower or upper triangle of
aand, if applicable,b. (Default: lower)eigvals_only: bool, optionalWhether to calculate only eigenvalues and no eigenvectors. (Default: both are calculated)
subset_by_index: iterable, optionalIf 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_value: iterable, optionalIf 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.inffor the unconstrained ends.driver: str, optionalDefines 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.
type: int, optionalFor the generalized problems, this keyword specifies the problem type to be solved for
wandv(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_a: bool, optionalWhether to overwrite data in
a(may improve performance). Default is False.overwrite_b: bool, optionalWhether to overwrite data in
b(may improve performance). Default is False.check_finite: bool, optionalWhether 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,) ndarrayThe N (N<=M) selected eigenvalues, in ascending order, each repeated according to its multiplicity.
v: (M, N) ndarrayThe normalized eigenvector corresponding to the eigenvalue
w[i]is the columnv[:,i]. Only returned ifeigvals_only=False.
Raises
: LinAlgErrorIf 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.
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 and he 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)))✓
w = eigh(A, eigvals_only=True)
✓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])
✗w, v = eigh(A, subset_by_index=[1, 1])
✓w
✗v.shape # only a single column is returned
✓See also
- eig
eigenvalues and right eigenvectors for non-symmetric arrays
- eigh_tridiagonal
eigenvalues and right eigenvectors for symmetric/Hermitian tridiagonal matrices
- eigvalsh
eigenvalues of symmetric or Hermitian arrays
Aliases
-
scipy.linalg.eigh