-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab1_tasks_calculation.py
187 lines (165 loc) · 4.77 KB
/
lab1_tasks_calculation.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import lab1_gauss_elimination.gauss_elimination as ge
import lab1_gauss_seidel.gauss_seidel as gs
import condition_number as cn
import matrix_helpers as mh
import timeit
# 1) For both elimination and seidel
matrix_1 = [
[1, -2, 1, 2],
[2, -5, -1, -1],
[-7, 0, 1, -2]
]
ge.GaussElimination(
matrixAB=matrix_1,
vars=['x1','x2','x3'],
print_only_results=False,
matrix_name="First matrix (Elimination)"
)
gs.GaussSeidel(
matrixAB=matrix_1,
equations=None,
vars=['x1','x2','x3'],
e=0.001,
print_only_results=True,
matrix_name="First matrix (Seidel)"
)
# 2) For elimination only, test the speed with both elimination and seidel ---------------------
matrix_2 = [
[5, 5, -3, 4, -11],
[1, -4, 6, -4, -10],
[-2, -5, 4, -5, -12],
[-3, -3, 5, -5, 8]
]
matrix_2_updated = [
[5, 5, -3, 4, -11],
[-2, -5, 4, -5, -12],
[1, -4, 6, -4, -10],
[-3, -3, 5, -5, 8]
]
def timer_elimination():
ge.GaussElimination(
matrixAB=matrix_2,
vars=['x1', 'x2', 'x3', 'x4'],
print_only_results=True,
matrix_name="Second matrix (Elimination)",
print_results=False,
without_print=True
)
def timer_seidel():
gs.GaussSeidel(
matrixAB=matrix_2,
equations=None,
vars=['x1', 'x2', 'x3', 'x4'],
e=0.00001,
print_only_results=True,
matrix_name="Second matrix (Seidel)",
max_iterations=1000,
show_errors_list=False,
auto_adjust_matrix=True,
without_print=True
)
def timer_seidel2():
gs.GaussSeidel(
matrixAB=matrix_2_updated,
equations=None,
vars=['x1', 'x2', 'x3', 'x4'],
e=0.00001,
print_only_results=True,
matrix_name="Second matrix (Seidel)",
max_iterations=1000,
show_errors_list=False,
auto_adjust_matrix=False,
without_print=True
)
def timer_seidel3():
gs.GaussSeidel(
matrixAB=matrix_2_updated,
equations=None,
vars=['x1', 'x2', 'x3', 'x4'],
e=0.0001,
print_only_results=True,
matrix_name="Second matrix (Seidel)",
max_iterations=1000,
show_errors_list=False,
auto_adjust_matrix=False,
without_print=True
)
#print("Gauss Elimination - time elapsed\n(number of calls = 10000):\n", timeit.timeit("timer_elimination()",
# setup="from __main__ import timer_elimination", number=10000), 'seconds \n')
#print("Gauss Seidel with auto-adjustment of matrix and e = 0.00001 - time elapsed\n (number of calls = 10000):\n", timeit.timeit('timer_seidel()',
# setup="from __main__ import timer_seidel", number=10000), 'seconds\n')
#print("Gauss Seidel without auto-adjustment of matrix and e = 0.00001 - time elapsed\n (number of calls = 10000):\n", timeit.timeit('timer_seidel2()',
# setup="from __main__ import timer_seidel2", number=10000), 'seconds\n')
#print("Gauss Seidel without auto-adjustment of matrix and e = 0.0001 - time elapsed\n (number of calls = 10000):\n", timeit.timeit('timer_seidel3()',
# setup="from __main__ import timer_seidel3", number=10000), 'seconds\n')
ge.GaussElimination(
matrixAB=matrix_2,
vars=['x1', 'x2', 'x3', 'x4'],
print_only_results=False,
matrix_name="Second matrix (Elimination)",
without_print=False
)
gs.GaussSeidel(
matrixAB=matrix_2,
equations=None,
vars=['x1', 'x2', 'x3', 'x4'],
e=0.00001,
print_only_results=True,
matrix_name="Second matrix (Seidel)",
max_iterations=1000,
show_errors_list=False,
auto_adjust_matrix=True
)
# 3) For seidel only, check the condition number, find how big are errors of calculation -----
matrix_3 = [
[2, -1, -1, 5],
[1, 3, -2, 7],
[1, 2, 3, 10]
]
gs.GaussSeidel(
matrixAB=matrix_3,
equations=None,
vars=['x1', 'x2', 'x3'],
e=0.001,
print_only_results=True,
matrix_name="Third matrix (Seidel)"
)
cn.ConditionNumber(
matrixAB=matrix_3,
print_result=True
)
# 4) For seidel only ------------------------------------------------------------------------
matrix_4 = [
[8, 5, 3, 30],
[-2, 8, 1, 15],
[1, 3, -10, 42]
]
ge.GaussElimination(
matrixAB=matrix_4,
vars=['x1', 'x2', 'x3'],
print_only_results=True,
matrix_name="Fourth matrix (Elimination)"
)
gs.GaussSeidel(
matrixAB=matrix_4,
equations=None,
vars=['x1', 'x2', 'x3'],
e=0.001,
print_only_results=True,
matrix_name="Fourth matrix (Seidel)"
)
# For gauss only, check the condition number, find how big are erros of calculation --------
matrix_5 = [
[0.78, 0.563, 0.217],
[0.913, 0.659, 0.254]
]
ge.GaussElimination(
matrixAB=matrix_5,
vars=['x1', 'x2'],
print_only_results=True,
matrix_name="Fifth matrix (Elimination)"
)
cn.ConditionNumber(
matrixAB=matrix_5,
print_result=True
)