Mantid
Loading...
Searching...
No Matches
FunctionGenerator.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#include <utility>
8
12
13namespace Mantid::API {
14
15using namespace Kernel;
16
18FunctionGenerator::FunctionGenerator(const IFunction_sptr &source) : m_source(source), m_dirty(true) {
19 if (source) {
20 m_nOwnParams = source->nParams();
21 }
22 declareAttribute("NumDeriv", Attribute(false));
23}
24
26
29void FunctionGenerator::setSource(IFunction_sptr source) const { m_source = std::move(source); }
30
32void FunctionGenerator::setParameter(size_t i, const double &value, bool explicitlySet) {
33 if (i < m_nOwnParams) {
34 m_source->setParameter(i, value, explicitlySet);
35 m_dirty = true;
36 } else {
38 m_target->setParameter(i - m_nOwnParams, value, explicitlySet);
39 }
40}
41
43void FunctionGenerator::setParameterDescription(size_t i, const std::string &description) {
44 if (i < m_nOwnParams) {
45 m_source->setParameterDescription(i, description);
46 } else {
48 m_target->setParameterDescription(i - m_nOwnParams, description);
49 }
50}
51
53double FunctionGenerator::getParameter(size_t i) const {
54 if (i < m_nOwnParams) {
55 return m_source->getParameter(i);
56 } else {
58 return m_target->getParameter(i - m_nOwnParams);
59 }
60}
61
63bool FunctionGenerator::hasParameter(const std::string &name) const {
64 if (isSourceName(name)) {
65 return m_source->hasParameter(name);
66 } else {
68 return m_target->hasParameter(name);
69 }
70}
71
73void FunctionGenerator::setParameter(const std::string &name, const double &value, bool explicitlySet) {
74 auto i = parameterIndex(name);
75 setParameter(i, value, explicitlySet);
76}
77
79void FunctionGenerator::setParameterDescription(const std::string &name, const std::string &description) {
80 auto i = parameterIndex(name);
81 setParameterDescription(i, description);
82}
83
85double FunctionGenerator::getParameter(const std::string &name) const {
86 auto i = parameterIndex(name);
87 return getParameter(i);
88}
89
93 return m_source->nParams() + m_target->nParams();
94}
95
97size_t FunctionGenerator::parameterIndex(const std::string &name) const {
98 if (isSourceName(name)) {
99 return m_source->parameterIndex(name);
100 } else {
102 return m_target->parameterIndex(name) + m_nOwnParams;
103 }
104}
105
107std::string FunctionGenerator::parameterName(size_t i) const {
108 if (i < m_nOwnParams) {
109 return m_source->parameterName(i);
110 } else {
112 return m_target->parameterName(i - m_nOwnParams);
113 }
114}
115
117std::string FunctionGenerator::parameterDescription(size_t i) const {
118 if (i < m_nOwnParams) {
119 return m_source->parameterDescription(i);
120 } else {
122 return m_target->parameterDescription(i - m_nOwnParams);
123 }
124}
125
128 if (i < m_nOwnParams) {
129 return m_source->isExplicitlySet(i);
130 } else {
132 return m_target->isExplicitlySet(i - m_nOwnParams);
133 }
134}
135
137double FunctionGenerator::getError(size_t i) const {
138 if (i < m_nOwnParams) {
139 return m_source->getError(i);
140 } else {
142 return m_target->getError(i - m_nOwnParams);
143 }
144}
145
147double FunctionGenerator::getError(const std::string &name) const {
148 auto index = parameterIndex(name);
149 return getError(index);
150}
151
153void FunctionGenerator::setError(size_t i, double err) {
154 if (i < m_nOwnParams) {
155 m_source->setError(i, err);
156 } else {
158 m_target->setError(i - m_nOwnParams, err);
159 }
160}
161
163void FunctionGenerator::setError(const std::string &name, double err) {
164 auto index = parameterIndex(name);
165 setError(index, err);
166}
167
170 if (i < m_nOwnParams) {
171 m_source->setParameterStatus(i, status);
172 } else {
174 m_target->setParameterStatus(i - m_nOwnParams, status);
175 }
176}
177
180 if (i < m_nOwnParams) {
181 return m_source->getParameterStatus(i);
182 } else {
184 return m_target->getParameterStatus(i - m_nOwnParams);
185 }
186}
187
190 if (ref.getLocalFunction() == this) {
191 auto index = ref.getLocalIndex();
192 auto np = nParams();
193 if (index < np) {
194 return index;
195 }
196 return np;
197 }
199 return m_target->getParameterIndex(ref) + m_nOwnParams;
200}
201
206}
207
209void FunctionGenerator::declareParameter(const std::string & /*name*/, double /*initValue*/,
210 const std::string & /*description*/) {
211 throw Kernel::Exception::NotImplementedError("FunctionGenerator cannot not have its own parameters.");
212}
213
217 return IFunction::nAttributes() + m_source->nAttributes() + m_target->nAttributes();
218}
219
221std::vector<std::string> FunctionGenerator::getAttributeNames() const {
224}
225
227IFunction::Attribute FunctionGenerator::getAttribute(const std::string &attName) const {
228 if (IFunction::hasAttribute(attName)) {
229 return IFunction::getAttribute(attName);
230 } else if (isSourceName(attName)) {
231 return m_source->getAttribute(attName);
232 } else {
234 return m_target->getAttribute(attName);
235 }
236}
237
239void FunctionGenerator::setAttribute(const std::string &attName, const IFunction::Attribute &att) {
240 if (IFunction::hasAttribute(attName)) {
241 IFunction::setAttribute(attName, att);
242 m_dirty = true;
243 m_target.reset();
244 } else if (isSourceName(attName)) {
245 m_source->setAttribute(attName, att);
246 m_dirty = true;
247 } else {
249 m_target->setAttribute(attName, att);
250 }
251}
252
254bool FunctionGenerator::hasAttribute(const std::string &attName) const {
255 if (IFunction::hasAttribute(attName)) {
256 return true;
257 }
258 if (isSourceName(attName)) {
259 return m_source->hasAttribute(attName);
260 } else {
262 return m_target->hasAttribute(attName);
263 }
264}
273std::string FunctionGenerator::attributeName(size_t i) const {
274 if (i < IFunction::nAttributes()) {
275 return IFunction::attributeName(i);
276 } else if (i < IFunction::nAttributes() + m_source->nAttributes()) {
277 return m_source->attributeName(i - IFunction::nAttributes());
278 } else if (i < nAttributes()) {
279 return m_target->attributeName(i - (IFunction::nAttributes() + m_source->nAttributes()));
280 } else {
281 throw(std::runtime_error("Attribute index out of range"));
282 }
283}
284
285// Evaluates the function
288 if (!m_target) {
289 throw std::logic_error("FunctionGenerator failed to generate target function.");
290 }
291 m_target->function(domain, values);
292}
293
296bool FunctionGenerator::isSourceName(const std::string &aName) const {
297 if (aName.empty()) {
298 throw std::invalid_argument("Parameter or attribute name cannot be empty string.");
299 }
300 return (aName.front() != 'f' || aName.find('.') == std::string::npos);
301}
302
305 if (m_dirty) {
307 }
308 if (!m_target) {
309 throw std::logic_error("FunctionGenerator failed to generate target function.");
310 }
311}
312
315 auto tie = IFunction::getTie(i);
316 if (!tie) {
317 return nullptr;
318 }
319 if (i < m_nOwnParams) {
320 tie = m_source->getTie(i);
321 } else {
323 tie = m_target->getTie(i - m_nOwnParams);
324 }
325 return tie;
326}
327
330 auto constraint = IFunction::getConstraint(i);
331 if (constraint == nullptr) {
332 if (i < m_nOwnParams) {
333 constraint = m_source->getConstraint(i);
334 } else {
336 constraint = m_target->getConstraint(i - m_nOwnParams);
337 }
338 }
339 return constraint;
340}
341
342} // namespace Mantid::API
double value
The value of the point.
Definition: FitMW.cpp:51
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
Base class that represents the domain of a function.
ParameterTie * getTie(size_t i) const override
Get the tie for i-th parameter.
IFunction_sptr m_source
Function that calculates parameters of the target function.
IConstraint * getConstraint(size_t i) const override
Get the i-th constraint.
size_t parameterIndex(const std::string &name) const override
Returns the index of parameter name.
void setError(size_t i, double err) override
Set the fitting error for a parameter.
void init() override
overwrite IFunction base class method, which declare function parameters
double getParameter(size_t i) const override
Get i-th parameter.
void function(const FunctionDomain &domain, FunctionValues &values) const override
Evaluate the function.
virtual bool isSourceName(const std::string &aName) const
Test if a name (parameter's or attribute's) belongs to m_source.
void setParameter(size_t, const double &value, bool explicitlySet=true) override
Set i-th parameter.
void setParameterStatus(size_t i, ParameterStatus status) override
Change status of parameter.
void setAttribute(const std::string &name, const Attribute &) override
Set a value to attribute attName.
void declareParameter(const std::string &name, double initValue=0, const std::string &description="") override
Declare a new parameter.
std::vector< std::string > getAttributeNames() const override
Returns a list of attribute names.
bool hasAttribute(const std::string &name) const override
Check if attribute attName exists.
std::string parameterName(size_t i) const override
Returns the name of parameter i.
bool isExplicitlySet(size_t i) const override
Checks if a parameter has been set explicitly.
size_t nParams() const override
Total number of parameters.
IFunction_sptr m_target
Function that actually calculates the output.
virtual void updateTargetFunction() const =0
Update target function.
bool m_dirty
Flag indicating that updateTargetFunction() is required.
void setUpForFit() override
Set up the function for a fit.
void checkTargetFunction() const
Update target function if necessary.
double getError(size_t i) const override
Get the fitting error for a parameter.
size_t getParameterIndex(const ParameterReference &ref) const override
Return parameter index from a parameter reference.
void setSource(IFunction_sptr source) const
Set the source function.
size_t nAttributes() const override
Returns the number of attributes associated with the function.
std::string parameterDescription(size_t i) const override
Returns the description of parameter i.
bool hasParameter(const std::string &name) const override
Check if function has a parameter with this name.
Attribute getAttribute(const std::string &name) const override
Return a value of attribute attName.
size_t m_nOwnParams
Cached number of parameters in m_source.
void setParameterDescription(size_t, const std::string &description) override
Set i-th parameter description.
ParameterStatus getParameterStatus(size_t i) const override
Get status of parameter.
std::string attributeName(size_t i) const override
Return the name of the ith attribute.
A class to store values calculated by a function.
An interface to a constraint.
Definition: IConstraint.h:26
Attribute is a non-fitting parameter.
Definition: IFunction.h:282
virtual Attribute getAttribute(const std::string &name) const
Return a value of attribute attName.
Definition: IFunction.cpp:1394
void declareAttribute(const std::string &name, const API::IFunction::Attribute &defaultValue)
Declare a single attribute.
Definition: IFunction.cpp:1418
ParameterStatus
Describe parameter status in relation to fitting: Active: Fit varies such parameter directly.
Definition: IFunction.h:649
virtual void tie(const std::string &parName, const std::string &expr, bool isDefault=false)
Tie a parameter to other parameters (or a constant)
Definition: IFunction.cpp:214
virtual ParameterTie * getTie(size_t i) const
Get the tie of i-th parameter.
Definition: IFunction.cpp:356
virtual void setAttribute(const std::string &name, const Attribute &)
Set a value to attribute attName.
Definition: IFunction.cpp:1409
friend class FunctionGenerator
Definition: IFunction.h:701
virtual void setUpForFit()
Set up the function for a fit.
Definition: IFunction.cpp:433
virtual std::vector< std::string > getAttributeNames() const
Returns a list of attribute names.
Definition: IFunction.cpp:1368
virtual std::string attributeName(size_t index) const
Get name of ith attribute.
Definition: IFunction.cpp:1382
virtual std::string name() const =0
Returns the function's name.
virtual bool hasAttribute(const std::string &name) const
Check if attribute attName exists.
Definition: IFunction.cpp:1339
virtual size_t nAttributes() const
Returns the number of attributes associated with the function.
Definition: IFunction.cpp:1336
virtual IConstraint * getConstraint(size_t i) const
Get constraint of i-th parameter.
Definition: IFunction.cpp:392
A reference to a parameter in a function.
std::size_t getLocalIndex() const
Return parameter index in the local function.
IFunction * getLocalFunction() const
Return pointer to the local function.
Ties fitting parameters.
Definition: ParameterTie.h:35
Marks code as not implemented yet.
Definition: Exception.h:138
std::shared_ptr< IFunction > IFunction_sptr
shared pointer to the function base class
Definition: IFunction.h:732