-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpivot partiel.py
More file actions
191 lines (165 loc) · 6.4 KB
/
pivot partiel.py
File metadata and controls
191 lines (165 loc) · 6.4 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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""
DUPIN Léa - Aéro 2 classe F2
Ma 223 - Tp 1 : Méthode de Gauss pour la résolution de systèmes linéaires.
Programme de résolution d'un système linéaire avec la méthode du pivot partiel.
Institut Polytechnique des Sciences Avancées - IPSA Paris
"""
# ---------------- Import des modules nécessaires
import numpy as np
import math
import matplotlib.pyplot as plt
from time import process_time
# ---------------- Partie pivot de Gauss
def ResolutionSystTriSup(Taug):
# On créé une copie de Taug
A = np.copy(Taug)
# On récupère la taille de A
n, m = np.shape(A)
# On créé une matrice colonne X remplie de 0
X = np.zeros(n)
# On calcule les termes solutions de cette matrice X
for k in range(n - 1, -1, -1):
S = 0
for j in range(k + 1, n):
S = S + A[k, j] * X[j]
X[k] = (A[k, -1] - S) / A[k, k]
# On renvoie la matrice solution X
return(X)
# ---------------- Partie pivot partiel
def PivotPartiel(A, k, n):
# On récupère la meilleure valeur possible du pivot
value = k
for p in range(k, n):
if abs(A[p][k]) > abs(A[value][k]):
value = p
return(value)
def Transposition(A, i, p):
# On calcule la matrice transposée
copy = A[i].copy()
A[i] = A[p]
A[p] = copy
return(A)
def ReducGauss_PivotPartiel(Aaug):
# On créé une copie de Aaug
A = np.copy(Aaug)
# On récupère la taille de A
n, m = np.shape(A)
# On calcule les nouveaux éléments de la matrice A
# grâce au meilleur pivot possible
for k in range(0, n - 1):
p = PivotPartiel(A, k, n)
pivot = A[p, k]
A = Transposition(A, k, p)
if pivot == 0:
print("Le pivot est nul")
else:
for i in range(k + 1, n):
G = A[i, k] / pivot
A[i, :] = A[i, :] - G * A[k, :]
# On renvoie la matrice A, matrice réduite de Aaug
return(A)
def Gauss_PivotPartiel(A, B):
# On créé Aaug en ajoutant B à A
stack = np.column_stack([A, B])
# On réduit la matrice
reduc = ReducGauss_PivotPartiel(stack)
# On trouve la solution du système
solution = ResolutionSystTriSup(reduc)
# On renvoie la matrice solution du système
return(solution)
# ---------------- Calculs de précision
def precision(A, X, B):
# On récupère la taille de B (nombre de colonnes)
n = len(B)
# On la redimensionne
B = np.reshape(B, (1, n))
# on récupère les éléments de X et B dans une matrice 1-D
X = np.ravel(X)
B = np.ravel(B)
# On calcule l'erreur
a = np.dot(A, X) - B
# On renvoie la norme ||A X - B||
return(np.linalg.norm(a))
# ---------------- Graphiques
def graph():
# Mise en page pour mettre les 3 graphiques
plt.gcf().subplots_adjust(wspace = 0.5, hspace = 0.5)
plt.subplot(3, 1, 1)
# Demande de la taille de la matrice maximale à calculer
taille = int(input("Taille max de la matrice souhaitée ? \n"))
# ------ Temps de calcul
print("\n------------Pivot partiel------------")
time_list_pivot_partiel = []
for i in range(0, taille + 1):
A = np.random.rand(i, i)
B = np.random.rand(i, 1)
Gauss_PivotPartiel(A, B)
t = process_time()
time_list_pivot_partiel.append(t)
# Calculs de temps & création de la liste les contenant tous
T_list_pivot_partiel = []
for i in range(len(time_list_pivot_partiel)):
if i == len(time_list_pivot_partiel)-1:
T = time_list_pivot_partiel[-1] - time_list_pivot_partiel[0]
elif i == len(time_list_pivot_partiel):
None
else:
T = time_list_pivot_partiel[i + 1] - time_list_pivot_partiel[i]
T_list_pivot_partiel.append(T)
# Affichage des temps en console
if taille > 100:
for i in range(0, len(T_list_pivot_partiel) - 1, 100):
print("Le temps de calcul pour une matrice de taille ", i, "est de :", T_list_pivot_partiel[i], "secondes.")
print("Le temps de calcul pour une matrice de taille ", taille, "est de :", T_list_pivot_partiel[-2], "secondes.")
print("\nLe temps de calcul total est de", T_list_pivot_partiel[-1], "secondes")
minutes = int(T_list_pivot_partiel[-1]//60)
secondes = int(T_list_pivot_partiel[-1] % 60)
if minutes == 1:
print("Soit environ", minutes, "minute et", secondes, "secondes.")
elif minutes > 1:
print("Soit environ", minutes, "minutes et", secondes, "secondes.")
# On supprime le temps total afin de pouvoir afficher les temps de calcul
del(T_list_pivot_partiel[- 1])
abscisse = []
for i in range(0, taille):
abscisse.append(i)
# -- Création de la courbe
plt.plot(abscisse, T_list_pivot_partiel, color = 'm', label = 'Méthode du pivot partiel')
# -- Affichage de la courbe
# Graphique 1 : Temps / taille plt.title("Temps de calcul en fonction de la taille de la matrice")
plt.ylabel('Temps de calcul en secondes')
plt.xlabel('Taille de la matrice')
plt.grid(True)
plt.legend(loc = 'best')
# Graphique 2 : Temps en échelle logarithmique / taille
plt.subplot(3, 1, 2)
plt.plot(abscisse, T_list_pivot_partiel, color = 'm', label = 'Méthode du pivot partiel')
plt.title("Temps de calcul en fonction de la taille de la matrice \n Echelle logarithmique ")
plt.ylabel('Temps de calcul en secondes (log)')
plt.xlabel('Taille de la matrice')
plt.yscale('log')
plt.grid(True)
plt.legend(loc = 'best')
# ------ Erreur en fonction de la taille
plt.subplot(3, 1, 3)
print("\n------------Pivot Partiel------------")
size = []
erreur = []
for i in range(0, taille + 1):
A = np.random.rand(i, i)
B = np.random.rand(i, 1)
X = Gauss_PivotPartiel(A, B)
result = precision(A, X, B)
size.append(i)
erreur.append(result)
# Graphique 3 : Erreur / taille
# -- Création de la courbe
plt.plot(size, erreur, color = 'm', label = 'Méthode du pivot partiel')
# -- Affichage de la courbe
plt.xlabel('Taille de la matrice')
plt.ylabel('Erreur ||A X - B||')
plt.title('Erreur en fonction de la taille de la matrice')
plt.grid(True)
plt.legend(loc = 'best')
plt.show()
graph()