Logo ROOT  
Reference Guide
GSLMultiRootFunctionAdapter.h
Go to the documentation of this file.
1// @(#)root/mathmore:$Id$
2// Authors: L. Moneta, Mar 2011
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_multiroot_function signature type
28// usable for any array of function pointers
29// implementing operator()(const double *x) and (if needed)
30// Gradient(const double *x, double * g)
31//
32// The class is very similar to GSLMultiFitFunctionAdapter,
33// but in that case the array is for function references (or value)
34//
35#ifndef ROOT_Math_GSLMultiRootFunctionAdapter
36#define ROOT_Math_GSLMultiRootFunctionAdapter
37
38#include "gsl/gsl_vector.h"
39#include "gsl/gsl_matrix.h"
40
41#include <cassert>
42
43namespace ROOT {
44namespace Math {
45
46
47
48 /**
49 Class for adapting a C++ functor class to C function pointers used by GSL MultiRoot
50 Algorithm
51 The templated C++ function class must implement:
52
53 <em> double operator( const double * x)</em>
54 and if the derivatives are required:
55 <em> void Gradient( const double * x, double * g)</em>
56 and
57 <em> void FdF( const double * x, double &f, double * g)</em>
58
59
60 @ingroup MultiRoot
61 */
62
63
64 // FuncVector must contain a vector of pointers to functions
65 // this same as MultiFit but here need to use pointers where there we used class elements
66
67template<class FuncVector>
69
70
71
72public:
73
74 static int F( const gsl_vector * x, void * p, gsl_vector * f ) {
75 // p is a pointer to an iterator of functions
76 unsigned int n = f->size;
77 // need to copy iterator otherwise next time the function is called it wont work
78 FuncVector & funcVec = *( reinterpret_cast< FuncVector *> (p) );
79 if (n == 0) return -1;
80 for (unsigned int i = 0; i < n ; ++i) {
81 gsl_vector_set(f, i, (*funcVec[i])(x->data) );
82 }
83 return 0;
84 }
85
86
87 static int Df( const gsl_vector * x, void * p, gsl_matrix * h) {
88
89 // p is a pointer to an iterator of functions
90 unsigned int n = h->size1;
91 unsigned int npar = h->size2;
92 if (n == 0) return -1;
93 if (npar == 0) return -2;
94 FuncVector & funcVec = *( reinterpret_cast< FuncVector *> (p) );
95 for (unsigned int i = 0; i < n ; ++i) {
96 double * g = (h->data)+i*npar; //pointer to start of i-th row
97 assert ( npar == (funcVec[i])->NDim() );
98 (funcVec[i])->Gradient(x->data, g);
99 }
100 return 0;
101 }
102
103 /// evaluate derivative and function at the same time
104 static int FDf( const gsl_vector * x, void * p, gsl_vector * f, gsl_matrix * h) {
105 // should be implemented in the function
106 // p is a pointer to an iterator of functions
107 unsigned int n = h->size1;
108 unsigned int npar = h->size2;
109 if (n == 0) return -1;
110 if (npar == 0) return -2;
111 FuncVector & funcVec = *( reinterpret_cast< FuncVector *> (p) );
112 assert ( f->size == n);
113 for (unsigned int i = 0; i < n ; ++i) {
114 assert ( npar == (funcVec[i])->NDim() );
115 double fval = 0;
116 double * g = (h->data)+i*npar; //pointer to start of i-th row
117 (funcVec[i])->FdF(x->data, fval, g);
118 gsl_vector_set(f, i, fval );
119 }
120 return 0;
121 }
122
123};
124
125
126} // namespace Math
127} // namespace ROOT
128
129
130#endif /* ROOT_Math_GSLMultiRootFunctionAdapter */
#define f(i)
Definition: RSha256.hxx:104
#define g(i)
Definition: RSha256.hxx:105
#define h(i)
Definition: RSha256.hxx:106
Class for adapting a C++ functor class to C function pointers used by GSL MultiRoot Algorithm The tem...
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 F(const gsl_vector *x, void *p, gsl_vector *f)
static int Df(const gsl_vector *x, void *p, gsl_matrix *h)
Double_t x[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
Namespace for new Math classes and functions.
VSD Structures.
Definition: StringConv.hxx:21