File: GSLMultiMinFunctionAdapter.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 (99 lines) | stat: -rw-r--r-- 3,498 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
// @(#)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 GSLMultiMinFunctionAdapter
//
// Generic adapter for gsl_multimin_function signature
// usable for any c++ class which defines operator( )
//
// Created by: Lorenzo Moneta  at Fri Nov 12 16:58:51 2004
//
// Last update: Fri Nov 12 16:58:51 2004
//
#ifndef ROOT_Math_GSLMultiMinFunctionAdapter
#define ROOT_Math_GSLMultiMinFunctionAdapter

#include "gsl/gsl_vector.h"

namespace ROOT {
namespace Math {




  /**
     Class for adapting any multi-dimension C++ functor class to C function pointers used by
     GSL MultiMin algorithms.
     The templated C++ function class must implement:

    <em> double operator( const double *  x)</em>
    and if the derivatives are required:
    <em> void Gradient( const double *   x, double * g)</em>

    This class defines static methods with will be used to fill the
    \a gsl_multimin_function and
    \a gsl_multimin_function_fdf structs used by GSL.
    See for examples the
    <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>

   @ingroup MultiMin

  */


  template<class UserFunc>
  struct  GSLMultiMinFunctionAdapter {

    static double F( const gsl_vector * x, void * p) {

      UserFunc * function = reinterpret_cast< UserFunc *> (p);
      // get pointer to data from gsl_vector
      return (*function)( x->data );
    }


    static void Df(  const gsl_vector * x, void * p,  gsl_vector * g) {

      UserFunc * function = reinterpret_cast< UserFunc *> (p);
      (*function).Gradient( x->data, g->data );

    }

    static void Fdf( const gsl_vector * x, void * p, double *f, gsl_vector * g ) {

      UserFunc * function = reinterpret_cast< UserFunc *> (p);
//       *f  = (*function) ( x );
//       *df = (*function).Gradient( x );

      (*function).FdF( x->data, *f, g->data);
    }

  };


} // namespace Math
} // namespace ROOT


#endif /* ROOT_Math_GSLMultiMinFunctionAdapter */