Mantid
Loading...
Searching...
No Matches
LinearBackground.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//----------------------------------------------------------------------
12
14
15using namespace CurveFitting;
16
17using namespace Kernel;
18
19using namespace API;
20
21DECLARE_FUNCTION(LinearBackground)
22
23void LinearBackground::init() {
24 declareParameter("A0", 0.0, "coefficient for constant term");
25 declareParameter("A1", 0.0, "coefficient for linear term");
26}
27
28void LinearBackground::function1D(double *out, const double *xValues, const size_t nData) const {
29 const double a0 = getParameter("A0");
30 const double a1 = getParameter("A1");
31
32 for (size_t i = 0; i < nData; i++) {
33 out[i] = a0 + a1 * xValues[i];
34 }
35}
36
37void LinearBackground::functionDeriv1D(Jacobian *out, const double *xValues, const size_t nData) {
38 for (size_t i = 0; i < nData; i++) {
39 out->set(i, 0, 1);
40 out->set(i, 1, xValues[i]);
41 }
42}
43
49void LinearBackground::fit(const std::vector<double> &X, const std::vector<double> &Y) {
50 if (X.size() != Y.size()) {
51 throw std::runtime_error("Background fit: different array sizes");
52 }
53 size_t n = X.size();
54 if (n == 0) {
55 setParameter("A0", 0);
56 setParameter("A1", 0);
57 return;
58 } else if (n == 1) {
59 setParameter("A0", Y[0]);
60 setParameter("A1", 0);
61 return;
62 }
63 double x_mean = 0;
64 double y_mean = 0;
65 double x2_mean = 0;
66 double xy_mean = 0;
67 for (size_t i = 0; i < n; i++) {
68 double x = X[i];
69 double y = Y[i];
70 x_mean += x;
71 y_mean += y;
72 x2_mean += x * x;
73 xy_mean += x * y;
74 }
75 x_mean /= static_cast<double>(n);
76 y_mean /= static_cast<double>(n);
77 x2_mean /= static_cast<double>(n);
78 xy_mean /= static_cast<double>(n);
79
80 double a1 = (xy_mean - x_mean * y_mean) / (x2_mean - x_mean * x_mean);
81 double a0 = y_mean - a1 * x_mean;
82
83 setParameter("A0", a0);
84 setParameter("A1", a1);
85}
86
94void LinearBackground::histogram1D(double *out, double left, const double *right, const size_t nBins) const {
95
96 const double a0 = getParameter("A0");
97 const double a1 = getParameter("A1");
98
99 auto cumulFun = [a0, a1](double x) { return (a0 + 0.5 * a1 * x) * x; };
100
101 double cLeft = cumulFun(left);
102 for (size_t i = 0; i < nBins; ++i) {
103 double cRight = cumulFun(right[i]);
104 out[i] = cRight - cLeft;
105 cLeft = cRight;
106 }
107}
108
115void LinearBackground::histogramDerivative1D(Jacobian *jacobian, double left, const double *right,
116 const size_t nBins) const {
117
118 double xl = left;
119 for (size_t i = 0; i < nBins; ++i) {
120 double xr = right[i];
121 jacobian->set(i, 0, xr - xl);
122 jacobian->set(i, 1, 0.5 * (xr * xr - xl * xl));
123 xl = xr;
124 }
125}
126
127} // namespace Mantid::CurveFitting::Functions
#define DECLARE_FUNCTION(classname)
Macro for declaring a new type of function to be used with the FunctionFactory.
double left
Definition: LineProfile.cpp:80
double right
Definition: LineProfile.cpp:81
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 setParameter(size_t, const double &value, bool explicitlySet=true) override
Set i-th parameter.
double getParameter(size_t i) const override
Get i-th parameter.
Provide linear function interface to IFunction.
void function1D(double *out, const double *xValues, const size_t nData) const override
Function you want to fit to.
void histogram1D(double *out, double left, const double *right, const size_t nBins) const override
Calculate histogram data.
void functionDeriv1D(API::Jacobian *out, const double *xValues, const size_t nData) override
Derivatives of function with respect to active parameters.
void fit(const std::vector< double > &X, const std::vector< double > &Y) override
Do linear fit to the data in X and Y.
void histogramDerivative1D(API::Jacobian *jacobian, double left, const double *right, const size_t nBins) const override
Devivatives of the histogram.