Mantid
Loading...
Searching...
No Matches
Polynomial.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
10using namespace Mantid::Kernel;
11
12using namespace Mantid::API;
13
14using namespace std;
15
17
18using namespace CurveFitting;
19
20DECLARE_FUNCTION(Polynomial)
21
22//----------------------------------------------------------------------------------------------
26 declareParameter("A0");
27 declareAttribute("n", Attribute(0));
28}
29
30//----------------------------------------------------------------------------------------------
33void Polynomial::function1D(double *out, const double *xValues, const size_t nData) const {
34 // 1. Use a vector for all coefficient
35 vector<double> coeff(m_n + 1, 0.0);
36 for (int i = 0; i < m_n + 1; ++i)
37 coeff[i] = getParameter(i);
38
39 // 2. Calculate
40 for (size_t i = 0; i < nData; ++i) {
41 double x = xValues[i];
42 double temp = coeff[0];
43 double nx = x;
44 for (int j = 1; j <= m_n; ++j) {
45 temp += coeff[j] * nx;
46 nx *= x;
47 }
48 out[i] = temp;
49 }
50}
51
52//----------------------------------------------------------------------------------------------
55void Polynomial::functionDeriv1D(API::Jacobian *out, const double *xValues, const size_t nData) {
56 for (size_t i = 0; i < nData; i++) {
57 double x = xValues[i];
58 double nx = 1;
59 for (int j = 0; j <= m_n; ++j) {
60 out->set(i, j, nx);
61 nx *= x;
62 }
63 }
64}
65
66//----------------------------------------------------------------------------------------------
70std::vector<std::string> Polynomial::getAttributeNames() const { return {"n"}; }
71
72//----------------------------------------------------------------------------------------------
78API::IFunction::Attribute Polynomial::getAttribute(const std::string &attName) const {
79 if (attName == "n") {
80 return Attribute(m_n);
81 }
82
83 throw std::invalid_argument("Polynomial: Unknown attribute " + attName);
84}
85
86//----------------------------------------------------------------------------------------------
93void Polynomial::setAttribute(const std::string &attName, const API::IFunction::Attribute &att) {
94 if (attName == "n") {
95 // set the polynomial order
96
97 auto newN = att.asInt();
98 if (newN < 0) {
99 throw std::invalid_argument("Polynomial: polynomial order cannot be negative.");
100 }
101
102 // Save old values
103 std::vector<double> oldValues(std::min(m_n, newN) + 1);
104 for (size_t i = 0; i < oldValues.size(); ++i) {
105 oldValues[i] = getParameter(i);
106 }
107
108 if (m_n >= 0) {
110 }
111 m_n = att.asInt();
112 for (int i = 0; i <= m_n; ++i) {
113 std::string parName = "A" + std::to_string(i);
114 declareParameter(parName);
115 }
116
117 // Reset old values to new parameters
118 for (size_t i = 0; i < oldValues.size(); ++i) {
119 setParameter(i, oldValues[i]);
120 }
121 }
122}
123
124//----------------------------------------------------------------------------------------------
127bool Polynomial::hasAttribute(const std::string &attName) const { return attName == "n"; }
128
129} // namespace Mantid::CurveFitting::Functions
#define DECLARE_FUNCTION(classname)
Macro for declaring a new type of function to be used with the FunctionFactory.
Mantid::API::IFunction::Attribute Attribute
Definition: IsoRotDiff.cpp:25
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
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 setParameter(size_t, const double &value, bool explicitlySet=true) override
Set i-th parameter.
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.
Polynomial : N-th polynomial background function.
Definition: Polynomial.h:19
bool hasAttribute(const std::string &attName) const override
Check if attribute attName exists.
Definition: Polynomial.cpp:127
void setAttribute(const std::string &attName, const Attribute &) override
Set a value to attribute attName.
Definition: Polynomial.cpp:93
void function1D(double *out, const double *xValues, const size_t nData) const override
Function to calcualte polynomial.
Definition: Polynomial.cpp:33
Attribute getAttribute(const std::string &attName) const override
Return a value of attribute attName.
Definition: Polynomial.cpp:78
void functionDeriv1D(API::Jacobian *out, const double *xValues, const size_t nData) override
Function to calculate derivative analytically.
Definition: Polynomial.cpp:55
std::vector< std::string > getAttributeNames() const override
Returns a list of attribute names.
Definition: Polynomial.cpp:70
STL namespace.
std::string to_string(const wide_integer< Bits, Signed > &n)