Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
AnalyticalGradientCalculator.cxx
Go to the documentation of this file.
1// @(#)root/minuit2:$Id$
2// Authors: M. Winkler, F. James, L. Moneta, A. Zsenei 2003-2005
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2005 LCG ROOT Math team, CERN/PH-SFT *
7 * *
8 **********************************************************************/
9
11#include "Minuit2/FCNBase.h"
15#include "Minuit2/MnMatrix.h"
16#include "Minuit2/MnPrint.h"
17#include <cassert>
18
19namespace ROOT {
20namespace Minuit2 {
21
23{
24 // evaluate analytical gradient. take care of parameter transformations
25
26 std::vector<double> grad = fGradFunc.Gradient(fTransformation(par.Vec()));
27 assert(grad.size() == fTransformation.Parameters().size());
28
29 MnAlgebraicVector v(par.Vec().size());
30 for (unsigned int i = 0; i < par.Vec().size(); i++) {
31 unsigned int ext = fTransformation.ExtOfInt(i);
33 double dd = fTransformation.DInt2Ext(i, par.Vec()(i));
34 v(i) = dd * grad[ext];
35 } else {
36 v(i) = grad[ext];
37 }
38 }
39
40 MnPrint print("AnalyticalGradientCalculator");
41 print.Debug("User given gradient in Minuit2", v);
42
43 // in case we can compute Hessian do not waste re-computing G2 here
45 return FunctionGradient(v);
46
47 // compute G2 if possible
48 MnAlgebraicVector g2(par.Vec().size());
49 if (!this->G2(par, g2)) {
50 print.Error("Error computing G2");
51 return FunctionGradient(v);
52 }
53 return FunctionGradient(v,g2);
54}
55
57{
58 // needed from base class
59 return (*this)(par);
60}
61
62// G2 can be computed directly without Hessian or via the Hessian
64 return fGradFunc.HasG2() || fGradFunc.HasHessian();
65}
66
68 return fGradFunc.HasHessian();
69}
70
71
73{
74 // compute Hessian using external gradient
75 unsigned int n = par.Vec().size();
76 assert(hmat.size() == n *(n+1)/2);
77 // compute external Hessian and then transform
78 std::vector<double> extHessian = fGradFunc.Hessian(fTransformation(par.Vec()));
79 if (extHessian.empty()) {
80 MnPrint print("AnalyticalGradientCalculator::Hessian");
81 print.Info("FCN cannot compute Hessian matrix");
82 return false;
83 }
84 unsigned int next = sqrt(extHessian.size());
85 // we need now to transform the matrix from external to internal coordinates
86 for (unsigned int i = 0; i < n; i++) {
87 unsigned int iext = fTransformation.ExtOfInt(i);
88 double dxdi = 1.;
89 if (fTransformation.Parameters()[iext].HasLimits()) {
90 dxdi = fTransformation.DInt2Ext(i, par.Vec()(i));
91 }
92 for (unsigned int j = i; j < n; j++) {
93 double dxdj = 1.;
94 unsigned int jext = fTransformation.ExtOfInt(j);
95 if (fTransformation.Parameters()[jext].HasLimits()) {
96 dxdj = fTransformation.DInt2Ext(j, par.Vec()(j));
97 }
98 hmat(i, j) = dxdi * extHessian[i*next+ j] * dxdj;
99 }
100 }
101 return true;
102}
103
105 // compute G2 using external calculator
106 unsigned int n = par.Vec().size(); // n is size of internal parameters
107 assert(g2.size() == n );
108 std::vector<double> extG2 = fGradFunc.G2(fTransformation(par.Vec()));
109 if (extG2.empty()) {
110 MnPrint print("AnalyticalGradientCalculator::G2");
111 print.Info("FCN cannot compute the 2nd derivatives vector (G2)");
112 return false;
113 }
114 assert(extG2.size() == fTransformation.Parameters().size());
115 // we need now to transform the matrix from external to internal coordinates
116 for (unsigned int i = 0; i < n; i++) {
117 unsigned int iext = fTransformation.ExtOfInt(i);
118 if (fTransformation.Parameters()[iext].HasLimits()) {
119 double dxdi = fTransformation.DInt2Ext(i, par.Vec()(i));
120 g2(i) = dxdi * dxdi * extG2[iext];
121 } else {
122 g2(i) = extG2[iext];
123 }
124 }
125 return true;
126}
127
128} // namespace Minuit2
129
130} // namespace ROOT
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
bool G2(const MinimumParameters &, MnAlgebraicVector &) const override
compute second derivatives (diagonal of Hessian)
FunctionGradient operator()(const MinimumParameters &) const override
bool Hessian(const MinimumParameters &, MnAlgebraicSymMatrix &) const override
compute Hessian matrix
virtual bool HasHessian() const
Definition FCNBase.h:132
virtual std::vector< double > G2(std::vector< double > const &) const
return second derivatives (diagonal of the Hessian matrix)
Definition FCNBase.h:127
virtual std::vector< double > Hessian(std::vector< double > const &) const
return Hessian
Definition FCNBase.h:130
virtual std::vector< double > Gradient(std::vector< double > const &) const
Definition FCNBase.h:115
virtual bool HasG2() const
Definition FCNBase.h:134
Class describing a symmetric matrix of size n.
Definition LASymMatrix.h:45
unsigned int size() const
unsigned int size() const
Definition LAVector.h:231
const MnAlgebraicVector & Vec() const
void Debug(const Ts &... args)
Definition MnPrint.h:147
void Error(const Ts &... args)
Definition MnPrint.h:129
void Info(const Ts &... args)
Definition MnPrint.h:141
unsigned int ExtOfInt(unsigned int internal) const
double DInt2Ext(unsigned int, double) const
const std::vector< MinuitParameter > & Parameters() const
const MinuitParameter & Parameter(unsigned int) const
const Int_t n
Definition legend1.C:16
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...