ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/REPOS_ERICCA/magic/lib/solveur/src/matpleine.cpp
Revision: 166
Committed: Thu Oct 23 21:23:28 2008 UTC (16 years, 6 months ago) by souaissa
Original Path: magic/lib/solveur/solveur/src/matpleine.cpp
File size: 25055 byte(s)
Error occurred while calculating annotation data.
Log Message:
mise a jour du solveur : ajout de la resolution partie plastique

File Contents

# Content
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*NbCols+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(NbRows, NbCols,0.);
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
299 //another indexing operator, but takes both rows and columns
300 //remember, a zero (0) element DOES exist in this Matrice, although not in a real Matrice
301 double& Matrice ::operator ()(const int _i, const int _j) const
302 {
303 assert((_i < NbRows) && (_j < NbCols));
304 assert((_i >= 0) && (_j >= 0));
305
306 return Data[_i][_j];
307 }
308
309 //returns true if the calling Matrice is an identity Matrice
310 bool Matrice::IsIdentity() const
311 {
312 if(NbCols != NbRows) return false;
313
314 for(int i=0; i<NbCols; ++i) {
315 for(int j=0; j<NbRows; ++j) {
316 if(i == j) {
317 if(Data[i][j] != 1) return false;
318 }
319 else if(Data[i][j] != 0) return false;
320 }
321 }
322
323 return true;
324 }
325
326 //returns true if every element in the calling Matrice is zero (0)
327 bool Matrice::IsZeroMatrice() const
328 {
329 for(int i=0; i<NbRows; ++i) {
330 for(int j=0; j<NbCols; ++j) {
331 if(Data[i][j] != 0) return false;
332 }
333 }
334
335 return true;
336 }
337
338
339 //renvoie la somme des tous les elemnets de la matrice
340 //sur les lignes et sur les colones
341
342 double Matrice::Get_SumAll() const
343 {
344 double sum = 0;
345
346 for(int i=0; i<NbRows; ++i) {
347 for(int j=0; j<NbCols; ++j) {
348 sum += Data[i][j];
349 }
350 }
351
352 return sum;
353 }
354
355
356
357 //Renvoie la somme de tous les termes d une ligne dans la matrice
358
359 double Matrice::Get_SumRow(const int Row) const
360 {
361 double sum = double(0.);
362
363 for(int j=0; j<NbCols; ++j) {
364 sum += Data[Row][j];
365 }
366
367 return sum;
368 }
369
370 //Renvoie la somme de tous les ternes d<une colonnne dans la matrice
371
372 double Matrice::Get_SumColumn(const int Col) const
373 {
374 double sum = double(0);
375
376 for(int i=0; i<NbRows; ++i) {
377 sum += Data[i][Col];
378 }
379
380 return sum;
381 }
382
383
384
385
386
387 //Renvoie le terme le plus grand dans la matrice
388
389 double Matrice::Get_Max() const
390 {
391 double max = Data[0][0];
392
393 for(int i=0; i<NbRows; ++i) {
394 for(int j=0; j<NbCols; ++j) {
395 if(Data[i][j] > max) max = Data[i][j];
396 }
397 }
398
399 return max;
400 }
401 // Renvoie le termes le plus grand en valeur absolue dans la matrice
402
403 double Matrice::Get_AbMax() const
404 {
405 double max = fabs(Data[0][0]);
406
407 for(int i=0; i<NbRows; ++i) {
408 for(int j=0; j<NbCols; ++j) {
409 if(fabs(Data[i][j]) > max) max = fabs(Data[i][j]);
410 }
411 }
412
413 return max;
414 }
415
416 //Renvoie le terme le plus petit dans la matrice
417
418 double Matrice ::Get_Min() const
419 {
420 double min = Data[0][0];
421
422 for(int i=0; i<NbRows; ++i) {
423 for(int j=0; j<NbCols; ++j) {
424 if(Data[i][j] < min) min = Data[i][j];
425 }
426 }
427
428 return min;
429 }
430
431 //returns the largest value in row 'Row' in the Matrice
432 double Matrice::Get_RowMax(const int Row) const
433 {
434 double max = Data[Row][0];
435
436 for(int j=0; j<NbCols; ++j) {
437 if(Data[Row][j] > max) max = Data[Row][j];
438 }
439
440 return max;
441 }
442
443 //returns the smallest value in row 'Row' in the Matrice
444 double Matrice::Get_RowMin(const int Row) const
445 {
446 double min = Data[Row][0];
447
448 for(int j=0; j<NbCols; ++j) {
449 if(Data[Row][j] < min) min = Data[Row][j];
450 }
451
452 return min;
453 }
454
455 //returns the largest value in column 'Col' in the Matrice
456 double Matrice::Get_ColumnMax(const int Col) const
457 {
458 double max = Data[0][Col];
459
460 for(int i=0; i<NbRows; ++i) {
461 if(Data[i][Col] > max) max = Data[i][Col];
462 }
463
464 return max;
465 }
466
467 //returns the smallest value in column 'Col' in the Matrice
468 double Matrice::Get_ColumnMin(const int Col) const
469 {
470 double min = Data[0][Col];
471
472 for(int i=0; i<NbRows; ++i) {
473 if(Data[i][Col] < min) min = Data[i][Col];
474 }
475
476 return min;
477 }
478
479
480 // Renvoie la matrice dans un tableau a une dimension;
481
482 double* Matrice ::Get_DataOneDimen() const
483 {
484 double* newData;
485
486 newData = new double[NbRows * NbCols];
487
488 for(int i=0; i<NbRows; ++i) {
489 for(int j=0; j<NbCols; ++j) {
490 newData[(i*NbRows)+j] = Data[i][j];
491 }
492 }
493
494 return newData;
495 }
496
497 // Renvoie la matrice dans un tableu a deux dimension
498
499 double** Matrice ::Get_DataTwoDimen() const
500 {
501 double** newData;
502
503 newData = new double*[NbRows];
504 for(int i=0; i<NbRows; ++i) {
505 newData[i] = new double[NbCols];
506 for(int j=0; j<NbCols; ++j) {
507 newData[i][j] = Data[i][j];
508 }
509 }
510
511 return newData;
512 }
513
514 //returns number of rows of the Matrice
515 int Matrice ::Get_NbRows() const
516 {
517 return NbRows;
518 }
519
520 //returns number of columns of the Matrice
521 int Matrice::Get_NbCols() const
522 {
523 return NbCols;
524 }
525
526 //clears every entry in the calling Matrice
527 Matrice& Matrice::Clear()
528 {
529 for(int i=0; i<NbRows; ++i) {
530 for(int j=0; j<NbCols; ++j) {
531 Data[i][j] = 0;
532 }
533 }
534
535 return *this;
536 }
537
538 //Mettre tous les termes d<une ligne ala valeur zero
539
540 Matrice& Matrice::ClearRow(const int Row)
541 {
542 for(int j=0; j<NbCols; ++j) {
543 Data[Row][j] = 0;
544 }
545
546 return *this;
547 }
548
549 //Metrre tous les termes d' une aolonne en zero
550
551 Matrice& Matrice::ClearColumn(const int Col)
552 {
553 for(int i=0; i<NbRows; ++i) {
554 Data[i][Col] = 0;
555 }
556
557 return *this;
558 }
559
560 //Metrre tous les termes D<une matrice a la veleur '_d'
561 Matrice& Matrice::Fill(const double _d)
562 {
563 for(int i=0; i<NbRows; ++i) {
564 for(int j=0; j<NbCols; ++j) {
565 Data[i][j] = _d;
566 }
567 }
568
569 return *this;
570 }
571
572 //fills every entry in the row 'Row' of the calling Matrice to '_d'
573 Matrice& Matrice::FillRow(const int Row, const double _d)
574 {
575 for(int j=0; j<NbCols; ++j) {
576 Data[Row][j] = _d;
577 }
578
579 return *this;
580 }
581
582 //fills every entry in the column 'Col' of the calling Matrice to '_d'
583 Matrice& Matrice::FillColumn(const int Col, const double _d)
584 {
585 for(int i=0; i<NbRows; ++i) {
586 Data[i][Col] = _d;
587 }
588
589 return *this;
590 }
591
592
593
594
595
596 Matrice& Matrice::Get_SubMatrice(const int RowSpot, const int ColSpot, const int RowLen, const int ColLen) const
597 {
598 static Matrice temp;
599 temp = Matrice( RowLen, ColLen,0.0);
600
601 for(int i=RowSpot, k=0; i<(RowLen+RowSpot); i++, k++) {
602 for(int j=ColSpot, l=0; j<(ColLen+ColSpot); j++, l++) {
603 temp(k,l) = Data[i][j];
604 }
605 }
606
607 return temp;
608 }
609
610 //changes the calling Matrice into a subMatrice starting at spot (RowSpot, ColSpot)
611 //and with lengths of 'RowLen' rows and 'ColLen' columns
612 Matrice& Matrice::Set_SubMatrice(Matrice& sub,const int RowSpot, const int ColSpot, const int RowLen, const int ColLen)
613 {
614
615 for(int i=RowSpot, k=0;i<RowSpot+RowLen;i++,++k)
616 {
617 for(int j=ColSpot, l=0;j< ColSpot+ColLen;j++,++l)
618 Data[i][j]=sub(k,l);
619 }
620
621 return *this;
622 }
623
624 Matrice Matrice::Get_SubMatrice(vecteur& lgn,vecteur& col)
625 {
626 int t_tmp=lgn.getDim();
627 Matrice tmp(t_tmp,t_tmp,0.);
628 for(int i=0;i<t_tmp;i++)
629 {
630 int I= lgn[i];
631 for(int j=0;j<t_tmp;j++)
632 {
633 int J= col[j];
634 tmp.Data[i][j] = Data[I][J];
635 }
636 }
637 return tmp;
638 }
639
640 //swaps two rows, Row1 and Row2, from the calling Matrice
641 Matrice& Matrice::Get_SwapRows(const int Row1, const int Row2)
642 {
643 double* temp;
644
645 temp = new double[NbCols];
646
647 for(int j=0; j<NbCols; ++j) {
648 temp[j] = Data[Row1][j];
649 Data[Row1][j] = Data[Row2][j];
650 Data[Row2][j] = temp[j];
651 }
652
653 delete[] temp;
654
655 return *this;
656 }
657
658 //swaps two columns, Col1 and Col2, from the calling Matrice
659 Matrice& Matrice::Get_SwapCols(const int Col1, const int Col2)
660 {
661 double* temp;
662
663 temp = new double[NbRows];
664
665 for(int i=0; i<NbRows; ++i) {
666 temp[i] = Data[i][Col1];
667 Data[i][Col1] = Data[i][Col2];
668 Data[i][Col2] = temp[i];
669 }
670
671 delete[] temp;
672
673 return *this;
674 }
675
676 //returns the transposition of the calling Matrice
677 Matrice& Matrice::Get_Transpose() const
678 {
679 static Matrice temp;
680 temp = Matrice(NbCols, NbRows,0.0);
681
682 for(int i=0; i<NbRows; ++i) {
683 for(int j=0; j<NbCols; ++j) {
684 temp.Data[j][i] = Data[i][j];
685 }
686 }
687
688 return temp;
689 }
690
691 //transposes the calling Matrice
692 Matrice& Matrice::Transpose()
693 {
694 *this = this->Get_Transpose();
695
696 return *this;
697 }
698
699
700
701
702
703 //CMAR = Concatenate Matrice As Rows
704 //concatenates Matrice 'obj' on to the right of the calling Matrice
705 Matrice& Matrice::Get_ConMatAsRow(const Matrice& obj)
706 {
707 assert(NbCols == obj.NbCols);// ErrorMsg("mismatched matrices in row concatenation", true);
708
709 Matrice temp((NbRows + obj.NbRows), NbCols,0.0);
710 int i;
711 for(int i=0; i<NbRows; ++i) {
712 for(int j=0; j<NbCols; ++j) {
713 temp.Data[i][j] = Data[i][j];
714 }
715 }
716 for(int k=0; i<temp.NbRows; ++i, ++k) {
717 for(int j=0; j<NbCols; ++j) {
718 temp.Data[i][j] = obj.Data[k][j];
719 }
720 }
721
722 *this = temp;
723
724 return *this;
725 }
726
727 //CMAC = Concatenate Matrice As Columns
728 //concatenates Matrice 'obj' on to the bottom of the calling Matrice
729 Matrice& Matrice::Get_ConMatAsCol(const Matrice& obj)
730 {
731 assert(NbRows == obj.NbRows);// ErrorMsg("mismatched matrices in column concatenation", true);
732
733 Matrice temp(NbRows, (NbCols + obj.NbCols),0.0);
734 int i,j;
735 for(i=0; i<NbRows; ++i) {
736 for( j=0; j<NbCols; ++j) {
737 temp.Data[i][j] = Data[i][j];
738 }
739 for(int l=0; l<obj.NbCols; ++l, ++j) {
740 temp.Data[i][j] = obj.Data[i][l];
741 }
742 }
743
744 *this = temp;
745
746 return *this;
747 }
748
749 //CMAR = Concatenate Matrice As Rows
750 //returns a new Matrice that is the calling object + 'obj' on the right
751 Matrice& Matrice::GetCMAR(const Matrice& obj) const
752 {
753 static Matrice temp;
754
755 temp = *this;
756 temp.Get_ConMatAsRow(obj);
757
758 return temp;
759 }
760
761 //CMAC = Concatenate Matrice As Columns
762 //returns a new Matrice that is the valling object + 'obj' on the bottom
763 Matrice& Matrice::GetCMAC(const Matrice& obj) const
764 {
765 static Matrice temp;
766
767 temp = *this;
768 temp.Get_ConMatAsCol(obj);
769
770 return temp;
771 }
772
773 //adds a row onto the right of the calling Matrice
774 Matrice& Matrice::ConcatenateRow(const double* RowData)
775 {
776 Matrice temp( NbRows+1, NbCols,0.0);
777 int i;
778 for(i=0; i<NbRows; ++i) {
779 for(int j=0; j<NbCols; ++j) {
780 temp.Data[i][j] = Data[i][j];
781 }
782 }
783 int j;
784 for(i=NbRows, j=0; j<NbCols; ++j) {
785 temp.Data[i][j] = RowData[j];
786 }
787
788 *this = temp;
789
790 return *this;
791 }
792
793 //adds a column onto the bottom of the calling Matrice
794 Matrice& Matrice::ConcatenateColumn(const double* ColumnData)
795 {
796
797 Matrice temp( NbRows, NbCols+1,0.0);
798 int i;
799 for(i=0; i<NbRows; ++i) {
800 for(int j=0; j<NbCols; ++j) {
801 temp.Data[i][j] = Data[i][j];
802 }
803 }
804 int j;
805 for(j=NbCols, i=0; i<NbRows; ++i) {
806 temp.Data[i][j] = ColumnData[i];
807 }
808
809 *this = temp;
810
811 return *this;
812 }
813
814 //adds a row into the Matrice in the spot 'RowSpot'
815 Matrice& Matrice::Add_Row(const vecteur& RowData, const int RowSpot)
816 {
817 int New_NbRows=NbRows;
818 int New_NbCols=NbCols;
819
820 Matrice temp( NbRows+1, NbCols,0.0);
821
822 //if(NbRows==0)
823 New_NbRows=NbRows+1;
824
825
826
827 for(int i=0, k=0; i<New_NbRows; ++i, ++k) {
828 if(i == RowSpot) {
829 for(int j=0; j<New_NbCols; ++j) {
830 temp.Data[i][j] += RowData[j];
831 }
832 ++k;
833 }
834
835 if(k<New_NbRows){
836 for(int j=0; j<New_NbCols; ++j)
837 temp.Data[k][j] += Data[i][j];
838
839 }
840 }
841
842 *this = temp;
843
844 return *this;
845 }
846
847 //Ajouter une Colonne dans la Matrice in the spot 'ColumnSpot'
848
849 Matrice& Matrice::Add_Column(const vecteur& ColumnData, const int ColumnSpot)
850 {
851 int New_NbRows=NbRows;
852 int New_NbCols=NbCols;
853
854 Matrice temp(NbRows, NbCols+1,0.0);
855
856 // if(NbCols==0)
857 New_NbCols=NbCols+1;
858
859 for(int i=0; i<NbRows; ++i) {
860 for(int j=0, k=0; j<New_NbCols; ++j, ++k) {
861 if(j == ColumnSpot) {
862 temp.Data[i][k] += ColumnData[i];
863 ++k;
864 }
865 if(k<New_NbCols)
866 temp.Data[i][k] += Data[i][j];
867 }
868 }
869
870 *this = temp;
871
872 return *this;
873 }
874
875 //extraire une ligne
876
877 vecteur Matrice::Get_Row(int RowSpot)
878 {
879 vecteur RowData(NbCols);
880
881 for(int j=0; j<NbCols; ++j) {
882 // if(i == RowSpot) {
883 // for(int j=0; j<NbCols; ++j) {
884 RowData[j]=Data[RowSpot][j];
885 // }
886 // break;
887 // }
888
889 }
890 return RowData;
891 }
892
893 // Extraire une colone
894 vecteur Matrice::Get_Col(int ColSpot)
895 {
896 vecteur ColData(NbRows);
897
898 for(int i=0; i<NbRows; ++i) {
899 //for(int j=0; j<NbCols; ++j) {
900 //if(j == ColSpot) {
901 ColData[i]=Data[i][ColSpot];
902 // }
903 // break;
904 // }
905
906 }
907 return ColData;
908
909 }
910
911
912 //removes the specified row from the calling Matrice
913 Matrice& Matrice::RemoveRow(const int Row)
914 {
915 Matrice temp(NbRows-1, NbCols,0.0);
916
917 for(int i=0, k=0; i<NbRows; ++i, ++k) {
918 if(i == Row) ++i;
919 for(int j=0; j<NbCols; ++j) {
920 temp.Data[k][j] = Data[i][j];
921 }
922 }
923
924 *this = temp;
925
926 return *this;
927 }
928
929 //removes the specified column from the calling Matrice
930 Matrice& Matrice::RemoveColumn(const int Column)
931 {
932 Matrice temp(0.0, NbRows, NbCols-1);
933
934 for(int i=0; i<NbRows; ++i) {
935 for(int j=0, l=0; j<NbCols; ++j, ++l) {
936 if(j == Column) ++j;
937 temp.Data[i][l] = Data[i][j];
938 }
939 }
940
941 *this = temp;
942
943 return *this;
944 }
945
946
947 //returns the covariant of the calling Matrice (transposed(obj) * obj)
948 Matrice& Matrice::Get_Covariant() const
949 {
950 Matrice temp;
951
952 temp = *this;
953 temp.Transpose();
954
955 return(*this * temp);
956 }
957
958 //turns this Matrice into its covariant(obj = transposed(obj) * obj)
959 Matrice& Matrice::Set_Covariant()
960 {
961 *this = this->Get_Covariant();
962
963 return *this;
964 }
965
966
967 //returns an identity Matrice of size Diagonal
968 Matrice& Matrice::IdentityMatrice(int Diagonal)
969 {
970 static Matrice temp;
971 temp = Matrice( Diagonal, Diagonal,0.0);
972
973 for(int q=0; q<Diagonal; ++q) {
974 temp.Data[q][q] = 1;
975 }
976
977 return temp;
978 }
979
980
981 void Matrice:: affiche()
982 {
983 cout << "[";
984 for(int i=0; i<NbRows; ++i) {
985 for(int j=0; j<NbCols; ++j) {
986 cout << "[" << Data[i][j] << "]";
987 }
988 if(NbRows-i-1) cout << "\n ";
989 }
990 cout << "]\n";
991
992
993 }
994
995 //easily display the Matrice, usually to the console
996 //formatted output
997
998
999 Matrice& Matrice::Get_LU(vecteur& perm,double&d)
1000 {
1001 static const double petit=double(1.0e-20);
1002 int taille=NbRows;
1003 int imax,i,j,k;
1004 vector<double> v1(taille);
1005 double maxi,temp,som,dum;
1006 d=1;
1007 if(taille&&taille==NbCols)
1008 {
1009 for(i=0;i<taille;i++)
1010 {
1011 maxi=double(0);
1012 for(j=0;j<taille;j++)
1013 if((temp=(double)fabs(Data[i][j]))>maxi)
1014 maxi=temp;
1015 if(maxi==0)
1016 {
1017 this->Clear();
1018 return *this;
1019 }
1020 v1[i]=1./maxi;
1021 }
1022
1023 for(j=0;j<taille;j++)
1024 {
1025 //cherche les colonnnes de lamatrice L (triangulaire inferieur
1026 for(i=0;i<j;i++)
1027 {
1028 som=Data[i][j];
1029 for(k=0;k<i;k++)
1030 som-=Data[i][k]*Data[k][j];
1031 Data[i][j]=som;
1032 }
1033 maxi=0;
1034
1035 //cherches les lignes de la matrice triangulaire superieur
1036 for(i=j;i<taille;i++)
1037 {
1038 som=Data[i][j];
1039 for(k=0;k<j;k++)
1040 som-=Data[i][k]*Data[k][j];
1041 Data[i][j]=som;
1042 //vèrificstion de pivot
1043 if((dum=v1[i]*fabs(som))>=maxi)
1044 {
1045 maxi=dum;
1046 imax=i;
1047 }
1048 }
1049 //permuation des lignes imax et j
1050 if(j!=imax)
1051 {
1052 for(k=0;k<taille;k++)
1053 {
1054 dum=Data[imax][k];
1055 Data[imax][k]=Data[j][k];
1056 Data[j][k]=dum;
1057 }
1058 d*=-1;
1059 v1[imax]=v1[j];
1060 }
1061 // stoker dans perm la permuattion
1062 perm[j]=imax;
1063 // si le pivot est null
1064 if(Data[j][j]==0.0)
1065 Data[j][j]=petit;
1066 if(j!=taille-1)
1067 {
1068 dum=1./Data[j][j];
1069 for(i=j+1;i<taille;i++)
1070 Data[i][j]*=dum;
1071 }
1072 }
1073 }
1074 return *this;
1075 }
1076
1077
1078 vecteur Matrice::Get_Solution(vecteur& perm,const vecteur&b)
1079 {
1080 vecteur r1;
1081 int taille=NbRows,i,j,ip,ii=-1;
1082 double som;
1083
1084 if(taille&&taille==NbCols&&taille==(int)b.getDim())// Get_Size())
1085 {
1086 r1=b;
1087 for(i=0;i<taille;i++)
1088 {
1089 ip=perm[i];
1090 som=r1[ip];
1091 r1[ip]=r1[i];
1092 if(ii>=0)
1093 for(j=ii;j<=i-1;j++)
1094 som-=Data[i][j]*r1[j];
1095 else if(som) ii=i;
1096 r1[i]=som;
1097 }
1098 for(i=taille-1;i>=0;i--)
1099 {
1100 som=r1[i];
1101 for(j=i+1;j<taille;j++)
1102 som-=Data[i][j]*r1[j];
1103 r1[i]=som/Data[i][i];
1104 }
1105 }
1106 return r1;
1107 }
1108
1109
1110
1111 Matrice Matrice ::Get_Inverse(Matrice& K)
1112 {
1113 double d;
1114 Matrice inv(NbRows,0,0.);
1115 vecteur perm(NbRows);
1116 K= K. Get_LU(perm,d);
1117
1118 for(int i=0;i<NbRows;i++)
1119 {
1120 vecteur ei(NbRows);
1121 ei[i]=1.;
1122 if (i>0) ei[i-1]=0.;
1123 vecteur& xi=K.Get_Solution(perm,ei);
1124 inv.Add_Column(xi,i);
1125
1126 }
1127 return inv;
1128
1129 }
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140 void Matrice::Get_Solution(vecteur& perm, vecteur& b, vecteur& x)
1141 {
1142
1143 int taille=NbRows,i,j,ip,ii=-1;
1144 double som;
1145
1146
1147 for(i=0;i<taille;i++) x[i]=b[i];
1148
1149 for(i=0;i<taille;i++)
1150 {
1151 ip=perm[i];
1152 som=x[ip];
1153 x[ip]=x[i];
1154 if(ii>=0)
1155 for(j=ii;j<=i-1;j++)
1156 som-=Data[i][j]*x[j];
1157 else if(som) ii=i;
1158 x[i]=som;
1159 }
1160 for(i=taille-1;i>=0;i--)
1161 {
1162 som=x[i];
1163 for(j=i+1;j<taille;j++)
1164 som-=Data[i][j]*x[j];
1165 x[i]=som/Data[i][i];
1166 }
1167
1168
1169 }
1170
1171
1172 vecteur Matrice:: Multiply(const vecteur& v)
1173 {
1174 assert (NbCols==v.getDim());
1175 vecteur z(NbRows);
1176 for(int i=0; i<NbRows; ++i) {
1177 for(int j=0; j<NbCols; ++j) {
1178 z[i] += Data[i][j]*v[j];
1179 }
1180 }
1181
1182 return z;
1183
1184 }
1185
1186
1187
1188 //extraire une matrice de la ligne 1 jusqua iigne 2 et de la colone 1 jusqua la colonne2
1189 /*
1190 Matrice Matrice ::Get_SubMatrice(int ln1,int ln2, int cln1,int cln2)
1191 {
1192 int nb_lgne=ln2-ln1+1;
1193 int nb_colne=cln2-cln1+1;
1194 Matrice m(nb_lgne, nb_colne,0.);
1195
1196 for(int i=ln1;i<=ln2;i++)
1197 {
1198 int I=i-ln1;
1199 for(int j=cln1;j<=cln2;j++)
1200 {
1201 int J=j-cln1;
1202 m(I,J)=Data[i][j] ;
1203 }
1204
1205
1206 }
1207
1208 return m;
1209 }
1210 */
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223 std::ostream& operator <<(std::ostream& os, const Matrice& obj)
1224 {
1225 //ostr<<"MATRIX :"<<"#"<<obj.Get_NbRows()<<setw(2)<< "#"<<obj.Get_NbCols()<< endl;
1226 os<<"------------------------------------------------------------------------------------"<<endl;
1227 os<<"# MATRICE: DIM1= "<< obj.Get_NbRows()<<" , DIM2: "<<obj.Get_NbCols()<<" #"<<endl;
1228 os<<"------------------------------------------------------------------------------------"<<endl;
1229 for(int i=0;i<obj.Get_NbRows();i++) {
1230 for(int j=0;j<obj.Get_NbCols();j++) {
1231 os<<setw(18)<<obj(i,j);
1232 }
1233 os<<endl;
1234 }
1235 os<<"------------------------------------------------------------------------------------"<<endl;
1236 return os;
1237 /*
1238 for(int i=0; i<obj.Get_NbRows(); ++i) {
1239 for(int j=0; j<obj.Get_NbCols(); ++j) {
1240 if (i==0)ostr<<"["<<setw(9);
1241 ostr <<setw(10)<<obj[i][j];
1242 }
1243 if(obj.Get_NbRows()-i-1) ostr << "\n ";
1244 }
1245 ostr<<"]"<<endl;
1246
1247 return ostr; */
1248 }
1249
1250 vecteur operator * (const Matrice& m,const vecteur& v)
1251 {
1252 assert (m.Get_NbCols()==v.getDim()) ;
1253 vecteur z(m.Get_NbRows());
1254 for(int i=0; i<m.Get_NbRows(); ++i) {
1255 for(int j=0; j<m.Get_NbCols(); ++j) {
1256 z[i] += m(i,j)*v[j];
1257 }
1258 }
1259
1260 return z;
1261
1262 }
1263 /*
1264 void Matrice::convert_Mat(const Matrice &mat,double valmin,MatCr &mat1)
1265 {
1266 int i,j,k,taille=mat.Get_NbRows();
1267
1268 if(mat.Get_NbCols()==taille)
1269 {
1270 mat1._index.resize((unsigned int)(taille+1));
1271 mat1._values.resize((unsigned int)(taille+1));
1272 for(i=0;i<taille;mat1._values[(unsigned int)i]=mat(i,i),i++);
1273 mat1._index[0]=taille+1;
1274 k=taille;
1275 for(i=0;i<taille;i++)
1276 {
1277 for(j=0;j<taille;j++)
1278 if(fabs(mat(i,j))>valmin&&i!=j)
1279 {
1280 k++;
1281 mat1._index.push_back(j);
1282 mat1._values.push_back(mat(i,j));
1283 }
1284 mat1._index[i+1]=k+1;
1285 }
1286 }
1287 else
1288 {
1289 mat1._index.resize(0);
1290 mat1._values.resize(0);
1291 }
1292 }
1293
1294
1295 */
1296
1297