1 |
#include "gestionversion.h"
|
2 |
|
3 |
//---------------------------------------------------------------------------
|
4 |
|
5 |
#pragma hdrstop
|
6 |
#include <iomanip>
|
7 |
#include "matpleine.h"
|
8 |
//---------------------------------------------------------------------------
|
9 |
#pragma package(smart_init)
|
10 |
|
11 |
#include <math>
|
12 |
|
13 |
|
14 |
//------------------------------------
|
15 |
// Implementation of Matrice Class
|
16 |
//------------------------------------
|
17 |
|
18 |
|
19 |
#include <fstream>
|
20 |
#include <cmath>
|
21 |
|
22 |
|
23 |
Matrice::Matrice()
|
24 |
{
|
25 |
Data = NULL;
|
26 |
NbCols = NbRows = 0;
|
27 |
}
|
28 |
|
29 |
|
30 |
Matrice::Matrice( int Rows, int Cols,double InitVal)
|
31 |
{
|
32 |
NbRows = Rows;
|
33 |
NbCols = Cols;
|
34 |
|
35 |
Data = new double*[NbRows];
|
36 |
for(int i=0; i<NbRows; ++i) {
|
37 |
Data[i] = new double[NbCols];
|
38 |
for(int j=0; j<NbCols; ++j) {
|
39 |
Data[i][j] = InitVal;
|
40 |
}
|
41 |
}
|
42 |
}
|
43 |
|
44 |
|
45 |
Matrice::Matrice(double* m_pData, int Rows, int Cols)
|
46 |
{
|
47 |
NbRows = Rows;
|
48 |
NbCols = Cols;
|
49 |
|
50 |
Data = new double*[NbRows];
|
51 |
for(int i=0; i<NbRows; ++i) {
|
52 |
Data[i] = new double[NbCols];
|
53 |
for(int j=0; j<NbCols; ++j) {
|
54 |
Data[i][j] = m_pData[i*NbRows+j];
|
55 |
}
|
56 |
}
|
57 |
}
|
58 |
|
59 |
Matrice::Matrice(double** m_nData, int Rows, int Cols)
|
60 |
{
|
61 |
NbRows = Rows;
|
62 |
NbCols = Cols;
|
63 |
|
64 |
Data = new double*[NbRows];
|
65 |
for(int i=0; i<NbRows; ++i) {
|
66 |
Data[i] = new double[NbCols];
|
67 |
for(int j=0; j<NbCols; ++j) {
|
68 |
Data[i][j] = m_nData[i][j];
|
69 |
}
|
70 |
}
|
71 |
}
|
72 |
|
73 |
|
74 |
Matrice::Matrice(const Matrice& obj)
|
75 |
{
|
76 |
NbRows = obj.NbRows;
|
77 |
NbCols = obj.NbCols;
|
78 |
|
79 |
Data = new double*[NbRows];
|
80 |
for(int i=0; i<NbRows; ++i) {
|
81 |
Data[i] = new double[NbCols];
|
82 |
for(int j=0; j<NbCols; ++j) {
|
83 |
Data[i][j] = obj.Data[i][j];
|
84 |
}
|
85 |
}
|
86 |
}
|
87 |
|
88 |
|
89 |
Matrice::~Matrice()
|
90 |
{
|
91 |
if(Data){
|
92 |
for(int i=0; i<NbRows; ++i) {
|
93 |
delete[] Data[i];
|
94 |
Data[i] = NULL;
|
95 |
}
|
96 |
delete[] Data;
|
97 |
}
|
98 |
Data = NULL;
|
99 |
}
|
100 |
|
101 |
//assignment operator
|
102 |
Matrice& Matrice::operator =(const Matrice& obj)
|
103 |
{
|
104 |
if(&obj == this) return *this;
|
105 |
|
106 |
if(NbRows || NbCols) {
|
107 |
for(int i=0; i<NbRows; ++i) {
|
108 |
delete[] Data[i];
|
109 |
}
|
110 |
delete[] Data;
|
111 |
}
|
112 |
|
113 |
NbRows = obj.NbRows;
|
114 |
NbCols = obj.NbCols;
|
115 |
|
116 |
Data = new double*[NbRows];
|
117 |
for(int i=0; i<NbRows; ++i) {
|
118 |
Data[i] = new double[NbCols];
|
119 |
for(int j=0; j<NbCols; ++j) {
|
120 |
Data[i][j] = obj.Data[i][j];
|
121 |
}
|
122 |
}
|
123 |
|
124 |
return *this;
|
125 |
}
|
126 |
|
127 |
Matrice& Matrice::operator +(const Matrice& obj) const
|
128 |
{
|
129 |
assert((NbRows == obj.NbRows)&& (NbCols == obj.NbCols)) ;
|
130 |
|
131 |
|
132 |
static Matrice temp;
|
133 |
temp = Matrice( NbRows, NbCols,0.0);
|
134 |
|
135 |
for(int i=0; i<NbRows; ++i) {
|
136 |
for(int j=0; j<NbCols; ++j) {
|
137 |
temp.Data[i][j] = Data[i][j] + obj.Data[i][j];
|
138 |
}
|
139 |
}
|
140 |
|
141 |
return temp;
|
142 |
}
|
143 |
|
144 |
|
145 |
Matrice& Matrice::operator -(const Matrice& obj) const
|
146 |
{
|
147 |
assert((NbRows == obj.NbRows)&& (NbCols == obj.NbCols)) ;
|
148 |
|
149 |
static Matrice temp;
|
150 |
temp = Matrice(0.0, NbRows, NbCols);
|
151 |
|
152 |
for(int i=0; i<NbRows; ++i) {
|
153 |
for(int j=0; j<NbCols; ++j) {
|
154 |
temp.Data[i][j] = Data[i][j] - obj.Data[i][j];
|
155 |
}
|
156 |
}
|
157 |
|
158 |
return temp;
|
159 |
}
|
160 |
|
161 |
|
162 |
Matrice& Matrice::operator *(const Matrice& obj) const
|
163 |
{
|
164 |
assert(NbCols == obj.NbRows);
|
165 |
|
166 |
double sum = double(0);
|
167 |
double prod = double(1);
|
168 |
|
169 |
static Matrice temp;
|
170 |
temp = Matrice( NbRows, obj.NbCols,0.0);
|
171 |
|
172 |
for(int i=0; i<temp.NbRows; ++i) {
|
173 |
for(int j=0; j<temp.NbCols; ++j) {
|
174 |
sum = double(0);
|
175 |
for(int q=0; q<NbCols; ++q) {
|
176 |
prod = Data[i][q] * obj.Data[q][j];
|
177 |
sum += prod;
|
178 |
}
|
179 |
temp.Data[i][j] = sum;
|
180 |
}
|
181 |
}
|
182 |
|
183 |
return temp;
|
184 |
}
|
185 |
|
186 |
//multiply a Matrice by a double scalar
|
187 |
Matrice& Matrice::operator *(const double _d) const
|
188 |
{
|
189 |
static Matrice temp;
|
190 |
temp = Matrice(Data, NbRows,NbCols);
|
191 |
|
192 |
for(int i=0; i<NbRows; ++i) {
|
193 |
for(int j=0; j<NbCols; ++j) {
|
194 |
temp.Data[i][j] *= _d;
|
195 |
}
|
196 |
}
|
197 |
|
198 |
return temp;
|
199 |
}
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
//divide a Matrice by a double scalar
|
206 |
Matrice& Matrice::operator /(const double _d) const
|
207 |
{
|
208 |
assert(_d != 0) ;
|
209 |
|
210 |
static Matrice temp;
|
211 |
temp = Matrice(Data, NbCols, NbRows);
|
212 |
|
213 |
for(int i=0; i<temp.NbRows; ++i) {
|
214 |
for(int j=0; j<temp.NbCols; ++j) {
|
215 |
temp.Data[i][j] /= _d;
|
216 |
}
|
217 |
}
|
218 |
|
219 |
return temp;
|
220 |
}
|
221 |
|
222 |
|
223 |
|
224 |
//add two matrices and assign
|
225 |
Matrice& Matrice::operator +=(const Matrice& obj)
|
226 |
{
|
227 |
return(*this = *this + obj);
|
228 |
}
|
229 |
|
230 |
//subtract two matrices and assign
|
231 |
Matrice& Matrice::operator -=(const Matrice& obj)
|
232 |
{
|
233 |
return(*this = *this - obj);
|
234 |
}
|
235 |
|
236 |
//multiply two matrices and assign
|
237 |
Matrice& Matrice::operator *=(const Matrice& obj)
|
238 |
{
|
239 |
return(*this = *this * obj);
|
240 |
}
|
241 |
|
242 |
//multiply a Matrice by a double scalar and assign
|
243 |
Matrice& Matrice::operator *=(const double _d)
|
244 |
{
|
245 |
return(*this = *this * _d);
|
246 |
}
|
247 |
|
248 |
|
249 |
|
250 |
//divide a Matrice by a double scalar and assign
|
251 |
Matrice& Matrice::operator /=(const double _d)
|
252 |
{
|
253 |
return(*this = *this / _d);
|
254 |
}
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
//equality operator
|
261 |
bool Matrice::operator ==(const Matrice& obj) const
|
262 |
{
|
263 |
if(NbRows != obj.NbRows) return false;
|
264 |
if(NbCols != obj.NbCols) return false;
|
265 |
|
266 |
for(int i=0; i<NbRows; ++i) {
|
267 |
for(int j=0; j<NbCols; ++j) {
|
268 |
if(Data[i][j] != obj.Data[i][j]) return false;
|
269 |
}
|
270 |
}
|
271 |
|
272 |
return true;
|
273 |
}
|
274 |
|
275 |
//inequality operator
|
276 |
bool Matrice ::operator !=(const Matrice& obj) const
|
277 |
{
|
278 |
if(NbRows != obj.NbRows) return true;
|
279 |
if(NbCols != obj.NbCols) return true;
|
280 |
|
281 |
for(int i=0; i<NbRows; ++i) {
|
282 |
for(int j=0; j<NbCols; ++j) {
|
283 |
if(Data[i][j] != obj.Data[i][j]) return true;
|
284 |
}
|
285 |
}
|
286 |
|
287 |
return false;
|
288 |
}
|
289 |
|
290 |
//renvoie la ieme ligne
|
291 |
double* Matrice ::operator [](const int _i) const
|
292 |
{
|
293 |
assert((_i < NbRows)&& (_i >= 0));
|
294 |
|
295 |
return Data[_i];
|
296 |
}
|
297 |
|
298 |
//another indexing operator, but takes both rows and columns
|
299 |
//remember, a zero (0) element DOES exist in this Matrice, although not in a real Matrice
|
300 |
double& Matrice ::operator ()(const int _i, const int _j) const
|
301 |
{
|
302 |
assert((_i < NbRows) && (_j < NbCols));
|
303 |
assert((_i >= 0) && (_j >= 0));
|
304 |
|
305 |
return Data[_i][_j];
|
306 |
}
|
307 |
|
308 |
//returns true if the calling Matrice is an identity Matrice
|
309 |
bool Matrice::IsIdentity() const
|
310 |
{
|
311 |
if(NbCols != NbRows) return false;
|
312 |
|
313 |
for(int i=0; i<NbCols; ++i) {
|
314 |
for(int j=0; j<NbRows; ++j) {
|
315 |
if(i == j) {
|
316 |
if(Data[i][j] != 1) return false;
|
317 |
}
|
318 |
else if(Data[i][j] != 0) return false;
|
319 |
}
|
320 |
}
|
321 |
|
322 |
return true;
|
323 |
}
|
324 |
|
325 |
//returns true if every element in the calling Matrice is zero (0)
|
326 |
bool Matrice::IsZeroMatrice() const
|
327 |
{
|
328 |
for(int i=0; i<NbRows; ++i) {
|
329 |
for(int j=0; j<NbCols; ++j) {
|
330 |
if(Data[i][j] != 0) return false;
|
331 |
}
|
332 |
}
|
333 |
|
334 |
return true;
|
335 |
}
|
336 |
|
337 |
|
338 |
//renvoie la somme des tous les elemnets de la matrice
|
339 |
//sur les lignes et sur les colones
|
340 |
|
341 |
double Matrice::Get_SumAll() const
|
342 |
{
|
343 |
double sum = 0;
|
344 |
|
345 |
for(int i=0; i<NbRows; ++i) {
|
346 |
for(int j=0; j<NbCols; ++j) {
|
347 |
sum += Data[i][j];
|
348 |
}
|
349 |
}
|
350 |
|
351 |
return sum;
|
352 |
}
|
353 |
|
354 |
|
355 |
|
356 |
//Renvoie la somme de tous les termes d une ligne dans la matrice
|
357 |
|
358 |
double Matrice::Get_SumRow(const int Row) const
|
359 |
{
|
360 |
double sum = double(0.);
|
361 |
|
362 |
for(int j=0; j<NbCols; ++j) {
|
363 |
sum += Data[Row][j];
|
364 |
}
|
365 |
|
366 |
return sum;
|
367 |
}
|
368 |
|
369 |
//Renvoie la somme de tous les ternes d<une colonnne dans la matrice
|
370 |
|
371 |
double Matrice::Get_SumColumn(const int Col) const
|
372 |
{
|
373 |
double sum = double(0);
|
374 |
|
375 |
for(int i=0; i<NbRows; ++i) {
|
376 |
sum += Data[i][Col];
|
377 |
}
|
378 |
|
379 |
return sum;
|
380 |
}
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
//Renvoie le terme le plus grand dans la matrice
|
387 |
|
388 |
double Matrice::Get_Max() const
|
389 |
{
|
390 |
double max = Data[0][0];
|
391 |
|
392 |
for(int i=0; i<NbRows; ++i) {
|
393 |
for(int j=0; j<NbCols; ++j) {
|
394 |
if(Data[i][j] > max) max = Data[i][j];
|
395 |
}
|
396 |
}
|
397 |
|
398 |
return max;
|
399 |
}
|
400 |
// Renvoie le termes le plus grand en valeur absolue dans la matrice
|
401 |
|
402 |
double Matrice::Get_AbMax() const
|
403 |
{
|
404 |
double max = fabs(Data[0][0]);
|
405 |
|
406 |
for(int i=0; i<NbRows; ++i) {
|
407 |
for(int j=0; j<NbCols; ++j) {
|
408 |
if(fabs(Data[i][j]) > max) max = fabs(Data[i][j]);
|
409 |
}
|
410 |
}
|
411 |
|
412 |
return max;
|
413 |
}
|
414 |
|
415 |
//Renvoie le terme le plus petit dans la matrice
|
416 |
|
417 |
double Matrice ::Get_Min() const
|
418 |
{
|
419 |
double min = Data[0][0];
|
420 |
|
421 |
for(int i=0; i<NbRows; ++i) {
|
422 |
for(int j=0; j<NbCols; ++j) {
|
423 |
if(Data[i][j] < min) min = Data[i][j];
|
424 |
}
|
425 |
}
|
426 |
|
427 |
return min;
|
428 |
}
|
429 |
|
430 |
//returns the largest value in row 'Row' in the Matrice
|
431 |
double Matrice::Get_RowMax(const int Row) const
|
432 |
{
|
433 |
double max = Data[Row][0];
|
434 |
|
435 |
for(int j=0; j<NbCols; ++j) {
|
436 |
if(Data[Row][j] > max) max = Data[Row][j];
|
437 |
}
|
438 |
|
439 |
return max;
|
440 |
}
|
441 |
|
442 |
//returns the smallest value in row 'Row' in the Matrice
|
443 |
double Matrice::Get_RowMin(const int Row) const
|
444 |
{
|
445 |
double min = Data[Row][0];
|
446 |
|
447 |
for(int j=0; j<NbCols; ++j) {
|
448 |
if(Data[Row][j] < min) min = Data[Row][j];
|
449 |
}
|
450 |
|
451 |
return min;
|
452 |
}
|
453 |
|
454 |
//returns the largest value in column 'Col' in the Matrice
|
455 |
double Matrice::Get_ColumnMax(const int Col) const
|
456 |
{
|
457 |
double max = Data[0][Col];
|
458 |
|
459 |
for(int i=0; i<NbRows; ++i) {
|
460 |
if(Data[i][Col] > max) max = Data[i][Col];
|
461 |
}
|
462 |
|
463 |
return max;
|
464 |
}
|
465 |
|
466 |
//returns the smallest value in column 'Col' in the Matrice
|
467 |
double Matrice::Get_ColumnMin(const int Col) const
|
468 |
{
|
469 |
double min = Data[0][Col];
|
470 |
|
471 |
for(int i=0; i<NbRows; ++i) {
|
472 |
if(Data[i][Col] < min) min = Data[i][Col];
|
473 |
}
|
474 |
|
475 |
return min;
|
476 |
}
|
477 |
|
478 |
|
479 |
// Renvoie la matrice dans un tableau a une dimension;
|
480 |
|
481 |
double* Matrice ::Get_DataOneDimen() const
|
482 |
{
|
483 |
double* newData;
|
484 |
|
485 |
newData = new double[NbRows * NbCols];
|
486 |
|
487 |
for(int i=0; i<NbRows; ++i) {
|
488 |
for(int j=0; j<NbCols; ++j) {
|
489 |
newData[(i*NbRows)+j] = Data[i][j];
|
490 |
}
|
491 |
}
|
492 |
|
493 |
return newData;
|
494 |
}
|
495 |
|
496 |
// Renvoie la matrice dans un tableu a deux dimension
|
497 |
|
498 |
double** Matrice ::Get_DataTwoDimen() const
|
499 |
{
|
500 |
double** newData;
|
501 |
|
502 |
newData = new double*[NbRows];
|
503 |
for(int i=0; i<NbRows; ++i) {
|
504 |
newData[i] = new double[NbCols];
|
505 |
for(int j=0; j<NbCols; ++j) {
|
506 |
newData[i][j] = Data[i][j];
|
507 |
}
|
508 |
}
|
509 |
|
510 |
return newData;
|
511 |
}
|
512 |
|
513 |
//returns number of rows of the Matrice
|
514 |
int Matrice ::Get_NbRows() const
|
515 |
{
|
516 |
return NbRows;
|
517 |
}
|
518 |
|
519 |
//returns number of columns of the Matrice
|
520 |
int Matrice::Get_NbCols() const
|
521 |
{
|
522 |
return NbCols;
|
523 |
}
|
524 |
|
525 |
//clears every entry in the calling Matrice
|
526 |
Matrice& Matrice::Clear()
|
527 |
{
|
528 |
for(int i=0; i<NbRows; ++i) {
|
529 |
for(int j=0; j<NbCols; ++j) {
|
530 |
Data[i][j] = 0;
|
531 |
}
|
532 |
}
|
533 |
|
534 |
return *this;
|
535 |
}
|
536 |
|
537 |
//Mettre tous les termes d<une ligne ala valeur zero
|
538 |
|
539 |
Matrice& Matrice::ClearRow(const int Row)
|
540 |
{
|
541 |
for(int j=0; j<NbCols; ++j) {
|
542 |
Data[Row][j] = 0;
|
543 |
}
|
544 |
|
545 |
return *this;
|
546 |
}
|
547 |
|
548 |
//Metrre tous les termes d' une aolonne en zero
|
549 |
|
550 |
Matrice& Matrice::ClearColumn(const int Col)
|
551 |
{
|
552 |
for(int i=0; i<NbRows; ++i) {
|
553 |
Data[i][Col] = 0;
|
554 |
}
|
555 |
|
556 |
return *this;
|
557 |
}
|
558 |
|
559 |
//Metrre tous les termes D<une matrice a la veleur '_d'
|
560 |
Matrice& Matrice::Fill(const double _d)
|
561 |
{
|
562 |
for(int i=0; i<NbRows; ++i) {
|
563 |
for(int j=0; j<NbCols; ++j) {
|
564 |
Data[i][j] = _d;
|
565 |
}
|
566 |
}
|
567 |
|
568 |
return *this;
|
569 |
}
|
570 |
|
571 |
//fills every entry in the row 'Row' of the calling Matrice to '_d'
|
572 |
Matrice& Matrice::FillRow(const int Row, const double _d)
|
573 |
{
|
574 |
for(int j=0; j<NbCols; ++j) {
|
575 |
Data[Row][j] = _d;
|
576 |
}
|
577 |
|
578 |
return *this;
|
579 |
}
|
580 |
|
581 |
//fills every entry in the column 'Col' of the calling Matrice to '_d'
|
582 |
Matrice& Matrice::FillColumn(const int Col, const double _d)
|
583 |
{
|
584 |
for(int i=0; i<NbRows; ++i) {
|
585 |
Data[i][Col] = _d;
|
586 |
}
|
587 |
|
588 |
return *this;
|
589 |
}
|
590 |
|
591 |
|
592 |
|
593 |
|
594 |
|
595 |
Matrice& Matrice::Get_SubMatrice(const int RowSpot, const int ColSpot, const int RowLen, const int ColLen) const
|
596 |
{
|
597 |
static Matrice temp;
|
598 |
temp = Matrice( RowLen, ColLen,0.0);
|
599 |
|
600 |
for(int i=RowSpot, k=0; i<(RowLen+RowSpot); ++i, ++k) {
|
601 |
for(int j=ColSpot, l=0; j<(ColLen+ColSpot); ++j, ++l) {
|
602 |
temp.Data[k][l] = Data[i][j];
|
603 |
}
|
604 |
}
|
605 |
|
606 |
return temp;
|
607 |
}
|
608 |
|
609 |
//changes the calling Matrice into a subMatrice starting at spot (RowSpot, ColSpot)
|
610 |
//and with lengths of 'RowLen' rows and 'ColLen' columns
|
611 |
Matrice& Matrice::Set_SubMatrice(const int RowSpot, const int ColSpot, const int RowLen, const int ColLen)
|
612 |
{
|
613 |
Matrice temp;
|
614 |
|
615 |
temp = Get_SubMatrice(RowSpot, ColSpot, RowLen, ColLen);
|
616 |
|
617 |
*this = temp;
|
618 |
|
619 |
return *this;
|
620 |
}
|
621 |
|
622 |
Matrice Matrice::Get_SubMatrice(vecteur& lgn,vecteur& col)
|
623 |
{
|
624 |
int t_tmp=lgn.getDim();
|
625 |
Matrice tmp(t_tmp,t_tmp,0.);
|
626 |
for(int i=0;i<t_tmp;i++)
|
627 |
{
|
628 |
int I= lgn[i];
|
629 |
for(int j=0;j<t_tmp;j++)
|
630 |
{
|
631 |
int J= col[j];
|
632 |
tmp.Data[i][j] = Data[I][J];
|
633 |
}
|
634 |
}
|
635 |
return tmp;
|
636 |
}
|
637 |
|
638 |
//swaps two rows, Row1 and Row2, from the calling Matrice
|
639 |
Matrice& Matrice::Get_SwapRows(const int Row1, const int Row2)
|
640 |
{
|
641 |
double* temp;
|
642 |
|
643 |
temp = new double[NbCols];
|
644 |
|
645 |
for(int j=0; j<NbCols; ++j) {
|
646 |
temp[j] = Data[Row1][j];
|
647 |
Data[Row1][j] = Data[Row2][j];
|
648 |
Data[Row2][j] = temp[j];
|
649 |
}
|
650 |
|
651 |
delete[] temp;
|
652 |
|
653 |
return *this;
|
654 |
}
|
655 |
|
656 |
//swaps two columns, Col1 and Col2, from the calling Matrice
|
657 |
Matrice& Matrice::Get_SwapCols(const int Col1, const int Col2)
|
658 |
{
|
659 |
double* temp;
|
660 |
|
661 |
temp = new double[NbRows];
|
662 |
|
663 |
for(int i=0; i<NbRows; ++i) {
|
664 |
temp[i] = Data[i][Col1];
|
665 |
Data[i][Col1] = Data[i][Col2];
|
666 |
Data[i][Col2] = temp[i];
|
667 |
}
|
668 |
|
669 |
delete[] temp;
|
670 |
|
671 |
return *this;
|
672 |
}
|
673 |
|
674 |
//returns the transposition of the calling Matrice
|
675 |
Matrice& Matrice::Get_Transpose() const
|
676 |
{
|
677 |
static Matrice temp;
|
678 |
temp = Matrice(NbCols, NbRows,0.0);
|
679 |
|
680 |
for(int i=0; i<NbRows; ++i) {
|
681 |
for(int j=0; j<NbCols; ++j) {
|
682 |
temp.Data[j][i] = Data[i][j];
|
683 |
}
|
684 |
}
|
685 |
|
686 |
return temp;
|
687 |
}
|
688 |
|
689 |
//transposes the calling Matrice
|
690 |
Matrice& Matrice::Transpose()
|
691 |
{
|
692 |
*this = this->Get_Transpose();
|
693 |
|
694 |
return *this;
|
695 |
}
|
696 |
|
697 |
|
698 |
|
699 |
|
700 |
|
701 |
//CMAR = Concatenate Matrice As Rows
|
702 |
//concatenates Matrice 'obj' on to the right of the calling Matrice
|
703 |
Matrice& Matrice::Get_ConMatAsRow(const Matrice& obj)
|
704 |
{
|
705 |
assert(NbCols == obj.NbCols);// ErrorMsg("mismatched matrices in row concatenation", true);
|
706 |
|
707 |
Matrice temp((NbRows + obj.NbRows), NbCols,0.0);
|
708 |
int i;
|
709 |
for(int i=0; i<NbRows; ++i) {
|
710 |
for(int j=0; j<NbCols; ++j) {
|
711 |
temp.Data[i][j] = Data[i][j];
|
712 |
}
|
713 |
}
|
714 |
for(int k=0; i<temp.NbRows; ++i, ++k) {
|
715 |
for(int j=0; j<NbCols; ++j) {
|
716 |
temp.Data[i][j] = obj.Data[k][j];
|
717 |
}
|
718 |
}
|
719 |
|
720 |
*this = temp;
|
721 |
|
722 |
return *this;
|
723 |
}
|
724 |
|
725 |
//CMAC = Concatenate Matrice As Columns
|
726 |
//concatenates Matrice 'obj' on to the bottom of the calling Matrice
|
727 |
Matrice& Matrice::Get_ConMatAsCol(const Matrice& obj)
|
728 |
{
|
729 |
assert(NbRows == obj.NbRows);// ErrorMsg("mismatched matrices in column concatenation", true);
|
730 |
|
731 |
Matrice temp(NbRows, (NbCols + obj.NbCols),0.0);
|
732 |
int i,j;
|
733 |
for(i=0; i<NbRows; ++i) {
|
734 |
for( j=0; j<NbCols; ++j) {
|
735 |
temp.Data[i][j] = Data[i][j];
|
736 |
}
|
737 |
for(int l=0; l<obj.NbCols; ++l, ++j) {
|
738 |
temp.Data[i][j] = obj.Data[i][l];
|
739 |
}
|
740 |
}
|
741 |
|
742 |
*this = temp;
|
743 |
|
744 |
return *this;
|
745 |
}
|
746 |
|
747 |
//CMAR = Concatenate Matrice As Rows
|
748 |
//returns a new Matrice that is the calling object + 'obj' on the right
|
749 |
Matrice& Matrice::GetCMAR(const Matrice& obj) const
|
750 |
{
|
751 |
static Matrice temp;
|
752 |
|
753 |
temp = *this;
|
754 |
temp.Get_ConMatAsRow(obj);
|
755 |
|
756 |
return temp;
|
757 |
}
|
758 |
|
759 |
//CMAC = Concatenate Matrice As Columns
|
760 |
//returns a new Matrice that is the valling object + 'obj' on the bottom
|
761 |
Matrice& Matrice::GetCMAC(const Matrice& obj) const
|
762 |
{
|
763 |
static Matrice temp;
|
764 |
|
765 |
temp = *this;
|
766 |
temp.Get_ConMatAsCol(obj);
|
767 |
|
768 |
return temp;
|
769 |
}
|
770 |
|
771 |
//adds a row onto the right of the calling Matrice
|
772 |
Matrice& Matrice::ConcatenateRow(const double* RowData)
|
773 |
{
|
774 |
Matrice temp( NbRows+1, NbCols,0.0);
|
775 |
int i;
|
776 |
for(i=0; i<NbRows; ++i) {
|
777 |
for(int j=0; j<NbCols; ++j) {
|
778 |
temp.Data[i][j] = Data[i][j];
|
779 |
}
|
780 |
}
|
781 |
int j;
|
782 |
for(i=NbRows, j=0; j<NbCols; ++j) {
|
783 |
temp.Data[i][j] = RowData[j];
|
784 |
}
|
785 |
|
786 |
*this = temp;
|
787 |
|
788 |
return *this;
|
789 |
}
|
790 |
|
791 |
//adds a column onto the bottom of the calling Matrice
|
792 |
Matrice& Matrice::ConcatenateColumn(const double* ColumnData)
|
793 |
{
|
794 |
|
795 |
Matrice temp( NbRows, NbCols+1,0.0);
|
796 |
int i;
|
797 |
for(i=0; i<NbRows; ++i) {
|
798 |
for(int j=0; j<NbCols; ++j) {
|
799 |
temp.Data[i][j] = Data[i][j];
|
800 |
}
|
801 |
}
|
802 |
int j;
|
803 |
for(j=NbCols, i=0; i<NbRows; ++i) {
|
804 |
temp.Data[i][j] = ColumnData[i];
|
805 |
}
|
806 |
|
807 |
*this = temp;
|
808 |
|
809 |
return *this;
|
810 |
}
|
811 |
|
812 |
//adds a row into the Matrice in the spot 'RowSpot'
|
813 |
Matrice& Matrice::Add_Row(const vecteur& RowData, const int RowSpot)
|
814 |
{
|
815 |
int New_NbRows=NbRows;
|
816 |
int New_NbCols=NbCols;
|
817 |
|
818 |
Matrice temp( NbRows+1, NbCols,0.0);
|
819 |
|
820 |
if(NbRows==0) New_NbRows=NbRows+1;
|
821 |
|
822 |
|
823 |
|
824 |
for(int i=0, k=0; i<New_NbRows; ++i, ++k) {
|
825 |
if(i == RowSpot) {
|
826 |
for(int j=0; j<New_NbCols; ++j) {
|
827 |
temp.Data[i][j] = RowData[j];
|
828 |
}
|
829 |
++k;
|
830 |
}
|
831 |
|
832 |
if(k<New_NbRows){
|
833 |
for(int j=0; j<New_NbCols; ++j)
|
834 |
temp.Data[k][j] = Data[i][j];
|
835 |
|
836 |
}
|
837 |
}
|
838 |
|
839 |
*this = temp;
|
840 |
|
841 |
return *this;
|
842 |
}
|
843 |
|
844 |
//Ajouter une Colonne dans la Matrice in the spot 'ColumnSpot'
|
845 |
|
846 |
Matrice& Matrice::Add_Column(const vecteur& ColumnData, const int ColumnSpot)
|
847 |
{
|
848 |
int New_NbRows=NbRows;
|
849 |
int New_NbCols=NbCols;
|
850 |
|
851 |
Matrice temp(NbRows, NbCols+1,0.0);
|
852 |
|
853 |
if(NbCols==0) New_NbCols=NbCols+1;
|
854 |
|
855 |
for(int i=0; i<NbRows; ++i) {
|
856 |
for(int j=0, k=0; j<New_NbCols; ++j, ++k) {
|
857 |
if(j == ColumnSpot) {
|
858 |
temp.Data[i][k] = ColumnData[i];
|
859 |
++k;
|
860 |
}
|
861 |
if(k<New_NbCols)
|
862 |
temp.Data[i][k] = Data[i][j];
|
863 |
}
|
864 |
}
|
865 |
|
866 |
*this = temp;
|
867 |
|
868 |
return *this;
|
869 |
}
|
870 |
|
871 |
//extraire une ligne
|
872 |
|
873 |
vecteur Matrice::Get_Row(int RowSpot)
|
874 |
{
|
875 |
vecteur RowData(NbCols);
|
876 |
|
877 |
for(int i=0; i<NbRows; ++i) {
|
878 |
if(i == RowSpot) {
|
879 |
for(int j=0; j<NbCols; ++j) {
|
880 |
RowData[j]=Data[i][j];
|
881 |
}
|
882 |
break;
|
883 |
}
|
884 |
|
885 |
}
|
886 |
return RowData;
|
887 |
}
|
888 |
|
889 |
// Extraire une colone
|
890 |
vecteur Matrice::Get_Col(int ColSpot)
|
891 |
{
|
892 |
vecteur ColData(NbCols);
|
893 |
|
894 |
for(int i=0; i<NbRows; ++i) {
|
895 |
for(int j=0; j<NbCols; ++j) {
|
896 |
if(j == ColSpot) {
|
897 |
ColData[j]=Data[i][j];
|
898 |
}
|
899 |
break;
|
900 |
}
|
901 |
|
902 |
}
|
903 |
return ColData;
|
904 |
|
905 |
}
|
906 |
|
907 |
|
908 |
//removes the specified row from the calling Matrice
|
909 |
Matrice& Matrice::RemoveRow(const int Row)
|
910 |
{
|
911 |
Matrice temp(NbRows-1, NbCols,0.0);
|
912 |
|
913 |
for(int i=0, k=0; i<NbRows; ++i, ++k) {
|
914 |
if(i == Row) ++i;
|
915 |
for(int j=0; j<NbCols; ++j) {
|
916 |
temp.Data[k][j] = Data[i][j];
|
917 |
}
|
918 |
}
|
919 |
|
920 |
*this = temp;
|
921 |
|
922 |
return *this;
|
923 |
}
|
924 |
|
925 |
//removes the specified column from the calling Matrice
|
926 |
Matrice& Matrice::RemoveColumn(const int Column)
|
927 |
{
|
928 |
Matrice temp(0.0, NbRows, NbCols-1);
|
929 |
|
930 |
for(int i=0; i<NbRows; ++i) {
|
931 |
for(int j=0, l=0; j<NbCols; ++j, ++l) {
|
932 |
if(j == Column) ++j;
|
933 |
temp.Data[i][l] = Data[i][j];
|
934 |
}
|
935 |
}
|
936 |
|
937 |
*this = temp;
|
938 |
|
939 |
return *this;
|
940 |
}
|
941 |
|
942 |
|
943 |
//returns the covariant of the calling Matrice (transposed(obj) * obj)
|
944 |
Matrice& Matrice::Get_Covariant() const
|
945 |
{
|
946 |
Matrice temp;
|
947 |
|
948 |
temp = *this;
|
949 |
temp.Transpose();
|
950 |
|
951 |
return(*this * temp);
|
952 |
}
|
953 |
|
954 |
//turns this Matrice into its covariant(obj = transposed(obj) * obj)
|
955 |
Matrice& Matrice::Set_Covariant()
|
956 |
{
|
957 |
*this = this->Get_Covariant();
|
958 |
|
959 |
return *this;
|
960 |
}
|
961 |
|
962 |
|
963 |
//returns an identity Matrice of size Diagonal
|
964 |
Matrice& Matrice::IdentityMatrice(int Diagonal)
|
965 |
{
|
966 |
static Matrice temp;
|
967 |
temp = Matrice( Diagonal, Diagonal,0.0);
|
968 |
|
969 |
for(int q=0; q<Diagonal; ++q) {
|
970 |
temp.Data[q][q] = 1;
|
971 |
}
|
972 |
|
973 |
return temp;
|
974 |
}
|
975 |
|
976 |
|
977 |
void Matrice:: affiche()
|
978 |
{
|
979 |
cout << "[";
|
980 |
for(int i=0; i<NbRows; ++i) {
|
981 |
for(int j=0; j<NbCols; ++j) {
|
982 |
cout << "[" << Data[i][j] << "]";
|
983 |
}
|
984 |
if(NbRows-i-1) cout << "\n ";
|
985 |
}
|
986 |
cout << "]\n";
|
987 |
|
988 |
|
989 |
}
|
990 |
|
991 |
//easily display the Matrice, usually to the console
|
992 |
//formatted output
|
993 |
|
994 |
|
995 |
Matrice& Matrice::Get_LU(vecteur& perm,double&d)
|
996 |
{ |
997 |
static const double petit=double(1.0e-20); |
998 |
int taille=NbRows; |
999 |
int imax,i,j,k; |
1000 |
vector<double> v1(taille); |
1001 |
double maxi,temp,som,dum; |
1002 |
d=1; |
1003 |
if(taille&&taille==NbCols) |
1004 |
{ |
1005 |
for(i=0;i<taille;i++) |
1006 |
{ |
1007 |
maxi=double(0); |
1008 |
for(j=0;j<taille;j++) |
1009 |
if((temp=(double)fabs(Data[i][j]))>maxi) |
1010 |
maxi=temp; |
1011 |
if(maxi==0) |
1012 |
{ |
1013 |
this->Clear(); |
1014 |
return *this; |
1015 |
} |
1016 |
v1[i]=1./maxi; |
1017 |
} |
1018 |
|
1019 |
for(j=0;j<taille;j++) |
1020 |
{ |
1021 |
//cherche les colonnnes de lamatrice L (triangulaire inferieur |
1022 |
for(i=0;i<j;i++) |
1023 |
{ |
1024 |
som=Data[i][j]; |
1025 |
for(k=0;k<i;k++) |
1026 |
som-=Data[i][k]*Data[k][j]; |
1027 |
Data[i][j]=som; |
1028 |
} |
1029 |
maxi=0; |
1030 |
|
1031 |
//cherches les lignes de la matrice triangulaire superieur |
1032 |
for(i=j;i<taille;i++) |
1033 |
{ |
1034 |
som=Data[i][j]; |
1035 |
for(k=0;k<j;k++) |
1036 |
som-=Data[i][k]*Data[k][j]; |
1037 |
Data[i][j]=som; |
1038 |
//vèrificstion de pivot |
1039 |
if((dum=v1[i]*fabs(som))>=maxi) |
1040 |
{ |
1041 |
maxi=dum; |
1042 |
imax=i; |
1043 |
} |
1044 |
} |
1045 |
//permuation des lignes imax et j |
1046 |
if(j!=imax) |
1047 |
{ |
1048 |
for(k=0;k<taille;k++) |
1049 |
{ |
1050 |
dum=Data[imax][k]; |
1051 |
Data[imax][k]=Data[j][k]; |
1052 |
Data[j][k]=dum; |
1053 |
} |
1054 |
d*=-1; |
1055 |
v1[imax]=v1[j]; |
1056 |
} |
1057 |
// stoker dans perm la permuattion |
1058 |
perm[j]=imax; |
1059 |
// si le pivot est null |
1060 |
if(Data[j][j]==0.0) |
1061 |
Data[j][j]=petit; |
1062 |
if(j!=taille-1) |
1063 |
{ |
1064 |
dum=1./Data[j][j]; |
1065 |
for(i=j+1;i<taille;i++) |
1066 |
Data[i][j]*=dum; |
1067 |
} |
1068 |
} |
1069 |
} |
1070 |
return *this; |
1071 |
}
|
1072 |
|
1073 |
|
1074 |
vecteur Matrice::Get_Solution(vecteur& perm,const vecteur&b)
|
1075 |
{ |
1076 |
vecteur r1; |
1077 |
int taille=NbRows,i,j,ip,ii=-1; |
1078 |
double som; |
1079 |
|
1080 |
if(taille&&taille==NbCols&&taille==(int)b.getDim())// Get_Size()) |
1081 |
{ |
1082 |
r1=b; |
1083 |
for(i=0;i<taille;i++) |
1084 |
{ |
1085 |
ip=perm[i]; |
1086 |
som=r1[ip]; |
1087 |
r1[ip]=r1[i]; |
1088 |
if(ii>=0) |
1089 |
for(j=ii;j<=i-1;j++) |
1090 |
som-=Data[i][j]*r1[j]; |
1091 |
else if(som) ii=i; |
1092 |
r1[i]=som; |
1093 |
} |
1094 |
for(i=taille-1;i>=0;i--) |
1095 |
{ |
1096 |
som=r1[i]; |
1097 |
for(j=i+1;j<taille;j++) |
1098 |
som-=Data[i][j]*r1[j]; |
1099 |
r1[i]=som/Data[i][i]; |
1100 |
} |
1101 |
} |
1102 |
return r1; |
1103 |
} |
1104 |
|
1105 |
|
1106 |
|
1107 |
Matrice Matrice ::Get_Inverse() |
1108 |
{ |
1109 |
Matrice inv; |
1110 |
|
1111 |
vecteur ei(NbRows); |
1112 |
for(int i=0;i<NbRows;i++) |
1113 |
{ |
1114 |
vecteur perm(NbRows); |
1115 |
ei[i]=1.; |
1116 |
if (i>0) ei[i-1]=0.; |
1117 |
vecteur& xi=Get_Solution(perm,ei); |
1118 |
|
1119 |
|
1120 |
} |
1121 |
|
1122 |
|
1123 |
} |
1124 |
|
1125 |
|
1126 |
|
1127 |
|
1128 |
|
1129 |
|
1130 |
|
1131 |
|
1132 |
|
1133 |
|
1134 |
void Matrice::Get_Solution(vecteur& perm, vecteur& b, vecteur& x) |
1135 |
{ |
1136 |
|
1137 |
int taille=NbRows,i,j,ip,ii=-1; |
1138 |
double som; |
1139 |
|
1140 |
|
1141 |
for(i=0;i<taille;i++) x[i]=b[i]; |
1142 |
|
1143 |
for(i=0;i<taille;i++) |
1144 |
{ |
1145 |
ip=perm[i]; |
1146 |
som=x[ip]; |
1147 |
x[ip]=x[i]; |
1148 |
if(ii>=0) |
1149 |
for(j=ii;j<=i-1;j++) |
1150 |
som-=Data[i][j]*x[j]; |
1151 |
else if(som) ii=i; |
1152 |
x[i]=som; |
1153 |
} |
1154 |
for(i=taille-1;i>=0;i--) |
1155 |
{ |
1156 |
som=x[i]; |
1157 |
for(j=i+1;j<taille;j++) |
1158 |
som-=Data[i][j]*x[j]; |
1159 |
x[i]=som/Data[i][i]; |
1160 |
} |
1161 |
|
1162 |
|
1163 |
} |
1164 |
|
1165 |
|
1166 |
vecteur Matrice:: Multiply(const vecteur& v) |
1167 |
{ |
1168 |
assert (NbCols==v.getDim()); |
1169 |
vecteur z(NbRows); |
1170 |
for(int i=0; i<NbRows; ++i) { |
1171 |
for(int j=0; j<NbCols; ++j) { |
1172 |
z[i] += Data[i][j]*v[j];
|
1173 |
}
|
1174 |
} |
1175 |
|
1176 |
return z; |
1177 |
|
1178 |
} |
1179 |
|
1180 |
|
1181 |
|
1182 |
//extraire une matrice de la ligne 1 jusqua iigne 2 et de la colone 1 jusqua la colonne2 |
1183 |
|
1184 |
Matrice Matrice ::Get_SubMatrice(int ln1,int ln2, int cln1,int cln2) |
1185 |
{ |
1186 |
int nb_lgne=ln2-ln1+1; |
1187 |
int nb_colne=cln2-cln1+1; |
1188 |
Matrice m(nb_lgne, nb_colne,0.); |
1189 |
|
1190 |
for(int i=ln1;i<=ln2;i++) |
1191 |
{ |
1192 |
int I=i-ln1; |
1193 |
for(int j=cln1;j<=cln2;j++) |
1194 |
{ |
1195 |
int J=j-cln1; |
1196 |
m(I,J)=Data[i][j] ; |
1197 |
} |
1198 |
|
1199 |
|
1200 |
} |
1201 |
|
1202 |
return m; |
1203 |
} |
1204 |
|
1205 |
|
1206 |
|
1207 |
|
1208 |
|
1209 |
|
1210 |
|
1211 |
|
1212 |
|
1213 |
|
1214 |
|
1215 |
|
1216 |
|
1217 |
std::ostream& operator <<(std::ostream& os, const Matrice& obj) |
1218 |
{
|
1219 |
//ostr<<"MATRIX :"<<"#"<<obj.Get_NbRows()<<setw(2)<< "#"<<obj.Get_NbCols()<< endl;
|
1220 |
os<<"------------------------------------------------------------------------------------"<<endl;
|
1221 |
os<<"# MATRICE: DIM1= "<< obj.Get_NbRows()<<" , DIM2: "<<obj.Get_NbCols()<<" #"<<endl;
|
1222 |
os<<"------------------------------------------------------------------------------------"<<endl;
|
1223 |
for(int i=0;i<obj.Get_NbRows();i++) {
|
1224 |
for(int j=0;j<obj.Get_NbCols();j++) {
|
1225 |
os<<setw(18)<<obj(i,j);
|
1226 |
}
|
1227 |
os<<endl;
|
1228 |
}
|
1229 |
os<<"------------------------------------------------------------------------------------"<<endl;
|
1230 |
return os;
|
1231 |
/*
|
1232 |
for(int i=0; i<obj.Get_NbRows(); ++i) {
|
1233 |
for(int j=0; j<obj.Get_NbCols(); ++j) {
|
1234 |
if (i==0)ostr<<"["<<setw(9);
|
1235 |
ostr <<setw(10)<<obj[i][j];
|
1236 |
}
|
1237 |
if(obj.Get_NbRows()-i-1) ostr << "\n ";
|
1238 |
}
|
1239 |
ostr<<"]"<<endl;
|
1240 |
|
1241 |
return ostr; */
|
1242 |
}
|
1243 |
|
1244 |
vecteur operator * (const Matrice& m,const vecteur& v)
|
1245 |
{ |
1246 |
assert (m.Get_NbCols()==v.getDim()) ; |
1247 |
vecteur z(m.Get_NbRows()); |
1248 |
for(int i=0; i<m.Get_NbRows(); ++i) { |
1249 |
for(int j=0; j<m.Get_NbCols(); ++j) { |
1250 |
z[i] += m(i,j)*v[j];
|
1251 |
}
|
1252 |
} |
1253 |
|
1254 |
return z; |
1255 |
|
1256 |
} |
1257 |
/* |
1258 |
void Matrice::convert_Mat(const Matrice &mat,double valmin,MatCr &mat1) |
1259 |
{ |
1260 |
int i,j,k,taille=mat.Get_NbRows(); |
1261 |
|
1262 |
if(mat.Get_NbCols()==taille) |
1263 |
{ |
1264 |
mat1._index.resize((unsigned int)(taille+1)); |
1265 |
mat1._values.resize((unsigned int)(taille+1)); |
1266 |
for(i=0;i<taille;mat1._values[(unsigned int)i]=mat(i,i),i++); |
1267 |
mat1._index[0]=taille+1; |
1268 |
k=taille; |
1269 |
for(i=0;i<taille;i++) |
1270 |
{ |
1271 |
for(j=0;j<taille;j++) |
1272 |
if(fabs(mat(i,j))>valmin&&i!=j) |
1273 |
{ |
1274 |
k++; |
1275 |
mat1._index.push_back(j); |
1276 |
mat1._values.push_back(mat(i,j)); |
1277 |
} |
1278 |
mat1._index[i+1]=k+1; |
1279 |
} |
1280 |
} |
1281 |
else |
1282 |
{ |
1283 |
mat1._index.resize(0); |
1284 |
mat1._values.resize(0); |
1285 |
} |
1286 |
} |
1287 |
|
1288 |
|
1289 |
*/
|
1290 |
|
1291 |
|