Logo ROOT   6.13/01
Reference Guide
GSLMultiFitFunctionAdapter.h
Go to the documentation of this file.
1 // @(#)root/mathmore:$Id$
2 // Authors: L. Moneta, Dec 2006
3 
4  /**********************************************************************
5  * *
6  * Copyright (c) 2004 ROOT Foundation, 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 GSLMultiMinFunctionAdapter
26 //
27 // Generic adapter for gsl_multimin_function signature
28 // usable for any c++ class which defines operator( )
29 //
30 // Created by: Lorenzo Moneta at Fri Nov 12 16:58:51 2004
31 //
32 // Last update: Fri Nov 12 16:58:51 2004
33 //
34 #ifndef ROOT_Math_GSLMultiFitFunctionAdapter
35 #define ROOT_Math_GSLMultiFitFunctionAdapter
36 
37 #include "gsl/gsl_vector.h"
38 #include "gsl/gsl_matrix.h"
39 
40 #include <cassert>
41 
42 namespace ROOT {
43 namespace Math {
44 
45 
46 
47  /**
48  Class for adapting a C++ functor class to C function pointers used by GSL MultiFit
49  Algorithm
50  The templated C++ function class must implement:
51 
52  <em> double operator( const double * x)</em>
53  and if the derivatives are required:
54  <em> void Gradient( const double * x, double * g)</em>
55  and
56  <em> void FdF( const double * x, double &f, double * g)</em>
57 
58  This class defines static methods with will be used to fill the
59  \a gsl_multimin_function and
60  \a gsl_multimin_function_fdf structs used by GSL.
61  See for examples the
62  <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Providing-a-function-to-minimize.html#Providing-a-function-to-minimize">GSL online manual</A>
63 
64  @ingroup MultiMin
65  */
66 
67 
68 template<class FuncVector>
70 
71 public:
72 
73  static int F( const gsl_vector * x, void * p, gsl_vector * f ) {
74  // p is a pointer to an iterator of functions
75  unsigned int n = f->size;
76  // need to copy iterator otherwise next time the function is called it wont work
77  FuncVector & funcVec = *( reinterpret_cast< FuncVector *> (p) );
78  if (n == 0) return -1;
79  for (unsigned int i = 0; i < n ; ++i) {
80  gsl_vector_set(f, i, (funcVec[i])(x->data) );
81  }
82  return 0;
83  }
84 
85 
86  static int Df( const gsl_vector * x, void * p, gsl_matrix * h) {
87 
88  // p is a pointer to an iterator of functions
89  unsigned int n = h->size1;
90  unsigned int npar = h->size2;
91  if (n == 0) return -1;
92  if (npar == 0) return -2;
93  FuncVector & funcVec = *( reinterpret_cast< FuncVector *> (p) );
94  for (unsigned int i = 0; i < n ; ++i) {
95  double * g = (h->data)+i*npar; //pointer to start of i-th row
96  assert ( npar == (funcVec[i]).NDim() );
97  (funcVec[i]).Gradient(x->data, g);
98  }
99  return 0;
100  }
101 
102  /// evaluate derivative and function at the same time
103  static int FDf( const gsl_vector * x, void * p, gsl_vector * f, gsl_matrix * h) {
104  // should be implemented in the function
105  // p is a pointer to an iterator of functions
106  unsigned int n = h->size1;
107  unsigned int npar = h->size2;
108  if (n == 0) return -1;
109  if (npar == 0) return -2;
110  FuncVector & funcVec = *( reinterpret_cast< FuncVector *> (p) );
111  assert ( f->size == n);
112  for (unsigned int i = 0; i < n ; ++i) {
113  assert ( npar == (funcVec[i]).NDim() );
114  double fval = 0;
115  double * g = (h->data)+i*npar; //pointer to start of i-th row
116  (funcVec[i]).FdF(x->data, fval, g);
117  gsl_vector_set(f, i, fval );
118  }
119  return 0;
120  }
121 
122 };
123 
124 
125 } // namespace Math
126 } // namespace ROOT
127 
128 
129 #endif /* ROOT_Math_GSLMultiMinFunctionAdapter */
Namespace for new ROOT classes and functions.
Definition: TFoamSampler.h:19
static int F(const gsl_vector *x, void *p, gsl_vector *f)
static int FDf(const gsl_vector *x, void *p, gsl_vector *f, gsl_matrix *h)
evaluate derivative and function at the same time
static int Df(const gsl_vector *x, void *p, gsl_matrix *h)
* x
Deprecated and error prone model selection interface.
Definition: TRolke.cxx:630
Class for adapting a C++ functor class to C function pointers used by GSL MultiFit Algorithm The temp...
Namespace for new Math classes and functions.