// @(#)root/mathmore:$Id$
// Author: L. Moneta, A. Zsenei   08/2005
 /**********************************************************************
  *                                                                    *
  * Copyright (c) 2004 moneta,  CERN/PH-SFT                            *
  *                                                                    *
  * This library is free software; you can redistribute it and/or      *
  * modify it under the terms of the GNU General Public License        *
  * as published by the Free Software Foundation; either version 2     *
  * of the License, or (at your option) any later version.             *
  *                                                                    *
  * This library is distributed in the hope that it will be useful,    *
  * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   *
  * General Public License for more details.                           *
  *                                                                    *
  * You should have received a copy of the GNU General Public License  *
  * along with this library (see file COPYING); if not, write          *
  * to the Free Software Foundation, Inc., 59 Temple Place, Suite      *
  * 330, Boston, MA 02111-1307 USA, or contact the author.             *
  *                                                                    *
  **********************************************************************/

// Header file for class GSLMinimizer1D
//
// Created by: moneta  at Wed Dec  1 15:04:51 2004
//
// Last update: Wed Dec  1 15:04:51 2004
//

#ifndef ROOT_Math_GSLMinimizer1D
#define ROOT_Math_GSLMinimizer1D

#include "Math/IMinimizer1D.h"
#include "Math/GSLFunctionAdapter.h"


namespace ROOT {
namespace Math {

   namespace Minim1D {

      /**
          Enumeration with One Dimensional Minimizer Algorithms.
          The algorithms are implemented using GSL, see the
          <A HREF="http://www.gnu.org/software/gsl/manual/gsl-ref_33.html#SEC447">GSL manual</A>.

          The algorithms available are:
          <ul>
          <li><em>Golden Section Algorithm</em>, simplest method of bracketing the minimum of a function
          <li><em>Brent Algorithm</em>, which combines a parabolic interpolation with the golden section algorithm
          </ul>
          @ingroup Min1D
      */

      enum Type {kGOLDENSECTION,
                 kBRENT
      };
   }

   class GSL1DMinimizerWrapper;
   class GSLFunctionWrapper;

//______________________________________________________________________________________
/**

Minimizer for arbitrary one dimensional functions.

Implemented using GSL, for detailed description see:
<A HREF="http://www.gnu.org/software/gsl/manual/html_node/One-dimensional-Minimization.html">GSL online doc</A>

The algorithms uspported are only bracketing algorithm which do not use derivatives information.
The algorithms which can be chosen at construction time are  GOLDENSECTION, whic is the simplest method
but the slowest and BRENT (the default one) which combines the golden section with a parabolic interpolation.


This class does not support copying
@ingroup Min1D
*/

   class GSLMinimizer1D: public IMinimizer1D {

   public:

      /**
         Construct the minimizer passing the minimizer type using the Minim1D::Algorithm enumeration
      */

      explicit GSLMinimizer1D(Minim1D::Type type=Minim1D::kBRENT);

      /**
         Destructor: free allocated resources
      */
      virtual ~GSLMinimizer1D();

   private:
      // usually copying is non trivial, so we make this unaccessible
      GSLMinimizer1D(const GSLMinimizer1D &);
      GSLMinimizer1D & operator = (const GSLMinimizer1D &);

   public:


      /**
          Set, or reset, minimizer to use the function f and the initial search interval [xlow, xup], with a guess for the location of the minimum xmin.
          The condition : \f$ f(xlow) > f(xmin) < f(xup)\f$  must be satisfied
      */
      template <class UserFunc>
      void SetFunction( const UserFunc & f, double xmin, double xlow, double xup) {
         const void * p = &f;
         SetFunction(  &GSLFunctionAdapter<UserFunc>::F, const_cast<void *>(p), xmin, xlow, xup );
      }

      /**
          Set, or reset, minimizer to use the function f and the initial search interval [xlow, xup], with a guess for the location of the minimum xmin.
          The condition : \f$ f(xlow) > f(xmin) < f(xup) \f$ must be satisfied

          Method specialized on the GSL function type
      */
      void SetFunction( GSLFuncPointer  f, void * params, double xmin, double xlow, double xup);

      /**
          Perform a minimizer iteration and
          if an unexepcted problem occurr then an error code will be returned
      */
      int Iterate();


      /**
          Return current estimate of the position of the minimum
      */
      double XMinimum() const;

      /**
         Return current lower bound of the minimization interval
      */
      double XLower() const;

      /**
         Return current upper bound of the minimization interval
      */
      double XUpper() const;

      /**
          Return function value at current estimate of the minimum
      */
      double FValMinimum() const;

      /**
         Return function value at current lower bound of the minimization interval
      */
      double FValLower() const;

      /**
         Return function value at current upper bound of the minimization interval
      */
      double FValUpper() const;


      /**
         Find minimum position iterating until convergence specified by the absolute and relative tolerance or
         the maximum number of iteration is reached
         Return true is result is successfull
         \@param maxIter maximum number of iteration
         \@param absTol desired absolute error in the minimum position
         \@param absTol desired relative error in the minimum position
      */
      bool Minimize( int maxIter, double absTol, double relTol);


      /**
         Return number of iteration used to find minimum
      */
      int Iterations() const {
         return fIter;
      }

      /**
         Return status of last minimization
       */
      int Status() const { return fStatus; }

      /**
         Return name of minimization algorithm
      */
      const char * Name() const;

      /**
         Test convergence of the interval.
         The test returns success if
         \f[
         |x_{min}-x_{truemin}| < epsAbs + epsRel *x_{truemin}
         \f]
      */
      static int TestInterval( double xlow, double xup, double epsAbs, double epsRel);


   protected:


   private:

      double fXmin;
      double fXlow;
      double fXup;
      double fMin;
      double fLow;
      double fUp;
      int fIter;
      int fStatus;    // status of last minimization (==0 ok =1 failed)
      bool fIsSet;


      GSL1DMinimizerWrapper * fMinimizer;
      GSLFunctionWrapper * fFunction;

   };

} // end namespace Math

} // end namespace ROOT


#endif /* ROOT_Math_GSLMinimizer1D */
 GSLMinimizer1D.h:1
 GSLMinimizer1D.h:2
 GSLMinimizer1D.h:3
 GSLMinimizer1D.h:4
 GSLMinimizer1D.h:5
 GSLMinimizer1D.h:6
 GSLMinimizer1D.h:7
 GSLMinimizer1D.h:8
 GSLMinimizer1D.h:9
 GSLMinimizer1D.h:10
 GSLMinimizer1D.h:11
 GSLMinimizer1D.h:12
 GSLMinimizer1D.h:13
 GSLMinimizer1D.h:14
 GSLMinimizer1D.h:15
 GSLMinimizer1D.h:16
 GSLMinimizer1D.h:17
 GSLMinimizer1D.h:18
 GSLMinimizer1D.h:19
 GSLMinimizer1D.h:20
 GSLMinimizer1D.h:21
 GSLMinimizer1D.h:22
 GSLMinimizer1D.h:23
 GSLMinimizer1D.h:24
 GSLMinimizer1D.h:25
 GSLMinimizer1D.h:26
 GSLMinimizer1D.h:27
 GSLMinimizer1D.h:28
 GSLMinimizer1D.h:29
 GSLMinimizer1D.h:30
 GSLMinimizer1D.h:31
 GSLMinimizer1D.h:32
 GSLMinimizer1D.h:33
 GSLMinimizer1D.h:34
 GSLMinimizer1D.h:35
 GSLMinimizer1D.h:36
 GSLMinimizer1D.h:37
 GSLMinimizer1D.h:38
 GSLMinimizer1D.h:39
 GSLMinimizer1D.h:40
 GSLMinimizer1D.h:41
 GSLMinimizer1D.h:42
 GSLMinimizer1D.h:43
 GSLMinimizer1D.h:44
 GSLMinimizer1D.h:45
 GSLMinimizer1D.h:46
 GSLMinimizer1D.h:47
 GSLMinimizer1D.h:48
 GSLMinimizer1D.h:49
 GSLMinimizer1D.h:50
 GSLMinimizer1D.h:51
 GSLMinimizer1D.h:52
 GSLMinimizer1D.h:53
 GSLMinimizer1D.h:54
 GSLMinimizer1D.h:55
 GSLMinimizer1D.h:56
 GSLMinimizer1D.h:57
 GSLMinimizer1D.h:58
 GSLMinimizer1D.h:59
 GSLMinimizer1D.h:60
 GSLMinimizer1D.h:61
 GSLMinimizer1D.h:62
 GSLMinimizer1D.h:63
 GSLMinimizer1D.h:64
 GSLMinimizer1D.h:65
 GSLMinimizer1D.h:66
 GSLMinimizer1D.h:67
 GSLMinimizer1D.h:68
 GSLMinimizer1D.h:69
 GSLMinimizer1D.h:70
 GSLMinimizer1D.h:71
 GSLMinimizer1D.h:72
 GSLMinimizer1D.h:73
 GSLMinimizer1D.h:74
 GSLMinimizer1D.h:75
 GSLMinimizer1D.h:76
 GSLMinimizer1D.h:77
 GSLMinimizer1D.h:78
 GSLMinimizer1D.h:79
 GSLMinimizer1D.h:80
 GSLMinimizer1D.h:81
 GSLMinimizer1D.h:82
 GSLMinimizer1D.h:83
 GSLMinimizer1D.h:84
 GSLMinimizer1D.h:85
 GSLMinimizer1D.h:86
 GSLMinimizer1D.h:87
 GSLMinimizer1D.h:88
 GSLMinimizer1D.h:89
 GSLMinimizer1D.h:90
 GSLMinimizer1D.h:91
 GSLMinimizer1D.h:92
 GSLMinimizer1D.h:93
 GSLMinimizer1D.h:94
 GSLMinimizer1D.h:95
 GSLMinimizer1D.h:96
 GSLMinimizer1D.h:97
 GSLMinimizer1D.h:98
 GSLMinimizer1D.h:99
 GSLMinimizer1D.h:100
 GSLMinimizer1D.h:101
 GSLMinimizer1D.h:102
 GSLMinimizer1D.h:103
 GSLMinimizer1D.h:104
 GSLMinimizer1D.h:105
 GSLMinimizer1D.h:106
 GSLMinimizer1D.h:107
 GSLMinimizer1D.h:108
 GSLMinimizer1D.h:109
 GSLMinimizer1D.h:110
 GSLMinimizer1D.h:111
 GSLMinimizer1D.h:112
 GSLMinimizer1D.h:113
 GSLMinimizer1D.h:114
 GSLMinimizer1D.h:115
 GSLMinimizer1D.h:116
 GSLMinimizer1D.h:117
 GSLMinimizer1D.h:118
 GSLMinimizer1D.h:119
 GSLMinimizer1D.h:120
 GSLMinimizer1D.h:121
 GSLMinimizer1D.h:122
 GSLMinimizer1D.h:123
 GSLMinimizer1D.h:124
 GSLMinimizer1D.h:125
 GSLMinimizer1D.h:126
 GSLMinimizer1D.h:127
 GSLMinimizer1D.h:128
 GSLMinimizer1D.h:129
 GSLMinimizer1D.h:130
 GSLMinimizer1D.h:131
 GSLMinimizer1D.h:132
 GSLMinimizer1D.h:133
 GSLMinimizer1D.h:134
 GSLMinimizer1D.h:135
 GSLMinimizer1D.h:136
 GSLMinimizer1D.h:137
 GSLMinimizer1D.h:138
 GSLMinimizer1D.h:139
 GSLMinimizer1D.h:140
 GSLMinimizer1D.h:141
 GSLMinimizer1D.h:142
 GSLMinimizer1D.h:143
 GSLMinimizer1D.h:144
 GSLMinimizer1D.h:145
 GSLMinimizer1D.h:146
 GSLMinimizer1D.h:147
 GSLMinimizer1D.h:148
 GSLMinimizer1D.h:149
 GSLMinimizer1D.h:150
 GSLMinimizer1D.h:151
 GSLMinimizer1D.h:152
 GSLMinimizer1D.h:153
 GSLMinimizer1D.h:154
 GSLMinimizer1D.h:155
 GSLMinimizer1D.h:156
 GSLMinimizer1D.h:157
 GSLMinimizer1D.h:158
 GSLMinimizer1D.h:159
 GSLMinimizer1D.h:160
 GSLMinimizer1D.h:161
 GSLMinimizer1D.h:162
 GSLMinimizer1D.h:163
 GSLMinimizer1D.h:164
 GSLMinimizer1D.h:165
 GSLMinimizer1D.h:166
 GSLMinimizer1D.h:167
 GSLMinimizer1D.h:168
 GSLMinimizer1D.h:169
 GSLMinimizer1D.h:170
 GSLMinimizer1D.h:171
 GSLMinimizer1D.h:172
 GSLMinimizer1D.h:173
 GSLMinimizer1D.h:174
 GSLMinimizer1D.h:175
 GSLMinimizer1D.h:176
 GSLMinimizer1D.h:177
 GSLMinimizer1D.h:178
 GSLMinimizer1D.h:179
 GSLMinimizer1D.h:180
 GSLMinimizer1D.h:181
 GSLMinimizer1D.h:182
 GSLMinimizer1D.h:183
 GSLMinimizer1D.h:184
 GSLMinimizer1D.h:185
 GSLMinimizer1D.h:186
 GSLMinimizer1D.h:187
 GSLMinimizer1D.h:188
 GSLMinimizer1D.h:189
 GSLMinimizer1D.h:190
 GSLMinimizer1D.h:191
 GSLMinimizer1D.h:192
 GSLMinimizer1D.h:193
 GSLMinimizer1D.h:194
 GSLMinimizer1D.h:195
 GSLMinimizer1D.h:196
 GSLMinimizer1D.h:197
 GSLMinimizer1D.h:198
 GSLMinimizer1D.h:199
 GSLMinimizer1D.h:200
 GSLMinimizer1D.h:201
 GSLMinimizer1D.h:202
 GSLMinimizer1D.h:203
 GSLMinimizer1D.h:204
 GSLMinimizer1D.h:205
 GSLMinimizer1D.h:206
 GSLMinimizer1D.h:207
 GSLMinimizer1D.h:208
 GSLMinimizer1D.h:209
 GSLMinimizer1D.h:210
 GSLMinimizer1D.h:211
 GSLMinimizer1D.h:212
 GSLMinimizer1D.h:213
 GSLMinimizer1D.h:214
 GSLMinimizer1D.h:215
 GSLMinimizer1D.h:216
 GSLMinimizer1D.h:217
 GSLMinimizer1D.h:218
 GSLMinimizer1D.h:219
 GSLMinimizer1D.h:220
 GSLMinimizer1D.h:221
 GSLMinimizer1D.h:222
 GSLMinimizer1D.h:223
 GSLMinimizer1D.h:224