Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions math/mathcore/inc/Fit/Chi2FCN.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ class Chi2FCN : public BasicFCN<DerivFunType, ModelFunType, BinData> {
FitUtil::Evaluate<T>::EvalChi2Gradient(BaseFCN::ModelFunction(), BaseFCN::Data(), x, g, fNEffPoints,
fExecutionPolicy);
}
/// In some cases, the gradient algorithm will use information from the previous step, these can be passed
/// in with this overload. The `previous_*` arrays can also be used to return second derivative and step size
/// so that these can be passed forward again as well at the call site, if necessary.
/// \warning This implementation just calls the two-parameter overload.
virtual void Gradient(const double *x, double *g, double */*previous_grad*/, double */*previous_g2*/, double */*previous_gstep*/) const
{
Gradient(x, g);
}

/// get type of fit method function
virtual typename BaseObjFunction::Type_t Type() const { return BaseObjFunction::kLeastSquare; }
Expand Down Expand Up @@ -154,6 +162,15 @@ class Chi2FCN : public BasicFCN<DerivFunType, ModelFunType, BinData> {
Gradient(x, fGrad.data());
return fGrad[icoord];
}
/// In some cases, the derivative algorithm will use information from the previous step, these can be passed
/// in with this overload. The `previous_*` arrays can also be used to return second derivative and step size
/// so that these can be passed forward again as well at the call site, if necessary.
/// \warning This implementation just calls the two-parameter overload.
virtual double DoDerivative(const double *x, unsigned int icoord, double * /*previous_grad*/, double * /*previous_g2*/,
double * /*previous_gstep*/) const
{
return DoDerivative(x, icoord);
}


mutable unsigned int fNEffPoints; // number of effective points used in the fit
Expand Down
16 changes: 16 additions & 0 deletions math/mathcore/inc/Fit/LogLikelihoodFCN.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ class LogLikelihoodFCN : public BasicFCN<DerivFunType,ModelFunType,UnBinData> {
FitUtil::Evaluate<typename BaseFCN::T>::EvalLogLGradient(BaseFCN::ModelFunction(), BaseFCN::Data(), x, g,
fNEffPoints, fExecutionPolicy);
}
/// In some cases, the gradient algorithm will use information from the previous step, these can be passed
/// in with this overload. The `previous_*` arrays can also be used to return second derivative and step size
/// so that these can be passed forward again as well at the call site, if necessary.
/// \warning This overload just calls the two-parameter version.
virtual void Gradient(const double *x, double *g, double */*previous_grad*/, double */*previous_g2*/, double */*previous_gstep*/) const
{
Gradient(x, g);
}

/// get type of fit method function
virtual typename BaseObjFunction::Type_t Type() const { return BaseObjFunction::kLogLikelihood; }
Expand Down Expand Up @@ -162,6 +170,14 @@ class LogLikelihoodFCN : public BasicFCN<DerivFunType,ModelFunType,UnBinData> {
Gradient(x, &fGrad[0]);
return fGrad[icoord];
}
/// In some cases, the derivative algorithm will use information from the previous step, these can be passed
/// in with this overload. The `previous_*` arrays can also be used to return second derivative and step size
/// so that these can be passed forward again as well at the call site, if necessary.
virtual double DoDerivative(const double *x, unsigned int icoord, double * /*previous_grad*/, double * /*previous_g2*/,
double * /*previous_gstep*/) const
{
return DoDerivative(x, icoord);
}


//data member
Expand Down
17 changes: 17 additions & 0 deletions math/mathcore/inc/Fit/PoissonLikelihoodFCN.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ class PoissonLikelihoodFCN : public BasicFCN<DerivFunType,ModelFunType,BinData>
FitUtil::Evaluate<typename BaseFCN::T>::EvalPoissonLogLGradient(BaseFCN::ModelFunction(), BaseFCN::Data(), x, g,
fNEffPoints, fExecutionPolicy);
}
/// In some cases, the gradient algorithm will use information from the previous step, these can be passed
/// in with this overload. The `previous_*` arrays can also be used to return second derivative and step size
/// so that these can be passed forward again as well at the call site, if necessary.
/// \warning This implementation just calls the two-parameter overload.
virtual void Gradient(const double *x, double *g, double */*previous_grad*/, double */*previous_g2*/, double */*previous_gstep*/) const
{
Gradient(x, g);
}

/// get type of fit method function
virtual typename BaseObjFunction::Type_t Type() const { return BaseObjFunction::kLogLikelihood; }
Expand Down Expand Up @@ -170,6 +178,15 @@ class PoissonLikelihoodFCN : public BasicFCN<DerivFunType,ModelFunType,BinData>
Gradient(x, &fGrad[0]);
return fGrad[icoord];
}
/// In some cases, the derivative algorithm will use information from the previous step, these can be passed
/// in with this overload. The `previous_*` arrays can also be used to return second derivative and step size
/// so that these can be passed forward again as well at the call site, if necessary.
/// \warning This implementation just calls the two-parameter overload.
virtual double DoDerivative(const double *x, unsigned int icoord, double * /*previous_grad*/, double * /*previous_g2*/,
double * /*previous_gstep*/) const
{
return DoDerivative(x, icoord);
}


//data member
Expand Down
31 changes: 31 additions & 0 deletions math/mathcore/inc/Math/Functor.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ private :
return fFunc.Derivative(x,icoord);
}

// TODO: implementing this will require extending with another function signature in GradFunctor
/// \warning This overload just calls the two-parameter version.
inline double DoDerivative(const double *x, unsigned int icoord, double */*previous_grad*/, double */*previous_g2*/,
double */*previous_gstep*/) const
{
return DoDerivative(x, icoord);
}


unsigned int fDim;
mutable Func fFunc; // should here be a reference and pass a non-const ref in ctor
Expand Down Expand Up @@ -211,6 +219,13 @@ private :
return fGradFunc(x, icoord);
}

// TODO: implementing this will require extending with another function signature in GradFunctor
/// \warning This overload just calls the two-parameter version.
inline double DoDerivative(const double *x, unsigned int icoord, double */*previous_grad*/, double */*previous_g2*/,
double */*previous_gstep*/) const
{
return DoDerivative(x, icoord);
}

unsigned int fDim;
mutable Func fFunc;
Expand Down Expand Up @@ -348,6 +363,14 @@ private :
return ((*fObj).*fGradMemFn)(x,icoord);
}

// TODO: implementing this will require extending with another function signature in GradFunctor
/// \warning This overload just calls the two-parameter version.
inline double DoDerivative(const double *x, unsigned int icoord, double */*previous_grad*/, double */*previous_g2*/,
double */*previous_gstep*/) const
{
return DoDerivative(x, icoord);
}

unsigned int fDim;
mutable PointerToObj fObj;
PointerToMemFn fMemFn;
Expand Down Expand Up @@ -702,6 +725,14 @@ private :
return fImpl->Derivative(x,icoord);
}

// TODO: implementing this will require extending with another function signature in GradFunctor
/// \warning This overload just calls the two-parameter version.
inline double DoDerivative(const double *x, unsigned int icoord, double */*previous_grad*/, double */*previous_g2*/,
double */*previous_gstep*/) const
{
return DoDerivative(x, icoord);
}

std::unique_ptr<Impl> fImpl; // pointer to base grad functor handler


Expand Down
26 changes: 26 additions & 0 deletions math/mathcore/inc/Math/IFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ namespace ROOT {
Return the partial derivative with respect to the passed coordinate
*/
T Derivative(const T *x, unsigned int icoord = 0) const { return DoDerivative(x, icoord); }
/// In some cases, the derivative algorithm will use information from the previous step, these can be passed
/// in with this overload. The `previous_*` arrays can also be used to return second derivative and step size
/// so that these can be passed forward again as well at the call site, if necessary.
T Derivative(const T *x, unsigned int icoord, T *previous_grad, T *previous_g2,
T *previous_gstep) const
{
return DoDerivative(x, icoord, previous_grad, previous_g2, previous_gstep);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return DoDerivative(x, icoord, previous_grad, previous_g2, previous_gstep);
return DoDerivativeWithPrevResult(x, icoord, previous_grad, previous_g2, previous_gstep);

}

/**
Optimized method to evaluate at the same time the function value and derivative at a point x.
Expand All @@ -232,6 +240,14 @@ namespace ROOT {
function to evaluate the derivative with respect each coordinate. To be implemented by the derived class
*/
virtual T DoDerivative(const T *x, unsigned int icoord) const = 0;
/// In some cases, the derivative algorithm will use information from the previous step, these can be passed
/// in with this overload. The `previous_*` arrays can also be used to return second derivative and step size
/// so that these can be passed forward again as well at the call site, if necessary.
virtual T DoDerivative(const T *x, unsigned int icoord, T * /*previous_grad*/, T * /*previous_g2*/,
T * /*previous_gstep*/) const
Comment on lines +246 to +247

@guitargeek guitargeek Aug 31, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
virtual T DoDerivative(const T *x, unsigned int icoord, T * /*previous_grad*/, T * /*previous_g2*/,
T * /*previous_gstep*/) const
virtual T DoDerivativeWithPreviousResult(const T *x, unsigned int icoord, T * /*previous_grad*/, T * /*previous_g2*/,
T * /*previous_gstep*/) const

What about giving these new functions a different name? There is no reason to also name it DoDerivative, and like this we avoid the compiler warning when only the old DoDerivative signature is overridden.

If a shorter but also descriptive name other than DoDerivativeWithPreviousResult comes to our mind, I would also be open to it.

{
return DoDerivative(x, icoord);
};
};

//___________________________________________________________________________________
Expand Down Expand Up @@ -346,6 +362,16 @@ namespace ROOT {
grad[icoord] = BaseGrad::Derivative(x, icoord);
}

/// In some cases, the gradient algorithm will use information from the previous step, these can be passed
/// in with this overload. The `previous_*` arrays can also be used to return second derivative and step size
/// so that these can be passed forward again as well at the call site, if necessary.
virtual void Gradient(const T *x, T *grad, T *previous_grad, T *previous_g2, T *previous_gstep) const

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
virtual void Gradient(const T *x, T *grad, T *previous_grad, T *previous_g2, T *previous_gstep) const
virtual void GradientWithPrevResult(const T *x, T *grad, T *previous_grad, T *previous_g2, T *previous_gstep) const

See equivalent comment about DoGradient.

{
unsigned int ndim = NDim();
for (unsigned int icoord = 0; icoord < ndim; ++icoord)
grad[icoord] = BaseGrad::Derivative(x, icoord, previous_grad, previous_g2, previous_gstep);
}

using BaseFunc::NDim;

/**
Expand Down
8 changes: 8 additions & 0 deletions math/mathcore/inc/Math/MinimTransformFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ class MinimTransformFunction : public IMultiGradFunction {
//std::cout << "Derivative icoord (ext)" << fIndex[icoord] << " dtrafo " << dExtdInt << " " << deriv << std::endl;
return deriv * dExtdInt;
}
/// In some cases, the derivative algorithm will use information from the previous step, these can be passed
/// in with this overload. The `previous_*` arrays can also be used to return second derivative and step size
/// so that these can be passed forward again as well at the call site, if necessary.
virtual double DoDerivative(const double *x, unsigned int icoord, double * /*previous_grad*/, double * /*previous_g2*/,
double * /*previous_gstep*/) const
{
return DoDerivative(x, icoord);
}

// copy constructor for this class (disable by having it private)
MinimTransformFunction( const MinimTransformFunction & ) :
Expand Down
18 changes: 18 additions & 0 deletions math/mathcore/test/fit/testMinim.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ public :
}

}
void Gradient(const double *x, double *g, double */*previous_grad*/, double */*previous_g2*/, double */*previous_gstep*/) const
{
Gradient(x, g);
}

#ifdef USE_FDF
void FdF (const double * x, double & f, double * g) const {
Expand Down Expand Up @@ -240,6 +244,11 @@ public :
Gradient(x,&g[0]);
return g[i];
}
double DoDerivative(const double *x, unsigned int icoord, double * /*previous_grad*/, double * /*previous_g2*/,
double * /*previous_gstep*/) const
{
return DoDerivative(x, icoord);
}

private:

Expand Down Expand Up @@ -322,6 +331,10 @@ public :


}
void Gradient(const double *x, double *g, double */*previous_grad*/, double */*previous_g2*/, double */*previous_gstep*/) const
{
Gradient(x, g);
}

private:

Expand All @@ -342,6 +355,11 @@ public :
Gradient(x,&g[0]);
return g[i];
}
double DoDerivative(const double *x, unsigned int icoord, double * /*previous_grad*/, double * /*previous_g2*/,
double * /*previous_gstep*/) const
{
return DoDerivative(x, icoord);
}

void DoCalculatefi(const double * x) const {
// calculate the i- element ; F(X) = Sum {fi]
Expand Down
17 changes: 17 additions & 0 deletions math/mathmore/inc/Math/GSLNLSMinimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ class LSResidualFunc : public IMultiGradFunction {
double f0 = 0;
FdF(x,f0,g);
}
/// In some cases, the gradient algorithm will use information from the previous step, these can be passed
/// in with this overload. The `previous_*` arrays can also be used to return second derivative and step size
/// so that these can be passed forward again as well at the call site, if necessary.
/// \warning This implementation just calls the two-parameter overload.
virtual void Gradient(const double *x, double *g, double */*previous_grad*/, double */*previous_g2*/, double */*previous_gstep*/) const
{
Gradient(x, g);
}

void FdF (const double * x, double & f, double * g) const {
unsigned int n = NDim();
Expand Down Expand Up @@ -129,6 +137,15 @@ class LSResidualFunc : public IMultiGradFunction {
fX2[icoord] += kEps;
return ( DoEval(&fX2.front()) - DoEval(x) )/kEps;
}
/// In some cases, the derivative algorithm will use information from the previous step, these can be passed
/// in with this overload. The `previous_*` arrays can also be used to return second derivative and step size
/// so that these can be passed forward again as well at the call site, if necessary.
/// \warning This implementation just calls the two-parameter overload.
virtual double DoDerivative(const double *x, unsigned int icoord, double * /*previous_grad*/, double * /*previous_g2*/,
double * /*previous_gstep*/) const
{
return DoDerivative(x, icoord);
}

unsigned int fIndex;
const ROOT::Math::FitMethodFunction * fChi2;
Expand Down
9 changes: 9 additions & 0 deletions math/mathmore/inc/Math/MultiNumGradFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ class MultiNumGradFunction : public IMultiGradFunction {

// calculate derivative using mathcore derivator
double DoDerivative (const double * x, unsigned int icoord ) const;
/// In some cases, the derivative algorithm will use information from the previous step, these can be passed
/// in with this overload. The `previous_*` arrays can also be used to return second derivative and step size
/// so that these can be passed forward again as well at the call site, if necessary.
/// \warning This implementation just calls the two-parameter overload.
virtual double DoDerivative(const double *x, unsigned int icoord, double * /*previous_grad*/, double * /*previous_g2*/,
double * /*previous_gstep*/) const
{
return DoDerivative(x, icoord);
}

// adapat internal function type to IMultiGenFunction needed by derivative calculation
const IMultiGenFunction * fFunc;
Expand Down
12 changes: 12 additions & 0 deletions math/minuit2/inc/Minuit2/FCNGradAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ class FCNGradAdapter : public FCNGradientBase {
});
return fGrad;
}
std::vector<double> Gradient(const std::vector<double> &v, double *previous_grad, double *previous_g2,
double *previous_gstep) const override
Comment on lines +57 to +58

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::vector<double> Gradient(const std::vector<double> &v, double *previous_grad, double *previous_g2,
double *previous_gstep) const override
std::vector<double> GradientWithPrevResult(const std::vector<double> &v, double *previous_grad, double *previous_g2,
double *previous_gstep) const override

{
fFunc.Gradient(&v[0], &fGrad[0], previous_grad, previous_g2, previous_gstep);

MnPrint("FCNGradAdapter").Debug([&](std::ostream &os) {
os << "gradient in FCNAdapter = {";
for (unsigned int i = 0; i < fGrad.size(); ++i)
os << fGrad[i] << (i == fGrad.size() - 1 ? '}' : '\t');
});
return fGrad;
}
// forward interface
// virtual double operator()(int npar, double* params,int iflag = 4) const;
bool CheckGradient() const override { return false; }
Expand Down
2 changes: 2 additions & 0 deletions math/minuit2/inc/Minuit2/FCNGradientBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class FCNGradientBase : public FCNBase {
virtual ~FCNGradientBase() {}

virtual std::vector<double> Gradient(const std::vector<double> &) const = 0;
virtual std::vector<double> Gradient(const std::vector<double> &parameters, double */*previous_grad*/, double */*previous_g2*/,
double */*previous_gstep*/) const { return Gradient(parameters); };
Comment on lines +44 to +45

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
virtual std::vector<double> Gradient(const std::vector<double> &parameters, double */*previous_grad*/, double */*previous_g2*/,
double */*previous_gstep*/) const { return Gradient(parameters); };
virtual std::vector<double> GradientWithPrevResult(const std::vector<double> &parameters,
double * /*previous_grad*/, double * /*previous_g2*/,
double * /*previous_gstep*/) const { return Gradient(parameters); };

Besides the name change, try to not have the character sequence */* but put a space after the first * to not confuse syntax highlighting of GitHub and many editors.


virtual bool CheckGradient() const { return true; }

Expand Down
3 changes: 1 addition & 2 deletions math/minuit2/inc/Minuit2/NumericalDerivator.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ class NumericalDerivator {
double Ext2int(const ROOT::Fit::ParameterSettings &parameter, double val) const;
double DInt2Ext(const ROOT::Fit::ParameterSettings &parameter, double val) const;

void SetInitialGradient(const ROOT::Math::IBaseFunctionMultiDim *function,
const std::vector<ROOT::Fit::ParameterSettings> &parameters,
void SetInitialGradient(const std::vector<ROOT::Fit::ParameterSettings> &parameters,
std::vector<DerivatorElement> &gradient);

inline bool AlwaysExactlyMimicMinuit2() const { return fAlwaysExactlyMimicMinuit2; }
Expand Down
Loading