# 幂迭代法求特征值和特征向量

## 数学表述

$A = \left[ \begin{matrix} X_{11}&\ldots&X_{1n} \newline X_{21} & \ldots& X_{2n} \newline &\vdots& \newline X_{n1}&\ldots &X_{nn} \newline \end{matrix} \right]$

$b_{k+1} = \frac{Ab_k}{||Ab_k ||}..........(1)$

import numpy as np
def power_iteration(A, num_simulations: int):
# Ideally choose a random vector
# To decrease the chance that our vector
# Is orthogonal to the eigenvector
b_k = np.random.rand(A.shape[1])

for _ in range(num_simulations):
# calculate the matrix-by-vector product Ab
b_k1 = np.dot(A, b_k)

# calculate the norm
b_k1_norm = np.linalg.norm(b_k1)

# re normalize the vector
b_k = b_k1 / b_k1_norm

return b_k


## 例子

$A = \left[ \begin{matrix} 2&1\newline 1&2\newline \end{matrix} \right]$

$|A-\lambda I| = \left| \begin{matrix} 2-\lambda &1\newline 1 &2-\lambda\newline \end{matrix} \right| = 3 - 4\lambda + \lambda^2$

$$\lambda_2 = 3$$ 时：

$|(A-\lambda I)V| = 0$

$(A-3I)V = \left[ \begin{array} {cccc} -1 &1\newline 1 &-1\newline \end{array} \right] \left[ \begin{array} {cccc} v_1\newline v_2\newline \end{array} \right] = \left[ \begin{array} {cccc} 0\newline 0\newline \end{array} \right]$

A = np.array([[2,1],[1,2]])
V = power_iteration(A, 100).reshape(1,-1)
V


array([[0.70710678, 0.70710678]])

$\lambda = VAV^T........(2)$

lbd = np.dot(V,np.dot(A,V.T))
lbd


array([[3.]])

## 扩展

$B = A - \frac{\lambda}{||V||^2}V^TV ........(3)$

$b_{k+1}' = \frac{Bb_k'}{||Bb_k' ||} ...........(4)$

B = A - lbd / np.linalg.norm(V)**2 * np.dot(V.T,V)
V_2 = power_iteration(B, 100)
lbd1 = np.dot(V_2.T,np.dot(B,V_2))
lbd1


1.0000000000000002

posted @ 2020-01-29 12:49  fahaizhong  阅读(2726)  评论(1编辑  收藏  举报