ROOT  6.06/09
Reference Guide
MultiNumGradFunction.h
Go to the documentation of this file.
1 // @(#)root/mathmore:$Id$
2 // Author: L. Moneta Wed Dec 20 14:36:31 2006
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * This library is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU General Public License *
10  * as published by the Free Software Foundation; either version 2 *
11  * of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16  * General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this library (see file COPYING); if not, write *
20  * to the Free Software Foundation, Inc., 59 Temple Place, Suite *
21  * 330, Boston, MA 02111-1307 USA, or contact the author. *
22  * *
23  **********************************************************************/
24 
25 // Header file for class MultiNumGradFunction
26 
27 #ifndef ROOT_Math_MultiNumGradFunction
28 #define ROOT_Math_MultiNumGradFunction
29 
30 
31 #ifndef ROOT_Math_IFunction
32 #include "Math/IFunction.h"
33 #endif
34 
35 #ifndef ROOT_Math_WrappedFunction
36 #include "Math/WrappedFunction.h"
37 #endif
38 
39 
40 namespace ROOT {
41 
42  namespace Math {
43 
44 
45 /**
46  MultiNumGradFunction class to wrap a normal function in a
47  gradient function using numerical gradient calculation
48  provided by the class Derivator (based on GSL numerical derivation)
49 
50 
51  @ingroup MultiMin
52 */
54 
55 public:
56 
57 
58  /**
59  Constructor from a IMultiGenFunction interface
60  */
62  fFunc(&f),
63  fDim(f.NDim() ),
64  fNCalls(0),
65  fOwner(false)
66  {}
67 
68  /**
69  Constructor from a generic function (pointer or reference) and number of dimension
70  implementiong operator () (double * x)
71  */
72 
73  template<class FuncType>
74  MultiNumGradFunction (FuncType f, int n) :
75  fDim( n ),
76  fNCalls(0),
77  fOwner(true)
78  {
79  // create a wrapped function
81  }
82 
83  /**
84  Destructor (no operations)
85  */
87  if (fOwner) delete fFunc;
88  }
89 
90 
91  // method inheritaed from IFunction interface
92 
93  unsigned int NDim() const { return fDim; }
94 
95  unsigned int NCalls() const { return fNCalls; }
96 
98  if (!fOwner)
99  return new MultiNumGradFunction(*fFunc);
100  else {
101  // we need to copy the pointer to the wrapped function
103  f->fOwner = true;
104  return f;
105  }
106  }
107 
108  // set ownership
109  void SetOwnership(bool on = true) { fOwner = on; }
110 
111  /// precision value used for calculating the derivative step-size
112  /// h = eps * |x|. The default is 0.001, give a smaller in case function chanes rapidly
113  static void SetDerivPrecision(double eps);
114 
115  /// get precision value used for calculating the derivative step-size
116  static double GetDerivPrecision();
117 
118 
119 private:
120 
121 
122  double DoEval(const double * x) const {
123  fNCalls++;
124  return (*fFunc)(x);
125  }
126 
127  // calculate derivative using mathcore derivator
128  double DoDerivative (const double * x, unsigned int icoord ) const;
129 
130  // adapat internal function type to IMultiGenFunction needed by derivative calculation
132  unsigned int fDim;
133  mutable unsigned int fNCalls;
134  bool fOwner;
135 
136  static double fgEps; // epsilon used in derivative calculation h ~ eps |x|
137 
138 };
139 
140  } // end namespace Math
141 
142 } // end namespace ROOT
143 
144 
145 #endif /* ROOT_Math_NumGradFunction */
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
Definition: IFunction.h:322
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
~MultiNumGradFunction()
Destructor (no operations)
unsigned int NDim() const
Retrieve the dimension of the function.
ClassImp(TIterator) Bool_t TIterator return false
Compare two iterator objects.
Definition: TIterator.cxx:20
Double_t x[n]
Definition: legend1.C:17
MultiNumGradFunction(FuncType f, int n)
Constructor from a generic function (pointer or reference) and number of dimension implementiong oper...
static void SetDerivPrecision(double eps)
precision value used for calculating the derivative step-size h = eps * |x|.
double DoDerivative(const double *x, unsigned int icoord) const
function to evaluate the derivative with respect each coordinate.
double DoEval(const double *x) const
Implementation of the evaluation function.
MultiNumGradFunction(const IMultiGenFunction &f)
Constructor from a IMultiGenFunction interface.
static double GetDerivPrecision()
get precision value used for calculating the derivative step-size
IMultiGenFunction * Clone() const
Clone a function.
MultiNumGradFunction class to wrap a normal function in a gradient function using numerical gradient ...
double f(double x)
Template class to wrap any C++ callable object implementing operator() (const double * x) in a multi-...
Namespace for new Math classes and functions.
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:63
const Int_t n
Definition: legend1.C:16
virtual IBaseFunctionMultiDim * Clone() const =0
Clone a function.