Bonjour à vous !

Je tenais d'abord à remercier les participants de ce forum pour toute l'entraide dont ils font part tout les jours.
J'essaye aussi souvent que faire se peut d' apporter aussi mon aide. C'est la première fois, de mémoire, que je demande un avis sur mon travail sur dvp.com.

Passons au vif du sujet : j'implémente depuis plusieurs semaines une classe Matrix, comme ça pour le fun ; une classe Matrix qui n'a pas pour but de concurrencer OpenBLAS ou Eigen au niveau des perfs, mais qui tient tout de même la route au niveau des performances. Je n'étais pas prêt à faire des compromis au niveau de la compréhension du code et n'est donc pas, volontairement, ajouté l'utilisation des registres SSE/AVX, ni de choses trop... exotiques.
Je souhaitais juste une bibliothèque facile à lire, qui gère tout contenu numérique, Coplien et qui gère un maximum d'opérations sur les matrices.
Il me reste encore :
- Inverse d'une matrice
- Déterminant d'une matrice
- Proposer une transposition inplace qui ne crée pas de nouvelle matrice

Hier soir je suis passé en mode template, avec un tpp, mais j'ai préféré tout rajouter dans le header, par commodité uniquement (désolé mais avec les templates je ne m'en sortais plus, beaucoup font cela apparemment, juste un .h)

Voici le code (bonne lecture).
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
/*
*   Created by N.Richard
*   Date de création : 5 mai 2014
*   Date de version : 14 mai 2014
*   Version 1.0
*   Principe : Bibliothèque simple pour la gestion de matrices (forme coplien)
*/
 
#ifndef MATRIX_H
#define MATRIX_H
 
#include "Utilities.h"
#ifdef _OPENMP
    #include <omp.h>
#endif //_OPENMP
#include <iostream>
#include <stdexcept>
#include <string.h>
 
template <typename T>
class Matrix {
 
public:
    //Constructors
    Matrix (int nbrows, int nbcolumns): _rows(nbrows), _cols(nbcolumns), _values(NULL)
    {
        if (nbrows <= 0 || nbcolumns <= 0)
            throw std::domain_error("Matrix can't have a zero size");
        _values = new T[nbrows * nbcolumns]();
    }
 
    Matrix(const Matrix& mat) : _rows(mat.getNbRows()), _cols(mat.getNbCols()), _values(NULL)
    { 
 
        if (mat._values != NULL) {
                _values = new T[_rows*_cols]();
                memcpy(_values,mat._values, _rows*_cols * sizeof(T));
        }
    } 
 
    //Operators override
    Matrix& operator= (const Matrix &mat)
    {
        if (this != &mat){
 
            Matrix tmp (mat);
 
            _rows = tmp._rows; 
            _cols = tmp._cols; 
            std::swap(_values, tmp._values); 
        }
        return *this;
    }
 
    bool operator== (const Matrix& mat)
    {
        if ((_rows != mat._rows)
            || (_cols != mat._cols))
            return false;
 
        for(int i=0; i < _rows*_cols ; ++i)
            if (!equal(_values[i], mat._values[i]))
                return false;
 
        return true;
    }
 
    bool operator!= (const Matrix& mat) const
    {
        return !(*this == mat);    
    }
 
    bool operator!() const 
    {
        return isNull();
    }
 
    Matrix operator~() const 
    {
        return transpose();
    }
 
    T& operator() (int nbrows, int nbcolumns)
    {
        #ifndef NDEBUG
        if (nbrows >= _rows || nbcolumns >= _cols
            || nbrows < 0 || nbcolumns < 0)
            throw std::out_of_range("const Matrix subscript out of bounds");
        #endif  //NDEBUG
        return _values[_cols*nbrows + nbcolumns];
    }   
 
    T operator() (int nbrows, int nbcolumns) const
    {
        #ifndef NDEBUG
        if (nbrows >= _rows || nbcolumns >= _cols
            || nbrows < 0 || nbcolumns < 0)
            throw std::out_of_range("const Matrix subscript out of bounds");
        #endif  //NDEBUG
        return _values[_cols*nbrows + nbcolumns];
    }
 
    Matrix transpose() const
    {
        Matrix transposeMat (_cols, _rows);
 
        for(int i=0 ; i < _cols ; ++i)
            for(int j=0 ; j < _rows ; ++j)
                transposeMat.set(i,j,at(j,i));
 
        return transposeMat;
    }
 
    Matrix add(const Matrix& mat) const
    {
        if(_cols != mat._cols || _rows != mat._rows)
            throw std::domain_error("Matrix type not compatible for addition. Both matrices must have the same number of rows and columns"); 
 
        Matrix addMat (_rows, _cols); 
 
        #ifdef _OPENMP
        #pragma omp parallel for
        #endif //_OPENMP
        for (int i = 0; i < _rows; ++i)
            for (int j = 0; j < _cols; ++j)
                addMat(i,j) = at(i,j) + mat(i,j);
 
        return addMat;
    }
 
    Matrix mult(const Matrix& mat) const
    {
        if(_cols != mat._rows || _rows != mat._cols)
            throw std::domain_error("Matrix type not compatible for multiplication"); 
 
        Matrix multMat (_rows, _cols);
 
        #ifdef _OPENMP
        #pragma omp parallel for
        #endif //_OPENMP
        for (int i = 0; i < _rows; ++i)
            for(int k = 0; k < _rows; ++k)
                for(int j = 0; j < _cols; ++j)
                    multMat(i,j) += at(i,k) * mat(k,j);
 
        return multMat;
    }
 
    template <typename NumericType> 
    Matrix mult (const NumericType op) const
    {
        Matrix multMat (_rows, _cols);
 
        #ifdef _OPENMP
        #pragma omp parallel for
        #endif //_OPENMP
        for (int i = 0; i < _rows; ++i)
            for(int j = 0; j < _cols; ++j)
                multMat(i,j) += at(i,j) * op;
 
        return multMat;
    }
 
    Matrix pow (const int p) const
    {
        if(!isSquare())
            throw std::domain_error("Matrix type not compatible for pow operation. Matrix must be square"); 
 
        if (p == 0)
            return Matrix::identity (_rows, _cols);
        else if (p == 1)
            return *this;
        else if (p%2 == 0){
            Matrix powmat(_rows, _cols, false);
            powmat = pow(p/2);
            return powmat*powmat;
        }
        else if (p%2 == 1){
            Matrix powmat(_rows, _cols, false);
            powmat = pow (p/2);
            return powmat*powmat**this;
        }
        else 
            throw std::domain_error("For the moment, this library don't manage negative or float pow. As soon as possible !"); 
 
        return *this;
    }
 
    static Matrix identity(const int nbrows, const int nbcolumns)
    {
        if(nbrows != nbcolumns)
            throw std::domain_error("Matrix type not compatible for identity. Matrix must be square"); 
 
        Matrix identityMat (nbrows, nbcolumns); 
 
        for (int i = 0; i < nbrows; ++i)
            identityMat.set(i, i, static_cast<T>(1));
 
        return identityMat;
    }
 
    void clear(T value = 0)
    {
        for(int i=0 ; i < _cols ; ++i)
            for(int j=0 ; j < _rows ; ++j)
                set(i,j,value);
    }
 
    //Getters & Setters
    T at(int i, int j) const
    {
        return operator()(i,j);
    }
 
    void set(const int i, const int j, T value)
    {
        operator()(i,j) = value;
    }
 
    int getNbRows() const
    {
        return _rows;
    }
 
    int getNbCols() const
    {
        return _cols;
    }
 
    static int getCoutWidth()
    {
        return Matrix::COUTWIDTH;
    }
 
    static void setCoutWidth(const int width)
    {
        Matrix::COUTWIDTH = width+1;
    }
 
    bool isSquare () const
    {
        return (_cols == _rows);
    }
 
    bool isIdentity() const
    {
        if (!isDiagonal())
            return false;
 
        for(int i=0; i < _rows; ++i)
            if(!equal(at(i,i),1))
                return false;
 
        return true;
    }
 
    bool isDiagonal() const
    {
        if (!isSquare())
            return false;
 
        for(int i=0; i < _rows; ++i)
            for(int j=0; j < _cols; ++j)
                if (i!=j && !equal(at(i,j),0))
                    return false;
 
        return true;
    }
 
    bool isNull() const
    {
        for(int i=0; i < _rows; ++i)
            for(int j=0; j < _cols; ++j)
                if (!equal(at(i,j), 0))
                    return false;
 
        return true;
    }
 
    bool isUpperTriangular() const
    {
        if (!isSquare())
            return false;
 
        for(int i = 1; i < _rows; ++i)
            for (int j = 0; i != j && j < _cols ; ++j)
                if (!equal(at(i,j), 0))
                    return false;
 
        return true;
    }
 
    bool isLowerTriangular() const
    {
        if (!isSquare())
            return false;
 
        for(int i = 0; i < _rows; ++i)
            for (int j = i+1; j < _cols ; ++j)
                if (!equal(at(i,j), 0))
                    return false;
 
        return true;
    }
 
    //Matrix display
    void show () const
    {
        std::cout <<*this;    
    }
 
    //Destructor
    ~Matrix()
    {
        delete[] _values;
    }
 
    static int COUTWIDTH;
 
private:
    int _rows, _cols;
    T* _values;
 
};
 
template<typename T>
int Matrix<T>::COUTWIDTH = 8;
 
template<typename T, typename NumericType> 
Matrix<T> operator* (const NumericType op, const Matrix<T>& mat)
{
    return mat.mult(op);
}
 
template<typename T, typename NumericType> 
Matrix<T> operator* (const Matrix<T>& mat, const NumericType op)
{
    return mat.mult(op);
}
 
template<typename T, typename U>
Matrix<T> operator* (const Matrix<T>& mat1, const Matrix<U>& mat2)
{
    return mat1.mult(mat2);
}
 
template<typename T, typename NumericType> 
Matrix<T>& operator*= (const NumericType op, Matrix<T>& mat)
{
    return (mat = mat.mult(op));
}
 
template<typename T, typename NumericType> 
Matrix<T>& operator*= (Matrix<T>& mat,const NumericType op)
{
    return (mat = mat.mult(op));
}
 
template<typename T, typename U>
Matrix<T>& operator*= (Matrix<T>& mat1, const Matrix<U>& mat2)
{
    return (mat1 = mat1.mult(mat2));
}
 
template<typename T, typename U>
Matrix<T> operator+ (const Matrix<T>& mat1, const Matrix<U>& mat2)
{
    return mat1.add(mat2);
}
 
template<typename T, typename U>
Matrix<T>& operator+= (Matrix<T>& mat1, const Matrix<U>& mat2)
{
    return (mat1 = mat1.add(mat2));
}
 
template <typename T>
std::ostream& operator << (std::ostream& out, const Matrix<T>& mat)
{
    out <<"Matrix " <<mat.getNbRows() <<"x" <<mat.getNbCols() <<std::endl;
 
    for(int i=0 ; i <= mat.getNbCols()*Matrix<T>::COUTWIDTH+1; ++i)
            out <<"=";
    out <<std::endl;   
 
    for(int i=0 ; i < mat.getNbRows(); ++i)
    {
        out <<"|";
        out.width(Matrix<T>::COUTWIDTH);
        out <<mat(i, 0);
 
        for(int j=1 ; j < mat.getNbCols(); ++j)
        {
            out.width(Matrix<T>::COUTWIDTH);
            out << std::right <<mat(i,j);
        }
        out <<"|" <<std::endl;
    }
 
    return out;
}
 
#endif //MATRIX_H
J'écouterai toutes les remarques constructives. N'hésitez surtout pas.
Je sais que beaucoup verront des choses qui leurs feront saigner les yeux. C'est justement ça que je veux corriger
Merci à tous et à toutes