0022904: Clean up sccsid variables
[occt.git] / src / math / math_Matrix.cxx
CommitLineData
7fd59977 1//#ifndef DEB
2#define No_Standard_RangeError
3#define No_Standard_OutOfRange
4#define No_Standard_DimensionError
5//#endif
6
7#include <math_Matrix.ixx>
8#include <math_Vector.hxx>
9
10#include <Standard_DimensionError.hxx>
11#include <Standard_DivideByZero.hxx>
12#include <Standard_RangeError.hxx>
13#include <math_SingularMatrix.hxx>
14#include <math_NotSquare.hxx>
15#include <math_Gauss.hxx>
16
17
18void math_Matrix::SetLowerRow(const Standard_Integer LowerRow) {
19
20 Array.SetLowerRow(LowerRow);
21 Standard_Integer Rows = RowNumber();
22 LowerRowIndex = LowerRow;
23 UpperRowIndex = LowerRowIndex + Rows - 1;
24}
25
26void math_Matrix::SetLowerCol(const Standard_Integer LowerCol) {
27
28 Array.SetLowerCol(LowerCol);
29 Standard_Integer Cols = ColNumber();
30 LowerColIndex = LowerCol;
31 UpperColIndex = LowerColIndex + Cols - 1;
32}
33
34math_Matrix::math_Matrix (const Standard_Integer LowerRow,
35 const Standard_Integer UpperRow,
36 const Standard_Integer LowerCol,
37 const Standard_Integer UpperCol):
38
39 LowerRowIndex(LowerRow),
40 UpperRowIndex(UpperRow),
41 LowerColIndex(LowerCol),
42 UpperColIndex(UpperCol),
43 Array(LowerRow, UpperRow,
44 LowerCol, UpperCol)
45{
46
47 Standard_RangeError_Raise_if((LowerRow > UpperRow) ||
48 (LowerCol > UpperCol), "");
49}
50
51math_Matrix::math_Matrix (const Standard_Integer LowerRow,
52 const Standard_Integer UpperRow,
53 const Standard_Integer LowerCol,
54 const Standard_Integer UpperCol,
55 const Standard_Real InitialValue):
56
57 LowerRowIndex(LowerRow),
58 UpperRowIndex(UpperRow),
59 LowerColIndex(LowerCol),
60 UpperColIndex(UpperCol),
61 Array(LowerRow, UpperRow,
62 LowerCol, UpperCol)
63{
64
65 Standard_RangeError_Raise_if((LowerRow > UpperRow) ||
66 (LowerCol > UpperCol), "");
67 Array.Init(InitialValue);
68}
69
70math_Matrix::math_Matrix (const Standard_Address Tab,
71 const Standard_Integer LowerRow,
72 const Standard_Integer UpperRow,
73 const Standard_Integer LowerCol,
74 const Standard_Integer UpperCol) :
75
76 LowerRowIndex(LowerRow),
77 UpperRowIndex(UpperRow),
78 LowerColIndex(LowerCol),
79 UpperColIndex(UpperCol),
80 Array(*((const Standard_Real *)Tab),
81 LowerRow, UpperRow,
82 LowerCol, UpperCol)
83{
84
85 Standard_RangeError_Raise_if((LowerRow > UpperRow) ||
86 (LowerCol > UpperCol), "");
87}
88
89void math_Matrix::Init(const Standard_Real InitialValue)
90{
91 Array.Init(InitialValue);
92}
93
94math_Matrix::math_Matrix (const math_Matrix& Other):
95
96LowerRowIndex(Other.LowerRow()),
97UpperRowIndex(Other.UpperRow()),
98LowerColIndex(Other.LowerCol()),
99UpperColIndex(Other.UpperCol()),
100Array(Other.Array)
101{
102}
103
104
105
106math_Matrix math_Matrix::Divided (const Standard_Real Right) const
107{
108 Standard_DivideByZero_Raise_if(Abs(Right) <= RealEpsilon(), "");
109 math_Matrix temp = Multiplied(1./Right);
110 return temp;
111}
112
113
114Standard_Real math_Matrix::Determinant() const
115{
116 math_Gauss Sol(*this);
117
118 if(Sol.IsDone()) {
119 return Sol.Determinant();
120 }
121 else {
122 return 0.0;
123 }
124}
125
126void math_Matrix::Transpose()
127{
128 math_NotSquare_Raise_if(RowNumber() != ColNumber(), "");
129
130 Standard_Integer Row = LowerRowIndex;
131 Standard_Integer Col = LowerColIndex;
132 SetLowerCol(LowerRowIndex);
133 Standard_Real Temp;
134 for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) {
135 for(Standard_Integer J = I; J <= UpperColIndex; J++) {
136 Temp = Array(I, J);
137 Array(I, J) = Array(J, I);
138 Array(J, I) = Temp;
139 }
140 }
141 SetLowerRow(Col);
142 SetLowerCol(Row);
143}
144
145math_Matrix math_Matrix::Transposed() const
146{
147 math_Matrix Result(LowerColIndex, UpperColIndex,
148 LowerRowIndex, UpperRowIndex);
149
150 for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) {
151 for(Standard_Integer J = LowerColIndex; J <= UpperColIndex; J++) {
152 Result.Array(J, I) = Array(I, J);
153 }
154 }
155 return Result;
156}
157
158void math_Matrix::Invert()
159{
160 math_NotSquare_Raise_if(RowNumber() != ColNumber(), "");
161
162 math_Gauss Sol(*this);
163 if(Sol.IsDone()) {
164 Sol.Invert(*this);
165 }
166 else {
167 math_SingularMatrix::Raise(); // SingularMatrix Exception;
168 }
169}
170
171math_Matrix math_Matrix::Inverse() const
172{
173
174 math_Matrix Result = *this;
175 Result.Invert();
176 return Result;
177}
178
179void math_Matrix::Multiply (const Standard_Real Right)
180{
181 for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) {
182 for(Standard_Integer J = LowerColIndex; J <= UpperColIndex; J++) {
183 Array(I, J) = Array(I, J) * Right;
184 }
185 }
186}
187
188math_Matrix math_Matrix::Multiplied (const Standard_Real Right) const
189{
190 math_Matrix Result(LowerRowIndex, UpperRowIndex,
191 LowerColIndex, UpperColIndex);
192 for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) {
193 for(Standard_Integer J = LowerColIndex; J <= UpperColIndex; J++) {
194 Result.Array(I, J) = Array(I, J) * Right;
195 }
196 }
197 return Result;
198}
199
200math_Matrix math_Matrix::TMultiplied (const Standard_Real Right) const
201{
202 math_Matrix Result(LowerRowIndex, UpperRowIndex,
203 LowerColIndex, UpperColIndex);
204 for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) {
205 for(Standard_Integer J = LowerColIndex; J <= UpperColIndex; J++) {
206 Result.Array(I, J) = Array(I, J) * Right;
207 }
208 }
209 return Result;
210}
211
212
213
214void math_Matrix::Divide (const Standard_Real Right)
215{
216 Standard_DivideByZero_Raise_if(Abs(Right) <= RealEpsilon(), "");
217
218 for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) {
219 for(Standard_Integer J = LowerColIndex; J <= UpperColIndex; J++) {
220 Array(I, J) = Array(I, J) / Right;
221 }
222 }
223}
224
225void math_Matrix::Add (const math_Matrix& Right)
226{
227 Standard_DimensionError_Raise_if((RowNumber() != Right.RowNumber()) ||
228 (ColNumber() != Right.ColNumber()),
229 "");
230
231 Standard_Integer I2 = Right.LowerRowIndex;
232 for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) {
233 Standard_Integer J2 = Right.LowerColIndex;
234 for(Standard_Integer J = LowerColIndex; J <= UpperColIndex; J++) {
235 Array(I, J) = Array(I, J) + Right.Array(I2, J2);
236 J2++;
237 }
238 I2++;
239 }
240}
241
242void math_Matrix::Subtract (const math_Matrix& Right)
243{
244 Standard_DimensionError_Raise_if((RowNumber() != Right.RowNumber()) ||
245 (ColNumber() != Right.ColNumber()),
246 "");
247
248 Standard_Integer I2 = Right.LowerRowIndex;
249 for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) {
250 Standard_Integer J2 = Right.LowerColIndex;
251 for(Standard_Integer J = LowerColIndex; J <= UpperColIndex; J++) {
252 Array(I, J) = Array(I, J) - Right.Array(I2, J2);
253 J2++;
254 }
255 I2++;
256 }
257}
258
259void math_Matrix::Set(const Standard_Integer I1,const Standard_Integer I2,
260 const Standard_Integer J1,const Standard_Integer J2,
261 const math_Matrix& M)
262{
263
264 Standard_DimensionError_Raise_if((I1 < LowerRowIndex) ||
265 (I2 > UpperRowIndex) ||
266 (J1 < LowerColIndex) ||
267 (J2 > UpperColIndex) ||
268 (I1 > I2) || (J1 > J2) ||
269 (I2-I1+1 != M.RowNumber()) ||
270 (J2-J1+1 != M.ColNumber()), "");
271
272 Standard_Integer II = M.LowerRow();
273 for(Standard_Integer I = I1; I <= I2; I++) {
274 Standard_Integer JJ = M.LowerCol();
275 for(Standard_Integer J = J1; J <= J2; J++) {
276 Array(I, J) = M.Array(II, JJ);
277 JJ++;
278 }
279 II++;
280 }
281}
282
283void math_Matrix::SetRow (const Standard_Integer Row,
284 const math_Vector& V)
285{
286
287 Standard_RangeError_Raise_if((Row < LowerRowIndex) ||
288 (Row > UpperRowIndex) , "");
289
290 Standard_DimensionError_Raise_if(ColNumber() != V.Length(), "");
291
292 Standard_Integer I = V.LowerIndex;
293 for(Standard_Integer Index = LowerColIndex; Index <= UpperColIndex; Index++) {
294 Array(Row, Index) = V.Array(I);
295 I++;
296 }
297}
298
299void math_Matrix::SetCol (const Standard_Integer Col,
300 const math_Vector& V)
301{
302
303 Standard_RangeError_Raise_if((Col < LowerColIndex) ||
304 (Col > UpperColIndex) , "");
305
306 Standard_DimensionError_Raise_if(RowNumber() != V.Length(), "");
307
308 Standard_Integer I = V.LowerIndex;
309 for(Standard_Integer Index = LowerRowIndex; Index <= UpperRowIndex; Index++) {
310 Array(Index, Col) = V.Array(I);
311 I++;
312 }
313}
314
315void math_Matrix::SetDiag(const Standard_Real Value)
316{
317
318 math_NotSquare_Raise_if(RowNumber() != ColNumber(), "");
319
320 for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) {
321 Array(I, I) = Value;
322 }
323}
324math_Vector math_Matrix::Row (const Standard_Integer Row) const
325{
326
327 math_Vector Result(LowerColIndex, UpperColIndex);
328
329 for(Standard_Integer Index = LowerColIndex; Index <= UpperColIndex; Index++) {
330 Result.Array(Index) = Array(Row, Index);
331 }
332 return Result;
333}
334
335math_Vector math_Matrix::Col (const Standard_Integer Col) const
336{
337
338 math_Vector Result(LowerRowIndex, UpperRowIndex);
339
340 for(Standard_Integer Index = LowerRowIndex; Index <= UpperRowIndex; Index++) {
341 Result.Array(Index) = Array(Index, Col);
342 }
343 return Result;
344}
345
346void math_Matrix::SwapRow(const Standard_Integer Row1,
347 const Standard_Integer Row2)
348{
349
350 Standard_RangeError_Raise_if((Row1 < LowerRowIndex) ||
351 (Row1 > UpperRowIndex) ||
352 (Row2 < LowerRowIndex) ||
353 (Row2 > UpperRowIndex), "");
354
355 math_Vector V1 = Row(Row1);
356 math_Vector V2 = Row(Row2);
357 SetRow(Row1,V2);
358 SetRow(Row2,V1);
359}
360
361void math_Matrix::SwapCol(const Standard_Integer Col1,
362 const Standard_Integer Col2)
363{
364
365 Standard_RangeError_Raise_if((Col1 < LowerColIndex) ||
366 (Col1 > UpperColIndex) ||
367 (Col2 < LowerColIndex) ||
368 (Col2 > UpperColIndex), "");
369
370 math_Vector V1 = Col(Col1);
371 math_Vector V2 = Col(Col2);
372 SetCol(Col1,V2);
373 SetCol(Col2,V1);
374}
375
376
377
378math_Matrix math_Matrix::Multiplied (const math_Matrix& Right) const
379{
380
381 Standard_DimensionError_Raise_if(ColNumber() != Right.RowNumber(), "");
382
383 math_Matrix Result(LowerRowIndex, UpperRowIndex,
384 Right.LowerColIndex, Right.UpperColIndex);
385
386 Standard_Real Som;
387 for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) {
388 for(Standard_Integer J2 = Right.LowerColIndex; J2 <= Right.UpperColIndex; J2++) {
389 Som = 0.0;
390 Standard_Integer I2 = Right.LowerRowIndex;
391 for(Standard_Integer J = LowerColIndex; J <= UpperColIndex; J++) {
392 Som = Som + Array(I, J) * Right.Array(I2, J2);
393 I2++;
394 }
395 Result.Array(I, J2) = Som;
396 }
397 }
398 return Result;
399}
400
401math_Matrix math_Matrix::TMultiply (const math_Matrix& Right) const
402{
403
404 Standard_DimensionError_Raise_if(RowNumber() != Right.RowNumber(), "");
405
406 math_Matrix Result(LowerColIndex, UpperColIndex,
407 Right.LowerColIndex, Right.UpperColIndex);
408
409 Standard_Real Som;
410 for(Standard_Integer I = LowerColIndex; I <= UpperColIndex; I++) {
411 for(Standard_Integer J2 = Right.LowerColIndex; J2 <= Right.UpperColIndex; J2++) {
412 Som = 0.0;
413 Standard_Integer I2 = Right.LowerRowIndex;
414 for(Standard_Integer J = LowerRowIndex; J <= UpperRowIndex; J++) {
415 Som = Som + Array(J, I) * Right.Array(I2, J2);
416 I2++;
417 }
418 Result.Array(I, J2) = Som;
419 }
420 }
421 return Result;
422}
423
424math_Matrix math_Matrix::Added (const math_Matrix& Right) const
425{
426
427 Standard_DimensionError_Raise_if((RowNumber() != Right.RowNumber()) ||
428 (ColNumber() != Right.ColNumber()),
429 "");
430
431 math_Matrix Result(LowerRowIndex, UpperRowIndex,
432 LowerColIndex, UpperColIndex);
433
434 Standard_Integer I2 = Right.LowerRowIndex;
435 for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) {
436 Standard_Integer J2 = Right.LowerColIndex;
437 for(Standard_Integer J = LowerColIndex; J <= UpperColIndex; J++) {
438 Result.Array(I, J) = Array(I, J) + Right.Array(I2, J2);
439 J2++;
440 }
441 I2++;
442 }
443 return Result;
444}
445
446math_Matrix math_Matrix::Opposite ()
447{
448
449 math_Matrix Result(LowerRowIndex, UpperRowIndex,
450 LowerColIndex, UpperColIndex);
451
452 for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) {
453 for(Standard_Integer J = LowerColIndex; J <= UpperColIndex; J++) {
454 Result.Array(I, J) = - Array(I, J);
455 }
456 }
457 return Result;
458}
459
460math_Matrix math_Matrix::Subtracted (const math_Matrix& Right) const
461{
462
463 Standard_DimensionError_Raise_if((RowNumber() != Right.RowNumber()) ||
464 (ColNumber() != Right.ColNumber()),
465 "");
466
467 math_Matrix Result(LowerRowIndex, UpperRowIndex,
468 LowerColIndex, UpperColIndex);
469
470 Standard_Integer I2 = Right.LowerRowIndex;
471 for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) {
472 Standard_Integer J2 = Right.LowerColIndex;
473 for(Standard_Integer J = LowerColIndex; J <= UpperColIndex; J++) {
474 Result.Array(I, J) = Array(I, J) - Right.Array(I2, J2);
475 J2++;
476 }
477 I2++;
478 }
479 return Result;
480}
481
482void math_Matrix::Multiply(const math_Vector& Left,
483 const math_Vector& Right)
484{
485
486 Standard_DimensionError_Raise_if((RowNumber() != Left.Length()) ||
487 (ColNumber() != Right.Length()),
488 "");
489
490 for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) {
491 for(Standard_Integer J = LowerColIndex; J <= UpperColIndex; J++) {
492 Array(I, J) = Left.Array(I) * Right.Array(J);
493 }
494 }
495}
496
497void math_Matrix::Multiply(const math_Matrix& Left,
498 const math_Matrix& Right)
499{
500
501 Standard_DimensionError_Raise_if((Left.ColNumber() != Right.RowNumber()) ||
502 (RowNumber() != Left.RowNumber()) ||
503 (ColNumber() != Right.ColNumber()),
504 "");
505
506 Standard_Real Som;
507 Standard_Integer I1 = Left.LowerRowIndex;
508 for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) {
509 Standard_Integer J2 = Right.LowerColIndex;
510 for(Standard_Integer J = LowerColIndex; J <= UpperColIndex; J++) {
511 Som = 0.0;
512 Standard_Integer J1 = Left.LowerColIndex;
513 Standard_Integer I2 = Right.LowerRowIndex;
514 for(Standard_Integer K = Left.LowerColIndex; K <= Left.UpperColIndex; K++) {
515 Som = Som + Left.Array(I1, J1) * Right.Array(I2, J2);
516 J1++;
517 I2++;
518 }
519 Array(I, J) = Som;
520 J2++;
521 }
522 I1++;
523 }
524}
525
526void math_Matrix::TMultiply(const math_Matrix& TLeft,
527 const math_Matrix& Right)
528{
529
530 Standard_DimensionError_Raise_if((TLeft.RowNumber() != Right.RowNumber()) ||
531 (RowNumber() != TLeft.ColNumber()) ||
532 (ColNumber() != Right.ColNumber()),
533 "");
534
535 Standard_Real Som;
536 Standard_Integer I1 = TLeft.LowerColIndex;
537 for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) {
538 Standard_Integer J2 = Right.LowerColIndex;
539 for(Standard_Integer J = LowerColIndex; J <= UpperColIndex; J++) {
540 Som = 0.0;
541 Standard_Integer J1 = TLeft.LowerRowIndex;
542 Standard_Integer I2 = Right.LowerRowIndex;
543 for(Standard_Integer K = TLeft.LowerRowIndex; K <= TLeft.UpperRowIndex; K++) {
544 Som = Som + TLeft.Array(J1, I1) * Right.Array(I2, J2);
545 J1++;
546 I2++;
547 }
548 Array(I, J) = Som;
549 J2++;
550 }
551 I1++;
552 }
553}
554
555void math_Matrix::Add (const math_Matrix& Left,
556 const math_Matrix& Right)
557{
558
559 Standard_DimensionError_Raise_if((RowNumber() != Right.RowNumber()) ||
560 (ColNumber() != Right.ColNumber()) ||
561 (Right.RowNumber() != Left.RowNumber()) ||
562 (Right.ColNumber() != Left.ColNumber()),
563 "");
564
565 Standard_Integer I1 = Left.LowerRowIndex;
566 Standard_Integer I2 = Right.LowerRowIndex;
567 for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) {
568 Standard_Integer J1 = Left.LowerColIndex;
569 Standard_Integer J2 = Right.LowerColIndex;
570 for(Standard_Integer J = LowerColIndex; J <= UpperColIndex; J++) {
571 Array(I, J) = Left.Array(I1, J1) + Right.Array(I2, J2);
572 J1++;
573 J2++;
574 }
575 I1++;
576 I2++;
577 }
578}
579
580void math_Matrix::Subtract(const math_Matrix& Left,
581 const math_Matrix& Right)
582{
583
584 Standard_DimensionError_Raise_if((RowNumber() != Right.RowNumber()) ||
585 (ColNumber() != Right.ColNumber()) ||
586 (Right.RowNumber() != Left.RowNumber()) ||
587 (Right.ColNumber() != Left.ColNumber()),
588 "");
589
590 Standard_Integer I1 = Left.LowerRowIndex;
591 Standard_Integer I2 = Right.LowerRowIndex;
592 for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) {
593 Standard_Integer J1 = Left.LowerColIndex;
594 Standard_Integer J2 = Right.LowerColIndex;
595 for(Standard_Integer J = LowerColIndex; J <= UpperColIndex; J++) {
596 Array(I, J) = Left.Array(I1, J1) - Right.Array(I2, J2);
597 J1++;
598 J2++;
599 }
600 I1++;
601 I2++;
602 }
603}
604
605
606void math_Matrix::Multiply(const math_Matrix& Right)
607{
608
609 Standard_DimensionError_Raise_if(ColNumber() != Right.RowNumber(), "");
610
611 Standard_Real Som;
612 for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) {
613 for(Standard_Integer J2 = Right.LowerColIndex; J2 <= Right.UpperColIndex; J2++) {
614 Som = 0.0;
615 Standard_Integer I2 = Right.LowerRowIndex;
616 for(Standard_Integer J = LowerColIndex; J <= UpperColIndex; J++) {
617 Som = Som + Array(I, J) * Right.Array(I2, J2);
618 I2++;
619 }
620 Array(I, J2) = Som;
621 }
622 }
623}
624
625
626
627math_Vector math_Matrix::Multiplied(const math_Vector& Right)const
628{
629
630 Standard_DimensionError_Raise_if(ColNumber() != Right.Length(), "");
631
632 math_Vector Result(LowerRowIndex, UpperRowIndex);
633
634 for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) {
635 Result.Array(I) = 0.0;
636 Standard_Integer II = Right.LowerIndex;
637 for(Standard_Integer J = LowerColIndex; J <= UpperColIndex; J++) {
638 Result.Array(I) = Result.Array(I) + Array(I, J) * Right.Array(II);
639 II++;
640 }
641 }
642 return Result;
643}
644
645math_Matrix& math_Matrix::Initialized(const math_Matrix& Other)
646{
647
648 Standard_DimensionError_Raise_if((RowNumber() != Other.RowNumber()) ||
649 (ColNumber() != Other.ColNumber()), "");
650
651 (Other.Array).Copy(Array);
652 return *this;
653}
654
655
656
657
658void math_Matrix::Dump(Standard_OStream& o)const
659
660{
661 o << "math_Matrix of RowNumber = " << RowNumber();
662 o << " and ColNumber = " << ColNumber() << "\n";
663
664 for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) {
665 for(Standard_Integer J = LowerColIndex; J <= UpperColIndex; J++) {
666 o << "math_Matrix ( " << I << ", " << J << " ) = ";
667 o << Array(I, J) << "\n";
668 }
669 }
670}
671