Mantid
Loading...
Searching...
No Matches
FullprofPolynomial.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
12using namespace CurveFitting;
13
14DECLARE_FUNCTION(FullprofPolynomial)
15
16//----------------------------------------------------------------------------------------------
19FullprofPolynomial::FullprofPolynomial() : m_n(6), m_bkpos(1.) {
20 // Declare first 6th order polynomial as default
21 for (int i = 0; i < m_n; ++i) {
22 std::string parName = "A" + std::to_string(i);
23 declareParameter(parName);
24 }
25}
26
27//----------------------------------------------------------------------------------------------
30void FullprofPolynomial::function1D(double *out, const double *xValues, const size_t nData) const {
31 // Generate a vector for all coefficient
32 std::vector<double> B(m_n, 0.0);
33 for (int i = 0; i < m_n; ++i)
34 B[i] = getParameter(i);
35
36 // Calculate
37 for (size_t i = 0; i < nData; ++i) {
38 double tof = xValues[i];
39 double x = tof / m_bkpos - 1.;
40 double pow_x = 1.;
41#if 1
42 // It is the first try as benchmark
43 double y_b = 0.;
44 for (int j = 0; j < m_n; ++j) {
45 y_b += B[j] * pow_x;
46 pow_x *= x;
47 }
48#else
49 // This is the efficient coding
50 double y_b = B[0];
51 for (size_t j = 1; j < m_n; ++j) {
52 pow_x *= x;
53 y_b += B[j] * pow_x;
54 }
55#endif
56
57 out[i] = y_b;
58 }
59}
60
61//----------------------------------------------------------------------------------------------
64void FullprofPolynomial::functionDeriv1D(API::Jacobian *out, const double *xValues, const size_t nData) {
65 for (size_t i = 0; i < nData; i++) {
66 double tof = xValues[i];
67 double x = tof / m_bkpos - 1.;
68
69 // Order 0
70 double pow_x = 1.0;
71 out->set(i, 0, pow_x);
72 // Rest
73 for (int j = 1; j < m_n; ++j) {
74 // It does dirivative to B_j
75 pow_x *= x;
76 out->set(i, j, pow_x);
77 }
78 }
79}
80
81//----------------------------------------------------------------------------------------------
85std::vector<std::string> FullprofPolynomial::getAttributeNames() const { return {"n", "Bkpos"}; }
86
87//----------------------------------------------------------------------------------------------
94 if (attName == "n") {
95 return Attribute(m_n);
96 } else if (attName == "Bkpos")
97 return Attribute(m_bkpos);
98
99 throw std::invalid_argument("Polynomial: Unknown attribute " + attName);
100}
101
102//----------------------------------------------------------------------------------------------
109void FullprofPolynomial::setAttribute(const std::string &attName, const API::IFunction::Attribute &att) {
110 if (attName == "n") {
111 // set theFullprofPolynomial order
112 int attint = att.asInt();
113 if (attint < 0) {
114 throw std::invalid_argument("Polynomial:FullprofPolynomial order cannot be negative.");
115 } else if (attint != 6 && attint != 12) {
116 throw std::runtime_error("FullprofPolynomial's order must be either 6 or 12. ");
117 } else if (attint != m_n) {
118 // Only order is (either 6 or 12) and different from current order
120
121 m_n = attint;
122 for (int i = 0; i < m_n; ++i) {
123 std::string parName = "A" + std::to_string(i);
124 declareParameter(parName);
125 }
126 }
127 } else if (attName == "Bkpos") {
128 // Background original position
129 m_bkpos = att.asDouble();
130 }
131}
132
133//----------------------------------------------------------------------------------------------
136bool FullprofPolynomial::hasAttribute(const std::string &attName) const {
137 bool has = false;
138 if (attName == "n")
139 has = true;
140 else if (attName == "Bkpos")
141 has = true;
142
143 return has;
144}
145
146} // namespace Mantid::CurveFitting::Functions
#define DECLARE_FUNCTION(classname)
Macro for declaring a new type of function to be used with the FunctionFactory.
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
double asDouble() const
Returns double value if attribute is a double, throws exception otherwise.
Definition: IFunction.cpp:739
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.
void clearAllParameters()
Nonvirtual member which removes all declared parameters.
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.
FullprofPolynomial : Polynomial background defined in Fullprof.
void functionDeriv1D(API::Jacobian *out, const double *xValues, const size_t nData) override
Function to calculate derivative analytically.
void setAttribute(const std::string &attName, const Attribute &) override
Set a value to attribute attName.
void function1D(double *out, const double *xValues, const size_t nData) const override
Function to calcualteFullprofPolynomial.
bool hasAttribute(const std::string &attName) const override
Check if attribute attName exists.
std::vector< std::string > getAttributeNames() const override
Returns a list of attribute names.
Attribute getAttribute(const std::string &attName) const override
Return a value of attribute attName.
std::string to_string(const wide_integer< Bits, Signed > &n)