-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpower_iteration.py
More file actions
62 lines (45 loc) · 2.16 KB
/
Copy pathpower_iteration.py
File metadata and controls
62 lines (45 loc) · 2.16 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import numpy as np
def power_iteration(matrix, num_iterations=1000, epsilon=1e-8):
"""
Power Iteration Algorithm to compute the dominant eigenvalue and eigenvector of a matrix.
Args:
matrix (np.ndarray): The input matrix.
num_iterations (int): The number of iterations for the algorithm (default is 1000).
epsilon (float): The convergence threshold for the eigenvalue (default is 1e-8).
Returns:
float: The dominant eigenvalue.
np.ndarray: The eigenvector corresponding to the dominant eigenvalue.
"""
# Get the dimensions of the input matrix
n, m = matrix.shape
# Initialize a random vector as the starting vector for power iteration
initial_vector = np.random.rand(m)
# Normalize the initial vector
initial_vector = initial_vector / np.linalg.norm(initial_vector)
# Initialize the previous eigenvalue
prev_eigenvalue = 0
# Perform power iteration
for i in range(num_iterations):
# Update the vector by multiplying with the matrix
updated_vector = np.dot(matrix, initial_vector)
# Compute the eigenvalue by taking the dot product of updated vector and initial vector
eigenvalue = np.dot(updated_vector, initial_vector)
# Normalize the updated vector
updated_vector = updated_vector / np.linalg.norm(updated_vector)
# Check for convergence
if np.abs(eigenvalue - prev_eigenvalue) < epsilon:
break
# Update the initial vector and eigenvalue for the next iteration
initial_vector = updated_vector
prev_eigenvalue = eigenvalue
# Compute the eigenvector corresponding to the dominant eigenvalue
eigenvector = initial_vector
return eigenvalue, eigenvector
# Example usage:
# # Define the input matrix
# matrix = np.array([[4, 2], [1, 3]])
# # Call the power_iteration function to compute the eigenvalue and eigenvector
# eigenvalue, eigenvector = power_iteration(matrix)
# # Print the results
# print("Dominant eigenvalue:", eigenvalue)
# print("Eigenvector corresponding to the dominant eigenvalue:", eigenvector)