BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ResidualFunctionAdapter.h
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Fit/RootAdapter/ResidualFunctionAdapter.h
6 //! @brief Defines class ResidualFunctionAdapter.
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 
15 #ifndef BORNAGAIN_FIT_ROOTADAPTER_RESIDUALFUNCTIONADAPTER_H
16 #define BORNAGAIN_FIT_ROOTADAPTER_RESIDUALFUNCTIONADAPTER_H
17 
19 #include "Fit/Kernel/KernelTypes.h"
20 #include "Fit/Kernel/Parameters.h"
21 #include <functional>
22 #include <memory>
23 #include <vector>
24 
26 
27 namespace Fit
28 {
29 
30 //! Provides RootResidualFunction which will be minimizer by ROOT.
31 //! Converts ROOT calls to the call of fcn_residual_t.
32 
34 {
35 public:
36  ResidualFunctionAdapter(fcn_residual_t func, const Parameters& parameters);
37 
39 
40 private:
41  void calculate_gradients(const std::vector<double>& pars);
42  std::vector<double> get_residuals(const std::vector<double>& pars);
43 
44  //! evaluate method for gradients and residuals called directly from the minimizer
45  double element_residual(const std::vector<double>& pars, unsigned int index,
46  std::vector<double>& gradients);
47  //! Evaluate chi2
48  double chi2(const std::vector<double>& pars);
49 
50  //! Length of vector with residuals, should stay the same during minimization.
51  size_t m_datasize;
52  fcn_residual_t m_fcn; //!< user function to minimize
54  std::vector<double> m_residuals;
55  std::vector<std::vector<double>> m_gradients; // [m_npars][m_ndatasize]
56  std::unique_ptr<RootResidualFunction> m_root_objective;
57 };
58 
59 } // namespace Fit
60 
61 #endif // BORNAGAIN_FIT_ROOTADAPTER_RESIDUALFUNCTIONADAPTER_H
Defines class IFunctionAdapter.
Defines common types for fitting library.
std::function< std::vector< double >(const Fit::Parameters &)> fcn_residual_t
Definition: KernelTypes.h:38
Defines class Parameters.
Base class for objective function adapters, which converts user functions to minimize into the functi...
A collection of fit parameters.
Definition: Parameters.h:28
Provides RootResidualFunction which will be minimizer by ROOT.
const RootResidualFunction * rootResidualFunction()
fcn_residual_t m_fcn
user function to minimize
double element_residual(const std::vector< double > &pars, unsigned int index, std::vector< double > &gradients)
evaluate method for gradients and residuals called directly from the minimizer
std::vector< double > get_residuals(const std::vector< double > &pars)
std::vector< std::vector< double > > m_gradients
void calculate_gradients(const std::vector< double > &pars)
double chi2(const std::vector< double > &pars)
Evaluate chi2.
ResidualFunctionAdapter(fcn_residual_t func, const Parameters &parameters)
size_t m_datasize
Length of vector with residuals, should stay the same during minimization.
std::unique_ptr< RootResidualFunction > m_root_objective
Minimizer function with access to single data element residuals, required by Fumili2 and GSLMultiMin ...
Objective function types.