ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/REPOS_ERICCA/magic/lib/solveur/src/matpleine.cpp
Revision: 108
Committed: Tue Jun 17 13:01:28 2008 UTC (16 years, 11 months ago) by souaissa
Original Path: magic/lib/solveur/solveur/src/matpleine.cpp
File size: 21864 byte(s)
Error occurred while calculating annotation data.
Log Message:
solveur version du 17 juin 2008

File Contents

# Content
1 #include "gestionversion.h"
2
3 //---------------------------------------------------------------------------
4
5 #pragma hdrstop
6
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(0.0, NbRows, NbCols);
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(0.0, NbRows, obj.NbCols);
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, NbCols, NbRows);
191
192 for(int i=0; i<temp.NbRows; ++i) {
193 for(int j=0; j<temp.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
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 //sums all the values in the Matrice
339 double Matrice::Get_SumAll() const
340 {
341 double sum = 0;
342
343 for(int i=0; i<NbRows; ++i) {
344 for(int j=0; j<NbCols; ++j) {
345 sum += Data[i][j];
346 }
347 }
348
349 return sum;
350 }
351
352
353
354 //sums all the values in the row 'Row' of the Matrice
355 double Matrice::Get_SumRow(const int Row) const
356 {
357 double sum = double(0.);
358
359 for(int j=0; j<NbCols; ++j) {
360 sum += Data[Row][j];
361 }
362
363 return sum;
364 }
365
366 //sums all the values in the column 'Col' of the Matrice
367 double Matrice::Get_SumColumn(const int Col) const
368 {
369 double sum = double(0);
370
371 for(int i=0; i<NbRows; ++i) {
372 sum += Data[i][Col];
373 }
374
375 return sum;
376 }
377
378
379
380
381
382 //returns the largest value in the Matrice
383 double Matrice::Get_Max() const
384 {
385 double max = Data[0][0];
386
387 for(int i=0; i<NbRows; ++i) {
388 for(int j=0; j<NbCols; ++j) {
389 if(Data[i][j] > max) max = Data[i][j];
390 }
391 }
392
393 return max;
394 }
395
396 double Matrice::Get_AbMax() const
397 {
398 double max = fabs(Data[0][0]);
399
400 for(int i=0; i<NbRows; ++i) {
401 for(int j=0; j<NbCols; ++j) {
402 if(fabs(Data[i][j]) > max) max = fabs(Data[i][j]);
403 }
404 }
405
406 return max;
407 }
408
409 //returns the smallest value in the Matrice
410 double Matrice ::Get_Min() const
411 {
412 double min = Data[0][0];
413
414 for(int i=0; i<NbRows; ++i) {
415 for(int j=0; j<NbCols; ++j) {
416 if(Data[i][j] < min) min = Data[i][j];
417 }
418 }
419
420 return min;
421 }
422
423 //returns the largest value in row 'Row' in the Matrice
424 double Matrice::Get_RowMax(const int Row) const
425 {
426 double max = Data[Row][0];
427
428 for(int j=0; j<NbCols; ++j) {
429 if(Data[Row][j] > max) max = Data[Row][j];
430 }
431
432 return max;
433 }
434
435 //returns the smallest value in row 'Row' in the Matrice
436 double Matrice::Get_RowMin(const int Row) const
437 {
438 double min = Data[Row][0];
439
440 for(int j=0; j<NbCols; ++j) {
441 if(Data[Row][j] < min) min = Data[Row][j];
442 }
443
444 return min;
445 }
446
447 //returns the largest value in column 'Col' in the Matrice
448 double Matrice::Get_ColumnMax(const int Col) const
449 {
450 double max = Data[0][Col];
451
452 for(int i=0; i<NbRows; ++i) {
453 if(Data[i][Col] > max) max = Data[i][Col];
454 }
455
456 return max;
457 }
458
459 //returns the smallest value in column 'Col' in the Matrice
460 double Matrice::Get_ColumnMin(const int Col) const
461 {
462 double min = Data[0][Col];
463
464 for(int i=0; i<NbRows; ++i) {
465 if(Data[i][Col] < min) min = Data[i][Col];
466 }
467
468 return min;
469 }
470
471
472 double* Matrice ::Get_DataOneDimen() const
473 {
474 double* newData;
475
476 newData = new double[NbRows * NbCols];
477
478 for(int i=0; i<NbRows; ++i) {
479 for(int j=0; j<NbCols; ++j) {
480 newData[(i*NbRows)+j] = Data[i][j];
481 }
482 }
483
484 return newData;
485 }
486
487 //returns the data in a two-dimensional array
488 //the array is dynamically allocated
489 double** Matrice ::Get_DataTwoDimen() const
490 {
491 double** newData;
492
493 newData = new double*[NbRows];
494 for(int i=0; i<NbRows; ++i) {
495 newData[i] = new double[NbCols];
496 for(int j=0; j<NbCols; ++j) {
497 newData[i][j] = Data[i][j];
498 }
499 }
500
501 return newData;
502 }
503
504 //returns number of rows of the Matrice
505 int Matrice ::Get_NbRows() const
506 {
507 return NbRows;
508 }
509
510 //returns number of columns of the Matrice
511 int Matrice::Get_NbCols() const
512 {
513 return NbCols;
514 }
515
516 //clears every entry in the calling Matrice
517 Matrice& Matrice::Clear()
518 {
519 for(int i=0; i<NbRows; ++i) {
520 for(int j=0; j<NbCols; ++j) {
521 Data[i][j] = 0;
522 }
523 }
524
525 return *this;
526 }
527
528 //clears every entry in the row 'Row' from the calling Matrice
529 Matrice& Matrice::ClearRow(const int Row)
530 {
531 for(int j=0; j<NbCols; ++j) {
532 Data[Row][j] = 0;
533 }
534
535 return *this;
536 }
537
538 //clears every entry in the column 'Col' from the calling Matrice
539 Matrice& Matrice::ClearColumn(const int Col)
540 {
541 for(int i=0; i<NbRows; ++i) {
542 Data[i][Col] = 0;
543 }
544
545 return *this;
546 }
547
548 //fills every entry in the calling Matrice to '_d'
549 Matrice& Matrice::Fill(const double _d)
550 {
551 for(int i=0; i<NbRows; ++i) {
552 for(int j=0; j<NbCols; ++j) {
553 Data[i][j] = _d;
554 }
555 }
556
557 return *this;
558 }
559
560 //fills every entry in the row 'Row' of the calling Matrice to '_d'
561 Matrice& Matrice::FillRow(const int Row, const double _d)
562 {
563 for(int j=0; j<NbCols; ++j) {
564 Data[Row][j] = _d;
565 }
566
567 return *this;
568 }
569
570 //fills every entry in the column 'Col' of the calling Matrice to '_d'
571 Matrice& Matrice::FillColumn(const int Col, const double _d)
572 {
573 for(int i=0; i<NbRows; ++i) {
574 Data[i][Col] = _d;
575 }
576
577 return *this;
578 }
579
580
581
582
583
584 Matrice& Matrice::Get_SubMatrice(const int RowSpot, const int ColSpot, const int RowLen, const int ColLen) const
585 {
586 static Matrice temp;
587 temp = Matrice( RowLen, ColLen,0.0);
588
589 for(int i=RowSpot, k=0; i<(RowLen+RowSpot); ++i, ++k) {
590 for(int j=ColSpot, l=0; j<(ColLen+ColSpot); ++j, ++l) {
591 temp.Data[k][l] = Data[i][j];
592 }
593 }
594
595 return temp;
596 }
597
598 //changes the calling Matrice into a subMatrice starting at spot (RowSpot, ColSpot)
599 //and with lengths of 'RowLen' rows and 'ColLen' columns
600 Matrice& Matrice::Set_SubMatrice(const int RowSpot, const int ColSpot, const int RowLen, const int ColLen)
601 {
602 Matrice temp;
603
604 temp = Get_SubMatrice(RowSpot, ColSpot, RowLen, ColLen);
605
606 *this = temp;
607
608 return *this;
609 }
610
611 //swaps two rows, Row1 and Row2, from the calling Matrice
612 Matrice& Matrice::Get_SwapRows(const int Row1, const int Row2)
613 {
614 double* temp;
615
616 temp = new double[NbCols];
617
618 for(int j=0; j<NbCols; ++j) {
619 temp[j] = Data[Row1][j];
620 Data[Row1][j] = Data[Row2][j];
621 Data[Row2][j] = temp[j];
622 }
623
624 delete[] temp;
625
626 return *this;
627 }
628
629 //swaps two columns, Col1 and Col2, from the calling Matrice
630 Matrice& Matrice::Get_SwapCols(const int Col1, const int Col2)
631 {
632 double* temp;
633
634 temp = new double[NbRows];
635
636 for(int i=0; i<NbRows; ++i) {
637 temp[i] = Data[i][Col1];
638 Data[i][Col1] = Data[i][Col2];
639 Data[i][Col2] = temp[i];
640 }
641
642 delete[] temp;
643
644 return *this;
645 }
646
647 //returns the transposition of the calling Matrice
648 Matrice& Matrice::Get_Transposed() const
649 {
650 static Matrice temp;
651 temp = Matrice(NbCols, NbRows,0.0);
652
653 for(int i=0; i<NbRows; ++i) {
654 for(int j=0; j<NbCols; ++j) {
655 temp.Data[j][i] = Data[i][j];
656 }
657 }
658
659 return temp;
660 }
661
662 //transposes the calling Matrice
663 Matrice& Matrice::Transpose()
664 {
665 *this = this->Get_Transposed();
666
667 return *this;
668 }
669
670
671
672
673
674 //CMAR = Concatenate Matrice As Rows
675 //concatenates Matrice 'obj' on to the right of the calling Matrice
676 Matrice& Matrice::Get_ConMatAsRow(const Matrice& obj)
677 {
678 assert(NbCols == obj.NbCols);// ErrorMsg("mismatched matrices in row concatenation", true);
679
680 Matrice temp((NbRows + obj.NbRows), NbCols,0.0);
681 int i;
682 for(int i=0; i<NbRows; ++i) {
683 for(int j=0; j<NbCols; ++j) {
684 temp.Data[i][j] = Data[i][j];
685 }
686 }
687 for(int k=0; i<temp.NbRows; ++i, ++k) {
688 for(int j=0; j<NbCols; ++j) {
689 temp.Data[i][j] = obj.Data[k][j];
690 }
691 }
692
693 *this = temp;
694
695 return *this;
696 }
697
698 //CMAC = Concatenate Matrice As Columns
699 //concatenates Matrice 'obj' on to the bottom of the calling Matrice
700 Matrice& Matrice::Get_ConMatAsCol(const Matrice& obj)
701 {
702 assert(NbRows == obj.NbRows);// ErrorMsg("mismatched matrices in column concatenation", true);
703
704 Matrice temp(NbRows, (NbCols + obj.NbCols),0.0);
705 int i,j;
706 for(i=0; i<NbRows; ++i) {
707 for( j=0; j<NbCols; ++j) {
708 temp.Data[i][j] = Data[i][j];
709 }
710 for(int l=0; l<obj.NbCols; ++l, ++j) {
711 temp.Data[i][j] = obj.Data[i][l];
712 }
713 }
714
715 *this = temp;
716
717 return *this;
718 }
719
720 //CMAR = Concatenate Matrice As Rows
721 //returns a new Matrice that is the calling object + 'obj' on the right
722 Matrice& Matrice::GetCMAR(const Matrice& obj) const
723 {
724 static Matrice temp;
725
726 temp = *this;
727 temp.Get_ConMatAsRow(obj);
728
729 return temp;
730 }
731
732 //CMAC = Concatenate Matrice As Columns
733 //returns a new Matrice that is the valling object + 'obj' on the bottom
734 Matrice& Matrice::GetCMAC(const Matrice& obj) const
735 {
736 static Matrice temp;
737
738 temp = *this;
739 temp.Get_ConMatAsCol(obj);
740
741 return temp;
742 }
743
744 //adds a row onto the right of the calling Matrice
745 Matrice& Matrice::ConcatenateRow(const double* RowData)
746 {
747 Matrice temp( NbRows+1, NbCols,0.0);
748 int i;
749 for(i=0; i<NbRows; ++i) {
750 for(int j=0; j<NbCols; ++j) {
751 temp.Data[i][j] = Data[i][j];
752 }
753 }
754 int j;
755 for(i=NbRows, j=0; j<NbCols; ++j) {
756 temp.Data[i][j] = RowData[j];
757 }
758
759 *this = temp;
760
761 return *this;
762 }
763
764 //adds a column onto the bottom of the calling Matrice
765 Matrice& Matrice::ConcatenateColumn(const double* ColumnData)
766 {
767
768 Matrice temp( NbRows, NbCols+1,0.0);
769 int i;
770 for(i=0; i<NbRows; ++i) {
771 for(int j=0; j<NbCols; ++j) {
772 temp.Data[i][j] = Data[i][j];
773 }
774 }
775 int j;
776 for(j=NbCols, i=0; i<NbRows; ++i) {
777 temp.Data[i][j] = ColumnData[i];
778 }
779
780 *this = temp;
781
782 return *this;
783 }
784
785 //adds a row into the Matrice in the spot 'RowSpot'
786 Matrice& Matrice::Add_Row(const double* RowData, const int RowSpot)
787 {
788 Matrice temp( NbRows+1, NbCols,0.0);
789
790 for(int i=0, k=0; i<NbRows; ++i, ++k) {
791 if(i == RowSpot) {
792 for(int j=0; j<NbCols; ++j) {
793 temp.Data[i][j] = RowData[j];
794 }
795 ++k;
796 }
797 for(int j=0; j<NbCols; ++j) {
798 temp.Data[k][j] = Data[i][j];
799 }
800 }
801
802 *this = temp;
803
804 return *this;
805 }
806
807 //adds a column into the Matrice in the spot 'ColumnSpot'
808 Matrice& Matrice::Add_Column(const double* ColumnData, const int ColumnSpot)
809 {
810 Matrice temp(NbRows, NbCols+1,0.0);
811
812 for(int i=0; i<NbRows; ++i) {
813 for(int j=0, l=0; j<NbCols; ++j, ++l) {
814 if(j == ColumnSpot) {
815 temp.Data[i][l] = ColumnData[i];
816 ++l;
817 }
818 temp.Data[i][l] = Data[i][j];
819 }
820 }
821
822 *this = temp;
823
824 return *this;
825 }
826
827 //removes the specified row from the calling Matrice
828 Matrice& Matrice::RemoveRow(const int Row)
829 {
830 Matrice temp(NbRows-1, NbCols,0.0);
831
832 for(int i=0, k=0; i<NbRows; ++i, ++k) {
833 if(i == Row) ++i;
834 for(int j=0; j<NbCols; ++j) {
835 temp.Data[k][j] = Data[i][j];
836 }
837 }
838
839 *this = temp;
840
841 return *this;
842 }
843
844 //removes the specified column from the calling Matrice
845 Matrice& Matrice::RemoveColumn(const int Column)
846 {
847 Matrice temp(0.0, NbRows, NbCols-1);
848
849 for(int i=0; i<NbRows; ++i) {
850 for(int j=0, l=0; j<NbCols; ++j, ++l) {
851 if(j == Column) ++j;
852 temp.Data[i][l] = Data[i][j];
853 }
854 }
855
856 *this = temp;
857
858 return *this;
859 }
860
861
862 //returns the covariant of the calling Matrice (transposed(obj) * obj)
863 Matrice& Matrice::Get_Covariant() const
864 {
865 Matrice temp;
866
867 temp = *this;
868 temp.Transpose();
869
870 return(*this * temp);
871 }
872
873 //turns this Matrice into its covariant(obj = transposed(obj) * obj)
874 Matrice& Matrice::Set_Covariant()
875 {
876 *this = this->Get_Covariant();
877
878 return *this;
879 }
880
881
882 //returns an identity Matrice of size Diagonal
883 Matrice& Matrice::IdentityMatrice(int Diagonal)
884 {
885 static Matrice temp;
886 temp = Matrice( Diagonal, Diagonal,0.0);
887
888 for(int q=0; q<Diagonal; ++q) {
889 temp.Data[q][q] = 1;
890 }
891
892 return temp;
893 }
894
895
896 void Matrice:: affiche()
897 {
898 cout << "[";
899 for(int i=0; i<NbRows; ++i) {
900 for(int j=0; j<NbCols; ++j) {
901 cout << "[" << Data[i][j] << "]";
902 }
903 if(NbRows-i-1) cout << "\n ";
904 }
905 cout << "]\n";
906
907
908 }
909
910 //easily display the Matrice, usually to the console
911 //formatted output
912
913
914 Matrice& Matrice::Get_LU(int*perm,double&d)
915 {
916 static const double petit=double(1.0e-20);
917 int taille=NbRows;
918 int imax,i,j,k;
919 vector<double> v1(taille);
920 double maxi,temp,som,dum;
921 d=1;
922 if(taille&&taille==NbCols)
923 {
924 for(i=0;i<taille;i++)
925 {
926 maxi=double(0);
927 for(j=0;j<taille;j++)
928 if((temp=(double)fabs(Data[i][j]))>maxi)
929 maxi=temp;
930 if(maxi==0)
931 {
932 this->Clear();
933 return *this;
934 }
935 v1[i]=1./maxi;
936 }
937
938 for(j=0;j<taille;j++)
939 {
940 //cherche les colonnnes de lamatrice L (triangulaire inferieur
941 for(i=0;i<j;i++)
942 {
943 som=Data[i][j];
944 for(k=0;k<i;k++)
945 som-=Data[i][k]*Data[k][j];
946 Data[i][j]=som;
947 }
948 maxi=0;
949
950 //cherches les lignes de la matrice triangulaire superieur
951 for(i=j;i<taille;i++)
952 {
953 som=Data[i][j];
954 for(k=0;k<j;k++)
955 som-=Data[i][k]*Data[k][j];
956 Data[i][j]=som;
957 //vèrificstion de pivot
958 if((dum=v1[i]*fabs(som))>=maxi)
959 {
960 maxi=dum;
961 imax=i;
962 }
963 }
964 //permuation des lignes imax et j
965 if(j!=imax)
966 {
967 for(k=0;k<taille;k++)
968 {
969 dum=Data[imax][k];
970 Data[imax][k]=Data[j][k];
971 Data[j][k]=dum;
972 }
973 d*=-1;
974 v1[imax]=v1[j];
975 }
976 // stoker dans perm la permuattion
977 perm[j]=imax;
978 // si le pivot est null
979 if(Data[j][j]==0.0)
980 Data[j][j]=petit;
981 if(j!=taille-1)
982 {
983 dum=1./Data[j][j];
984 for(i=j+1;i<taille;i++)
985 Data[i][j]*=dum;
986 }
987 }
988 }
989 return *this;
990 }
991
992
993 Vecteur Matrice::Get_Solution(int*perm,const Vecteur&b)
994 {
995 Vecteur r1;
996 int taille=NbRows,i,j,ip,ii=-1;
997 double som;
998
999 if(taille&&taille==NbCols&&taille==(int)b.Get_Size())
1000 {
1001 r1=b;
1002 for(i=0;i<taille;i++)
1003 {
1004 ip=perm[i];
1005 som=r1[ip];
1006 r1[ip]=r1[i];
1007 if(ii>=0)
1008 for(j=ii;j<=i-1;j++)
1009 som-=Data[i][j]*r1[j];
1010 else if(som) ii=i;
1011 r1[i]=som;
1012 }
1013 for(i=taille-1;i>=0;i--)
1014 {
1015 som=r1[i];
1016 for(j=i+1;j<taille;j++)
1017 som-=Data[i][j]*r1[j];
1018 r1[i]=som/Data[i][i];
1019 }
1020 }
1021 return r1;
1022 }
1023
1024
1025 void Matrice::Get_Solution(int* perm, double* b, double* x)
1026 {
1027
1028 int taille=NbRows,i,j,ip,ii=-1;
1029 double som;
1030
1031
1032 for(i=0;i<taille;i++) x[i]=b[i];
1033
1034 for(i=0;i<taille;i++)
1035 {
1036 ip=perm[i];
1037 som=x[ip];
1038 x[ip]=x[i];
1039 if(ii>=0)
1040 for(j=ii;j<=i-1;j++)
1041 som-=Data[i][j]*x[j];
1042 else if(som) ii=i;
1043 x[i]=som;
1044 }
1045 for(i=taille-1;i>=0;i--)
1046 {
1047 som=x[i];
1048 for(j=i+1;j<taille;j++)
1049 som-=Data[i][j]*x[j];
1050 x[i]=som/Data[i][i];
1051 }
1052
1053
1054 }
1055
1056
1057 Vecteur Matrice:: Multiply(const Vecteur& v)
1058 {
1059 assert (NbCols==v.Get_Size());
1060 Vecteur z(NbRows);
1061 for(int i=0; i<NbRows; ++i) {
1062 for(int j=0; j<NbCols; ++j) {
1063 z[i] += Data[i][j]*v[j];
1064 }
1065 }
1066
1067 return z;
1068
1069 }
1070
1071
1072 std::ostream& operator <<(std::ostream& ostr, const Matrice& obj)
1073 {
1074 ostr << "[";
1075 for(int i=0; i<obj.Get_NbRows(); ++i) {
1076 for(int j=0; j<obj.Get_NbCols(); ++j) {
1077 ostr << "[" << obj[i][j] << "]";
1078 }
1079 if(obj.Get_NbRows()-i-1) ostr << "\n ";
1080 }
1081 ostr << "]\n";
1082
1083 return ostr;
1084 }
1085
1086 Vecteur operator * (const Matrice& m,const Vecteur& v)
1087 {
1088 assert (m.Get_NbCols()==v.Get_Size()) ;
1089 Vecteur z(m.Get_NbRows());
1090 for(int i=0; i<m.Get_NbRows(); ++i) {
1091 for(int j=0; j<m.Get_NbCols(); ++j) {
1092 z[i] += m(i,j)*v[j];
1093 }
1094 }
1095
1096 return z;
1097
1098 }
1099 /*
1100 void Matrice::convert_Mat(const Matrice &mat,double valmin,MatCr &mat1)
1101 {
1102 int i,j,k,taille=mat.Get_NbRows();
1103
1104 if(mat.Get_NbCols()==taille)
1105 {
1106 mat1._index.resize((unsigned int)(taille+1));
1107 mat1._values.resize((unsigned int)(taille+1));
1108 for(i=0;i<taille;mat1._values[(unsigned int)i]=mat(i,i),i++);
1109 mat1._index[0]=taille+1;
1110 k=taille;
1111 for(i=0;i<taille;i++)
1112 {
1113 for(j=0;j<taille;j++)
1114 if(fabs(mat(i,j))>valmin&&i!=j)
1115 {
1116 k++;
1117 mat1._index.push_back(j);
1118 mat1._values.push_back(mat(i,j));
1119 }
1120 mat1._index[i+1]=k+1;
1121 }
1122 }
1123 else
1124 {
1125 mat1._index.resize(0);
1126 mat1._values.resize(0);
1127 }
1128 }
1129
1130
1131 */
1132
1133