Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 ~FCNGradAdapter() override {}
41
42 bool HasGradient() const override { return true; }
43
44 double operator()(std::vector<double> const& v) const override { return fFunc.operator()(&v[0]); }
45 double operator()(const double *v) const { return fFunc.operator()(v); }
46
47 double Up() const override { return fUp; }
48
49 std::vector<double> Gradient(std::vector<double> const& v) const override
50 {
51 fFunc.Gradient(&v[0], &fGrad[0]);
52 return fGrad;
53 }
54 std::vector<double> GradientWithPrevResult(std::vector<double> const& v, double *previous_grad, double *previous_g2,
55 double *previous_gstep) const override
56 {
57 fFunc.GradientWithPrevResult(&v[0], &fGrad[0], previous_grad, previous_g2, previous_gstep);
58 return fGrad;
59 }
60
62 if (fFunc.returnsInMinuit2ParameterSpace()) {
64 } else {
66 }
67 }
68
69 /// return second derivatives (diagonal of the Hessian matrix)
70 std::vector<double> G2(std::vector<double> const& x) const override {
71 if (fG2Func)
72 return fG2Func(x);
73 if (fHessianFunc) {
74 unsigned int n = fFunc.NDim();
75 if (fG2Vec.empty() ) fG2Vec.resize(n);
76 if (fHessian.empty() ) fHessian.resize(n*n);
77 fHessianFunc(x,fHessian.data());
78 if (!fHessian.empty()) {
79 // get diagonal element of h
80 for (unsigned int i = 0; i < n; i++)
81 fG2Vec[i] = fHessian[i*n+i];
82 }
83 else fG2Vec.clear();
84 }
85 else
86 if (!fG2Vec.empty()) fG2Vec.clear();
87 return fG2Vec;
88 }
89
90 /// compute Hessian. Return Hessian as a std::vector of size(n*n)
91 std::vector<double> Hessian(std::vector<double> const& x ) const override {
92 unsigned int n = fFunc.NDim();
93 if (fHessianFunc) {
94 if (fHessian.empty() ) fHessian.resize(n * n);
95 bool ret = fHessianFunc(x,fHessian.data());
96 if (!ret) {
97 fHessian.clear();
98 fHessianFunc = nullptr;
99 }
100 } else {
101 fHessian.clear();
102 }
103
104 return fHessian;
105 }
106
107 bool HasG2() const override {
108 return bool(fG2Func);
109 }
110 bool HasHessian() const override {
111 return bool(fHessianFunc);
112 }
113
114 template<class Func>
115 void SetG2Function(Func f) { fG2Func = f;}
116
117 template<class Func>
119
120 void SetErrorDef(double up) override { fUp = up; }
121
122private:
124 double fUp;
125 mutable std::vector<double> fGrad;
126 mutable std::vector<double> fHessian;
127 mutable std::vector<double> fG2Vec;
128
129 std::function<std::vector<double>(std::vector<double> const& )> fG2Func;
130 mutable std::function<bool(std::vector<double> const& , double *)> fHessianFunc;
131};
132
133} // end namespace Minuit2
134
135} // end namespace ROOT
136
137#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...