Skip to content

Commit

Permalink
Working on extending Minuit2 Analytical gradient implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
egpbos committed Nov 10, 2017
1 parent 2466586 commit caac087
Show file tree
Hide file tree
Showing 20 changed files with 1,340 additions and 1,043 deletions.
103 changes: 62 additions & 41 deletions math/minuit2/inc/Minuit2/FCNGradAdapter.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// @(#)root/minuit2:$Id$
// Author: L. Moneta 10/2006
// Authors: L. Moneta, E.G.P. Bos 2006-2017

/**********************************************************************
* *
* Copyright (c) 2006 ROOT Foundation, CERN/PH-SFT *
* Copyright (c) 2017 Patrick Bos, Netherlands eScience Center *
* *
**********************************************************************/

Expand All @@ -19,65 +20,85 @@

namespace ROOT {

namespace Minuit2 {
namespace Minuit2 {

/**
/**
template wrapped class for adapting to FCNBase signature a IGradFunction
template wrapped class for adapting to FCNBase signature a IGradFunction
@author Lorenzo Moneta, Patrick Bos
@author Lorenzo Moneta
@ingroup Minuit
@ingroup Minuit
*/

*/
template< class Function>
class FCNGradAdapter : public FCNGradientBase {

template< class Function>
class FCNGradAdapter : public FCNGradientBase {
public:

public:
FCNGradAdapter(const Function & f, double up = 1.) :
fFunc(f) ,
fUp (up) ,
fGrad(std::vector<double>(fFunc.NDim() ) ),
fG2(fFunc.hasG2ndDerivative() ? std::vector<double>(fFunc.NDim()) : std::vector<double>(0) ),
fGStep(fFunc.hasGStepSize() ? std::vector<double>(fFunc.NDim()) : std::vector<double>(0) )
{}

FCNGradAdapter(const Function & f, double up = 1.) :
fFunc(f) ,
fUp (up) ,
fGrad(std::vector<double>(fFunc.NDim() ) )
~FCNGradAdapter() {}

{}

~FCNGradAdapter() {}
double operator()(const std::vector<double>& v) const {
return fFunc.operator()(&v[0]);
}
double operator()(const double * v) const {
return fFunc.operator()(v);
}

double Up() const {return fUp;}

double operator()(const std::vector<double>& v) const {
return fFunc.operator()(&v[0]);
}
double operator()(const double * v) const {
return fFunc.operator()(v);
}

double Up() const {return fUp;}

std::vector<double> Gradient(const std::vector<double>& v) const {
fFunc.Gradient(&v[0], &fGrad[0]);
virtual std::vector<double> Gradient(const std::vector<double>& v) const {
fFunc.Gradient(v.data(), fGrad.data());

#ifdef DEBUG
std::cout << " gradient in FCNAdapter = { " ;
std::cout << " gradient in FCNAdapter = { " ;
for (unsigned int i = 0; i < fGrad.size(); ++i)
std::cout << fGrad[i] << "\t";
std::cout << "}" << std::endl;
#endif
return fGrad;
}
// forward interface
//virtual double operator()(int npar, double* params,int iflag = 4) const;
bool CheckGradient() const { return false; }

private:
const Function & fFunc;
double fUp;
mutable std::vector<double> fGrad;
};

} // end namespace Minuit2
return fGrad;
}
// forward interface
//virtual double operator()(int npar, double* params,int iflag = 4) const;
bool CheckGradient() const { return false; }

virtual std::vector<double> G2ndDerivative(const std::vector<double>& v) const {
fFunc.G2ndDerivative(v.data(), fG2.data());
return fG2;
};

virtual std::vector<double> GStepSize(const std::vector<double>& v) const {
fFunc.GStepSize(v.data(), fGStep.data());
return fGStep;
};

virtual bool hasG2ndDerivative() const {
return fFunc.hasG2ndDerivative();
}

virtual bool hasGStepSize() const {
return fFunc.hasGStepSize();
}

private:
const Function & fFunc;
double fUp;
mutable std::vector<double> fGrad;
mutable std::vector<double> fG2;
mutable std::vector<double> fGStep;
};

} // end namespace Minuit2

} // end namespace ROOT

Expand Down
28 changes: 20 additions & 8 deletions math/minuit2/inc/Minuit2/FCNGradientBase.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// @(#)root/minuit2:$Id$
// Authors: M. Winkler, F. James, L. Moneta, A. Zsenei 2003-2005
// Authors: M. Winkler, F. James, L. Moneta, A. Zsenei, E.G.P. Bos 2003-2017

/**********************************************************************
* *
* Copyright (c) 2005 LCG ROOT Math team, CERN/PH-SFT *
* Copyright (c) 2017 Patrick Bos, Netherlands eScience Center *
* *
**********************************************************************/

Expand All @@ -14,7 +15,7 @@

namespace ROOT {

namespace Minuit2 {
namespace Minuit2 {

//________________________________________________________________________
/** Extension of the FCNBase for providing the analytical Gradient of the
Expand All @@ -29,17 +30,28 @@ namespace ROOT {
"false".
*/

class FCNGradientBase : public FCNBase {
class FCNGradientBase : public FCNBase {

public:
public:

virtual ~FCNGradientBase() {}
virtual ~FCNGradientBase() {}

virtual std::vector<double> Gradient(const std::vector<double>&) const = 0;
virtual std::vector<double> Gradient(const std::vector<double>&) const = 0;

virtual bool CheckGradient() const {return true;}
virtual std::vector<double> G2ndDerivative(const std::vector<double>&) const = 0;
virtual std::vector<double> GStepSize(const std::vector<double>&) const = 0;

};
virtual bool hasG2ndDerivative() const {
return false;
}

virtual bool hasGStepSize() const {
return false;
}

virtual bool CheckGradient() const {return true;}

};

} // namespace Minuit2

Expand Down
Loading

0 comments on commit caac087

Please sign in to comment.