-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPower_Method.py
39 lines (28 loc) · 1.05 KB
/
Power_Method.py
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
import numpy as np
def power(A_list, x0_list, max_iter):
A = np.array(A_list)
x0 = np.array(x0_list)
x = np.array(x0)
tol = 0.0001
for i in range(max_iter):
# matrix multiplication
y = np.dot(A, x)
# normalization and finding eiganvalue
eigenvalue = np.linalg.norm(y, np.inf)
x = y / eigenvalue
# checking the convergence criteria
if np.linalg.norm(np.dot(A, x) - (eigenvalue * x), np.inf) < tol:
break
return eigenvalue, x
m = int(input('Enter the number of rows or columns in matrix : '))
A_list = [0]*m
for i in range(len(A_list)):
A_list[i] = list(
map(float, input(f"Enter the numbers in the row--{i+1} : ").split(" ")))
x0_list = [0]*m
x0_list = list(
map(float, input('Enter the initial guess for the eiganvector : ').split(" ")))
max_iteration = int(input('How many iterations do you want : '))
ouput = power(A_list, x0_list, max_iteration)
print(
f'\nSo, the largest eiganvalue is {ouput[0]} and the corresponding eiganvector is {ouput[1]}')