Mantid
Loading...
Searching...
No Matches
ParDomain.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
12
13namespace Mantid::CurveFitting {
14
22 if (i >= m_creators.size())
23 throw std::range_error("Function domain index is out of range.");
24 if (!m_domain[i]) {
25 m_creators[i]->createDomain(m_domain[i], m_values[i]);
26 }
27 domain = m_domain[i];
28 values = m_values[i];
29}
30
36 const int n = static_cast<int>(getNDomains());
38 for (int i = 0; i < n; ++i) {
41 getDomainAndValues(static_cast<size_t>(i), domain, values);
42 if (!values) {
43 throw std::runtime_error("CostFunction: undefined FunctionValues.");
44 }
45 costFunction.addVal(domain, values);
46 }
47}
48
57 bool evalHessian) {
58 const auto n = static_cast<int>(getNDomains());
60 std::vector<API::IFunction_sptr> funs;
62 for (int i = 0; i < n; ++i) {
65 getDomainAndValues(i, domain, values);
66 auto simpleValues = std::dynamic_pointer_cast<API::FunctionValues>(values);
67 if (!simpleValues) {
68 throw std::runtime_error("CostFunction: undefined FunctionValues.");
69 }
70 std::vector<API::IFunction_sptr>::size_type k = PARALLEL_THREAD_NUMBER;
71 PARALLEL_CRITICAL(resize) {
72 if (k >= funs.size()) {
73 funs.resize(k + 1);
74 }
75 if (!funs[k]) {
76 funs[k] = costFunction.getFittingFunction()->clone();
77 }
78 }
79 costFunction.addValDerivHessian(funs[k], domain, simpleValues, evalDeriv, evalHessian);
80 }
81}
82
83} // namespace Mantid::CurveFitting
#define PARALLEL_THREAD_NUMBER
#define PARALLEL_FOR_NO_WSP_CHECK()
#define PARALLEL_CRITICAL(name)
#define PARALLEL_SET_DYNAMIC(val)
A semi-abstract class for a cost function for fitting functions.
virtual void addVal(API::FunctionDomain_sptr domain, API::FunctionValues_sptr values) const =0
Increment to the cost function by evaluating it on a domain.
virtual void addValDerivHessian(API::IFunction_sptr function, API::FunctionDomain_sptr domain, API::FunctionValues_sptr values, bool evalDeriv=true, bool evalHessian=true) const =0
Increments the cost function and its derivatives by evaluating them on a domain.
virtual API::IFunction_sptr getFittingFunction() const
Get fitting function.
void getDomainAndValues(size_t i, API::FunctionDomain_sptr &domain, API::FunctionValues_sptr &values) const override
Create and return i-th domain and i-th values, (i-1)th domain is released.
Definition: ParDomain.cpp:21
void additiveCostFunctionVal(const CostFunctions::CostFuncFitting &costFunction) override
Calculate the value of an additive cost function.
Definition: ParDomain.cpp:35
void additiveCostFunctionValDerivHessian(const CostFunctions::CostFuncFitting &costFunction, bool evalDeriv, bool evalHessian) override
Calculate the value, first and second derivatives of an additive cost function.
Definition: ParDomain.cpp:56
std::vector< API::FunctionDomain_sptr > m_domain
Currently active domain.
Definition: SeqDomain.h:61
std::vector< API::FunctionValues_sptr > m_values
Currently active values.
Definition: SeqDomain.h:63
virtual size_t getNDomains() const
Return the number of parts in the domain.
Definition: SeqDomain.cpp:23
std::vector< std::shared_ptr< API::IDomainCreator > > m_creators
Domain creators.
Definition: SeqDomain.h:65
std::shared_ptr< FunctionValues > FunctionValues_sptr
typedef for a shared pointer
std::shared_ptr< FunctionDomain > FunctionDomain_sptr
typedef for a shared pointer