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.
16 #include <math_BrentMinimum.hxx>
17 #include <math_Function.hxx>
18 #include <StdFail_NotDone.hxx>
20 static const Standard_Real CGOLD = 0.3819660; //0.5*(3 - sqrt(5));
23 //=======================================================================
25 //purpose : Shifts arguments
26 //=======================================================================
27 inline void SHFT(Standard_Real &theA, Standard_Real &theB,
28 Standard_Real &theC, Standard_Real &theD)
35 //=======================================================================
36 //function : math_BrentMinimum
37 //purpose : Constructor
38 //=======================================================================
39 math_BrentMinimum::math_BrentMinimum(const Standard_Real theTolX,
40 const Standard_Integer theNbIterations,
41 const Standard_Real theZEPS)
50 Done (Standard_False),
52 Itermax(theNbIterations),
57 //=======================================================================
58 //function : math_BrentMinimum
59 //purpose : Constructor
60 //=======================================================================
61 math_BrentMinimum::math_BrentMinimum(const Standard_Real theTolX,
62 const Standard_Real theFbx,
63 const Standard_Integer theNbIterations,
64 const Standard_Real theZEPS)
73 Done (Standard_False),
75 Itermax(theNbIterations),
80 //=======================================================================
81 //function : ~math_BrentMinimum
82 //purpose : Destructor
83 //=======================================================================
84 math_BrentMinimum::~math_BrentMinimum()
88 //=======================================================================
91 //=======================================================================
92 void math_BrentMinimum::Perform(math_Function& F,
93 const Standard_Real ax,
94 const Standard_Real bx,
95 const Standard_Real cx)
98 Standard_Real etemp, fu, p, q, r;
99 Standard_Real tol1, tol2, u, v, w, xm;
100 Standard_Real e = 0.0;
101 Standard_Real d = RealLast();
103 a = ((ax < cx) ? ax : cx);
104 b = ((ax > cx) ? ax : cx);
111 for (iter = 1; iter <= Itermax; iter++) {
113 tol1 = XTol * fabs(x) + EPSZ;
115 if (IsSolutionReached(F)) {
116 Done = Standard_True;
119 if (fabs(e) > tol1) {
120 r = (x - w) * (fx - fv);
121 q = (x - v) * (fx - fw);
122 p = (x - v) * q - (x - w) * r;
128 if (fabs(p) >= fabs(0.5 * q * etemp)
129 || p <= q * (a - x) || p >= q * (b - x)) {
130 e = (x >= xm ? a - x : b - x);
136 if (u - a < tol2 || b - u < tol2) d = Sign(tol1, xm - x);
140 e = (x >= xm ? a - x : b - x);
143 u = (fabs(d) >= tol1 ? x + d : x + Sign(tol1, d));
147 if (u >= x) a = x; else b = x;
149 SHFT(fv, fw, fx, fu);
152 if (u < x) a = u; else b = u;
153 if (fu <= fw || w == x) {
159 else if (fu <= fv || v == x || v == w) {
165 Done = Standard_False;
169 //=======================================================================
172 //=======================================================================
173 void math_BrentMinimum::Dump(Standard_OStream& o) const
175 o << "math_BrentMinimum ";
177 o << " Status = Done \n";
178 o << " Location value = " << x <<"\n";
179 o << " Minimum value = " << fx << "\n";
180 o << " Number of iterations = " << iter <<"\n";
183 o << " Status = not Done \n";