Logo ROOT   6.13/01
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 #include "Math/IFunction.h"
32 
33 #include "Math/WrappedFunction.h"
34 
35 
36 namespace ROOT {
37 
38  namespace Math {
39 
40 
41 /**
42  MultiNumGradFunction class to wrap a normal function in a
43  gradient function using numerical gradient calculation
44  provided by the class Derivator (based on GSL numerical derivation)
45 
46 
47  @ingroup MultiMin
48 */
50 
51 public:
52 
53 
54  /**
55  Constructor from a IMultiGenFunction interface
56  */
58  fFunc(&f),
59  fDim(f.NDim() ),
60  fNCalls(0),
61  fOwner(false)
62  {}
63 
64  /**
65  Constructor from a generic function (pointer or reference) and number of dimension
66  implementiong operator () (double * x)
67  */
68 
69  template<class FuncType>
70  MultiNumGradFunction (FuncType f, int n) :
71  fDim( n ),
72  fNCalls(0),
73  fOwner(true)
74  {
75  // create a wrapped function
77  }
78 
79  /**
80  Destructor (no operations)
81  */
83  if (fOwner) delete fFunc;
84  }
85 
86 
87  // method inheritaed from IFunction interface
88 
89  unsigned int NDim() const { return fDim; }
90 
91  unsigned int NCalls() const { return fNCalls; }
92 
94  if (!fOwner)
95  return new MultiNumGradFunction(*fFunc);
96  else {
97  // we need to copy the pointer to the wrapped function
99  f->fOwner = true;
100  return f;
101  }
102  }
103 
104  // set ownership
105  void SetOwnership(bool on = true) { fOwner = on; }
106 
107  /// precision value used for calculating the derivative step-size
108  /// h = eps * |x|. The default is 0.001, give a smaller in case function chanes rapidly
109  static void SetDerivPrecision(double eps);
110 
111  /// get precision value used for calculating the derivative step-size
112  static double GetDerivPrecision();
113 
114 
115 private:
116 
117 
118  double DoEval(const double * x) const {
119  fNCalls++;
120  return (*fFunc)(x);
121  }
122 
123  // calculate derivative using mathcore derivator
124  double DoDerivative (const double * x, unsigned int icoord ) const;
125 
126  // adapat internal function type to IMultiGenFunction needed by derivative calculation
128  unsigned int fDim;
129  mutable unsigned int fNCalls;
130  bool fOwner;
131 
132  static double fgEps; // epsilon used in derivative calculation h ~ eps |x|
133 
134 };
135 
136  } // end namespace Math
137 
138 } // end namespace ROOT
139 
140 
141 #endif /* ROOT_Math_NumGradFunction */
IMultiGenFunction * Clone() const
Clone a function.
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
Definition: IFunction.h:326
Namespace for new ROOT classes and functions.
Definition: TFoamSampler.h:19
~MultiNumGradFunction()
Destructor (no operations)
double DoDerivative(const double *x, unsigned int icoord) const
virtual IBaseFunctionMultiDimTempl< T > * Clone() const =0
Clone a function.
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|.
MultiNumGradFunction(const IMultiGenFunction &f)
Constructor from a IMultiGenFunction interface.
static double GetDerivPrecision()
get precision value used for calculating the derivative step-size
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:62
* x
Deprecated and error prone model selection interface.
Definition: TRolke.cxx:630
MultiNumGradFunction class to wrap a normal function in a gradient function using numerical gradient ...
Template class to wrap any C++ callable object implementing operator() (const double * x) in a multi-...
Namespace for new Math classes and functions.
unsigned int NDim() const
Retrieve the dimension of the function.
double DoEval(const double *x) const