Mantid
Loading...
Searching...
No Matches
ParamFunction.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 "MantidKernel/Logger.h"
13
14#include <cmath>
15#include <limits>
16#include <sstream>
17
18namespace Mantid::API {
19namespace {
20Kernel::Logger g_log("ParamFunction");
21}
22
29void ParamFunction::setParameter(size_t i, const double &value, bool explicitlySet) {
30 if (std::isnan(value)) {
31 // Check for NaN or -NaN
32 std::stringstream errmsg;
33 errmsg << "Trying to set a NaN value (" << value << ") to parameter " << this->parameterName(i);
34 throw std::invalid_argument(errmsg.str());
35 } else if (std::isinf(value)) {
36 // Infinity value
37 std::stringstream errmsg;
38 errmsg << "Trying to set an infinity value (" << value << ") to parameter " << this->parameterName(i);
39 throw std::invalid_argument(errmsg.str());
40 }
41
43 if (explicitlySet && value != m_parameters[i]) {
44 m_explicitlySet[i] = true;
45 }
47}
48
53void ParamFunction::setParameterDescription(size_t i, const std::string &description) {
55 m_parameterDescriptions[i] = description;
56}
57
62double ParamFunction::getParameter(size_t i) const {
64 return m_parameters[i];
65}
66
74void ParamFunction::setParameter(const std::string &name, const double &value, bool explicitlySet) {
75 auto it = std::find(m_parameterNames.cbegin(), m_parameterNames.cend(), name);
76 if (it == m_parameterNames.cend()) {
77 std::ostringstream msg;
78 msg << "ParamFunction tries to set value to non-exist parameter (" << name << ") "
79 << "of function " << this->name();
80 msg << "\nAllowed parameters: ";
81 for (const auto &parameterName : m_parameterNames) {
82 msg << parameterName << ", ";
83 }
84 throw std::invalid_argument(msg.str());
85 }
86 setParameter(static_cast<int>(it - m_parameterNames.begin()), value, explicitlySet);
87}
88
94void ParamFunction::setParameterDescription(const std::string &name, const std::string &description) {
95 auto it = std::find(m_parameterNames.cbegin(), m_parameterNames.cend(), name);
96 if (it == m_parameterNames.cend()) {
97 std::ostringstream msg;
98 msg << "ParamFunction tries to set description to non-exist parameter (" << name << "). ";
99 msg << "\nAllowed parameters: ";
100 for (const auto &parameterName : m_parameterNames)
101 msg << parameterName << ", ";
102 throw std::invalid_argument(msg.str());
103 }
104 setParameterDescription(static_cast<int>(it - m_parameterNames.begin()), description);
105}
106
112double ParamFunction::getParameter(const std::string &name) const {
113 auto it = std::find(m_parameterNames.cbegin(), m_parameterNames.cend(), name);
114 if (it == m_parameterNames.cend()) {
115 std::ostringstream msg;
116 msg << "ParamFunction tries to get value of non-existing parameter (" << name << ") "
117 << "to function " << this->name();
118 msg << "\nAllowed parameters: ";
119 for (const auto &parameterName : m_parameterNames)
120 msg << parameterName << ", ";
121 throw std::invalid_argument(msg.str());
122 }
123
124 double parvalue = m_parameters[it - m_parameterNames.cbegin()];
125
126 if (!std::isfinite(parvalue)) {
127 g_log.warning() << "Parameter " << name << " has a NaN or infinity value " << '\n';
128 }
129
130 return parvalue;
131}
132
138bool ParamFunction::hasParameter(const std::string &name) const {
139 return std::find(m_parameterNames.cbegin(), m_parameterNames.cend(), name) != m_parameterNames.cend();
140}
141
147size_t ParamFunction::parameterIndex(const std::string &name) const {
148 auto it = std::find(m_parameterNames.cbegin(), m_parameterNames.cend(), name);
149 if (it == m_parameterNames.cend()) {
150 std::ostringstream msg;
151 msg << "ParamFunction " << this->name() << " does not have parameter (" << name << ").";
152 throw std::invalid_argument(msg.str());
153 }
154 return std::distance(m_parameterNames.cbegin(), it);
155}
156
161std::string ParamFunction::parameterName(size_t i) const {
163 return m_parameterNames[i];
164}
165
170std::string ParamFunction::parameterDescription(size_t i) const {
172 return m_parameterDescriptions[i];
173}
174
180double ParamFunction::getError(size_t i) const {
182 return m_errors[i];
183}
184
190double ParamFunction::getError(const std::string &name) const {
191 auto it = std::find(m_parameterNames.cbegin(), m_parameterNames.cend(), name);
192 if (it == m_parameterNames.cend()) {
193 std::ostringstream msg;
194 msg << "ParamFunction tries to get error of non-existing parameter (" << name << ") "
195 << "to function " << this->name();
196 msg << "\nAllowed parameters: ";
197 for (const auto &parameterName : m_parameterNames)
198 msg << parameterName << ", ";
199 throw std::invalid_argument(msg.str());
200 }
201 return m_errors[static_cast<int>(it - m_parameterNames.begin())];
202}
203
209void ParamFunction::setError(size_t i, double err) {
211 m_errors[i] = err;
212}
213
219void ParamFunction::setError(const std::string &name, double err) {
220 auto it = std::find(m_parameterNames.cbegin(), m_parameterNames.cend(), name);
221 if (it == m_parameterNames.cend()) {
222 std::ostringstream msg;
223 msg << "ParamFunction tries to set error of non-existing parameter (" << name << ") "
224 << "to function " << this->name();
225 msg << "\nAllowed parameters: ";
226 for (const auto &parameterName : m_parameterNames)
227 msg << parameterName << ", ";
228 throw std::invalid_argument(msg.str());
229 }
230 m_errors[static_cast<int>(it - m_parameterNames.begin())] = err;
231}
232
239void ParamFunction::declareParameter(const std::string &name, double initValue, const std::string &description) {
240 auto it = std::find(m_parameterNames.cbegin(), m_parameterNames.cend(), name);
241 if (it != m_parameterNames.cend()) {
242 std::ostringstream msg;
243 msg << "ParamFunction parameter (" << name << ") already exists.";
244 throw std::invalid_argument(msg.str());
245 }
246
247 m_parameterStatus.emplace_back(Active);
248 m_parameterNames.emplace_back(name);
249 m_parameterDescriptions.emplace_back(description);
250 m_parameters.emplace_back(initValue);
251 m_errors.emplace_back(0.0);
252 m_explicitlySet.emplace_back(false);
253}
254
257 clearTies();
259 m_parameters.clear();
260 m_parameterNames.clear();
262 m_parameterStatus.clear();
263}
264
270 m_parameterStatus[i] = status;
271}
272
278 return m_parameterStatus[i];
279}
280
286 return &m_parameters[i];
287}
288
290bool ParamFunction::isExplicitlySet(size_t i) const {
292 return m_explicitlySet[i];
293}
294
301 if (ref.getLocalFunction() == this && ref.getLocalIndex() < nParams()) {
302 return ref.getLocalIndex();
303 }
304 return nParams();
305}
306
307} // namespace Mantid::API
double value
The value of the point.
Definition: FitMW.cpp:51
virtual void clearTies()
Remove all ties.
Definition: IFunction.cpp:366
virtual void clearConstraints()
Remove all constraints.
Definition: IFunction.cpp:431
ParameterStatus
Describe parameter status in relation to fitting: Active: Fit varies such parameter directly.
Definition: IFunction.h:649
virtual std::string name() const =0
Returns the function's name.
void setParameterDescription(size_t, const std::string &description) override
Set i-th parameter description.
void clearAllParameters()
Nonvirtual member which removes all declared parameters.
bool hasParameter(const std::string &name) const override
Check if function has a parameter with this name.
std::string parameterDescription(size_t i) const override
Returns the description of parameter i.
std::vector< std::string > m_parameterDescriptions
parameter descriptions
std::vector< double > m_parameters
Keeps parameter values.
double getError(size_t i) const override
Get the fitting error for a parameter.
std::vector< std::string > m_parameterNames
Keeps parameter names.
ParameterStatus getParameterStatus(size_t i) const override
Get status of parameter.
void setParameter(size_t, const double &value, bool explicitlySet=true) override
Set i-th parameter.
virtual double * getParameterAddress(size_t i)
Get the address of the parameter. For use in UserFunction with mu::Parser.
size_t parameterIndex(const std::string &name) const override
Returns the index of parameter name.
bool isExplicitlySet(size_t i) const override
Checks if a parameter has been set explicitly.
std::vector< double > m_errors
Keeps parameter errors.
void setParameterStatus(size_t i, ParameterStatus status) override
Change status of parameter.
size_t getParameterIndex(const ParameterReference &ref) const override
Return parameter index from a parameter reference.
std::string parameterName(size_t i) const override
Returns the name of parameter i.
std::vector< ParameterStatus > m_parameterStatus
Keeps status for each parameter.
void declareParameter(const std::string &name, double initValue=0, const std::string &description="") override
Declare a new parameter.
std::vector< bool > m_explicitlySet
Flags of explicitly set parameters.
size_t nParams() const override
Total number of parameters.
Definition: ParamFunction.h:53
void setError(size_t i, double err) override
Set the fitting error for a parameter.
void checkParameterIndex(size_t i) const
Check that a parameter index is in a valid range.
Definition: ParamFunction.h:94
double getParameter(size_t i) const override
Get i-th parameter.
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.
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
Kernel::Logger g_log("ExperimentInfo")
static logger object