IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Algorithmes et structures de données Discussion :

Inversion Matricielle : le pivot plus rapide que LU ? Oo


Sujet :

Algorithmes et structures de données

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Août 2006
    Messages
    38
    Détails du profil
    Informations personnelles :
    Âge : 36
    Localisation : France, Nord (Nord Pas de Calais)

    Informations forums :
    Inscription : Août 2006
    Messages : 38
    Par défaut Inversion Matricielle : le pivot plus rapide que LU ? Oo
    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
    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;
    }
    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
    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

  2. #2
    Rédacteur

    Homme Profil pro
    Comme retraité, des masses
    Inscrit en
    Avril 2007
    Messages
    2 978
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 84
    Localisation : Suisse

    Informations professionnelles :
    Activité : Comme retraité, des masses
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2007
    Messages : 2 978
    Par défaut
    Salut!
    Quelques remarques, en vrac:
    1. En général, on cherche à résoudre un système linéaire et le calcul de la matrice inverse ne sert à rien.
    2. Le chronométrage sur de si petites matrices n'est pas significatif. Tu devrais faire tes tests avec des matrices 1000x1000.
    3. Il serait beaucoup plus fiable de compter le nombre d'additions, de soustractions, de multiplications et de divisions.
    4. Sur un ordinateur, il y a des éléments qui n'ont rien à voir avec la complexité et qui peuvent jouer un rôle important; par exemple, si tu veux initialiser à 0 tous les termes d'une grosse matrice, il n'est pas indifférent de procéder ligne par ligne (recommandé en C) ou colonne par colonne (préférable en Fortran)
    5. Sous Windows, et peut-être sous d'autres OS, ton ordinateur fait des tas d'autres choses pendant qu'il exécute ton programme.
    6. Tu n'as aucune idée de comment tes résultats intermédiaires sont stockés: dans les registres du processeur, dans les diverses mémoires cache, dans la mémoire RAM.


    Jean-Marc Blanc

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Août 2006
    Messages
    38
    Détails du profil
    Informations personnelles :
    Âge : 36
    Localisation : France, Nord (Nord Pas de Calais)

    Informations forums :
    Inscription : Août 2006
    Messages : 38
    Par défaut
    Salut ! Merci pour tes réponses !


    Citation Envoyé par FR119492 Voir le message
    En général, on cherche à résoudre un système linéaire et le calcul de la matrice inverse ne sert à rien.
    Dans le cas général peut être, mais dans mon cas, j'en ai besoin !
    Dans le filtre de Kalman, il y a une inversion de matrice à faire.

    Citation Envoyé par FR119492 Voir le message
    Le chronométrage sur de si petites matrices n'est pas significatif. Tu devrais faire tes tests avec des matrices 1000x1000.
    J'ai réussi a optimiser un peu le méthode LU, mais c'est toujours plus lent que gauss.
    Voila mes résultats :

    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
    Matrice 500x500
    1 inversion
    LU 	: 57.8s
    Gauss 	: 47.8s
    ---------------
     
    Matrice 250x250
    4 inversions
    LU 	: 28.3s
    Gauss 	: 24.1s
    ---------------
     
    Matrice 100x100
    25 inversions
    LU 	: 11.8s
    Gauss 	: 9.9s
    ---------------
     
    Matrice 50x50
    100 inversions
    LU 	: 6.2s
    Gauss 	: 5.0s
    ---------------
     
    Matrice 10x10
    2500 inversions
    LU 	: 1.5s
    Gauss 	: 1.2s
    ---------------
     
    Matrice 1x1
    250000 inversions
    LU 	: 1.7s
    Gauss 	: 0.8s
    Classique 0.1s
    ---------------
    Citation Envoyé par FR119492 Voir le message
    Il serait beaucoup plus fiable de compter le nombre d'additions, de soustractions, de multiplications et de divisions.
    Les compter ? L'algo est assez complexe, ça ne serait pas une perte de temps dans ce cas là que d'essayer compter toutes les opérations ??

    Citation Envoyé par FR119492 Voir le message
    Sur un ordinateur, il y a des éléments qui n'ont rien à voir avec la complexité et qui peuvent jouer un rôle important; par exemple, si tu veux initialiser à 0 tous les termes d'une grosse matrice, il n'est pas indifférent de procéder ligne par ligne (recommandé en C) ou colonne par colonne (préférable en Fortran)
    Certes, mais comme je prend les même conventions dans tous mes algos (remplissage ligne par ligne), ici, la comparaison doit donc être fiable (même si pas optimale).

    Citation Envoyé par FR119492 Voir le message
    Sous Windows, et peut-être sous d'autres OS, ton ordinateur fait des tas d'autres choses pendant qu'il exécute ton programme.
    C'est vrai, mais je ne peux pas faire autrement (je ferme le maximum de programmes/processus et j'ai beau exécuter mon programme autant de fois que je veux, je trouve toujours le même écart)

    Citation Envoyé par FR119492 Voir le message
    Tu n'as aucune idée de comment tes résultats intermédiaires sont stockés: dans les registres du processeur, dans les diverses mémoires cache, dans la mémoire RAM.
    En effet, j'en ai aucune idée.

  4. #4
    Rédacteur

    Homme Profil pro
    Comme retraité, des masses
    Inscrit en
    Avril 2007
    Messages
    2 978
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 84
    Localisation : Suisse

    Informations professionnelles :
    Activité : Comme retraité, des masses
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2007
    Messages : 2 978
    Par défaut
    Salut!
    Dans le filtre de Kalman, il y a une inversion de matrice à faire.
    D'une manière générale, lorsqu'on écrit x = A^(-1) B , c'est une simple convention d'écriture qui signifie que x est la solution du système A x = B , et non qu'il faille calculer numériquement A^(-1) .
    Jean-Marc Blanc

  5. #5
    Rédacteur
    Avatar de Zavonen
    Profil pro
    Inscrit en
    Novembre 2006
    Messages
    1 772
    Détails du profil
    Informations personnelles :
    Âge : 77
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 1 772
    Par défaut
    Bien que le pivot et LU soient deux méthodes de résolution des systèmes linéaires. elles ne résolvent pas exactement le même problème.
    Voir par exemple:
    http://gilles.dubois10.free.fr/algebre_lineaire/lu.html
    Le pivot, comme je le fais remarquer est plus simple quand il s'agit de résoudre UN système unique. Mais dès que le second membre est considéré comme 'variable', la méthode LU est une méthode (entre autres) pouvant résoudre tous les systèmes ayant même matrice sans passer par un calcul d'inverse.
    En résumé LU et ses consœurs sont un compromis entre le pivot de Gauss et un calcul d'inverse, plus complexe qu'un pivot et (beaucoup) plus rapide qu'une inversion.
    Tes résultats ne me surprennent pas.
    Ce qu'on trouve est plus important que ce qu'on cherche.
    Maths de base pour les nuls (et les autres...)

  6. #6
    Membre averti
    Profil pro
    Inscrit en
    Août 2006
    Messages
    38
    Détails du profil
    Informations personnelles :
    Âge : 36
    Localisation : France, Nord (Nord Pas de Calais)

    Informations forums :
    Inscription : Août 2006
    Messages : 38
    Par défaut
    Ok. Merci pour vos réponses

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Access plus rapide que SQL server ????? (débutante)
    Par 24 faubourg dans le forum MS SQL Server
    Réponses: 10
    Dernier message: 21/12/2005, 17h36
  2. [D7] composants plus rapides que dbExpress pour Oracle 8i
    Par Magnus dans le forum Bases de données
    Réponses: 2
    Dernier message: 10/10/2005, 12h06
  3. Plus rapide que bresenham ?
    Par mathieu_t dans le forum Algorithmes et structures de données
    Réponses: 3
    Dernier message: 01/06/2005, 13h28
  4. [VB6] timer plus rapide que 1 d'interval
    Par windob dans le forum VB 6 et antérieur
    Réponses: 12
    Dernier message: 24/02/2004, 00h16
  5. Réponses: 8
    Dernier message: 31/10/2003, 16h21

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo