BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
RootResidualFunction.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Fit/Adapter/RootResidualFunction.cpp
6 //! @brief Implements class RootResidualFunction.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 
17 #include <utility>
18 
20  gradient_function_t gradient_fun, size_t npars,
21  size_t ndatasize)
22  : ROOT::Math::FitMethodFunction(static_cast<int>(npars), static_cast<int>(ndatasize))
23  , m_objective_fun(std::move(objective_fun))
24  , m_gradient_fun(std::move(gradient_fun))
25  , m_npars(npars)
26  , m_datasize(ndatasize)
27 {
28 }
29 
31 {
33 }
34 
36 {
38 }
39 
40 //! Returns residual value for given data element index. Transform call of ancient
41 //! pointer based function to safer gradient_function_t.
42 //! @param pars: array of fit parameter values from the minimizer
43 //! @param index: index of residual element
44 //! @param gradients: if not zero, then array where we have to put gradients
45 //! @return value of residual for given data element index
46 
47 double RootResidualFunction::DataElement(const double* pars, unsigned int index,
48  double* gradients) const
49 {
50  std::vector<double> par_values;
51  par_values.resize(m_npars, 0.0);
52  std::copy(pars, pars + m_npars, par_values.begin());
53 
54  std::vector<double> par_gradients;
55 
56  if (gradients)
57  par_gradients.resize(m_npars);
58 
59  // retrieving result from user function
60  double result = m_gradient_fun(par_values, index, par_gradients);
61 
62  // packing result back to minimizer's array
63  if (gradients)
64  for (size_t i = 0; i < m_npars; ++i)
65  gradients[i] = par_gradients[i];
66 
67  return result;
68 }
69 
70 double RootResidualFunction::DoEval(const double* pars) const
71 {
72  std::vector<double> par_values;
73  par_values.resize(m_npars, 0.0);
74  std::copy(pars, pars + m_npars, par_values.begin());
75  return m_objective_fun(par_values);
76 }
std::function< double(const std::vector< double > &)> scalar_function_t
Definition: Types.h:31
std::function< double(const std::vector< double > &, unsigned int, std::vector< double > &)> gradient_function_t
Definition: Types.h:34
Declares class RootResidualFunction.
Type_t
enumeration specyfing the possible fit method types
double DoEval(const double *pars) const override
evaluation of chi2
Type_t Type() const override
scalar_function_t m_objective_fun
User function to get value to minimizer.
double DataElement(const double *pars, unsigned int index, double *gradients=nullptr) const override
Evaluation of single data element residual. Will be called by ROOT minimizer.
RootResidualFunction(scalar_function_t objective_fun, gradient_function_t gradient_fun, size_t npars, size_t ndatasize)
Constructs RootResidualFunction.
ROOT::Math::IMultiGenFunction * Clone() const override
gradient_function_t m_gradient_fun
User function to get residual and gradients.
Various mathematical functions.
Definition: Bessel.h:26
BasicFitMethodFunction< ROOT::Math::IMultiGenFunction > FitMethodFunction
Definition: Fitter.h:40
Definition: TUUID.h:7