-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaugGaussLabel.py
32 lines (28 loc) · 1.06 KB
/
augGaussLabel.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
import numpy as np
def augGaussLabel(A): #Gaussian with row labels, to prevent
n = len(A) #unnecessary changes in entries with zero pivot elements
r = np.arange(n)
for i in range(0,n-1):
j = i
while j <= n-1 and A[r[j]][i] == 0:
j += 1
if j == n:
print("Error. Matrix is singular.")
elif j != i:
r[i],r[j] = r[j],r[i]
for j in range(i+1,n):
m = A[r[j]][i]/A[r[i]][i]
for k in range(i+1,n+1):
A[r[j]][k] -= m*A[r[i]][k]
x = np.zeros(n) # General backward substitution
x[n - 1] = A[r[n - 1]][n] / A[r[n - 1]][n - 1]
for i in range(n - 1, -1, -1):
x[i] = A[r[i]][n]
for j in range(i + 1, n):
x[i] -= A[r[i]][j] * x[j]
x[i] = x[i] / A[r[i]][i]
x[i] = round(x[i],7)
print(x)
return(x)
A = np.array([[1,2,3,0],[3,4,7,2],[6,5,9,11]],dtype=float) #example input
augGaussLabel(A) #4,1,-2