ROOT  6.06/09
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 #include <iostream>
43 
44 namespace ROOT {
45 namespace Math {
46 
47 
48 
49  /**
50  Class for adapting a C++ functor class to C function pointers used by GSL MultiFit
51  Algorithm
52  The templated C++ function class must implement:
53 
54  <em> double operator( const double * x)</em>
55  and if the derivatives are required:
56  <em> void Gradient( const double * x, double * g)</em>
57  and
58  <em> void FdF( const double * x, double &f, double * g)</em>
59 
60  This class defines static methods with will be used to fill the
61  \a gsl_multimin_function and
62  \a gsl_multimin_function_fdf structs used by GSL.
63  See for examples the
64  <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>
65 
66  @ingroup MultiMin
67  */
68 
69 
70 template<class FuncVector>
72 
73 public:
74 
75  static int F( const gsl_vector * x, void * p, gsl_vector * f ) {
76  // p is a pointer to an iterator of functions
77  unsigned int n = f->size;
78  // need to copy iterator otherwise next time the function is called it wont work
79  FuncVector & funcVec = *( reinterpret_cast< FuncVector *> (p) );
80  if (n == 0) return -1;
81  for (unsigned int i = 0; i < n ; ++i) {
82  gsl_vector_set(f, i, (funcVec[i])(x->data) );
83  }
84  return 0;
85  }
86 
87 
88  static int Df( const gsl_vector * x, void * p, gsl_matrix * h) {
89 
90  // p is a pointer to an iterator of functions
91  unsigned int n = h->size1;
92  unsigned int npar = h->size2;
93  if (n == 0) return -1;
94  if (npar == 0) return -2;
95  FuncVector & funcVec = *( reinterpret_cast< FuncVector *> (p) );
96  for (unsigned int i = 0; i < n ; ++i) {
97  double * g = (h->data)+i*npar; //pointer to start of i-th row
98  assert ( npar == (funcVec[i]).NDim() );
99  (funcVec[i]).Gradient(x->data, g);
100  }
101  return 0;
102  }
103 
104  /// evaluate derivative and function at the same time
105  static int FDf( const gsl_vector * x, void * p, gsl_vector * f, gsl_matrix * h) {
106  // should be implemented in the function
107  // p is a pointer to an iterator of functions
108  unsigned int n = h->size1;
109  unsigned int npar = h->size2;
110  if (n == 0) return -1;
111  if (npar == 0) return -2;
112  FuncVector & funcVec = *( reinterpret_cast< FuncVector *> (p) );
113  assert ( f->size == n);
114  for (unsigned int i = 0; i < n ; ++i) {
115  assert ( npar == (funcVec[i]).NDim() );
116  double fval = 0;
117  double * g = (h->data)+i*npar; //pointer to start of i-th row
118  (funcVec[i]).FdF(x->data, fval, g);
119  gsl_vector_set(f, i, fval );
120  }
121  return 0;
122  }
123 
124 };
125 
126 
127 } // namespace Math
128 } // namespace ROOT
129 
130 
131 #endif /* ROOT_Math_GSLMultiMinFunctionAdapter */
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
#define assert(cond)
Definition: unittest.h:542
TH1 * h
Definition: legend2.C:5
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
Double_t x[n]
Definition: legend1.C:17
static int Df(const gsl_vector *x, void *p, gsl_matrix *h)
double f(double x)
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.
const Int_t n
Definition: legend1.C:16