Mantid
Loading...
Searching...
No Matches
PolynomialCorrection.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//----------------------------------------------------------------------
14#include <cmath>
15
16using namespace Mantid::API;
17using namespace Mantid::Kernel;
18
19namespace Mantid::Algorithms {
20// Register the class into the algorithm factory
21DECLARE_ALGORITHM(PolynomialCorrection)
22
24 : UnaryOperation(), m_coeffs(), m_polySize(), m_isOperationMultiply(false) {}
25
27 // We need an array property for the coefficients of the polynomial: C0 + C1*x
28 // + C2*x*x + ....
29 declareProperty(std::make_unique<ArrayProperty<double>>("Coefficients",
30 std::make_shared<MandatoryValidator<std::vector<double>>>()),
31 "Array Property containing the coefficients of the polynomial correction "
32 "function in ascending powers of X. Can be given as a comma separated "
33 "list in string form.");
34 std::vector<std::string> propOptions{"Multiply", "Divide"};
35 declareProperty("Operation", "Multiply", std::make_shared<StringListValidator>(propOptions),
36 "The operation with which the correction is applied to the "
37 "data (default: Multiply)");
38}
39
41 // So this will be m_coeffs[0] + m_coeffs[1]*x + m_coeffs[2]*x^2 + ...
42 m_coeffs = getProperty("Coefficients");
43 m_polySize = m_coeffs.size();
44 std::string operation = getProperty("Operation");
45 m_isOperationMultiply = operation == "Multiply";
46}
47
48void PolynomialCorrection::performUnaryOperation(const double XIn, const double YIn, const double EIn, double &YOut,
49 double &EOut) {
50 // The first value of the coefficients vector is for the zeroth power of X.
51 double factor = m_coeffs[0];
52 double xPow = 1.0;
53 // Build up the polynomial in ascending powers of x
54 for (std::vector<double>::size_type i = 1; i < m_polySize; ++i) {
55 xPow *= XIn;
56 factor += m_coeffs[i] * xPow;
57 }
58
59 // Apply the correction factor to the data and error
61 YOut = YIn * factor;
62 EOut = EIn * std::abs(factor);
63 } else {
64 YOut = YIn / factor;
65 EOut = EIn / std::abs(factor);
66 }
67}
68
69} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
Corrects the data and error values on a workspace by the value of a polynomial function which is eval...
void performUnaryOperation(const double XIn, const double YIn, const double EIn, double &YOut, double &EOut) override
Carries out the Unary operation on the current 'cell'.
void retrieveProperties() override
A virtual function in which additional properties should be retrieved into member variables.
void defineProperties() override
A virtual function in which additional properties of an algorithm should be declared.
bool m_isOperationMultiply
True is the operation is multiply, false.
std::vector< double >::size_type m_polySize
correction function
std::vector< double > m_coeffs
Holds the coefficients for the polynomial.
UnaryOperation supports the implementation of a Unary operation on an input workspace.
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
Validator to check that a property is not left empty.