Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
GSLMinimizer1D.cxx
Go to the documentation of this file.
1// @(#)root/mathmore:$Id$
2// Authors: L. Moneta, A. Zsenei 08/2005
3 /**********************************************************************
4 * *
5 * Copyright (c) 2004 moneta, CERN/PH-SFT *
6 * *
7 * This library is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU General Public License *
9 * as published by the Free Software Foundation; either version 2 *
10 * of the License, or (at your option) any later version. *
11 * *
12 * This library is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this library (see file COPYING); if not, write *
19 * to the Free Software Foundation, Inc., 59 Temple Place, Suite *
20 * 330, Boston, MA 02111-1307 USA, or contact the author. *
21 * *
22 **********************************************************************/
23
24// Implementation file for class GSLMinimizer1D
25//
26// Created by: moneta at Wed Dec 1 15:04:51 2004
27//
28// Last update: Wed Dec 1 15:04:51 2004
29//
30
31#include <cassert>
32
33#include "Math/GSLMinimizer1D.h"
34#include "Math/Error.h"
35
36#include "GSLFunctionWrapper.h"
38
39
40#include "gsl/gsl_min.h"
41#include "gsl/gsl_errno.h"
42
43#include <iostream>
44#include <cmath>
45
46namespace ROOT {
47
48namespace Math {
49
50
52 fXmin(0), fXlow(0), fXup(0), fMin(0), fLow(0), fUp(0),
53 fIter(0), fStatus(-1), fIsSet(false),
54 fMinimizer(nullptr), fFunction(nullptr)
55{
56 // construct a minimizer passing the algorithm type as an enumeration
57
58 const gsl_min_fminimizer_type* T = nullptr ;
59 switch ( type )
60 {
62 T = gsl_min_fminimizer_goldensection;
63 break ;
64 case Minim1D::kBRENT :
65 T = gsl_min_fminimizer_brent;
66 break ;
67 default :
68 // default case is brent
69 T = gsl_min_fminimizer_brent;
70 break ;
71 }
72
75
76}
77
79{
80 // destructor: clean up minimizer and function pointers
81
82 if (fMinimizer) delete fMinimizer;
83 if (fFunction) delete fFunction;
84}
85
86void GSLMinimizer1D::SetFunction( GSLFuncPointer f, void * p, double xmin, double xlow, double xup) {
87 // set the function to be minimized
88 assert(fFunction);
89 assert(fMinimizer);
90 fXlow = xlow;
91 fXup = xup;
92 fXmin = xmin;
95
96#ifdef DEBUG
97 std::cout << " [ "<< xlow << " , " << xup << " ]" << std::endl;
98#endif
99
100 int status = gsl_min_fminimizer_set( fMinimizer->Get(), fFunction->GetFunc(), xmin, xlow, xup);
101 if (status != GSL_SUCCESS)
102 std::cerr <<"GSLMinimizer1D: Error: Interval [ "<< xlow << " , " << xup << " ] does not contain a minimum" << std::endl;
103
104
105 fIsSet = true;
106 fStatus = -1;
107 return;
108}
109
111 // perform an iteration and update values
112 if (!fIsSet) {
113 std::cerr << "GSLMinimizer1D- Error: Function has not been set in Minimizer" << std::endl;
114 return -1;
115 }
116
117 int status = gsl_min_fminimizer_iterate(fMinimizer->Get());
118 // update values
119 fXmin = gsl_min_fminimizer_x_minimum(fMinimizer->Get() );
120 fMin = gsl_min_fminimizer_f_minimum(fMinimizer->Get() );
121 // update interval values
122 fXlow = gsl_min_fminimizer_x_lower(fMinimizer->Get() );
123 fXup = gsl_min_fminimizer_x_upper(fMinimizer->Get() );
124 fLow = gsl_min_fminimizer_f_lower(fMinimizer->Get() );
125 fUp = gsl_min_fminimizer_f_upper(fMinimizer->Get() );
126 return status;
127}
128
130 // return x value at function minimum
131 return fXmin;
132}
133
135 // return lower x value of bracketing interval
136 return fXlow;
137}
138
140 // return upper x value of bracketing interval
141 return fXup;
142}
143
145 // return function value at minimum
146 return fMin;
147}
148
150 // return function value at x lower
151 return fLow;
152}
153
155 // return function value at x upper
156 return fUp;
157}
158
159const char * GSLMinimizer1D::Name() const {
160 // return name of minimization algorithm
161 return gsl_min_fminimizer_name(fMinimizer->Get() );
162}
163
164bool GSLMinimizer1D::Minimize (int maxIter, double absTol, double relTol)
165{
166 // find the minimum via multiple iterations
167 fStatus = -1;
168 int iter = 0;
169 int status = 0;
170 do {
171 iter++;
172 status = Iterate();
173 if (status != GSL_SUCCESS) {
174 MATH_ERROR_MSG("GSLMinimizer1D::Minimize","error returned when performing an iteration");
175 fStatus = status;
176 return false;
177 }
178
179#ifdef DEBUG
180 std::cout << "Min1D - iteration " << iter << " interval : [ " << fXlow << " , " << fXup << " ] min = " << fXmin
181 << " fmin " << fMin << " f(a) " << fLow << " f(b) " << fUp << std::endl;
182#endif
183
184
185 status = TestInterval(fXlow, fXup, absTol, relTol);
186 if (status == GSL_SUCCESS) {
187 fIter = iter;
188 fStatus = status;
189 return true;
190 }
191 }
192 while (status == GSL_CONTINUE && iter < maxIter);
193 if (status == GSL_CONTINUE) {
194 double tol = std::abs(fXup-fXlow);
195 MATH_INFO_MSGVAL("GSLMinimizer1D::Minimize","exceeded max iterations, reached tolerance is not sufficient",tol);
196 }
197 fStatus = status;
198 return false;
199}
200
201
202int GSLMinimizer1D::TestInterval( double xlow, double xup, double epsAbs, double epsRel) {
203// static function to test interval
204 return gsl_min_test_interval(xlow, xup, epsAbs, epsRel);
205}
206
207} // end namespace Math
208
209} // end namespace ROOT
210
#define MATH_INFO_MSGVAL(loc, txt, x)
Definition Error.h:101
#define MATH_ERROR_MSG(loc, str)
Definition Error.h:83
#define f(i)
Definition RSha256.hxx:104
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
float xmin
wrapper class for gsl_min_fminimizer structure
gsl_min_fminimizer * Get() const
Wrapper class to the gsl_function C structure.
void SetFuncPointer(GSLFuncPointer f)
set in the GSL C struct the pointer to the function evaluation
void SetParams(void *p)
set in the GSL C struct the extra-object pointer
double FValMinimum() const override
Return function value at current estimate of the minimum.
const char * Name() const override
Return name of minimization algorithm.
int Iterate()
Perform a minimizer iteration and if an unexpected problem occurs then an error code will be returned...
GSL1DMinimizerWrapper * fMinimizer
double XLower() const override
Return current lower bound of the minimization interval.
static int TestInterval(double xlow, double xup, double epsAbs, double epsRel)
Test convergence of the interval.
GSLMinimizer1D(Minim1D::Type type=Minim1D::kBRENT)
Construct the minimizer passing the minimizer type using the Minim1D::Algorithm enumeration.
void SetFunction(const UserFunc &f, double xmin, double xlow, double xup)
Set, or reset, minimizer to use the function f and the initial search interval [xlow,...
double XUpper() const override
Return current upper bound of the minimization interval.
double FValUpper() const override
Return function value at current upper bound of the minimization interval.
~GSLMinimizer1D() override
Destructor: free allocated resources.
double FValLower() const override
Return function value at current lower bound of the minimization interval.
GSLFunctionWrapper * fFunction
double XMinimum() const override
Return current estimate of the position of the minimum.
bool Minimize(int maxIter, double absTol, double relTol) override
Find minimum position iterating until convergence specified by the absolute and relative tolerance or...
Type
Enumeration with One Dimensional Minimizer Algorithms.
Namespace for new Math classes and functions.
double(* GSLFuncPointer)(double, void *)
Function pointer corresponding to gsl_function signature.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...