0022904: Clean up sccsid variables
[occt.git] / src / math / math_NewtonFunctionSetRoot.cxx
1 //#ifndef DEB
2 #define No_Standard_RangeError
3 #define No_Standard_OutOfRange
4 #define No_Standard_DimensionError
5 //#endif
6
7 #include <math_NewtonFunctionSetRoot.ixx>
8 #include <math_Recipes.hxx>
9 #include <math_FunctionSetWithDerivatives.hxx>
10
11 Standard_Boolean math_NewtonFunctionSetRoot::IsSolutionReached
12 //                              (math_FunctionSetWithDerivatives& F) 
13                               (math_FunctionSetWithDerivatives& ) 
14 {
15   
16   for(Standard_Integer i = DeltaX.Lower(); i <= DeltaX.Upper(); i++) {
17     if(Abs(DeltaX(i)) > TolX(i) || Abs(FValues(i)) > TolF) return Standard_False;
18   } 
19   return Standard_True;
20 }
21
22
23 // Constructeurs d'initialisation des champs (pour utiliser Perform)
24
25 math_NewtonFunctionSetRoot::math_NewtonFunctionSetRoot(
26                             math_FunctionSetWithDerivatives& F,
27                             const math_Vector& XTol,
28                             const Standard_Real FTol,
29                             const Standard_Integer NbIterations):
30                             TolX(1, F.NbVariables()),
31                             TolF(FTol),
32                             Indx(1, F.NbVariables()),
33                             Scratch(1, F.NbVariables()),
34                             Sol(1, F.NbVariables()),
35                             DeltaX(1, F.NbVariables()),
36                             FValues(1, F.NbVariables()),
37                             Jacobian(1, F.NbVariables(),
38                                      1, F.NbVariables()),
39                             Itermax(NbIterations)
40 {
41   for (Standard_Integer i = 1; i <= TolX.Length(); i++) {
42     TolX(i) = XTol(i);
43   }
44 }
45
46
47 math_NewtonFunctionSetRoot::math_NewtonFunctionSetRoot(
48                             math_FunctionSetWithDerivatives& F,
49                             const Standard_Real FTol,
50                             const Standard_Integer NbIterations):
51                             TolX(1, F.NbVariables()),
52                             TolF(FTol),
53                             Indx(1, F.NbVariables()),
54                             Scratch(1, F.NbVariables()),
55                             Sol(1, F.NbVariables()),
56                             DeltaX(1, F.NbVariables()),
57                             FValues(1, F.NbVariables()),
58                             Jacobian(1, F.NbVariables(),
59                                      1, F.NbVariables()),
60                             Itermax(NbIterations)
61 {
62 }
63
64
65 math_NewtonFunctionSetRoot::math_NewtonFunctionSetRoot
66                            (math_FunctionSetWithDerivatives& F,
67                             const math_Vector& StartingPoint,
68                             const math_Vector&    XTol,
69                             const Standard_Real    FTol,
70                             const Standard_Integer NbIterations) :
71                             
72                             TolX(1, F.NbVariables()),
73                             TolF(FTol),
74                             Indx    (1, F.NbVariables()),
75                             Scratch (1, F.NbVariables()),
76                             Sol     (1, F.NbVariables()),
77                             DeltaX  (1, F.NbVariables()),
78                             FValues (1, F.NbVariables()),
79                             Jacobian(1, F.NbVariables(),
80                                      1, F.NbVariables()),
81                             Itermax(NbIterations)
82 {
83   for (Standard_Integer i = 1; i <= TolX.Length(); i++) {
84     TolX(i) = XTol(i);
85   }
86   math_Vector UFirst(1, F.NbVariables()),
87               ULast(1, F.NbVariables());
88   UFirst.Init(RealFirst());
89   ULast.Init(RealLast());
90   Perform(F, StartingPoint, UFirst, ULast);
91 }
92
93 math_NewtonFunctionSetRoot::math_NewtonFunctionSetRoot
94                               (math_FunctionSetWithDerivatives& F,
95                                const math_Vector& StartingPoint,
96                                const math_Vector&    InfBound,
97                                const math_Vector&    SupBound,
98                                const math_Vector&    XTol,
99                                const Standard_Real    FTol,
100                                const Standard_Integer NbIterations) :
101                                                             
102                                TolX(1, F.NbVariables()),
103                                TolF(FTol),
104                                Indx    (1, F.NbVariables()),
105                                Scratch (1, F.NbVariables()),
106                                Sol     (1, F.NbVariables()),
107                                DeltaX  (1, F.NbVariables()),
108                                FValues (1, F.NbVariables()),
109                                Jacobian(1, F.NbVariables(),
110                                         1, F.NbVariables()),
111                                Itermax(NbIterations)
112 {
113   for (Standard_Integer i = 1; i <= TolX.Length(); i++) {
114     TolX(i) = XTol(i);
115   }
116   Perform(F, StartingPoint, InfBound, SupBound);
117 }
118
119 void math_NewtonFunctionSetRoot::Delete()
120 {}
121
122 void math_NewtonFunctionSetRoot::SetTolerance
123                               (const math_Vector& XTol)
124 {
125   for (Standard_Integer i = 1; i <= TolX.Length(); i++) {
126     TolX(i) = XTol(i);
127   }
128 }
129
130
131
132 void math_NewtonFunctionSetRoot::Perform(
133                            math_FunctionSetWithDerivatives& F,
134                            const math_Vector& StartingPoint,
135                            const math_Vector& InfBound,
136                            const math_Vector& SupBound) 
137 {
138
139   Standard_Real d;
140   Standard_Boolean OK;
141   Standard_Integer Error;
142   
143   Done = Standard_False;
144   Sol = StartingPoint;
145   OK = F.Values(Sol, FValues, Jacobian);
146   if(!OK) return;
147   for(Iter = 1; Iter <= Itermax; Iter++) {
148     for(Standard_Integer k = 1; k <= DeltaX.Length(); k++) {
149       DeltaX(k) = -FValues(k);
150     }
151     Error = LU_Decompose(Jacobian, Indx, d, Scratch, 1.0e-30);
152     if(Error) return; 
153     LU_Solve(Jacobian, Indx, DeltaX);
154     for(Standard_Integer i = 1; i <= Sol.Length(); i++) { 
155       Sol(i) += DeltaX(i);
156       
157       // Limitation de Sol dans les bornes [InfBound, SupBound] :
158       if (Sol(i) <= InfBound(i)) Sol(i) = InfBound(i);
159       if (Sol(i) >= SupBound(i)) Sol(i) = SupBound(i);
160       
161     }
162     OK = F.Values(Sol, FValues, Jacobian);
163     if(!OK) return;
164     if(IsSolutionReached(F)) { 
165       State = F.GetStateNumber();
166       Done = Standard_True; 
167       return;
168     }
169   }               
170 }
171
172 void math_NewtonFunctionSetRoot::Dump(Standard_OStream& o) const 
173 {
174   o <<"math_NewtonFunctionSetRoot ";
175   if (Done) {
176     o << " Status = Done \n";
177     o << " Vector solution = " << Sol <<"\n";
178     o << " Value of the function at this solution = \n";
179     o << FValues <<"\n";
180     o << " Number of iterations = " << Iter <<"\n";
181   }
182   else {
183     o << "Status = not Done \n";
184   }
185 }