-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGaussian_Elim_P2.py
More file actions
228 lines (149 loc) · 4.67 KB
/
Gaussian_Elim_P2.py
File metadata and controls
228 lines (149 loc) · 4.67 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"Scott Burstein Gaussian Elimination Matrix Solver Part 2"
import numpy as np
def fwd_solve(a, b):
""" fwd. substition for Lx = b,
returning a new vector x with the solution.
"""
n = len(b)
x = [0]*n
for i in range(n):
x[i] = b[i]
for j in range(i):
x[i] -= a[i][j]*x[j]
return x
def back_solve(a, b):
""" backward substitution, returning a new list """
n = len(b)
x = [0]*n
for i in range(n-1, -1, -1):
x[i] = b[i]
for j in range(i+1, n):
x[i] -= a[i][j] * x[j]
x[i] /= a[i][i]
return x
#I collaborated with Sarah Northover on ge_lu function.
def ge_lu(a):
""" LU factorization of the matrix a. Returns L and U in one matrix. """
n = len(a)
#print(n)
mat = [row[:] for row in a] # make a copy of a
pivot_element = [i for i in range(n)]
for k in range(n-1):
# reduce k-th column
# with pivoting: choose the pivot element,
# swap rows and update permutation vector.
elems = [0]*n
for x in range(k, n):
elems[x] = abs(mat[x][k])
max_index = elems.index(max(elems))
temp_val = pivot_element[k]
pivot_element[k] = pivot_element[max_index]
pivot_element[max_index] = temp_val # reduce k-th column
temp_val = mat[k]
mat[k] = mat[max_index]
mat[max_index] = temp_val
for i in range(k+1, n):
# zero out (i, k) entry
mat[i][k] /= mat[k][k]
for j in range(k+1, n): # row reduction for i-th row
mat[i][j] -= mat[i][k] * mat[k][j]
# update entry in row
return mat
'''
Other attempted approach which did not work using list comp. for elems and np.argmax
for k in range(n-1):
elems = [0]*n
elems = [abs(a[j][k]) for j in range(k, n)]
row_index_max = np.argmax(elems) + k
np.argmax(abs(A[k:n,k])) + k
pivot_element[[k, row_index_max]] = pivot_element[[row_index_max, k]]
a[[k, row_index_max]] = a[[row_index_max, k]]
for y in range(j):
for x in range(k):
elems.append(mat[x][y])
'''
# Template for version without pivoting
def linsolve(a, b):
"""
linsolve(a, b) is a function that solves equation for
matrices ax = b, where b is a column of values
and returns the solution. It processes the result
by using the ge_lu(), fwd_solve(), and back_solve() helper functions.
"""
n = len(b)
x = [0]*n
y = [0]*n # optional: change code to avoid need for this intermediate vec.
mat = ge_lu(a)
y = fwd_solve(mat, b)
x = back_solve(mat, y)
return x
# Template for version with pivoting
'''
def linsolve(a, b):
""" put a doc-string here describing the algorithm """
n = len(b)
x = [0]*n
y = [0]*n # optional: change code to avoid need for this intermediate vec.
mat, p = ge_lu(a)
y = fwd_solve(mat, b, p) # OR compute pb = P*b, then fwd_solve(mat, pb)
x = back_solve(mat, y)
return x
'''
def residual(a, x, b, abs_tf = True):
""" Calculates the residual A*x - b, returning a new list """
n = len(b)
res = [0]*n
Ax = [0]*n
for i in range(n):
for j in range(n):
Ax[i] += x[j] * a[i][j]
if abs_tf:
res[i] = abs(Ax[i] - b[i])
else:
res[i] = Ax[i] - b[i]
return res
def error_test(a, b):
""" Calculates x and the residual, printing
both.
"""
x_comp = linsolve(a, b) # computed solution
# calculate the residual for x_comp
res = residual(a, x_comp, b)
print(res)
max_res = max(res)
print("x was calculated as:\n", x_comp, "\n")
print('Maximum Residual')
print("-------------------")
print('{:.5e}'.format(max_res))
if __name__ == "__main__":
# typical structure of linsolve call:
'''
a = [[1,0,0], [0,1,0], [0,0,1]]
b = [1,2,3]
x = linsolve(a, b)
print(x)
'''
# testable 4x4 Matrix
a = [
[0,3,2,1],
[4,0,7,5],
[8,2,0,2],
[0,1,2,0]
]
b = [
-3,
2,
-2,
-5
]
x = linsolve(a, b)
print(x)
#testing ax-b
error_test(a, b)
'''
There is a maximum residual between the linsolve() method
and the computed solution for this ax-b computation of 5.00000e+00.
This seems unlikely, as the linsolve() method is proposed to be much more accurate.
There should not be an error of ~5 for the linsolve - computed methods.
Further testing will need to be done in order to determine issue.
'''