numpy.linalg.eigh#
- linalg.eigh(a,UPLO='L')[source]#
Return the eigenvalues and eigenvectors of a complex Hermitian(conjugate symmetric) or a real symmetric matrix.
Returns two objects, a 1-D array containing the eigenvalues ofa, anda 2-D square array or matrix (depending on the input type) of thecorresponding eigenvectors (in columns).
- Parameters:
- a(…, M, M) array
Hermitian or real symmetric matrices whose eigenvalues andeigenvectors are to be computed.
- UPLO{‘L’, ‘U’}, optional
Specifies whether the calculation is done with the lower triangularpart ofa (‘L’, default) or the upper triangular part (‘U’).Irrespective of this value only the real parts of the diagonal willbe considered in the computation to preserve the notion of a Hermitianmatrix. It therefore follows that the imaginary part of the diagonalwill always be treated as zero.
- Returns:
- A namedtuple with the following attributes:
- eigenvalues(…, M) ndarray
The eigenvalues in ascending order, each repeated according toits multiplicity.
- eigenvectors{(…, M, M) ndarray, (…, M, M) matrix}
The column
eigenvectors[:,i]is the normalized eigenvectorcorresponding to the eigenvalueeigenvalues[i]. Will return amatrix object ifa is a matrix object.
- Raises:
- LinAlgError
If the eigenvalue computation does not converge.
See also
eigvalsheigenvalues of real symmetric or complex Hermitian (conjugate symmetric) arrays.
eigeigenvalues and right eigenvectors for non-symmetric arrays.
eigvalseigenvalues of non-symmetric arrays.
scipy.linalg.eighSimilar function in SciPy (but also solves the generalized eigenvalue problem).
Notes
Broadcasting rules apply, see the
numpy.linalgdocumentation fordetails.The eigenvalues/eigenvectors are computed using LAPACK routines
_syevd,_heevd.The eigenvalues of real symmetric or complex Hermitian matrices are alwaysreal.[1] The arrayeigenvalues of (column) eigenvectors is unitary anda,eigenvalues, andeigenvectors satisfy the equations
dot(a,eigenvectors[:,i])=eigenvalues[i]*eigenvectors[:,i].References
[1]G. Strang,Linear Algebra and Its Applications, 2nd Ed., Orlando,FL, Academic Press, Inc., 1980, pg. 222.
Examples
>>>importnumpyasnp>>>fromnumpyimportlinalgasLA>>>a=np.array([[1,-2j],[2j,5]])>>>aarray([[ 1.+0.j, -0.-2.j], [ 0.+2.j, 5.+0.j]])>>>eigenvalues,eigenvectors=LA.eigh(a)>>>eigenvaluesarray([0.17157288, 5.82842712])>>>eigenvectorsarray([[-0.92387953+0.j , -0.38268343+0.j ], # may vary [ 0. +0.38268343j, 0. -0.92387953j]])
>>>(np.dot(a,eigenvectors[:,0])-...eigenvalues[0]*eigenvectors[:,0])# verify 1st eigenval/vec pairarray([5.55111512e-17+0.0000000e+00j, 0.00000000e+00+1.2490009e-16j])>>>(np.dot(a,eigenvectors[:,1])-...eigenvalues[1]*eigenvectors[:,1])# verify 2nd eigenval/vec pairarray([0.+0.j, 0.+0.j])
>>>A=np.matrix(a)# what happens if input is a matrix object>>>Amatrix([[ 1.+0.j, -0.-2.j], [ 0.+2.j, 5.+0.j]])>>>eigenvalues,eigenvectors=LA.eigh(A)>>>eigenvaluesarray([0.17157288, 5.82842712])>>>eigenvectorsmatrix([[-0.92387953+0.j , -0.38268343+0.j ], # may vary [ 0. +0.38268343j, 0. -0.92387953j]])
>>># demonstrate the treatment of the imaginary part of the diagonal>>>a=np.array([[5+2j,9-2j],[0+2j,2-1j]])>>>aarray([[5.+2.j, 9.-2.j], [0.+2.j, 2.-1.j]])>>># with UPLO='L' this is numerically equivalent to using LA.eig() with:>>>b=np.array([[5.+0.j,0.-2.j],[0.+2.j,2.-0.j]])>>>barray([[5.+0.j, 0.-2.j], [0.+2.j, 2.+0.j]])>>>wa,va=LA.eigh(a)>>>wb,vb=LA.eig(b)>>>waarray([1., 6.])>>>wbarray([6.+0.j, 1.+0.j])>>>vaarray([[-0.4472136 +0.j , -0.89442719+0.j ], # may vary [ 0. +0.89442719j, 0. -0.4472136j ]])>>>vbarray([[ 0.89442719+0.j , -0. +0.4472136j], [-0. +0.4472136j, 0.89442719+0.j ]])