[Numerical calculation methods (Huang Mingyou)] Calculation of matrix eigenvalues and eigenvectors (2): Jacobi clearance method (improvement of Jacobi rotation method) [Theory to program]

  eigenvalues ​​of the matrix (eigenvalue) and eigenvectors (eigenvector) has important mathematical and physical significance in many applications. Jacobi rotation method is an iterative method for calculating eigenvalues ​​and eigenvectors of symmetric matrices, Jacobi pass method is an improved version of the Jacobi rotation method. Its main purpose is to reduce calculation work and increase running speed.

  This article will introduce in detail the basic principles and steps of the Jacobi clearance method, and give its Python implementation.

1. Jacobi rotation method

  In each iteration of the Jacobi rotation method, it is necessary to select a position with the largest non-diagonal element, then construct the corresponding rotation matrix, and perform a similar transformation to gradually diagonalize the matrix.

  • A symmetric matrix is ​​a real matrix whose transpose is equal to itself.
  • 对于一个方阵 A A A, the amount of existence as a result λ λ λ Sum nonzero direction quantity v v v,使得 A v = λ v Av = λv Av=λv,那么 λ λ λ is A A A's special expedition, v v v 对应于 λ λ λ's special direction.

1. Basic idea

  The basic idea of ​​the Jacobi rotation method is to gradually diagonalize the symmetric matrix through a series of similar transformations, so that the non-diagonal elements tend to zero. During this process, the eigenvalues ​​gradually emerge on the diagonal, and the corresponding eigenvectors are gradually found. The following are the basic steps of the Jacobi rotation method:

  1. Select the rotation angle: Choose a rotation angle θ, usually so that the non-diagonal elements in the rotation matrix are zero, thereby achieving diagonalization, usually choose< a i=2>The one with the largest absolute value among the non-diagonal elements is used as the target of rotation.

  2. Construct a rotation matrix: Construct a rotation matrix J, which is an identity matrix. There are only two non-zero elements at the positions corresponding to the selected non-diagonal elements, and the rest position is zero. The values ​​of these two non-zero elements are determined by the rotation angle θ. For example, for a 2x2 matrix, the rotation matrix can be expressed as:
    J = [ cos ⁡ ( θ ) − sin ⁡ ( θ ) sin ⁡ ( θ ) cos ⁡ ( θ ) ] J = \begin{bmatrix} \cos(\theta) & -\sin(\theta) \\ \sin(\theta) & \cos(\theta) \end {bmatrix} J=[cos(θ)sin(θ)sin(θ)cos(θ)]

  3. Similar transformation: Calculate the similarity transformation matrix P P P,即 P T A P P^TAP PTAP, inside A A A This is the original square, P P P is a rotation matrix, and the calculation process is as follows:

P T A P = [ cos ⁡ ( θ ) sin ⁡ ( θ ) − sin ⁡ ( θ ) cos ⁡ ( θ ) ] T [ a 11 to 12 a 12 to 22 ] [ cos ⁡ ( θ ) − sin ⁡ ( θ ) sin ⁡ ( θ ) cos ⁡ ( θ ) ] P^TAP = \begin{bmatrix} \cos(\theta) & \sin(\theta) \\ -\sin(\theta) & \cos(\theta) \end{bmatrix}^T \begin{bmatrix} a_{11} & a_{12} \\ a_{12} & a_{22} \end{bmatrix} \begin{bmatrix} \cos(\theta) & -\sin(\theta) \\ \sin(\theta) & \cos(\theta)\end{bmatrix}PTAP=[cos(θ)sin(θ)sin(θ)cos(θ)]T[a11a12a12a22][cos(θ)sin(θ)sin(θ)cos(θ)]

  By matrix multiplication calculation, we can get P T A P P^TAP PNon-diagonal elements in TAP, assuming that these two elements are located in the matrix The positions of (1,2) and (2,1). Let a i j a_{ij} aij Both elements, so a i j = a 12 = a 21 a_{ij}= a_{12} = a_{21} aij=a12=a21

  Next, we hope that by choosing the appropriate θ \theta θuse a i j a_{ij} aij becomes zero, thereby achieving the purpose of diagonalization, that is, a 12 = a 21 a_{12} = a_{21} a12=a21, it can be further deduced

θ = 1 2 arctan ⁡ ( 2 ⋅ a i j a i i − a j j ) \theta = \frac{1}{2} \arctan\left(\frac{2 \cdot a_{ij}}{a_{ii} - a_{jj}}\right) i=21arctan(aiiajj2aij)

  • a i i = a j j a_{ii}=a_{jj} aii=ajj, use a r c c o t arccot ​​arccotin
  1. Iteration: Repeat steps 1-3 until the non-diagonal elements of matrix A tend to zero or meet certain accuracy requirements.

  2. Extract eigenvalues ​​and eigenvectors: The elements on the diagonal are the eigenvalues ​​of matrix A, and the column vectors in P are the features corresponding to these eigenvalues vector.

2. Precautions

  The advantage of Jacobi rotation method is that it can be used for symmetric matrices of any size, but its disadvantage is that it requires a large number of iterations and a large amount of calculation. In practical applications, other methods are usually combined to improve computational efficiency.

2. Jacobi clearance method

  Jacobi’s threshold method is an improved version of Jacobi’s rotation method. Its main purpose is to reduce computational work and increase running speed. This method gradually diagonalizes the symmetric matrix by dynamically adjusting the threshold and selectively rotating the non-diagonal elements according to the threshold.

1. Basic idea

  1. Compute the sum of squares of off-diagonal elements: For a symmetric matrix A A A, calculate the sum of squares of its off-diagonal elements, expressed as u ( A ) u(A) u(A). The square root of the square root is obtained r ( A ) = u ( A ) r(A) = \sqrt{u(A)} r(A)=u(A)

  2. Configuration start time θ \theta θ Destination setting one piece first opening θ 0 \theta_0 i0

  3. Draw non-angle element: 对于 a i j a_{ij} aij Inside i ≠ j i \neq j i=j, scan the upper or lower triangular part of the matrix.

  4. Perform selective rotation transformation: For non-diagonal elements whose absolute value is greater than the current threshold $\theta $ a i j a_{ij} < /span>aij, perform a Jacobi rotation transformation and rotate it to zero. The specific steps of rotation transformation are as follows:

    • Select rotation angle ϕ \phi ϕ,interpretation tan ⁡ ( 2 ϕ ) = 2 a i j a i i − a j j \tan(2\phi) = \frac{2a_ {ij}}{a_{ii} - a_{jj}} tan(2ϕ)=aiiajj2aijcalculate.
    • Construct rotation matrix J J J,That was removed J i i J_{ii} Jii J j j J_{jj} JjjAll elements outside are zero, and J i j J_{ij} Jij Sum J j i J_{ji} Jji Infinitesimal cos ⁡ ( ϕ ) \cos(\phi) cos(ϕ) − sin ⁡ ( ϕ ) -\sin(\phi) sin(ϕ)determined.
    • Perform similarity transformation A = J T A J A = J^T A J A=JTAJ
  5. Tuning θ \theta θ: When the absolute values ​​of all non-diagonal elements are less than the current threshold θ \theta θ time, θ i + 1 = γ ⋅ θ i \theta_{i+1} = \gamma \ cdot \theta_i ii+1=cii, inside γ \gamma γ is a reduction factor.

  6. Repeat steps 3-5: Repeat the above steps until a certain convergence condition is met, for example θ k < ϵ \theta_k < \epsilon ik<ϵ,使用 ϵ \epsilon ϵ is a small positive number.

2. Precautions

  By continuously adjusting the threshold and selectively performing rotation transformations, the Jacobi pass method gradually reduces the absolute values ​​of off-diagonal elements to achieve better numerical stability. The advantage of this method is that by intelligently selecting non-diagonal elements for transformation, it can effectively reduce the number of iterations and improve computational efficiency.

3. Python implementation

import numpy as np


def jacobi_threshold_method(A, epsilon=1e-10, gamma=0.9):
    n = A.shape[0]
    theta = np.sqrt(np.sum(np.abs(np.triu(A, k=1)) ** 2))
    eigenvectors = np.eye(n)

    while theta > epsilon:
        for i in range(n):
            for j in range(i + 1, n):
                if np.abs(A[i, j]) > theta:
                    # 计算旋转角度
                    phi = 0.5 * np.arctan2(2 * A[i, j], A[i, i] - A[j, j])

                    # 构造旋转矩阵
                    J = np.eye(n)
                    J[i, i] = J[j, j] = np.cos(phi)
                    J[i, j] = -np.sin(phi)
                    J[j, i] = np.sin(phi)

                    # 执行相似变换
                    A = np.dot(np.dot(J.T, A), J)

                    # 更新特征向量
                    eigenvectors = np.dot(eigenvectors, J)

        # 缩小阈值
        theta *= gamma

    # 提取特征值和特征向量
    eigenvalues = np.diag(A)

    return eigenvalues, eigenvectors


# 示例矩阵
A = np.array([[2, -1, 0], [-1, 2, -1], [0, -1, 2]])

# 执行 Jacobi 过关法
eigenvalues, eigenvectors = jacobi_threshold_method(A)

print("特征值:", eigenvalues)
print("特征向量:")
np.set_printoptions(precision=4, suppress=True)
print(eigenvectors)

Insert image description here

Iterative process (debugging)

  • first:

Insert image description here

Insert image description here

………

  • final:
    Insert image description here

Guess you like

Origin blog.csdn.net/m0_63834988/article/details/134678554