|
| 1 | +import numpy as np |
| 2 | + |
| 3 | + |
| 4 | +def power_iteration( |
| 5 | + input_matrix: np.array, vector: np.array, error_tol=1e-12, max_iterations=100 |
| 6 | +) -> [float, np.array]: |
| 7 | + """ |
| 8 | + Power Iteration. |
| 9 | + Find the largest eignevalue and corresponding eigenvector |
| 10 | + of matrix input_matrix given a random vector in the same space. |
| 11 | + Will work so long as vector has component of largest eigenvector. |
| 12 | + input_matrix must be symmetric. |
| 13 | +
|
| 14 | + Input |
| 15 | + input_matrix: input matrix whose largest eigenvalue we will find. |
| 16 | + Numpy array. np.shape(input_matrix) == (N,N). |
| 17 | + vector: random initial vector in same space as matrix. |
| 18 | + Numpy array. np.shape(vector) == (N,) or (N,1) |
| 19 | +
|
| 20 | + Output |
| 21 | + largest_eigenvalue: largest eigenvalue of the matrix input_matrix. |
| 22 | + Float. Scalar. |
| 23 | + largest_eigenvector: eigenvector corresponding to largest_eigenvalue. |
| 24 | + Numpy array. np.shape(largest_eigenvector) == (N,) or (N,1). |
| 25 | +
|
| 26 | + >>> import numpy as np |
| 27 | + >>> input_matrix = np.array([ |
| 28 | + ... [41, 4, 20], |
| 29 | + ... [ 4, 26, 30], |
| 30 | + ... [20, 30, 50] |
| 31 | + ... ]) |
| 32 | + >>> vector = np.array([41,4,20]) |
| 33 | + >>> power_iteration(input_matrix,vector) |
| 34 | + (79.66086378788381, array([0.44472726, 0.46209842, 0.76725662])) |
| 35 | + """ |
| 36 | + |
| 37 | + # Ensure matrix is square. |
| 38 | + assert np.shape(input_matrix)[0] == np.shape(input_matrix)[1] |
| 39 | + # Ensure proper dimensionality. |
| 40 | + assert np.shape(input_matrix)[0] == np.shape(vector)[0] |
| 41 | + |
| 42 | + # Set convergence to False. Will define convergence when we exceed max_iterations |
| 43 | + # or when we have small changes from one iteration to next. |
| 44 | + |
| 45 | + convergence = False |
| 46 | + lamda_previous = 0 |
| 47 | + iterations = 0 |
| 48 | + error = 1e12 |
| 49 | + |
| 50 | + while not convergence: |
| 51 | + # Multiple matrix by the vector. |
| 52 | + w = np.dot(input_matrix, vector) |
| 53 | + # Normalize the resulting output vector. |
| 54 | + vector = w / np.linalg.norm(w) |
| 55 | + # Find rayleigh quotient |
| 56 | + # (faster than usual b/c we know vector is normalized already) |
| 57 | + lamda = np.dot(vector.T, np.dot(input_matrix, vector)) |
| 58 | + |
| 59 | + # Check convergence. |
| 60 | + error = np.abs(lamda - lamda_previous) / lamda |
| 61 | + iterations += 1 |
| 62 | + |
| 63 | + if error <= error_tol or iterations >= max_iterations: |
| 64 | + convergence = True |
| 65 | + |
| 66 | + lamda_previous = lamda |
| 67 | + |
| 68 | + return lamda, vector |
| 69 | + |
| 70 | + |
| 71 | +def test_power_iteration() -> None: |
| 72 | + """ |
| 73 | + >>> test_power_iteration() # self running tests |
| 74 | + """ |
| 75 | + # Our implementation. |
| 76 | + input_matrix = np.array([[41, 4, 20], [4, 26, 30], [20, 30, 50]]) |
| 77 | + vector = np.array([41, 4, 20]) |
| 78 | + eigen_value, eigen_vector = power_iteration(input_matrix, vector) |
| 79 | + |
| 80 | + # Numpy implementation. |
| 81 | + |
| 82 | + # Get eigen values and eigen vectors using built in numpy |
| 83 | + # eigh (eigh used for symmetric or hermetian matrices). |
| 84 | + eigen_values, eigen_vectors = np.linalg.eigh(input_matrix) |
| 85 | + # Last eigen value is the maximum one. |
| 86 | + eigen_value_max = eigen_values[-1] |
| 87 | + # Last column in this matrix is eigen vector corresponding to largest eigen value. |
| 88 | + eigen_vector_max = eigen_vectors[:, -1] |
| 89 | + |
| 90 | + # Check our implementation and numpy gives close answers. |
| 91 | + assert np.abs(eigen_value - eigen_value_max) <= 1e-6 |
| 92 | + # Take absolute values element wise of each eigenvector. |
| 93 | + # as they are only unique to a minus sign. |
| 94 | + assert np.linalg.norm(np.abs(eigen_vector) - np.abs(eigen_vector_max)) <= 1e-6 |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + import doctest |
| 99 | + |
| 100 | + doctest.testmod() |
| 101 | + test_power_iteration() |
0 commit comments