我要评分
获取效率
正确性
完整性
易理解

linalg.eigh

Description

Return the eigenvalues and eigenvectors of a complex Hermitian (conjugate symmetric) or a real symmetric matrix.

It returns two objects, a 1D array containing the eigenvalues of a, and a 2D square array or matrix (depending on the input type) of the corresponding eigenvectors (in columns).

Mandatory Input Parameters

Parameter

Type

Description

a

(…, M, M) array

Hermitian or real symmetric matrices

Optional Input Parameters

Parameter

Type

Default Value

Description

UPLO

{'L', 'U'},

'L'

Specifies whether the calculation is done with the lower triangular part ('L') or the upper triangular part ('U') of a. Irrespective of this value, only the real parts of the diagonal will be considered in the calculation to preserve the notion of a Hermitian matrix. It therefore follows that the imaginary part of the diagonal will always be treated as zero.

Return Value

Parameter

Type

Description

w

(…, M) ndarray

The eigenvalues in ascending order, each repeated according to its multiplicity.

v

{(…, M, M) ndarray, (…, M, M) matrix}

The column v[:, i] is the normalized eigenvector corresponding to the eigenvalue w[i]. A matrix object is returned if a is a matrix object.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
>>> import numpy as np
>>> # Vector input
>>> a = np.array([[1,-2j], [2j, 5]])
>>> a
array([[ 1.+0.j, -0.-2.j],
       [ 0.+2.j,  5.+0.j]])
>>> w, v = np.linalg.eigh(a)
>>> w, v
(array([0.17157288, 5.82842712]), array([[-0.92387953-0.j        , -0.38268343+0.j        ],
       [ 0.        +0.38268343j,  0.        -0.92387953j]]))
>>> 
>>> np.dot(a, v[:, 0]) - w[0] * v[:, 0]
array([5.55111512e-17+0.0000000e+00j, 0.00000000e+00+1.2490009e-16j])
>>> np.dot(a, v[:, 1]) - w[1] * v[:, 1]
array([0.+0.j, 0.+0.j])
>>> 
>>> # Matrix input
>>> A = np.matrix(a)
>>> A
matrix([[ 1.+0.j, -0.-2.j],
        [ 0.+2.j,  5.+0.j]])
>>> w, v = np.linalg.eigh(A)
>>> w; v
array([0.17157288, 5.82842712])
matrix([[-0.92387953-0.j        , -0.38268343+0.j        ],
        [ 0.        +0.38268343j,  0.        -0.92387953j]])
>>> 
>>> # Treatment of the imaginary part of the diagonal
>>> a = np.array([[5+2j, 9-2j], [0+2j, 2-1j]])
>>> a
array([[5.+2.j, 9.-2.j],
       [0.+2.j, 2.-1.j]])
>>> b = np.array([[5.+0.j, 0.-2.j], [0.+2.j, 2.-0.j]])
>>> b
array([[5.+0.j, 0.-2.j],
       [0.+2.j, 2.+0.j]])
>>> 
>>> np.linalg.eigh(a)
(array([1., 6.]), array([[-0.4472136 -0.j        , -0.89442719+0.j        ],
       [ 0.        +0.89442719j,  0.        -0.4472136j ]]))
>>> 
>>> np.linalg.eigh(b)
(array([1., 6.]), array([[-0.4472136 -0.j        , -0.89442719+0.j        ],
       [ 0.        +0.89442719j,  0.        -0.4472136j ]]))
>>>