Mantid
Loading...
Searching...
No Matches
CompositeFunction.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2009 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#pragma once
8
9//----------------------------------------------------------------------
10// Includes
11//----------------------------------------------------------------------
12#include "MantidAPI/IFunction.h"
13#include "MantidAPI/Jacobian.h"
14
15#include <map>
16
17namespace Mantid {
18namespace API {
44class MANTID_API_DLL CompositeFunction : public virtual IFunction {
45public:
48
49 /* Overriden methods */
50
52 std::string name() const override { return "CompositeFunction"; }
54 void setWorkspace(std::shared_ptr<const Workspace> ws) override;
56 void setMatrixWorkspace(std::shared_ptr<const API::MatrixWorkspace> workspace, size_t wi, double startX,
57 double endX) override;
58 void setStepSizeMethod(const StepSizeMethod stepSizeMethod) override;
59
61 void function(const FunctionDomain &domain, FunctionValues &values) const override;
63 void functionDeriv(const FunctionDomain &domain, Jacobian &jacobian) override;
64
66 void setParameter(size_t, const double &value, bool explicitlySet = true) override;
68 void setParameterDescription(size_t, const std::string &description) override;
70 [[nodiscard]] double getParameter(size_t i) const override;
72 [[nodiscard]] double getParameter(size_t i, size_t j) const;
74 void setParameter(const std::string &name, const double &value, bool explicitlySet = true) override;
76 void setParameterDescription(const std::string &name, const std::string &description) override;
78 [[nodiscard]] double getParameter(const std::string &name) const override;
80 bool hasParameter(const std::string &name) const override;
82 bool hasAttribute(const std::string &name) const override;
84 [[nodiscard]] Attribute getAttribute(const std::string &name) const override;
85 // Set an attribute value
86 void setAttribute(const std::string &name, const API::IFunction::Attribute &value) override;
87 // Register the functions usage
88 void registerFunctionUsage(bool internal) override;
90 [[nodiscard]] size_t nParams() const override;
91 // Total number of attributes, which includes global and local function
92 // attributes
93 [[nodiscard]] size_t nAttributes() const override;
94 // Total number of global attributes, defined at the composite function level
95 [[nodiscard]] size_t nGlobalAttributes() const noexcept { return IFunction::nAttributes(); }
97 [[nodiscard]] size_t parameterIndex(const std::string &name) const override;
99 [[nodiscard]] std::string parameterName(size_t i) const override;
100 [[nodiscard]] std::string parameterName(size_t i, size_t j) const;
102 [[nodiscard]] std::string attributeName(size_t i) const override;
104 [[nodiscard]] std::string parameterDescription(size_t i) const override;
106 [[nodiscard]] bool isExplicitlySet(size_t i) const override;
108 [[nodiscard]] double getError(size_t i) const override;
110 [[nodiscard]] double getError(size_t i, size_t j) const;
112 [[nodiscard]] double getError(const std::string &name) const override;
114 void setError(size_t i, double err) override;
116 void setError(const std::string &name, double err) override;
119 [[nodiscard]] double activeParameter(size_t i) const override;
122 void setActiveParameter(size_t i, double value) override;
124 void updateActive(const double *in);
126 [[nodiscard]] std::string nameOfActive(size_t i) const override;
128 [[nodiscard]] std::string descriptionOfActive(size_t i) const override;
129
131 [[nodiscard]] size_t getParameterIndex(const ParameterReference &ref) const override;
133 [[nodiscard]] IFunction_sptr getContainingFunction(const ParameterReference &ref) const;
134
136 void applyTies() override;
138 void clearTies() override;
139 // Unhide base class function: removeTie(string). Avoids Intel compiler
140 // warning
141 using IFunction::removeTie;
143 bool removeTie(size_t i) override;
145 [[nodiscard]] ParameterTie *getTie(size_t i) const override;
146
148 [[nodiscard]] IConstraint *getConstraint(size_t i) const override;
150 void setUpForFit() override;
152 void removeConstraint(const std::string &parName) override;
154 [[nodiscard]] size_t getNumberDomains() const override;
156 [[nodiscard]] std::vector<std::shared_ptr<IFunction>> createEquivalentFunctions() const override;
158 [[nodiscard]] bool hasFunction(const std::string &functionName) const;
160 [[nodiscard]] IFunction_sptr getFunction(std::size_t i) const override;
162 [[nodiscard]] std::size_t nFunctions() const override { return m_functions.size(); }
163
164 /* CompositeFunction own methods */
165
167 virtual size_t addFunction(IFunction_sptr f);
169 void removeFunction(size_t i);
171 void replaceFunction(size_t functionIndex, const IFunction_sptr &f);
173 void replaceFunctionPtr(const IFunction_sptr &f_old, const IFunction_sptr &f_new);
175 [[nodiscard]] std::size_t functionIndex(const std::string &functionName) const;
177 [[nodiscard]] std::size_t functionIndex(std::size_t i) const;
178 [[nodiscard]] std::size_t attributeFunctionIndex(std::size_t i) const;
180 [[nodiscard]] size_t parameterLocalIndex(size_t i, bool recursive = false) const;
182 [[nodiscard]] std::string parameterLocalName(size_t i, bool recursive = false) const;
184 void checkFunction();
186 void clear();
188 virtual size_t nLocalAttributes() const { return 0; }
190 virtual std::vector<std::string> getLocalAttributeNames() const { return std::vector<std::string>(); }
192 virtual Attribute getLocalAttribute(size_t i, const std::string &attName) const {
193 (void)i;
194 throw std::invalid_argument("Attribute " + attName + " not found in function " + this->name());
195 }
197 virtual void setLocalAttribute(size_t i, const std::string &attName, const Attribute &) {
198 (void)i;
199 throw std::invalid_argument("Attribute " + attName + " not found in function " + this->name());
200 }
202 virtual bool hasLocalAttribute(const std::string &) const { return false; }
203 template <typename T> void setLocalAttributeValue(size_t i, const std::string &attName, const T &value) {
204 setLocalAttribute(i, attName, Attribute(value));
205 }
206 void setLocalAttributeValue(size_t i, const std::string &attName, const char *value) {
207 setLocalAttribute(i, attName, Attribute(std::string(value)));
208 }
210 void setParameterStatus(size_t i, ParameterStatus status) override;
212 ParameterStatus getParameterStatus(size_t i) const override;
214 static std::pair<std::string, size_t> parseName(const std::string &varName);
215
216protected:
218 void init() override;
220 void declareParameter(const std::string &name, double initValue = 0, const std::string &description = "") override;
221
223 void declareAttribute(const std::string &name, const API::IFunction::Attribute &defaultValue);
225 void declareAttribute(const std::string &name, const API::IFunction::Attribute &defaultValue,
226 const Kernel::IValidator &validator);
228 std::string writeToString(const std::string &parentLocalAttributesStr = "") const override;
229
230 size_t paramOffset(size_t i) const { return m_paramOffsets[i]; }
231
232private:
233 // get attribute offset from attribute index
234 size_t getAttributeOffset(size_t attributeIndex) const;
236 std::vector<IFunction_sptr> m_functions;
240 std::vector<size_t> m_paramOffsets;
243 std::vector<size_t> m_IFunction;
244 // Attribute index vector, where the index is the attribute number and the
245 // value is the containing function e.g the attributes in function 1, {A1},
246 // are given by A1 = {index for index < m_attributeIndex.size() |
247 // m_attributeIndex[index] = 1}
248 std::vector<size_t> m_attributeIndex;
250 size_t m_nParams;
254 mutable size_t m_iConstraintFunction;
255 // Global attributes
256 void createDefaultGlobalAttributes();
257 std::vector<std::string> m_globalAttributeNames;
258};
259
261using CompositeFunction_sptr = std::shared_ptr<CompositeFunction>;
263using CompositeFunction_const_sptr = std::shared_ptr<const CompositeFunction>;
264
267class PartialJacobian : public Jacobian {
269 size_t m_iY0;
271 size_t m_iP0;
273public:
279 PartialJacobian(Jacobian *J, size_t iP0)
280 : m_J(J), m_iY0(0), m_iP0(iP0) //,m_iaP0(iap0)
281 {}
287 PartialJacobian(Jacobian *J, size_t iY0, size_t iP0) : m_J(J), m_iY0(iY0), m_iP0(iP0) {}
294 void set(size_t iY, size_t iP, double value) override { m_J->set(m_iY0 + iY, m_iP0 + iP, value); }
300 double get(size_t iY, size_t iP) override { return m_J->get(m_iY0 + iY, m_iP0 + iP); }
303 void zero() override {
304 throw Kernel::Exception::NotImplementedError("zero() is not implemented for PartialJacobian");
305 }
310 void addNumberToColumn(const double &value, const size_t &iP) override { m_J->addNumberToColumn(value, m_iP0 + iP); }
311};
312
313} // namespace API
314} // namespace Mantid
double value
The value of the point.
Definition: FitMW.cpp:51
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
Mantid::API::IFunction::Attribute Attribute
Definition: IsoRotDiff.cpp:25
A composite function is a function containing other functions.
void updateActive(const double *in)
Update parameters after a fitting iteration.
virtual size_t nLocalAttributes() const
Returns the number of attributes associated with the function.
size_t m_nParams
Total number of parameters.
virtual std::vector< std::string > getLocalAttributeNames() const
Returns a list of attribute names.
size_t nGlobalAttributes() const noexcept
std::vector< size_t > m_attributeIndex
size_t paramOffset(size_t i) const
size_t m_nAttributes
Total number of attributes.
std::vector< size_t > m_paramOffsets
Individual function parameter offsets (function index in m_functions) e.g.
std::size_t nFunctions() const override
Number of functions.
void setLocalAttributeValue(size_t i, const std::string &attName, const T &value)
size_t m_iConstraintFunction
Function counter to be used in nextConstraint.
std::vector< std::string > m_globalAttributeNames
void setLocalAttributeValue(size_t i, const std::string &attName, const char *value)
virtual bool hasLocalAttribute(const std::string &) const
Check if attribute attName exists.
std::string name() const override
Returns the function's name.
std::vector< size_t > m_IFunction
Keeps the function index for each declared parameter (parameter declared index)
virtual Attribute getLocalAttribute(size_t i, const std::string &attName) const
Return a value of attribute attName.
std::vector< IFunction_sptr > m_functions
Pointers to the included functions.
virtual void setLocalAttribute(size_t i, const std::string &attName, const Attribute &)
Set a value to attribute attName.
Base class that represents the domain of a function.
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
This is an interface to a fitting function - a semi-abstarct class.
Definition: IFunction.h:163
Represents the Jacobian in IFitFunction::functionDeriv.
Definition: Jacobian.h:22
virtual double get(size_t iY, size_t iP)=0
Get the value to a Jacobian matrix element.
virtual void set(size_t iY, size_t iP, double value)=0
Set a value to a Jacobian matrix element.
A reference to a parameter in a function.
Ties fitting parameters.
Definition: ParameterTie.h:35
A Jacobian for individual functions.
size_t m_iY0
fitting data index offset in the overall Jacobian for a
void zero() override
Zero all matrix elements.
size_t m_iP0
particular function
double get(size_t iY, size_t iP) override
Overridden Jacobian::get(...).
PartialJacobian(Jacobian *J, size_t iY0, size_t iP0)
Constructor.
PartialJacobian(Jacobian *J, size_t iP0)
particular function
Jacobian * m_J
pointer to the overall Jacobian
void addNumberToColumn(const double &value, const size_t &iP) override
Add number to all iY (data) Jacobian elements for a given iP (parameter)
void set(size_t iY, size_t iP, double value) override
Overridden Jacobian::set(...).
Marks code as not implemented yet.
Definition: Exception.h:138
IValidator is the basic interface for all validators for properties.
Definition: IValidator.h:43
std::shared_ptr< const CompositeFunction > CompositeFunction_const_sptr
shared pointer to the composite function base class (const version)
std::shared_ptr< IFunction > IFunction_sptr
shared pointer to the function base class
Definition: IFunction.h:732
std::shared_ptr< CompositeFunction > CompositeFunction_sptr
shared pointer to the composite function base class
Helper class which provides the Collimation Length for SANS instruments.