-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTP2PARTIE2.py
84 lines (77 loc) · 2.25 KB
/
TP2PARTIE2.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def gaussian_elimination(A, b):
n = len(b)
m = len(A[0])
for k in range(n - 1):
pivot = A[k][k]
if pivot != 0:
for i in range(k + 1, n):
q = A[i][k]
A[i][k] = 0
b[i] = b[i] - (q / pivot) * b[k]
for j in range(k + 1, m):
A[i][j] = A[i][j] - (A[k][j] * q / pivot)
print("iteratuion ",k)
print("A: ")
afficher_matrice(A)
print("b: ")
afficher_vecteur(b)
else:
print("Problem: pivot is zero")
return None
afficher_matrice(A)
afficher_vecteur(b)
return A, b
def lireMatrice(T,l,c):
for i in range(l):
for j in range(c):
T[i][j] = int(input(f"Entrer la case ({i},{j}): "))
return T
def lireVecteur(V,N):
for i in range(N):
V[i] = int(input(f"Entrer l'élément {i}: "))
def afficher_vecteur(V):
N = len(V)
for i in range(N):
print(V[i], end=" ")
print()
def Algorithmede_la_methodede_descente(A, b):
n = len(b)
x = [0] * n
x[0] = b[0] / A[0][0]
for i in range(1, n):
somme = 0
for j in range(i):
somme += A[i][j] * x[j]
x[i] = (b[i] - somme) / A[i][i]
return x
def Algorithmede_la_methodede_remontee(A, b):
n = len(b)
x = [0] * n
x[-1] = b[-1] / A[-1][-1]
for i in range(n - 2, -1, -1):
somme = 0
for k in range(i + 1, n):
somme += A[i][k] * x[k]
x[i] = (b[i] - somme) / A[i][i]
return x
def afficher_matrice(T):
N = len(T)
for i in range(N):
for j in range(N):
print(T[i][j], end=" ")
print()
def menu():
l = int(input("\nEntrez LE nombre des lignes de matrice :"))
c = int(input("\nEntrez LE nombre des colones de matrice :"))
print("\nEntrez la matrice triangulaire inférieure :")
A = [[0] * l for _ in range(c)]
A = lireMatrice(A, l,c)
t = int(input("\nEntrez la taille de vecteur :"))
print("\nentre le vecteur b :")
b = [0] * t
lireVecteur(b, t)
gaussian_elimination(A,b)
X=Algorithmede_la_methodede_remontee(A,b)
afficher_vecteur(X)
if __name__ == "__main__":
menu()