File: GSLMultiMinFunctionWrapper.h

package info (click to toggle)
bornagain 23.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,956 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 356; ruby: 173; xml: 130; makefile: 45; ansic: 24
file content (163 lines) | stat: -rw-r--r-- 4,770 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// @(#)root/mathmore:$Id$
// Authors: L. Moneta, 12/2006

 /**********************************************************************
  *                                                                    *
  * Copyright (c) 2004 ROOT Foundation,  CERN/PH-SFT                   *
  *                                                                    *
  * This library is free software; you can redistribute it and/or      *
  * modify it under the terms of the GNU General Public License        *
  * as published by the Free Software Foundation; either version 2     *
  * of the License, or (at your option) any later version.             *
  *                                                                    *
  * This library is distributed in the hope that it will be useful,    *
  * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   *
  * General Public License for more details.                           *
  *                                                                    *
  * You should have received a copy of the GNU General Public License  *
  * along with this library (see file COPYING); if not, write          *
  * to the Free Software Foundation, Inc., 59 Temple Place, Suite      *
  * 330, Boston, MA 02111-1307 USA, or contact the author.             *
  *                                                                    *
  **********************************************************************/

// Header file for class GSLMultiMinFunctionWrapper
//
// Created by: moneta  at Sat Nov 13 14:54:41 2004
//
// Last update: Sat Nov 13 14:54:41 2004
//
#ifndef ROOT_Math_GSLMultiMinFunctionWrapper
#define ROOT_Math_GSLMultiMinFunctionWrapper

#include "gsl/gsl_multimin.h"

#include "GSLMultiMinFunctionAdapter.h"


#include <cassert>

namespace ROOT {
namespace Math {



   typedef double ( * GSLMultiMinFuncPointer ) ( const gsl_vector *, void *);
   typedef void   ( * GSLMultiMinDfPointer )   ( const gsl_vector *, void *, gsl_vector *);
   typedef void   ( * GSLMultiMinFdfPointer ) ( const gsl_vector *, void *, double *, gsl_vector *);


/**
   wrapper to a multi-dim function withtout  derivatives for multi-dimensional
   minimization algorithm

   @ingroup MultiMin
*/

class GSLMultiMinFunctionWrapper {

public:

   GSLMultiMinFunctionWrapper()
   {
      fFunc.f = 0;
      fFunc.n = 0;
      fFunc.params = 0;
   }

   void SetFuncPointer( GSLMultiMinFuncPointer f) { fFunc.f = f; }
   void SetDim  ( unsigned int n ) { fFunc.n = n; }
   void SetParams ( void * p) { fFunc.params = p; }

   /// Fill gsl function structure from a C++ Function class
   template<class FuncType>
   void SetFunction(const FuncType &f) {
      const void * p = &f;
      assert (p != 0);
      SetFuncPointer(&GSLMultiMinFunctionAdapter<FuncType >::F);
      SetDim( f.NDim() );
      SetParams(const_cast<void *>(p));
   }

   gsl_multimin_function * GetFunc() { return &fFunc; }

    bool IsValid() {
       return (fFunc.f != 0) ? true : false;
    }


  private:

    gsl_multimin_function fFunc;

  };


/**
   Wrapper for a multi-dimensional function with derivatives used in GSL multidim
   minimization algorithm

   @ingroup MultiMin

 */
 class GSLMultiMinDerivFunctionWrapper {

 public:

    GSLMultiMinDerivFunctionWrapper()
    {
       fFunc.f = 0;
       fFunc.df = 0;
       fFunc.fdf = 0;
       fFunc.n = 0;
       fFunc.params = 0;
    }


    void SetFuncPointer( GSLMultiMinFuncPointer f) { fFunc.f = f; }
    void SetDerivPointer( GSLMultiMinDfPointer f) { fFunc.df = f; }
    void SetFdfPointer( GSLMultiMinFdfPointer f) { fFunc.fdf = f; }
    void SetDim  ( unsigned int n ) { fFunc.n = n; }
    void SetParams ( void * p) { fFunc.params = p; }

    /// Fill gsl function structure from a C++ Function class
    template<class FuncType>
    void SetFunction(const FuncType &f) {
       const void * p = &f;
       assert (p != 0);
       SetFuncPointer(&GSLMultiMinFunctionAdapter<FuncType >::F);
       SetDerivPointer(&GSLMultiMinFunctionAdapter<FuncType >::Df);
       SetFdfPointer(&GSLMultiMinFunctionAdapter<FuncType >::Fdf);
       SetDim( f.NDim() );
       SetParams(const_cast<void *>(p));
     }

    gsl_multimin_function_fdf * GetFunc() { return &fFunc; }

#ifdef NEEDED_LATER
    // evaluate the function
    double operator() (const double * x) {
       // vx must be a gsl_vector
       return GSL_MULTIMIN_FN_EVAL(&fFunc, vx);
    }
#endif

   /// check if function is valid (has been set)
    bool IsValid() {
       return (fFunc.f != 0) ? true : false;
    }

 private:

    gsl_multimin_function_fdf fFunc;

  };




} // namespace Math
} // namespace ROOT

#endif /* ROOT_Math_GSLMultiMinFunctionWrapper */