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_BrentMinimum.ixx>
16 #include <math_Function.hxx>
18 #define CGOLD 0.3819660
22 #define MAX(a,b) ((a) > (b) ? (a) : (b))
23 #define SIGN(a,b) ((b) > 0.0 ? fabs(a) : -fabs(a))
24 #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d)
26 //=======================================================================
27 //function : math_BrentMinimum
28 //purpose : Constructor
29 //=======================================================================
30 math_BrentMinimum::math_BrentMinimum(const Standard_Real theTolX,
31 const Standard_Integer theNbIterations,
32 const Standard_Real theZEPS)
41 Done (Standard_False),
43 Itermax(theNbIterations),
48 //=======================================================================
49 //function : math_BrentMinimum
50 //purpose : Constructor
51 //=======================================================================
52 math_BrentMinimum::math_BrentMinimum(const Standard_Real theTolX,
53 const Standard_Real theFbx,
54 const Standard_Integer theNbIterations,
55 const Standard_Real theZEPS)
64 Done (Standard_False),
66 Itermax(theNbIterations),
71 //=======================================================================
72 //function : ~math_BrentMinimum
73 //purpose : Destructor
74 //=======================================================================
75 math_BrentMinimum::~math_BrentMinimum()
79 //=======================================================================
82 //=======================================================================
83 void math_BrentMinimum::Perform(math_Function& F,
84 const Standard_Real ax,
85 const Standard_Real bx,
86 const Standard_Real cx)
89 Standard_Real etemp, fu, p, q, r;
90 Standard_Real tol1, tol2, u, v, w, xm;
91 Standard_Real e = 0.0;
92 Standard_Real d = RealLast();
94 a = ((ax < cx) ? ax : cx);
95 b = ((ax > cx) ? ax : cx);
102 for(iter = 1; iter <= Itermax; iter++) {
104 tol1 = XTol * fabs(x) + EPSZ;
106 if(IsSolutionReached(F)) {
107 Done = Standard_True;
111 r = (x - w) * (fx - fv);
112 q = (x - v) * (fx - fw);
113 p = (x - v) * q - (x - w) * r;
119 if(fabs(p) >= fabs(0.5 * q * etemp)
120 || p <= q * ( a - x) || p >= q * (b - x)) {
121 e = (x >= xm ? a - x : b - x);
127 if(u - a < tol2 || b - u < tol2) d = SIGN(tol1, xm - x);
131 e = (x >= xm ? a - x : b - x);
134 u = (fabs(d) >= tol1 ? x + d : x + SIGN(tol1, d));
138 if(u >= x) a = x; else b = x;
140 SHFT(fv, fw, fx, fu);
143 if(u < x) a = u; else b = u;
144 if(fu <= fw || w == x) {
150 else if(fu <= fv || v == x || v == w) {
156 Done = Standard_False;
160 //=======================================================================
163 //=======================================================================
164 void math_BrentMinimum::Dump(Standard_OStream& o) const
166 o << "math_BrentMinimum ";
168 o << " Status = Done \n";
169 o << " Location value = " << x <<"\n";
170 o << " Minimum value = " << fx << "\n";
171 o << " Number of iterations = " << iter <<"\n";
174 o << " Status = not Done \n";