BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
ResidualFunctionAdapter.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Fit/Adapter/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 #ifdef SWIG
16 #error no need to expose this header to Swig
17 #endif
18 
19 #ifndef USER_API
20 #ifndef BORNAGAIN_FIT_ADAPTER_RESIDUALFUNCTIONADAPTER_H
21 #define BORNAGAIN_FIT_ADAPTER_RESIDUALFUNCTIONADAPTER_H
22 
24 #include "Fit/Minimizer/Types.h"
25 #include "Fit/Param/Parameters.h"
26 #include <functional>
27 #include <memory>
28 #include <vector>
29 
31 
32 namespace mumufit {
33 
34 //! Provides RootResidualFunction which will be minimizer by ROOT.
35 //! Converts ROOT calls to the call of fcn_residual_t.
36 
38 public:
39  ResidualFunctionAdapter(fcn_residual_t func, const Parameters& parameters);
40 
42 
43 private:
44  void calculate_gradients(const std::vector<double>& pars);
45  std::vector<double> get_residuals(const std::vector<double>& pars);
46 
47  //! evaluate method for gradients and residuals called directly from the minimizer
48  double element_residual(const std::vector<double>& pars, unsigned int index,
49  std::vector<double>& gradients);
50  //! Evaluate chi2
51  double chi2(const std::vector<double>& pars);
52 
53  //! Length of vector with residuals, should stay the same during minimization.
54  size_t m_datasize;
55  fcn_residual_t m_fcn; //!< user function to minimize
57  std::vector<double> m_residuals;
58  std::vector<std::vector<double>> m_gradients; // [m_npars][m_ndatasize]
59  std::unique_ptr<RootResidualFunction> m_root_objective;
60 };
61 
62 } // namespace mumufit
63 
64 #endif // BORNAGAIN_FIT_ADAPTER_RESIDUALFUNCTIONADAPTER_H
65 #endif // USER_API
Defines interface IFunctionAdapter.
Defines common types for fitting library.
std::function< std::vector< double >(const mumufit::Parameters &)> fcn_residual_t
Definition: Types.h:41
Defines class Parameters.
Minimizer function with access to single data element residuals, required by Fumili2 and GSLMultiMin ...
Base class for objective function adapters, which converts user functions to minimize into the functi...
A collection of fit parameters.
Definition: Parameters.h:26
Provides RootResidualFunction which will be minimizer by ROOT. Converts ROOT calls to the call of fcn...
const RootResidualFunction * rootResidualFunction()
std::vector< std::vector< double > > m_gradients
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::unique_ptr< RootResidualFunction > m_root_objective
void calculate_gradients(const std::vector< double > &pars)
size_t m_datasize
Length of vector with residuals, should stay the same during minimization.
double chi2(const std::vector< double > &pars)
Evaluate chi2.
ResidualFunctionAdapter(fcn_residual_t func, const Parameters &parameters)
fcn_residual_t m_fcn
user function to minimize
The multi-library, multi-algorithm fit wrapper library.