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
13#include "Minuit2/FCNBase.h"
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>
35class FCNGradAdapter : public FCNBase {
36
37public:
38 FCNGradAdapter(const Function &f, double up = 1.) : fFunc(f), fUp(up), fGrad(std::vector<double>(fFunc.NDim())) {}
39
40 bool HasGradient() const override { return true; }
41
42 double operator()(std::vector<double> const& 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(std::vector<double> const& v) const override
48 {
49 fFunc.Gradient(&v[0], &fGrad[0]);
50 return fGrad;
51 }
52 std::vector<double> GradientWithPrevResult(std::vector<double> const& 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
60 if (fFunc.returnsInMinuit2ParameterSpace()) {
62 } else {
64 }
65 }
66
67 /// return second derivatives (diagonal of the Hessian matrix)
68 std::vector<double> G2(std::vector<double> const& x) const override {
69 if (fG2Func)
70 return fG2Func(x);
71 if (fHessianFunc) {
72 unsigned int n = fFunc.NDim();
73 if (fG2Vec.empty() ) fG2Vec.resize(n);
74 if (fHessian.empty() ) fHessian.resize(n*n);
75 fHessianFunc(x,fHessian.data());
76 if (!fHessian.empty()) {
77 // get diagonal element of h
78 for (unsigned int i = 0; i < n; i++)
79 fG2Vec[i] = fHessian[i*n+i];
80 }
81 else fG2Vec.clear();
82 }
83 else
84 if (!fG2Vec.empty()) fG2Vec.clear();
85 return fG2Vec;
86 }
87
88 /// compute Hessian. Return Hessian as a std::vector of size(n*n)
89 std::vector<double> Hessian(std::vector<double> const& x ) const override {
90 unsigned int n = fFunc.NDim();
91 if (fHessianFunc) {
92 if (fHessian.empty() ) fHessian.resize(n * n);
93 bool ret = fHessianFunc(x,fHessian.data());
94 if (!ret) {
95 fHessian.clear();
96 fHessianFunc = nullptr;
97 }
98 } else {
99 fHessian.clear();
100 }
101
102 return fHessian;
103 }
104
105 bool HasG2() const override {
106 return bool(fG2Func);
107 }
108 bool HasHessian() const override {
109 return bool(fHessianFunc);
110 }
111
112 template<class Func>
113 void SetG2Function(Func f) { fG2Func = f;}
114
115 template<class Func>
117
118 void SetErrorDef(double up) override { fUp = up; }
119
120private:
122 double fUp;
123 mutable std::vector<double> fGrad;
124 mutable std::vector<double> fHessian;
125 mutable std::vector<double> fG2Vec;
126
127 std::function<std::vector<double>(std::vector<double> const& )> fG2Func;
128 mutable std::function<bool(std::vector<double> const& , double *)> fHessianFunc;
129};
130
131} // end namespace Minuit2
132
133} // end namespace ROOT
134
135#endif // ROOT_Minuit2_FCNGradAdapter
#define f(i)
Definition RSha256.hxx:104
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Double_t(* Function)(Double_t)
Definition Functor.C:4
Interface (abstract class) defining the function to be minimized, which has to be implemented by the ...
Definition FCNBase.h:51
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.)
std::vector< double > G2(std::vector< double > const &x) const override
return second derivatives (diagonal of the Hessian matrix)
GradientParameterSpace gradParameterSpace() const override
double operator()(const double *v) const
std::vector< double > Gradient(std::vector< double > const &v) const override
std::vector< double > fG2Vec
double operator()(std::vector< double > const &v) const override
The meaning of the vector of parameters is of course defined by the user, who uses the values of thos...
bool HasG2() const override
std::function< std::vector< double >(std::vector< double > const &) fG2Func)
std::vector< double > GradientWithPrevResult(std::vector< double > const &v, double *previous_grad, double *previous_g2, double *previous_gstep) const override
std::function< bool(std::vector< double > const &, double *) fHessianFunc)
std::vector< double > Hessian(std::vector< double > const &x) const override
compute Hessian. Return Hessian as a std::vector of size(n*n)
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 > fHessian
bool HasGradient() const override
std::vector< double > fGrad
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
GradientParameterSpace
Definition FCNBase.h:35
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...