1 // Copyright (c) 1997-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #include <math_BissecNewton.ixx>
16 #include <math_FunctionWithDerivative.hxx>
18 //=======================================================================
19 //function : math_BissecNewton
20 //purpose : Constructor
21 //=======================================================================
22 math_BissecNewton::math_BissecNewton(const Standard_Real theXTolerance)
23 : TheStatus(math_NotBracketed),
33 //=======================================================================
34 //function : ~math_BissecNewton
35 //purpose : Destructor
36 //=======================================================================
37 math_BissecNewton::~math_BissecNewton()
41 //=======================================================================
44 //=======================================================================
45 void math_BissecNewton::Perform(math_FunctionWithDerivative& F,
46 const Standard_Real Bound1,
47 const Standard_Real Bound2,
48 const Standard_Integer NbIterations)
50 Standard_Boolean GOOD;
52 Standard_Real dxold, fh, fl;
53 Standard_Real swap, temp, xh, xl;
55 GOOD = F.Values(Bound1, fl, df);
57 Done = Standard_False;
58 TheStatus = math_FunctionError;
61 GOOD = F.Values(Bound2, fh, df);
63 Done = Standard_False;
64 TheStatus = math_FunctionError;
67 // Modified by Sergey KHROMOV - Wed Jan 22 12:06:45 2003 Begin
68 Standard_Real aFTol = RealEpsilon();
70 // if(fl * fh >= 0.0) {
71 if(fl * fh > aFTol*aFTol) {
72 Done = Standard_False;
73 TheStatus = math_NotBracketed;
77 if(fl < -aFTol || (fl < aFTol && fh < -aFTol)) {
88 // Modified by Sergey KHROMOV - Wed Jan 22 12:06:49 2003 End
89 x = 0.5 * (Bound1 + Bound2);
90 dxold = fabs(Bound2 - Bound1);
92 GOOD = F.Values(x, f, df);
94 Done = Standard_False;
95 TheStatus = math_FunctionError;
98 for(j = 1; j <= NbIterations; j++) {
99 if((((x - xh) * df - f) * ((x - xl) * df - f) >= 0.0)
100 || (fabs(2.0 * f) > fabs(dxold * df))) {
102 dx = 0.5 * (xh - xl);
106 Done = Standard_True;
117 Done = Standard_True;
121 if(IsSolutionReached(F)) {
123 Done = Standard_True;
126 GOOD = F.Values(x, f, df);
128 Done = Standard_False;
129 TheStatus = math_FunctionError;
142 Done = Standard_True;
146 TheStatus = math_TooManyIterations;
147 Done = Standard_False;
151 //=======================================================================
154 //=======================================================================
155 void math_BissecNewton::Dump(Standard_OStream& o) const {
157 o << "math_BissecNewton ";
159 o << " Status = Done \n";
160 o << " The Root is: " << x << endl;
161 o << " The value at this Root is: " << f << endl;
164 o << " Status = not Done \n";