import os
os.system('jupyter nbconvert --to html yourNotebook.ipynb')
import numpy as np
from tabulate import tabulate
import matplotlib.pyplot as plt
import os
def print_log(idx, v_list, lambda_list, diff_list):
info_list = [[i, v_list[i], lambda_list[i], diff_list[i]] for i in range(idx)]
print(tabulate(info_list, headers=["iteration","eigenvector", "eigenvalue","lambda_diff"]))
Power Iteration is utilizing the convergence of the sequence
For any matrix $A$ with a unique dominant eigenvalue, the sequence would converge to $A$'s eigenvector corresponding to its dominant eigenvalue.
To calculate an estimated natural eigenvalue for a give vector, we use Rayleigh Quotient to measure the value making this vector like an eigenvector the most.
$$ r(x) = \frac{x^T A x}{x^T x} $$def PowerMethod(A, convergence_condition=0.00001):
idx = 0
r, c = A.shape
if r != c:
raise Exception("not a square matrix")
# initialize eigenvectors
v = np.zeros(r)
v[-1] = 1
# initialize eigenvalues
lam = v.dot(A.dot(v))
while True:
idx = idx + 1
# new vector
v_new = A.dot(v)
v_new = v_new / np.linalg.norm(v_new)
lam_new = v_new.dot(A.dot(v_new))
if np.abs(lam_new - lam) < convergence_condition:
break
lam = lam_new
v = v_new
return v_new, lam_new, idx
if __name__ == '__main__':
A = np.array([[1.5,.5],[.5,1.5]])
PowerMethod(A)