Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
FCNGradAdapter.h
Go to the documentation of this file.
1// @(#)root/minuit2:$Id$
2// Author: L. Moneta 10/2006
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2006 ROOT Foundation, CERN/PH-SFT *
7 * *
8 **********************************************************************/
9
10#ifndef ROOT_Minuit2_FCNGradAdapter
11#define ROOT_Minuit2_FCNGradAdapter
12
14#include "Minuit2/MnPrint.h"
15
16#include <vector>
17#include <functional>
18
19namespace ROOT {
20
21namespace Minuit2 {
22
23/**
24
25
26template wrapped class for adapting to FCNBase signature a IGradFunction
27
28@author Lorenzo Moneta
29
30@ingroup Minuit
31
32*/
33
34template <class Function>
36
37public:
38 FCNGradAdapter(const Function &f, double up = 1.) : fFunc(f), fUp(up), fGrad(std::vector<double>(fFunc.NDim())) {}
39
40 ~FCNGradAdapter() override {}
41
42 double operator()(const std::vector<double> &v) const override { return fFunc.operator()(&v[0]); }
43 double operator()(const double *v) const { return fFunc.operator()(v); }
44
45 double Up() const override { return fUp; }
46
47 std::vector<double> Gradient(const std::vector<double> &v) const override
48 {
49 fFunc.Gradient(&v[0], &fGrad[0]);
50 return fGrad;
51 }
52 std::vector<double> GradientWithPrevResult(const std::vector<double> &v, double *previous_grad, double *previous_g2,
53 double *previous_gstep) const override
54 {
55 fFunc.GradientWithPrevResult(&v[0], &fGrad[0], previous_grad, previous_g2, previous_gstep);
56 return fGrad;
57 }
58 // forward interface
59 // virtual double operator()(int npar, double* params,int iflag = 4) const;
60 bool CheckGradient() const override { return false; }
61
63 if (fFunc.returnsInMinuit2ParameterSpace()) {
65 } else {
67 }
68 }
69
70 /// return second derivatives (diagonal of the Hessian matrix)
71 std::vector<double> G2(const std::vector<double> & x) const override {
72 if (fG2Func)
73 return fG2Func(x);
74 if (fHessianFunc) {
75 unsigned int n = fFunc.NDim();
76 if (fG2Vec.empty() ) fG2Vec.resize(n);
77 if (fHessian.empty() ) fHessian.resize(n*n);
78 fHessianFunc(x,fHessian.data());
79 if (!fHessian.empty()) {
80 // get diagonal element of h
81 for (unsigned int i = 0; i < n; i++)
82 fG2Vec[i] = fHessian[i*n+i];
83 }
84 else fG2Vec.clear();
85 }
86 else
87 if (!fG2Vec.empty()) fG2Vec.clear();
88 return fG2Vec;
89 }
90
91 /// compute Hessian. Return Hessian as a std::vector of size(n*n)
92 std::vector<double> Hessian(const std::vector<double> & x ) const override {
93 unsigned int n = fFunc.NDim();
94 if (fHessianFunc) {
95 if (fHessian.empty() ) fHessian.resize(n * n);
96 bool ret = fHessianFunc(x,fHessian.data());
97 if (!ret) {
98 fHessian.clear();
99 fHessianFunc = nullptr;
100 }
101 } else {
102 fHessian.clear();
103 }
104
105 return fHessian;
106 }
107
108 bool HasG2() const override {
109 return bool(fG2Func);
110 }
111 bool HasHessian() const override {
112 return bool(fHessianFunc);
113 }
114
115 template<class Func>
116 void SetG2Function(Func f) { fG2Func = f;}
117
118 template<class Func>
120
121 void SetErrorDef(double up) override { fUp = up; }
122
123private:
125 double fUp;
126 mutable std::vector<double> fGrad;
127 mutable std::vector<double> fHessian;
128 mutable std::vector<double> fG2Vec;
129
130 std::function<std::vector<double>(const std::vector<double> &)> fG2Func;
131 mutable std::function<bool(const std::vector<double> &, double *)> fHessianFunc;
132};
133
134} // end namespace Minuit2
135
136} // end namespace ROOT
137
138#endif // ROOT_Minuit2_FCNGradAdapter
#define f(i)
Definition RSha256.hxx:104
Double_t(* Function)(Double_t)
Definition Functor.C:4
template wrapped class for adapting to FCNBase signature a IGradFunction
double Up() const override
Error definition of the function.
FCNGradAdapter(const Function &f, double up=1.)
GradientParameterSpace gradParameterSpace() const override
double operator()(const double *v) const
std::function< std::vector< double >(const std::vector< double > &)> fG2Func
std::vector< double > Gradient(const std::vector< double > &v) const override
std::vector< double > Hessian(const std::vector< double > &x) const override
compute Hessian. Return Hessian as a std::vector of size(n*n)
std::vector< double > fG2Vec
bool CheckGradient() const override
std::function< bool(const std::vector< double > &, double *)> fHessianFunc
std::vector< double > GradientWithPrevResult(const std::vector< double > &v, double *previous_grad, double *previous_g2, double *previous_gstep) const override
bool HasG2() const override
double operator()(const std::vector< double > &v) const override
The meaning of the vector of parameters is of course defined by the user, who uses the values of thos...
void SetErrorDef(double up) override
add interface to set dynamically a new error definition Re-implement this function if needed.
bool HasHessian() const override
std::vector< double > G2(const std::vector< double > &x) const override
return second derivatives (diagonal of the Hessian matrix)
std::vector< double > fHessian
std::vector< double > fGrad
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
GradientParameterSpace
Extension of the FCNBase for providing the analytical Gradient of the function.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...