b311480e |
1 | // Copyright (c) 1997-1999 Matra Datavision |
973c2be1 |
2 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
3 | // |
973c2be1 |
4 | // This file is part of Open CASCADE Technology software library. |
b311480e |
5 | // |
d5f74e42 |
6 | // This library is free software; you can redistribute it and/or modify it under |
7 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
8 | // by the Free Software Foundation, with special exception defined in the file |
9 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
10 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
11 | // |
973c2be1 |
12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. |
b311480e |
14 | |
42cf5bc1 |
15 | #include <math_Gauss.hxx> |
5716d13b |
16 | |
42cf5bc1 |
17 | #include <math_Matrix.hxx> |
18 | #include <math_NotSquare.hxx> |
7fd59977 |
19 | #include <math_Recipes.hxx> |
7fd59977 |
20 | #include <Standard_DimensionError.hxx> |
21 | #include <Standard_NotImplemented.hxx> |
42cf5bc1 |
22 | #include <StdFail_NotDone.hxx> |
7fd59977 |
23 | |
b311480e |
24 | math_Gauss::math_Gauss(const math_Matrix& A, |
9f785738 |
25 | const Standard_Real MinPivot, |
7e785937 |
26 | const Message_ProgressRange& theProgress) |
27 | : LU (1, A.RowNumber(), 1, A.ColNumber()), |
5716d13b |
28 | Index(1, A.RowNumber()), |
29 | D (0.0), |
30 | Done (Standard_False) |
31 | { |
7fd59977 |
32 | math_NotSquare_Raise_if(A.RowNumber() != A.ColNumber(), " "); |
33 | LU = A; |
34 | Standard_Integer Error = LU_Decompose(LU, |
35 | Index, |
36 | D, |
9f785738 |
37 | MinPivot, |
7e785937 |
38 | theProgress); |
7fd59977 |
39 | if(!Error) { |
40 | Done = Standard_True; |
41 | } |
42 | else { |
43 | Done = Standard_False; |
44 | } |
45 | } |
46 | |
47 | void math_Gauss::Solve(const math_Vector& B, math_Vector& X) const{ |
48 | |
49 | StdFail_NotDone_Raise_if(!Done, " "); |
50 | |
51 | X = B; |
52 | LU_Solve(LU, |
53 | Index, |
54 | X); |
55 | } |
56 | |
57 | void math_Gauss::Solve (math_Vector& X) const{ |
58 | |
59 | StdFail_NotDone_Raise_if(!Done, " "); |
60 | |
61 | if(X.Length() != LU.RowNumber()) { |
9775fa61 |
62 | throw Standard_DimensionError(); |
7fd59977 |
63 | } |
64 | LU_Solve(LU, |
65 | Index, |
66 | X); |
67 | } |
68 | |
69 | Standard_Real math_Gauss::Determinant() const{ |
70 | |
71 | StdFail_NotDone_Raise_if(!Done, " "); |
72 | |
73 | Standard_Real Result = D; |
74 | for(Standard_Integer J = 1; J <= LU.UpperRow(); J++) { |
75 | Result *= LU(J,J); |
76 | } |
77 | return Result; |
78 | } |
79 | |
80 | void math_Gauss::Invert(math_Matrix& Inv) const{ |
81 | |
82 | StdFail_NotDone_Raise_if(!Done, " "); |
83 | |
84 | Standard_DimensionError_Raise_if((Inv.RowNumber() != LU.RowNumber()) || |
85 | (Inv.ColNumber() != LU.ColNumber()), |
86 | " "); |
87 | |
88 | Standard_Integer LowerRow = Inv.LowerRow(); |
89 | Standard_Integer LowerCol = Inv.LowerCol(); |
90 | math_Vector Column(1, LU.UpperRow()); |
91 | |
92 | Standard_Integer I, J; |
93 | for(J = 1; J <= LU.UpperRow(); J++) { |
94 | for(I = 1; I <= LU.UpperRow(); I++) { |
95 | Column(I) = 0.0; |
96 | } |
97 | Column(J) = 1.0; |
98 | LU_Solve(LU, Index, Column); |
99 | for(I = 1; I <= LU.RowNumber(); I++) { |
100 | Inv(I+LowerRow-1,J+LowerCol-1) = Column(I); |
101 | } |
102 | } |
103 | |
104 | } |
105 | |
106 | |
107 | void math_Gauss::Dump(Standard_OStream& o) const { |
108 | o << "math_Gauss "; |
109 | if(Done) { |
110 | o<< " Status = Done \n"; |
04232180 |
111 | o << " Determinant of A = " << D << std::endl; |
7fd59977 |
112 | } |
113 | else { |
114 | o << " Status = not Done \n"; |
115 | } |
116 | } |