-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPower Method.py
More file actions
105 lines (92 loc) · 2.33 KB
/
Power Method.py
File metadata and controls
105 lines (92 loc) · 2.33 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 1 09:49:17 2019
@author: shakil
"""
import math as m
import numpy as np
def calculate_norm(X, norm):
if(norm==1):
Σ=0
for x in X:
Σ+=x**2
return m.sqrt(Σ)
elif(norm==2):
Σ=0
for x in X:
Σ+=abs(x)
return Σ
elif(norm==3):
return max(np.absolute(X))
def power_method(A, u, ε, norm):
#for returning U later
U=u
counter=0
stop=False
while not stop:
counter+=1
μ=calculate_norm(A@u, norm)
U=(A@u)/μ
if(calculate_norm(U-u, norm)<=ε):
stop=True
return U, counter
else:
u=U
print("Welcome :)")
proceed=True
while(proceed):
print("Note:")
print("1.A must be a square matrix so entering the number of rows/columns is just enough!")
print("2.If A is n*n, b will be n*1!")
#number of rows and columns
n=int(input("Enter the dimensions of matrix A:"))
#getting A
l1=[]
print("Matrix A:")
for i in range(0, n):
temp1=[]
for j in range(0, n):
index_i=str(i+1)
index_j=str(j+1)
index=index_i+","+index_j
print("Item", index)
temp1.append(float(input()))
l1.append(temp1)
#using numpy
A=np.array(l1)
print(A)
print()
#getting u0
l2=[]
print("Initial Guess:")
for i in range(0, n):
temp2=[]
for j in range(0, 1):
index_i=str(i+1)
index_j=str(1)
index=index_i+","+index_j
print("Item", index)
temp2.append(float(input()))
l2.append(temp2)
#using numpy
u=np.array(l2)
print(u)
print()
ε=float(input("Enter the ε>0:"))
print()
print("Choose a norm:")
print("1.Euclidean norm")
print("2.Manhattan norm")
print("3.Infinity norm")
norm=int(input())
print()
answer, steps=power_method(A, u, ε, norm)
print("The final answer is:")
print(answer)
print()
print("Found in ", steps, "steps.")
print("Try again?[yes|no]")
what_to_do=input()
if(what_to_do=="no"):
proceed=False
print()