Bonjour !
Je suis en train de programmer une fonction qui inverse une matrice carré.
J'ai lu sur le net qu'apparemment, la décomposition LU serait la solution la plus rapide.
J'ai programmé la version LU + inversion de matrice trigonale et j'ai comparé le temps d'exécution avec un pivot de gauss classique.
Et bien surprise, le pivot est 2 fois plus rapide...
Du coup, je me pose cette question : pourquoi préfère-t-on souvent la version LU ??
Est-ce que j'ai un problème dans mon code ??
Voila ce que j'ai obtenu pour 10000 inversions de matrice 4x4
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Matrice a inverser : 2 4 3 1 5 9 8 3 3 4 5 2 4 5 7 2 *********************************************** 11 -7 6 -1 -1 1 -1 0 -5 3 -3 1 -2 1 1 -1 Inversion par decomposition LU : 0.764 secondes *********************************************** 11 -7 6 -1 -1 1 -1 0 -5 3 -3 1 -2 1 1 -1 Inversion par pivot de gauss : 0.39 secondes *********************************************** Process returned 0 (0x0) execution time : 1.170 s Press any key to continue.
Voila mon main.cpp
et mon fichier Matrix.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include <iostream> #include "Matrix.h" //#include "Kalman.h" #include <time.h> using namespace std; int main() { /*double tb[100] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 5, 3, 2, 8, 4, 5, 1, 9, 4, 5, 8, 2, 4, 4, 3, 2, 1, 8, 7, 6, 5, 6, 4, 5, 2, 8, 9, 0, 4, 3, 8, 4, 5, 8, 3, 1, 9, 8, 7, 6, 5, 9, 8, 8, 8, 7, 2, 3, 1, 1, 0, 3, 2, 4, 5, 9, 1, 2, 9, 9, 3, 2, 8, 6, 6, 4, 4, 4, 4, 3, 2, 3, 4, 5, 3, 2, 1, 9, 3, 2, 1, 9, 7, 3, 4, 2, 9, 2, 3, 2, 1};*/ //double tb[16] = {2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2}; double tb[16] = {2, 4, 3, 1, 5, 9, 8, 3, 3, 4, 5, 2, 4, 5, 7, 2}; //double tb[16] = {2, 3, 5, 7, 4, 2, 1, 9, 7, 4, 6, 0, 2, 4, 3, 3}; //double tb[9] = {2, 3, -3, 5, 7, -7, -5, -7, 7}; // Non inversible //double tb[9] = {2, -1, 0, -1, 2, -1, 0, -1, 2}; //double tb[9] = {2, 5, 7, 4, 2, 1, 4, 6, 0}; //double tb[4] = {1, 3, 5, 2}; Matrix<double> m(4, 4, tb); Matrix<double> d; //Affichage de la matrice à inverser cout << "Matrice a inverser : " << endl; m.display(); cout << "***********************************************" << endl << endl; //Inversion de la matrice : méthode LU clock_t t1=clock(); for(int i = 0; i < 10000; i++) d = m.inv_LU(); clock_t t2=clock(); d.display(); //Affichage de la matrice inverse cout << "Inversion par decomposition LU : " << (double)(t2-t1)/CLOCKS_PER_SEC << " secondes" << endl; cout << "***********************************************" << endl << endl; //Inversion de la matrice : méthode pivot t1=clock(); for(int i = 0; i < 10000; i++) d = m.inv_Gauss(); t2=clock(); d.display(); //Affichage de la matrice inverse cout << "Inversion par pivot de gauss : " << (double)(t2-t1)/CLOCKS_PER_SEC << " secondes" << endl; cout << "***********************************************" << endl << endl; return 0; }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578 #ifndef Matrix_DEF #define Matrix_DEF #include <vector> #include <iostream> using namespace std; template <typename T> class Matrix { private: int a_column; int a_row; vector<T> a_matrix; public: Matrix(); Matrix(const int, const T = 0); Matrix(const int, const T *); Matrix(const int, const T **); Matrix(const int, const int, const T = 0); Matrix(const int, const int, const T *); Matrix(const int, const int, const T **); Matrix(const Matrix<T> &); ~Matrix(); T operator()(const int, const int) const; T& operator()(const int, const int); Matrix<T>& operator=(const Matrix<T> &); Matrix<T>& operator+(const T &) const; Matrix<T>& operator+(const Matrix<T> &) const; Matrix<T>& operator+=(const T &); Matrix<T>& operator+=(const Matrix<T> &); Matrix<T>& operator-(const T &) const; Matrix<T>& operator-(const Matrix<T> &) const; Matrix<T>& operator-=(const T &); Matrix<T>& operator-=(const Matrix<T> &); Matrix<T>& operator*(const T &) const; Matrix<T>& operator*(const Matrix<T> &) const; Matrix<T>& operator*=(const T &); Matrix<T>& operator*=(const Matrix<T> &); Matrix<T>& operator/(const T &) const; Matrix<T>& operator/(const Matrix<T> &) const; Matrix<T>& operator/=(const T &); Matrix<T>& operator/=(const Matrix<T> &); int column() const; int row() const; T det() const; Matrix<T>& transpose() const; T trace() const; void display() const; static Matrix<T>& identity(const int); Matrix<double>& inv_LU() const; Matrix<double>& inv_trigonal_lower() const; Matrix<double>& inv_Gauss() const; }; template <typename T> Matrix<T>::Matrix() { a_column = 0; a_row = 0; a_matrix.clear(); } template <typename T> Matrix<T>::Matrix(const int row, const T val) { a_column = row; a_row = row; a_matrix.resize(a_row*a_column, val); } template <typename T> Matrix<T>::Matrix(const int row, const T *tab) { a_column = row; a_row = row; a_matrix.resize(a_row*a_column); for(int i = 0; i < a_row; i++) for(int j = 0; j < a_column; j++) (*this)(i, j) = tab[i*a_column+j]; } template <typename T> Matrix<T>::Matrix(const int row, const T **tab) { a_column = row; a_row = row; a_matrix.resize(a_row*a_column); for(int i = 0; i < a_row; i++) for(int j = 0; j < a_column; j++) (*this)(i, j) = tab[i][j]; } template <typename T> Matrix<T>::Matrix(const int row, const int col, const T val) { a_column = col; a_row = row; a_matrix.resize(a_row*a_column, val); } template <typename T> Matrix<T>::Matrix(const int row, const int col, const T *tab) { a_column = col; a_row = row; a_matrix.resize(a_row*a_column); for(int i = 0; i < a_row; i++) for(int j = 0; j < a_column; j++) (*this)(i, j) = tab[i*a_column+j]; } template <typename T> Matrix<T>::Matrix(const int row, const int col, const T **tab) { a_column = col; a_row = row; a_matrix.resize(a_row*a_column); for(int i = 0; i < a_row; i++) for(int j = 0; j < a_column; j++) (*this)(i, j) = tab[i][j]; } template <typename T> Matrix<T>::Matrix(const Matrix<T> &mat) { a_column = mat.a_column; a_row = mat.a_row; a_matrix.resize(a_row*a_column); for(int i = 0; i < a_row; i++) for(int j = 0; j < a_column; j++) (*this)(i, j) = mat(i, j); } template <typename T> Matrix<T>::~Matrix() { } template <typename T> T Matrix<T>::operator()(const int row, const int col) const { if(row < 0 || row >= a_row || col < 0 || col >= a_column) throw string("Error : Index out of range"); return a_matrix.at(row*a_column+col); } template <typename T> T& Matrix<T>::operator()(const int row, const int col) { if(row < 0 || row >= a_row || col < 0 || col >= a_column) throw string("Error : Index out of range"); return a_matrix.at(row*a_column+col); } template <typename T> Matrix<T>& Matrix<T>::operator=(const Matrix<T> &mat) { a_column = mat.a_column; a_row = mat.a_row; a_matrix.clear(); a_matrix.resize(a_row*a_column); for(int i = 0; i < a_row; i++) for(int j = 0; j < a_column; j++) (*this)(i, j) = mat(i, j); return *this; } template <typename T> Matrix<T>& Matrix<T>::operator+(const T &val) const { Matrix<T> *tmp = new Matrix<T>(*this); for(int i = 0; i < a_row; i++) for(int j = 0; j < a_column; j++) (*tmp)(i, j) = (*this)(i, j) + val; return *tmp; } template <typename T> Matrix<T>& Matrix<T>::operator+(const Matrix<T> &mat) const { if(mat.a_row != a_row || mat.a_column != a_column) throw string("Error : Matrix dimensions do not match"); Matrix<T> *tmp = new Matrix<T>(*this); for(int i = 0; i < a_row; i++) for(int j = 0; j < a_column; j++) (*tmp)(i, j) = (*this)(i, j) + mat(i, j); return *tmp; } template <typename T> Matrix<T>& Matrix<T>::operator+=(const T &val) { for(int i = 0; i < a_row; i++) for(int j = 0; j < a_column; j++) (*this)(i, j) += val; return *this; } template <typename T> Matrix<T>& Matrix<T>::operator+=(const Matrix<T> &mat) { if(mat.a_row != a_row || mat.a_column != a_column) throw string("Error : Matrix dimensions do not match"); for(int i = 0; i < a_row; i++) for(int j = 0; j < a_column; j++) (*this)(i, j) += mat(i, j); return *this; } template <typename T> Matrix<T>& Matrix<T>::operator-(const T &val) const { Matrix<T> *tmp = new Matrix<T>(*this); for(int i = 0; i < a_row; i++) for(int j = 0; j < a_column; j++) (*tmp)(i, j) = (*this)(i, j) - val; return *tmp; } template <typename T> Matrix<T>& Matrix<T>::operator-(const Matrix<T> &mat) const { if(mat.a_row != a_row || mat.a_column != a_column) throw string("Error : Matrix dimensions do not match"); Matrix<T> *tmp = new Matrix<T>(*this); for(int i = 0; i < a_row; i++) for(int j = 0; j < a_column; j++) (*tmp)(i, j) = (*this)(i, j) - mat(i, j); return *tmp; } template <typename T> Matrix<T>& Matrix<T>::operator-=(const T &val) { for(int i = 0; i < a_row; i++) for(int j = 0; j < a_column; j++) (*this)(i, j) -= val; return *this; } template <typename T> Matrix<T>& Matrix<T>::operator-=(const Matrix<T> &mat) { if(mat.a_row != a_row || mat.a_column != a_column) throw string("Error : Matrix dimensions do not match"); for(int i = 0; i < a_row; i++) for(int j = 0; j < a_column; j++) (*this)(i, j) -= mat(i, j); return *this; } template <typename T> Matrix<T>& Matrix<T>::operator*(const T &val) const { Matrix<T> *tmp = new Matrix<T>(*this); for(int i = 0; i < a_row; i++) for(int j = 0; j < a_column; j++) (*tmp)(i, j) = (*this)(i, j) * val; return *tmp; } template <typename T> Matrix<T>& Matrix<T>::operator*(const Matrix<T> &mat) const { if(a_column != mat.a_row) throw string("Error : Matrix dimensions do not match"); Matrix<T> *tmp = new Matrix<T>(a_row, mat.a_column); for(int i = 0; i < a_row; i++) { for(int j = 0; j < mat.a_column; j++) for(int k = 0; k < a_column; k++) (*tmp)(i, j) += (*this)(i, k) * mat(k, j); } return *tmp; } template <typename T> Matrix<T>& Matrix<T>::operator*=(const T &val) { for(int i = 0; i < a_row; i++) for(int j = 0; j < a_column; j++) (*this)(i, j) *= val; return *this; } template <typename T> Matrix<T>& Matrix<T>::operator*=(const Matrix<T> &mat) { (*this) = (*this) * mat; return *this; } template <typename T> Matrix<T>& Matrix<T>::operator/(const T &val) const { Matrix<T> *tmp = new Matrix<T>(*this); for(int i = 0; i < a_row; i++) for(int j = 0; j < a_column; j++) (*tmp)(i, j) = (*this)(i, j) / val; return *tmp; } template <typename T> Matrix<T>& Matrix<T>::operator/(const Matrix<T> &mat) const { Matrix<T> *tmp = new Matrix<T>(*this); tmp *= inv(mat); return *tmp; } template <typename T> Matrix<T>& Matrix<T>::operator/=(const T &val) { for(int i = 0; i < a_row; i++) for(int j = 0; j < a_column; j++) (*this)(i, j) /= val; return *this; } template <typename T> Matrix<T>& Matrix<T>::operator/=(const Matrix<T> &mat) { (*this) *= inv(mat); return *this; } template <typename T> int Matrix<T>::column() const { return a_column; } template <typename T> int Matrix<T>::row() const { return a_row; } template <typename T> T Matrix<T>::det() const { if(a_column != a_row) throw string("Error : Matrix must be square"); Matrix<double> l(a_row, a_row); Matrix<double> u(a_row, a_row); if((*this)(0, 0) != 0) { for(int i = 0; i < a_row; i++) { l(i, 0) = (*this)(i, 0); u(0, i) = (*this)(0, i)/l(0, 0); u(i, i) = 1.; } int i = 1; bool ok = true; while(ok && i < a_row) { for(int j = 1; j < a_row; j++) { if(i >= j) { double s = 0.; for(int k = 0; k < j; k++) s += l(i, k)*u(k, j); l(i, j) = (*this)(i, j) - s; } else if(l(i, i) == 0) ok = false; else { double s = 0; for(int k = 0; k < i; k++) s += l(i, k)*u(k, j); u(i, j) = ((*this)(i, j)-s) / l(i, i); } } i++; } } T det = 1; for(int i = 0; i < a_row; i++) det *= l(i, i) * u(i, i); return det; } template <typename T> Matrix<T>& Matrix<T>::transpose() const { Matrix<T> *tmp = new Matrix<double>(a_column, a_row); for(int i = 0; i < a_row; i++) for(int j = 0; j < a_column; j++) (*tmp)(j, i) = (*this)(i, j); return *tmp; } template <typename T> T Matrix<T>::trace() const { if(a_column != a_row) throw string("Error : Matrix must be square"); T trace = 0; for(int i = 0; i < a_row; i++) trace += (*this)(i, i); return trace; } template <typename T> void Matrix<T>::display() const { for(int i = 0; i < a_row; i++) { for(int j = 0; j < a_column; j++) cout << (*this)(i, j) << "\t"; cout << endl; } cout << endl; } template <typename T> Matrix<T>& Matrix<T>::identity(const int row) { Matrix<T> *tmp = new Matrix<T>(row, row); for(int i = 0; i < (*tmp).a_row; i++) (*tmp)(i, i) = 1; return *tmp; } template <typename T> Matrix<double>& Matrix<T>::inv_LU() const { //http://jmblanc.developpez.com/algorithmique/systemes-lineaires/?page=page_2#LII-F if(a_column != a_row) throw string("Error : Matrix must be square"); Matrix<double> l(a_row, a_row); Matrix<double> u(a_row, a_row); if((*this)(0, 0) != 0) { for(int i = 0; i < a_row; i++) { l(i, 0) = (*this)(i, 0); u(0, i) = (*this)(0, i)/l(0, 0); u(i, i) = 1.; } int i = 1; bool ok = true; while(ok && i < a_row) { for(int j = 1; j < a_row; j++) { if(i >= j) { double s = 0.; for(int k = 0; k < j; k++) s += l(i, k)*u(k, j); l(i, j) = (*this)(i, j) - s; } else if(l(i, i) == 0) ok = false; else { double s = 0; for(int k = 0; k < i; k++) s += l(i, k)*u(k, j); u(i, j) = ((*this)(i, j)-s) / l(i, i); } } i++; } } Matrix<double> *tmp = new Matrix<double>; (*tmp) = u.transpose().inv_trigonal_lower().transpose() * l.inv_trigonal_lower(); return *tmp; } template <typename T> Matrix<double>& Matrix<T>::inv_trigonal_lower() const { //http://jmblanc.developpez.com/algorithmique/systemes-lineaires/?page=page_2#LII-I Matrix<double> *tmp = new Matrix<double>(a_row, a_column); for(int i = 0; i < a_row; i++) (*tmp)(i, i) = 1/(*this)(i, i); /* Inversion matrice trigonale inferieur */ for(int r = 1; r < a_row; r++) { for(int c = 0; c < a_row-r; c++) { (*tmp)(r+c, c) = 0; for(int i = c; i < r+c; i++) (*tmp)(r+c, c) -= (*this)(r+c, i) * (*tmp)(i, c); (*tmp)(r+c, c) /= (*this)(r+c, r+c); } } /* Inversion matrice trigonale supérieur */ /*for(int r = 1; r < a_row; r++) { for(int c = 0; c < a_row-r; c++) { (*tmp)(c, c+r) = 0; for(int i = c; i < r+c; i++) (*tmp)(c, c+r) -= (*this)(i, c+r) * (*tmp)(c, i); (*tmp)(c, c+r) /= (*this)(r+c, r+c); } }*/ return *tmp; } template <typename T> Matrix<double>& Matrix<T>::inv_Gauss() const { Matrix<double> A(*this); Matrix<double> *I = new Matrix<double>(Matrix<double>::identity(a_row)); /* apparition des 0 sous la diagonale */ for(int i = 0; i < a_row - 1; i++) { for(int j = i+1; j < a_row; j++) { double coef = A(j, i) / A(i, i); for(int k = 0; k < a_row; k++) { A(j, k) -= A(i, k) * coef; (*I)(j, k) -= (*I)(i, k) * coef; } } } /* apparition des 0 au dessus de la diagonale */ for(int i = a_row-1; i > 0; i--) { for(int j = i-1; j >= 0; j--) { double coef = A(j, i) / A(i, i); for(int k = 0; k < a_row; k++) { A(j, k) -= A(i, k) * coef; (*I)(j, k) -= (*I)(i, k) * coef; } } } /* normalisation par ligne */ for(int i = 0; i < a_row; i++) for(int j = 0; j < a_row; j++) (*I)(i, j) /= A(i, i); return *I; // A est devenue l'identité et I la matrice inverse } #endif
Partager