GslMultiminFunctionFdfAdapter.hpp
Go to the documentation of this file.
1 #ifndef JULIAN_GSLMULTIMINFUNCTIONFDFADAPTER_HPP
2 #define JULIAN_GSLMULTIMINFUNCTIONFDFADAPTER_HPP
3 
4 #include <gsl/gsl_multimin.h>
5 #include <vector>
6 #include <utility>
7 
8 namespace julian {
9 
26  template< typename F, typename dF, typename FdF >
27  class GslMultiminFunctionFdfAdapter : public gsl_multimin_function_fdf {
28  public:
29 
38  const dF& func2,
39  const FdF& func3,
40  unsigned int n_) : func1_(func1), func2_(func2), func3_(func3) {
44  n = n_;
45  params = this;
46 
47  }
48  private:
49  const F& func1_;
50  const dF& func2_;
51  const FdF& func3_;
55  static double call_f(const gsl_vector* x, void *params) {
56  std::vector<double> x_std;
57 
58  for(unsigned int i = 0; i < static_cast<GslMultiminFunctionFdfAdapter*>(params)->n; ++i) {
59  x_std.push_back( gsl_vector_get(x,i));
60  }
61  return static_cast<GslMultiminFunctionFdfAdapter*>(params)->func1_(x_std);
62  }
63 
66  static void call_df(const gsl_vector* x, void *params ,gsl_vector* grad) {
67  std::vector<double> x_std;
68 
69  for(unsigned int i = 0; i < static_cast<GslMultiminFunctionFdfAdapter*>(params)->n; ++i) {
70  x_std.push_back( gsl_vector_get(x,i));
71  }
72 
73  auto grad_ = static_cast<GslMultiminFunctionFdfAdapter*>(params)->func2_(x_std);
74 
75  for (unsigned int i = 0 ; i < static_cast<GslMultiminFunctionFdfAdapter*>(params)->n; ++i) {
76  gsl_vector_set(grad, i, grad_.at(i));
77  }
78  }
79 
82  static void call_fdf(const gsl_vector* x, void *params, double *f, gsl_vector *grad) {
83  std::vector<double> x_std;
84 
85  for(unsigned int i = 0; i < static_cast<GslMultiminFunctionFdfAdapter*>(params)->n; ++i) {
86  x_std.push_back( gsl_vector_get(x,i));
87  }
88  std::pair<double, std::vector<double> > res = static_cast<GslMultiminFunctionFdfAdapter*>(params)->func3_(x_std);
89  *f = res.first;
90 
91  for (unsigned int i = 0 ; i < static_cast<GslMultiminFunctionFdfAdapter*>(params)->n; ++i) {
92  gsl_vector_set(grad, i, res.second.at(i));
93  }
94  }
95  };
96 
97 } // namespace julian
98 
99 #endif
Class implements adapter for gsl_multimin_function.
Definition: GslMultiminFunctionFdfAdapter.hpp:27
const dF & func2_
dF must be a functor calculating derivative of the function.
Definition: GslMultiminFunctionFdfAdapter.hpp:50
Definition: cadHoliday.cpp:3
static void call_fdf(const gsl_vector *x, void *params, double *f, gsl_vector *grad)
Method called by the GSL algorithm.
Definition: GslMultiminFunctionFdfAdapter.hpp:82
static double call_f(const gsl_vector *x, void *params)
Method called by the GSL algorithm.
Definition: GslMultiminFunctionFdfAdapter.hpp:55
GslMultiminFunctionFdfAdapter(const F &func1, const dF &func2, const FdF &func3, unsigned int n_)
Constructor.
Definition: GslMultiminFunctionFdfAdapter.hpp:37
const F & func1_
F must be a functor calculating the value of the function.
Definition: GslMultiminFunctionFdfAdapter.hpp:49
static void call_df(const gsl_vector *x, void *params, gsl_vector *grad)
Method called by the GSL algorithm.
Definition: GslMultiminFunctionFdfAdapter.hpp:66
const FdF & func3_
FdF must be a functor calculating value and derivative of the function.
Definition: GslMultiminFunctionFdfAdapter.hpp:51