Mantid
Loading...
Searching...
No Matches
SeqDomain.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#include <numeric>
13
14namespace Mantid::CurveFitting {
15
17size_t SeqDomain::size() const {
18 return std::accumulate(m_creators.cbegin(), m_creators.cend(), static_cast<size_t>(0),
19 [](size_t sum, const auto &creator) { return sum + creator->getDomainSize(); });
20}
21
23size_t SeqDomain::getNDomains() const { return m_creators.size(); }
24
32 if (i >= m_creators.size())
33 throw std::range_error("Function domain index is out of range.");
34 if (!m_domain[i] || i != m_currentIndex) {
35 m_domain[m_currentIndex].reset();
36 m_values[m_currentIndex].reset();
37 m_creators[i]->createDomain(m_domain[i], m_values[i]);
39 }
40 domain = m_domain[i];
41 values = m_values[i];
42}
43
49 m_creators.emplace_back(creator);
50 m_domain.emplace_back(API::FunctionDomain_sptr());
51 m_values.emplace_back(API::FunctionValues_sptr());
52}
53
62 return new SeqDomain;
63 } else if (type == API::IDomainCreator::Parallel) {
64 return new ParDomain;
65 }
66 throw std::invalid_argument("Unknown SeqDomain type");
67}
68
76 const size_t n = getNDomains();
77 for (size_t i = 0; i < n; ++i) {
78 values.reset();
79 getDomainAndValues(i, domain, values);
80 if (!values) {
81 throw std::runtime_error("CostFunction: undefined FunctionValues.");
82 }
83 costFunction.addVal(domain, values);
84 }
85}
86
87//------------------------------------------------------------------------------------------------
95 const size_t n = getNDomains();
96 for (size_t i = 0; i < n; ++i) {
97 values.reset();
98 getDomainAndValues(i, domain, values);
99 if (!values) {
100 throw std::runtime_error("Rwp: undefined FunctionValues.");
101 }
102 rwp.addVal(domain, values);
103 }
104}
105
114 bool evalHessian) {
117 const size_t n = getNDomains();
118 for (size_t i = 0; i < n; ++i) {
119 values.reset();
120 getDomainAndValues(i, domain, values);
121 if (!values) {
122 throw std::runtime_error("CostFunction: undefined FunctionValues.");
123 }
124 costFunction.addValDerivHessian(costFunction.getFittingFunction(), domain, values, evalDeriv, evalHessian);
125 }
126}
127
134void SeqDomain::rwpValDerivHessian(const CostFunctions::CostFuncRwp &rwp, bool evalDeriv, bool evalHessian) {
137 const size_t n = getNDomains();
138 for (size_t i = 0; i < n; ++i) {
139 values.reset();
140 getDomainAndValues(i, domain, values);
141 if (!values) {
142 throw std::runtime_error("Rwp: undefined FunctionValues.");
143 }
144 rwp.addValDerivHessian(rwp.getFittingFunction(), domain, values, evalDeriv, evalHessian);
145 }
146}
147
148} // namespace Mantid::CurveFitting
DomainType
Type of domain to create.
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 addVal(API::FunctionDomain_sptr domain, API::FunctionValues_sptr values) const override
Add a contribution to the cost function value from the fitting function evaluated on a particular dom...
void addValDerivHessian(API::IFunction_sptr function, API::FunctionDomain_sptr domain, API::FunctionValues_sptr values, bool evalDeriv=true, bool evalHessian=true) const override
Update the cost function, derivatives and hessian by adding values calculated on a domain.
Cost function for Rwp = (sum_i (( obs_i - cal_i )/sigma_i)**2 ) / (sum_i (obs_i/sigma_i)**2)
Definition: CostFuncRwp.h:52
An implementation of SeqDomain for parallel cost function and derivatives computation.
Definition: ParDomain.h:23
An implementation of CompositeDomain.
Definition: SeqDomain.h:30
virtual void getDomainAndValues(size_t i, API::FunctionDomain_sptr &domain, API::FunctionValues_sptr &values) const
Create and return i-th domain and i-th values, (i-1)th domain is released.
Definition: SeqDomain.cpp:31
size_t size() const override
Return the number of points in the domain.
Definition: SeqDomain.cpp:17
std::vector< API::FunctionDomain_sptr > m_domain
Currently active domain.
Definition: SeqDomain.h:61
size_t m_currentIndex
Current index.
Definition: SeqDomain.h:59
virtual void additiveCostFunctionValDerivHessian(const CostFunctions::CostFuncFitting &costFunction, bool evalDeriv, bool evalHessian)
Calculate the value, first and second derivatives of an additive cost function.
Definition: SeqDomain.cpp:113
void rwpValDerivHessian(const CostFunctions::CostFuncRwp &rwp, bool evalDeriv, bool evalHessian)
Calculate the value, first and second derivatives of a RWP cost function.
Definition: SeqDomain.cpp:134
void rwpVal(const CostFunctions::CostFuncRwp &rwp)
Calculate the value of a Rwp cost function.
Definition: SeqDomain.cpp:92
std::vector< API::FunctionValues_sptr > m_values
Currently active values.
Definition: SeqDomain.h:63
void addCreator(const API::IDomainCreator_sptr &creator)
Add new domain creator.
Definition: SeqDomain.cpp:48
virtual size_t getNDomains() const
Return the number of parts in the domain.
Definition: SeqDomain.cpp:23
static SeqDomain * create(API::IDomainCreator::DomainType type)
Create an instance of SeqDomain in one of two forms: either SeqDomain for sequential domain creation ...
Definition: SeqDomain.cpp:60
virtual void additiveCostFunctionVal(const CostFunctions::CostFuncFitting &costFunction)
Calculate the value of an additive cost function.
Definition: SeqDomain.cpp:73
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< IDomainCreator > IDomainCreator_sptr
Typedef for a shared pointer to IDomainCreator.
std::shared_ptr< FunctionDomain > FunctionDomain_sptr
typedef for a shared pointer