0025571: Avoid base Classes without virtual Destructors
[occt.git] / src / math / math_NewtonFunctionRoot.cxx
CommitLineData
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
7fd59977 15
42cf5bc1 16#include <math_FunctionWithDerivative.hxx>
17#include <math_NewtonFunctionRoot.hxx>
18#include <StdFail_NotDone.hxx>
7fd59977 19
20math_NewtonFunctionRoot::math_NewtonFunctionRoot (math_FunctionWithDerivative& F,
21 const Standard_Real Guess,
22 const Standard_Real EpsX ,
23 const Standard_Real EpsF ,
24 const Standard_Real A,
25 const Standard_Real B,
26 const Standard_Integer NbIterations ){
27 EpsilonX = EpsX;
28 EpsilonF = EpsF;
29 Binf = A;
30 Bsup = B;
31 Itermax = NbIterations;
32 Done = Standard_False;
33 X = RealLast();
34 DFx = 0;
35 Fx = RealLast();
36 It = 0;
37 Perform(F, Guess);
38}
39
40
41math_NewtonFunctionRoot::math_NewtonFunctionRoot (const Standard_Real A ,
42 const Standard_Real B,
43 const Standard_Real EpsX ,
44 const Standard_Real EpsF ,
45 const Standard_Integer NbIterations ){
46
47 Binf = A;
48 Bsup = B;
49 EpsilonX = EpsX;
50 EpsilonF = EpsF;
51 Itermax = NbIterations;
52 Done = Standard_False;
53 X = RealLast();
54 DFx = 0;
55 Fx = RealLast();
56 It = 0;
57}
58
59
60math_NewtonFunctionRoot::math_NewtonFunctionRoot (math_FunctionWithDerivative& F,
61 const Standard_Real Guess,
62 const Standard_Real EpsX ,
63 const Standard_Real EpsF ,
64 const Standard_Integer NbIterations ){
65 EpsilonX = EpsX;
66 EpsilonF = EpsF;
67 Itermax = NbIterations;
68 Binf = RealFirst();
69 Bsup = RealLast();
70 Done = Standard_False;
71 X = RealLast();
72 DFx = 0;
73 Fx = RealLast();
74 It = 0;
75 Perform(F, Guess);
76}
77
78
79void math_NewtonFunctionRoot::Perform(math_FunctionWithDerivative& F,
80 const Standard_Real Guess) {
81
82 Standard_Real Dx;
83 Standard_Boolean Ok;
84 Standard_Real AA, BB;
85
86 //--------------------------------------------------
87 //-- lbr le 12 Nov 97
88 //-- la meilleure estimation n est pas sauvee et on
89 //-- renvoie une solution plus fausse que Guess
90 Standard_Real BestX=X,BestFx=RealLast();
91 //--
92
93 if ( Binf < Bsup) {
94 AA = Binf;
95 BB = Bsup;
96 }
97 else {
98 AA = Bsup;
99 BB = Binf;
100 }
101
102 Dx = RealLast();
103 Fx = RealLast();
104 X = Guess;
105 It = 1;
106 while ( (It <= Itermax) && ( (Abs(Dx) > EpsilonX) ||
107 (Abs(Fx) > EpsilonF) ) ) {
108 Ok = F.Values(X,Fx,DFx);
109
110 Standard_Real AbsFx = Fx; if(AbsFx<0) AbsFx=-AbsFx;
111 if(AbsFx<BestFx) {
112 BestFx=AbsFx;
113 BestX =X;
114 }
115
116 if (Ok) {
117 if (DFx == 0.) {
118 Done = Standard_False;
119 It = Itermax + 1;
120 }
121 else {
122 Dx = Fx/DFx;
123 X -= Dx;
124 // Limitation des variations de X:
125 if (X <= AA) X = AA;
126 if (X >= BB) X = BB;
127 It++;
128 }
129 }
130 else {
131 Done = Standard_False;
132 It = Itermax + 1;
133 }
134 }
135 X = BestX;
136
137 if (It <= Itermax) {
138 Done = Standard_True;
139 }
140 else
141 {
142 Done = Standard_False;
143 }
144}
145
146
147void math_NewtonFunctionRoot::Dump(Standard_OStream& o) const {
148
149 o <<"math_NewtonFunctionRoot ";
150 if (Done) {
151 o << " Status = Done \n";
152 o << " Location found = " << X <<"\n";
153 o << " function value at this minimum = " << Fx <<"\n";
154 o << " Number of iterations = " << It <<"\n";
155 }
156 else {
157 o << "Status = not Done \n";
158 }
159}
160
161
162