-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolvers.py
More file actions
52 lines (45 loc) · 1.63 KB
/
solvers.py
File metadata and controls
52 lines (45 loc) · 1.63 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
# change
# another change
"""Routines for solving a linear system of equations."""
import numpy as np
def gaussian_eliminate(aa, bb):
"""Solves a linear system of equations (Ax = b) by Gauss-elimination
Args:
aa: Matrix with the coefficients. Shape: (n, n).
bb: Right hand side of the equation. Shape: (n,)
Returns:
Vector xx with the solution of the linear equation or None
if the equations are linearly dependent.
"""
nn = aa.shape[0]
xx = np.zeros((nn,), dtype=float)
"""Solving Matrix with partial pivoting"""
cc = np.array(aa)
for j in range(0, nn):
y = cc[j:, j] # array contains columns of aa
z = np.argmax(y) + j # z looks for row-position of highest abs val in column
for i in range(j+1, nn):
temp = np.array(aa[z])
temp2 = np.array(bb[z])
# swapping rows of aa and bb
if aa[i-1, j] != 0: # only performs shift if first entry of row isn't zero
aa[z], aa[i-1] = aa[i-1], temp
bb[z], bb[i-1] = bb[i-1], temp2
if aa[i, j] == 0:
continue
bb[i] = aa[j, j] / aa[i, j] * bb[i] - bb[j]
aa[i] = aa[j, j] / aa[i, j] * aa[i] - aa[j]
"""Solution-Vector"""
for k in range(nn-1, -1, -1):
for g in range(nn-1, k, -1):
bb[k] = bb[k] - aa[k, g] * xx[g]
if aa[k, k] == 0:
continue
xx[k] = bb[k] / aa[k, k]
# trying to catch the linear dependency
for t in range(0, nn):
if np.all(aa[t] == 0):
if abs(xx[t]) == 0:
return None
return xx
#test for