Mantid
Loading...
Searching...
No Matches
MultiDomainFunctionHelper.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 +
9
11
14 this->declareParameter("A", 0);
15 this->declareParameter("B", 0);
16 this->declareAttribute("Order", Attribute(1));
17}
18
19void MultiDomainFunctionTest_Function::function1D(double *out, const double *xValues, const size_t nData) const {
20 const double A = getParameter(0);
21 const double B = getParameter(1);
22 const int order = getAttribute("Order").asInt();
23
24 for (size_t i = 0; i < nData; ++i) {
25 double x = xValues[i];
26 switch (order) {
27 case 1:
28 out[i] = A + B * x;
29 break;
30 case 3:
31 out[i] = (A + B * x) * pow(x, 2);
32 break;
33 case 5:
34 out[i] = (A + B * x) * pow(x, 4);
35 break;
36 default:
37 throw std::runtime_error("Unknown attribute value.");
38 };
39 }
40}
42 const size_t nData) {
43 const int order = getAttribute("Order").asInt();
44 for (size_t i = 0; i < nData; ++i) {
45 double x = xValues[i];
46 switch (order) {
47 case 1:
48 out->set(i, 0, 1.0);
49 out->set(i, 1, x);
50 break;
51 case 3:
52 out->set(i, 0, pow(x, 2));
53 out->set(i, 1, pow(x, 3));
54 break;
55 case 5:
56 out->set(i, 0, pow(x, 4));
57 out->set(i, 1, pow(x, 5));
58 break;
59 default:
60 throw std::runtime_error("Unknown attribute value.");
61 };
62 }
63}
64
65std::shared_ptr<Mantid::API::MultiDomainFunction> makeMultiDomainFunction3() {
66 auto multi = std::make_shared<Mantid::API::MultiDomainFunction>();
67 multi->addFunction(std::make_shared<MultiDomainFunctionTest_Function>());
68 multi->addFunction(std::make_shared<MultiDomainFunctionTest_Function>());
69 multi->addFunction(std::make_shared<MultiDomainFunctionTest_Function>());
70
71 multi->getFunction(0)->setParameter("A", 0);
72 multi->getFunction(0)->setParameter("B", 0);
73
74 multi->getFunction(1)->setParameter("A", 0);
75 multi->getFunction(1)->setParameter("B", 0);
76
77 multi->getFunction(2)->setParameter("A", 0);
78 multi->getFunction(2)->setParameter("B", 0);
79
80 multi->clearDomainIndices();
81 multi->setDomainIndices(1, {0, 1});
82 multi->setDomainIndices(2, {0, 2});
83
84 return multi;
85}
86
87std::shared_ptr<Mantid::API::JointDomain> makeMultiDomainDomain3() {
88 auto domain = std::make_shared<Mantid::API::JointDomain>();
89 domain->addDomain(std::make_shared<Mantid::API::FunctionDomain1DVector>(0, 1, 9));
90 domain->addDomain(std::make_shared<Mantid::API::FunctionDomain1DVector>(1, 2, 10));
91 domain->addDomain(std::make_shared<Mantid::API::FunctionDomain1DVector>(2, 3, 11));
92
93 return domain;
94}
95
96const double A0 = 0.5, A1 = -4, A2 = 4;
97const double B0 = 5, B1 = -20, B2 = 16;
98const size_t nbins = 10;
99const double dX = 0.2;
100
102 Mantid::API::MatrixWorkspace_sptr ws1 = std::make_shared<WorkspaceTester>();
103 ws1->initialize(1, nbins, nbins);
104 auto &x = ws1->mutableX(0);
105 auto &y = ws1->mutableY(0);
106 const size_t numBins = y.size();
107 for (size_t i = 0; i < numBins; ++i) {
108 x[i] = -1.0 + dX * double(i);
109 const double t = x[i];
110 const double t2 = t * t;
111 y[i] = A0 + B0 * t + (A1 + B1 * t) * t2 + (A2 + B2 * t) * t2 * t2;
112 }
113
114 return ws1;
115}
116
118 Mantid::API::MatrixWorkspace_sptr ws2 = std::make_shared<WorkspaceTester>();
119 ws2->initialize(1, nbins, nbins);
120
121 auto &x = ws2->mutableX(0);
122 auto &y = ws2->mutableY(0);
123 const size_t numBins = y.size();
124 for (size_t i = 0; i < numBins; ++i) {
125 x[i] = -1.0 + dX * double(i);
126 const double t = x[i];
127 y[i] = A0 + B0 * t + (A1 + B1 * t) * t * t;
128 }
129
130 return ws2;
131}
132
134 Mantid::API::MatrixWorkspace_sptr ws3 = std::make_shared<WorkspaceTester>();
135 ws3->initialize(1, nbins, nbins);
136 auto &x = ws3->mutableX(0);
137 auto &y = ws3->mutableY(0);
138 const size_t numBins = y.size();
139 for (size_t i = 0; i < numBins; ++i) {
140 x[i] = -1.0 + dX * double(i);
141 const double t = x[i];
142 y[i] = A0 + B0 * t + (A2 + B2 * t) * t * t * t * t;
143 }
144
145 return ws3;
146}
147} // namespace Mantid::FrameworkTestHelpers
This is a specialization of IFunction for functions of one real argument.
Definition: IFunction1D.h:43
Attribute is a non-fitting parameter.
Definition: IFunction.h:282
int asInt() const
Returns int value if attribute is a int, throws exception otherwise.
Definition: IFunction.cpp:726
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
Represents the Jacobian in IFitFunction::functionDeriv.
Definition: Jacobian.h:22
virtual void set(size_t iY, size_t iP, double value)=0
Set a value to a Jacobian matrix element.
Implements the part of IFunction interface dealing with parameters.
Definition: ParamFunction.h:33
void declareParameter(const std::string &name, double initValue=0, const std::string &description="") override
Declare a new parameter.
double getParameter(size_t i) const override
Get i-th parameter.
void functionDeriv1D(Mantid::API::Jacobian *out, const double *xValues, const size_t nData) override
Derivatives of function with respect to active parameters.
void function1D(double *out, const double *xValues, const size_t nData) const override
Function you want to fit to.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< Mantid::API::JointDomain > makeMultiDomainDomain3()
std::shared_ptr< Mantid::API::MultiDomainFunction > makeMultiDomainFunction3()
Mantid::API::MatrixWorkspace_sptr makeMultiDomainWorkspace2()
Mantid::API::MatrixWorkspace_sptr makeMultiDomainWorkspace3()
Mantid::API::MatrixWorkspace_sptr makeMultiDomainWorkspace1()
Helper class which provides the Collimation Length for SANS instruments.