File: RootResidualFunction.cpp

package info (click to toggle)
bornagain 23.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,956 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 356; ruby: 173; xml: 130; makefile: 45; ansic: 24
file content (75 lines) | stat: -rw-r--r-- 2,731 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Fit/Residual/RootResidualFunction.cpp
//! @brief     Implements class RootResidualFunction.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "Fit/Residual/RootResidualFunction.h"
#include <utility>

RootResidualFunction::RootResidualFunction(const scalar_function_t& objective_fun,
                                           const gradient_function_t& gradient_fun, size_t npars,
                                           size_t ndatasize)
    : ROOT::Math::FitMethodFunction(static_cast<int>(npars), static_cast<int>(ndatasize))
    , m_objective_fun(objective_fun)
    , m_gradient_fun(gradient_fun)
    , m_npars(npars)
    , m_datasize(ndatasize)
{
}

RootResidualFunction::Type_t RootResidualFunction::Type() const
{
    return ROOT::Math::FitMethodFunction::kLeastSquare;
}

ROOT::Math::IMultiGenFunction* RootResidualFunction::Clone() const
{
    return new RootResidualFunction(m_objective_fun, m_gradient_fun, m_npars, m_datasize);
}

//! Returns residual value for given data element index. Transform call of ancient
//! pointer based function to safer gradient_function_t.
//! @param pars: array of fit parameter values from the minimizer
//! @param index: index of residual element
//! @param gradients: if not zero, then array where we have to put gradients
//! @return value of residual for given data element index

double RootResidualFunction::DataElement(const double* pars, unsigned int index,
                                         double* gradients) const
{
    std::vector<double> par_values;
    par_values.resize(m_npars, 0.0);
    std::copy(pars, pars + m_npars, par_values.begin());

    std::vector<double> par_gradients;

    if (gradients)
        par_gradients.resize(m_npars);

    // retrieving result from user function
    double result = m_gradient_fun(par_values, index, par_gradients);

    // packing result back to minimizer's array
    if (gradients)
        for (size_t i = 0; i < m_npars; ++i)
            gradients[i] = par_gradients[i];

    return result;
}

double RootResidualFunction::DoEval(const double* pars) const
{
    std::vector<double> par_values;
    par_values.resize(m_npars, 0.0);
    std::copy(pars, pars + m_npars, par_values.begin());
    return m_objective_fun(par_values);
}