Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROOT::Minuit2::FCNAdapter Class Reference

Adapter class to wrap user-provided functions into the FCNBase interface.

This class allows users to supply their function (and optionally its gradient, diagonal second derivatives, and Hessian) in the form of std::function objects. It adapts these functions so that they can be used transparently with the MINUIT minimizers via the FCNBase interface.

Typical usage:

  • Pass the function to minimize to the constructor.
  • Optionally set the gradient, G2 (second derivative diagonal), or Hessian functions using the provided setter methods.
  • MINUIT will then query these functions if available, or fall back to numerical approximations if they are not provided.

Definition at line 37 of file FCNAdapter.h.

Public Member Functions

 FCNAdapter (std::function< double(double const *)> f, double up=1.)
 Construct an adapter around a user-provided function.
 
std::vector< doubleG2 (std::vector< double > const &x) const override
 Return the diagonal elements of the Hessian (second derivatives).
 
std::vector< doubleGradient (std::vector< double > const &v) const override
 Evaluate the gradient of the function at the given parameter vector.
 
bool HasG2 () const override
 Indicate whether analytic second derivatives (diagonal of the Hessian) are available.
 
bool HasGradient () const override
 Indicate whether an analytic gradient has been provided.
 
bool HasHessian () const override
 Indicate whether an analytic Hessian has been provided.
 
std::vector< doubleHessian (std::vector< double > const &x) const override
 Return the full Hessian matrix.
 
double operator() (std::vector< double > const &v) const override
 Evaluate the function at the given parameter vector.
 
void SetErrorDef (double up) override
 Update the error definition parameter.
 
void SetG2Function (std::function< std::vector< double >(std::vector< double > const &)> f)
 Set the function providing diagonal second derivatives (G2).
 
void SetGradientFunction (std::function< void(double const *, double *)> f)
 Set the analytic gradient function.
 
void SetHessianFunction (std::function< bool(std::vector< double > const &, double *)> f)
 Set the function providing the full Hessian matrix.
 
double Up () const override
 Return the error definition parameter (up).
 
- Public Member Functions inherited from ROOT::Minuit2::FCNBase
virtual ~FCNBase ()=default
 
virtual double ErrorDef () const
 Error definition of the function.
 
virtual std::vector< doubleGradientWithPrevResult (std::vector< double > const &parameters, double *, double *, double *) const
 
virtual GradientParameterSpace gradParameterSpace () const
 

Private Types

using Function = std::function<double(double const *)>
 
using G2Function = std::function<std::vector<double>(std::vector<double> const &)>
 
using GradFunction = std::function<void(double const *, double *)>
 
using HessianFunction = std::function<bool(std::vector<double> const &, double *)>
 

Private Attributes

Function fFunc
 Wrapped function to minimize.
 
G2Function fG2Func
 Optional diagonal second-derivative function.
 
GradFunction fGradFunc
 Optional gradient function.
 
std::vector< doublefHessian
 Storage for intermediate Hessian values.
 
HessianFunction fHessianFunc
 Optional Hessian function.
 
double fUp = 1.
 Error definition parameter.
 

#include <Minuit2/FCNAdapter.h>

Inheritance diagram for ROOT::Minuit2::FCNAdapter:
[legend]

Member Typedef Documentation

◆ Function

Definition at line 164 of file FCNAdapter.h.

◆ G2Function

using ROOT::Minuit2::FCNAdapter::G2Function = std::function<std::vector<double>(std::vector<double> const &)>
private

Definition at line 166 of file FCNAdapter.h.

◆ GradFunction

using ROOT::Minuit2::FCNAdapter::GradFunction = std::function<void(double const *, double *)>
private

Definition at line 165 of file FCNAdapter.h.

◆ HessianFunction

using ROOT::Minuit2::FCNAdapter::HessianFunction = std::function<bool(std::vector<double> const &, double *)>
private

Definition at line 167 of file FCNAdapter.h.

Constructor & Destructor Documentation

◆ FCNAdapter()

ROOT::Minuit2::FCNAdapter::FCNAdapter ( std::function< double(double const *)> f,
double up = 1. )
inline

Construct an adapter around a user-provided function.

Parameters
fFunction to minimize. It must take a pointer to the parameter array (double const*) and return the function value.
upError definition parameter (defaults to 1.0).

Definition at line 45 of file FCNAdapter.h.

Member Function Documentation

◆ G2()

std::vector< double > ROOT::Minuit2::FCNAdapter::G2 ( std::vector< double > const & x) const
inlineoverridevirtual

Return the diagonal elements of the Hessian (second derivatives).

If a G2 function is set, it is used directly. If only a Hessian function is available, the diagonal is extracted from the full Hessian.

Parameters
xParameter vector.
Returns
Vector of second derivatives (one per parameter).

Reimplemented from ROOT::Minuit2::FCNBase.

Definition at line 91 of file FCNAdapter.h.

◆ Gradient()

std::vector< double > ROOT::Minuit2::FCNAdapter::Gradient ( std::vector< double > const & v) const
inlineoverridevirtual

Evaluate the gradient of the function at the given parameter vector.

Parameters
vParameter vector.
Returns
Gradient vector (∂f/∂xᵢ) at the specified parameters.

Reimplemented from ROOT::Minuit2::FCNBase.

Definition at line 77 of file FCNAdapter.h.

◆ HasG2()

bool ROOT::Minuit2::FCNAdapter::HasG2 ( ) const
inlineoverridevirtual

Indicate whether analytic second derivatives (diagonal of the Hessian) are available.

Returns
true if a G2 function or a Hessian function has been set, otherwise false.

Reimplemented from ROOT::Minuit2::FCNBase.

Definition at line 55 of file FCNAdapter.h.

◆ HasGradient()

bool ROOT::Minuit2::FCNAdapter::HasGradient ( ) const
inlineoverridevirtual

Indicate whether an analytic gradient has been provided.

Returns
true if a gradient function was set, otherwise false.

Reimplemented from ROOT::Minuit2::FCNBase.

Definition at line 50 of file FCNAdapter.h.

◆ HasHessian()

bool ROOT::Minuit2::FCNAdapter::HasHessian ( ) const
inlineoverridevirtual

Indicate whether an analytic Hessian has been provided.

Returns
true if a Hessian function was set, otherwise false.

Reimplemented from ROOT::Minuit2::FCNBase.

Definition at line 60 of file FCNAdapter.h.

◆ Hessian()

std::vector< double > ROOT::Minuit2::FCNAdapter::Hessian ( std::vector< double > const & x) const
inlineoverridevirtual

Return the full Hessian matrix.

If a Hessian function is available, it is used to fill the matrix. If the Hessian function fails, it is cleared and not used again.

Parameters
xParameter vector.
Returns
Flattened Hessian matrix in row-major order.

Reimplemented from ROOT::Minuit2::FCNBase.

Definition at line 118 of file FCNAdapter.h.

◆ operator()()

double ROOT::Minuit2::FCNAdapter::operator() ( std::vector< double > const & v) const
inlineoverridevirtual

Evaluate the function at the given parameter vector.

Parameters
vParameter vector.
Returns
Function value at the specified parameters.

Implements ROOT::Minuit2::FCNBase.

Definition at line 66 of file FCNAdapter.h.

◆ SetErrorDef()

void ROOT::Minuit2::FCNAdapter::SetErrorDef ( double up)
inlineoverridevirtual

Update the error definition parameter.

Parameters
upNew error definition value.

Reimplemented from ROOT::Minuit2::FCNBase.

Definition at line 161 of file FCNAdapter.h.

◆ SetG2Function()

void ROOT::Minuit2::FCNAdapter::SetG2Function ( std::function< std::vector< double >(std::vector< double > const &)> f)
inline

Set the function providing diagonal second derivatives (G2).

Parameters
fFunction taking a parameter vector and returning the diagonal of the Hessian matrix as a vector.

Definition at line 145 of file FCNAdapter.h.

◆ SetGradientFunction()

void ROOT::Minuit2::FCNAdapter::SetGradientFunction ( std::function< void(double const *, double *)> f)
inline

Set the analytic gradient function.

Parameters
fGradient function of type void(double const*, double*). The first argument is the parameter array, the second is the output array for the gradient values.

Definition at line 139 of file FCNAdapter.h.

◆ SetHessianFunction()

void ROOT::Minuit2::FCNAdapter::SetHessianFunction ( std::function< bool(std::vector< double > const &, double *)> f)
inline

Set the function providing the full Hessian matrix.

Parameters
fFunction of type bool(std::vector<double> const&, double*). The first argument is the parameter vector, the second is the output buffer (flattened matrix). The return value should be true on success, false on failure.

Definition at line 153 of file FCNAdapter.h.

◆ Up()

double ROOT::Minuit2::FCNAdapter::Up ( ) const
inlineoverridevirtual

Return the error definition parameter (up).

Returns
Current error definition value.

Implements ROOT::Minuit2::FCNBase.

Definition at line 71 of file FCNAdapter.h.

Member Data Documentation

◆ fFunc

Function ROOT::Minuit2::FCNAdapter::fFunc
private

Wrapped function to minimize.

Definition at line 172 of file FCNAdapter.h.

◆ fG2Func

G2Function ROOT::Minuit2::FCNAdapter::fG2Func
private

Optional diagonal second-derivative function.

Definition at line 174 of file FCNAdapter.h.

◆ fGradFunc

GradFunction ROOT::Minuit2::FCNAdapter::fGradFunc
private

Optional gradient function.

Definition at line 173 of file FCNAdapter.h.

◆ fHessian

std::vector<double> ROOT::Minuit2::FCNAdapter::fHessian
mutableprivate

Storage for intermediate Hessian values.

Definition at line 170 of file FCNAdapter.h.

◆ fHessianFunc

HessianFunction ROOT::Minuit2::FCNAdapter::fHessianFunc
mutableprivate

Optional Hessian function.

Definition at line 175 of file FCNAdapter.h.

◆ fUp

double ROOT::Minuit2::FCNAdapter::fUp = 1.
private

Error definition parameter.

Definition at line 169 of file FCNAdapter.h.

Libraries for ROOT::Minuit2::FCNAdapter:

The documentation for this class was generated from the following file: