brent_minimize.h

00001 //===========================================================================
00002 // GoTools - SINTEF Geometry Tools version 1.1
00003 //
00004 // GoTools module: CORE
00005 //
00006 // Copyright (C) 2000-2007 SINTEF ICT, Applied Mathematics, Norway.
00007 //
00008 // This program is free software; you can redistribute it and/or          
00009 // modify it under the terms of the GNU General Public License            
00010 // as published by the Free Software Foundation version 2 of the License. 
00011 //
00012 // This program is distributed in the hope that it will be useful,        
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of         
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          
00015 // GNU General Public License for more details.                           
00016 //
00017 // You should have received a copy of the GNU General Public License      
00018 // along with this program; if not, write to the Free Software            
00019 // Foundation, Inc.,                                                      
00020 // 59 Temple Place - Suite 330,                                           
00021 // Boston, MA  02111-1307, USA.                                           
00022 //
00023 // Contact information: E-mail: tor.dokken@sintef.no                      
00024 // SINTEF ICT, Department of Applied Mathematics,                         
00025 // P.O. Box 124 Blindern,                                                 
00026 // 0314 Oslo, Norway.                                                     
00027 //
00028 // Other licenses are also available for this software, notably licenses
00029 // for:
00030 // - Building commercial software.                                        
00031 // - Building software whose source code you wish to keep private.        
00032 //===========================================================================
00033 #ifndef _BRENT_MINIMIZE_H
00034 #define _BRENT_MINIMIZE_H
00035 
00036 #include "GeneralFunctionMinimizer.h"
00037 
00038 namespace Go
00039 {
00042 
00047 template<class Functor>
00048 class Fun2Fun
00049 {
00050 public:
00051     Fun2Fun(const Functor& f, double a, double b) : f_(f), a_(a), b_(b) {}
00052     double operator()(const double* arg) const
00053     {
00054         return f_(*arg);
00055     }
00057     double minPar(int n) const { return a_; }
00058     double maxPar(int n) const { return b_; }
00059 private:
00060     Functor f_;
00061     double a_;
00062     double b_;
00063 };
00064 
00065 //===========================================================================
00066 template<class Functor>
00067 inline double brent_minimize(const Functor& f,
00068                              double a, double b, double c,
00069                              double& parmin,
00070                              const double rel_tolerance = std::sqrt(std::numeric_limits<double>::epsilon()))
00071 //===========================================================================
00072 {
00073     Fun2Fun<Functor> f2(f, a, c);
00074     Go::FunctionMinimizer<Fun2Fun<Functor> > fmin(1, f2, &a, rel_tolerance);
00075     Go::Point dir(1);
00076     dir[0] = 1.0;
00077     double bracket[3];
00078     double fval_brak[3];
00079     bracket[0] = 0.0;
00080     bracket[1] = b-a;
00081     bracket[2] = c-a;
00082     fval_brak[0] = f(a);
00083     fval_brak[1] = f(b);
00084     fval_brak[2] = f(c);
00085     double minimum = fmin.linminBrent(dir, bracket, fval_brak);
00086     parmin = fmin.getPar(0);
00087     return minimum;
00088 }
00089 
00090 
00092 } // namespace Go
00093 
00094 #endif // _BRENT_MINIMIZE_H
00095 
00096 

Generated on Mon Jun 11 14:48:18 2007 for GoTools Core Library by  doxygen 1.5.1