-
Notifications
You must be signed in to change notification settings - Fork 73
/
gaussian_elemination.py
47 lines (34 loc) · 1005 Bytes
/
gaussian_elemination.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
from numpy import array
from numpy import zeros
from numpy import int16
def forward_elimination(mat, n):
for it in range(0, n-1):
for row in range(it+1, n):
alpha = mat[row][it]/mat[it][it]
for col in range(0, n+1):
mat[row][col] -= alpha*mat[it][col]
return mat
def backward_substitution(mat, n):
var = zeros(shape=(n), dtype=int16)
var[n-1] = mat[n-1][n]/mat[n-1][n-1]
for row in range(n-2,-1,-1):
total = 0
for col in range(row+1, n):
total += mat[row][col]*var[col]
var[row] = (mat[row][n]-total)/mat[row][row]
return var
def driver():
# take the dimension of the co-efficient matrix
n = int(input('Co-efficient matrix n: '))
# input as 1D array
# [[1,2],[3,4]] will be input as 1 2 3 4
mat = array([int(i) for i in input('Matrix: ').split()])
# reshape to n * n+1
mat = mat.reshape(n,n+1)
# make upper triangle
mat = forward_elimination(mat, n)
# variable value
var = backward_substitution(mat, n)
print(var)
if __name__ == '__main__':
driver()