-
Notifications
You must be signed in to change notification settings - Fork 0
/
Elimina_Gauss.py
68 lines (55 loc) · 1.74 KB
/
Elimina_Gauss.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
import numpy as np
import numpy.linalg as npla
def LU(A,f):
# get the size of the system
n = len(f)
# check the size
if (A.shape[0] != n) or (A.shape[1] != n):
print('\t Inconsistent size in LU decomposition')
info = 0
return M,info
# create the augmented matrix
M = np.zeros((n,n+1))
M[:,:-1] = A
M[:,-1] = f
# loop through all the colum
# to get rid of the lower part
for iC in range(n-1):
# for each column loop over all the lines
# that are below the diagonal
# to set to 0 their elements
for iL in range(iC+1,n):
# check if the diagonal element
# is null
if(M[iC,iC] == 0):
print('\t Zero on the diagonal, LU failed')
info = 0
return M,info
# eliminate the element
M[iL,:] = M[iL,:] - M[iL,iC]/M[iC,iC] * M[iC,:]
# if we succed we return info = 1 and the upper augmetned matrix
info = 1
return M,info
def BS(M):
########################################################
# Function to backsubstitute the results
# and get the final solution
########################################################
# get the size of the matrix
n = M.shape[0]
# loop over all the lines
# starting by the end
for iL in range(n-1,-1,-1):
# check if we have diagonal elements on the diagonal
if(M[iL,iL] == 0):
print('\t Zero on the diagonal, LU failed')
info = 0
return M,info
# divide the line by the diagonal element of M
M[iL,:] /= M[iL,iL]
# loop over all the lines that are above this onef
for iLL in range(iL-1,-1,-1):
M[iLL,:] -= M[iLL,iL]*M[iL,:]
info = 1
x=M[:,n]
return x,info