Mantid
Loading...
Searching...
No Matches
ChebfunBase.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2007 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#pragma once
8
9#include "MantidCurveFitting/DllConfig.h"
11
12#include <functional>
13#include <memory>
14#include <vector>
15
16namespace Mantid {
17
18namespace API {
19class IFunction;
20}
21
22namespace CurveFitting {
23namespace Functions {
24
26using ChebfunFunctionType = std::function<double(double)>;
27
66class MANTID_CURVEFITTING_DLL ChebfunBase {
67public:
68 ChebfunBase(size_t n, double start, double end, double tolerance = 0.0);
70 ChebfunBase(const ChebfunBase &) = default;
72 ChebfunBase(ChebfunBase &&) noexcept = default;
74 size_t order() const { return m_n; }
76 size_t size() const { return m_x.size(); }
78 double startX() const { return m_x.front(); }
80 double endX() const { return m_x.back(); }
82 double width() const { return endX() - startX(); }
84 const std::vector<double> &xPoints() const { return m_x; }
86 const std::vector<double> &integrationWeights() const;
88 double integrate(const std::vector<double> &p) const;
90 std::vector<double> calcA(const std::vector<double> &p) const;
92 std::vector<double> calcP(const std::vector<double> &a) const;
94 std::vector<double> fit(ChebfunFunctionType f) const;
96 std::vector<double> fit(const API::IFunction &f) const;
97
99 double eval(double x, const std::vector<double> &p) const;
101 void evalVector(const std::vector<double> &x, const std::vector<double> &p, std::vector<double> &res) const;
103 std::vector<double> evalVector(const std::vector<double> &x, const std::vector<double> &p) const;
105 void derivative(const std::vector<double> &a, std::vector<double> &aout) const;
107 std::shared_ptr<ChebfunBase> integral(const std::vector<double> &a, std::vector<double> &aout) const;
109 std::vector<double> roots(const std::vector<double> &a) const;
110
112 static std::shared_ptr<ChebfunBase> bestFit(double start, double end, ChebfunFunctionType, std::vector<double> &p,
113 std::vector<double> &a, double maxA = 0.0, double tolerance = 0.0,
114 size_t maxSize = 0);
116 static std::shared_ptr<ChebfunBase> bestFit(double start, double end, const API::IFunction &, std::vector<double> &p,
117 std::vector<double> &a, double maxA = 0.0, double tolerance = 0.0,
118 size_t maxSize = 0);
120 double tolerance() { return m_tolerance; }
121
124 template <class FunctionType>
125 static std::shared_ptr<ChebfunBase>
126 bestFitAnyTolerance(double start, double end, FunctionType f, std::vector<double> &p, std::vector<double> &a,
127 double maxA = 0.0, double tolerance = 0.0, size_t maxSize = 0);
128
130 std::vector<double> linspace(size_t n) const;
131 std::vector<double> smooth(const std::vector<double> &xvalues, const std::vector<double> &yvalues) const;
132
133private:
135 ChebfunBase &operator=(const ChebfunBase &other) = delete;
138 void calcX();
140 void calcIntegrationWeights() const;
141
143 std::vector<double> fitOdd(const ChebfunFunctionType &f, std::vector<double> &p) const;
145 std::vector<double> fitOdd(const API::IFunction &f, std::vector<double> &pEven) const;
147 static bool hasConverged(const std::vector<double> &a, double maxA, double tolerance, size_t shift = 0);
149 template <class FunctionType>
150 static std::shared_ptr<ChebfunBase> bestFitTempl(double start, double end, FunctionType f, std::vector<double> &p,
151 std::vector<double> &a, double maxA, double tolerance,
152 size_t maxSize);
153
155 const double m_tolerance;
157 size_t m_n;
159 double m_start;
161 double m_end;
163 std::vector<double> m_x;
165 std::vector<double> m_bw;
167 mutable std::vector<double> m_integrationWeights;
169 static const double g_tolerance;
171 static const size_t g_maxNumberPoints;
172};
173
174using ChebfunBase_sptr = std::shared_ptr<ChebfunBase>;
175
177template <class FunctionType>
178std::shared_ptr<ChebfunBase> ChebfunBase::bestFitAnyTolerance(double start, double end, FunctionType f,
179 std::vector<double> &p, std::vector<double> &a,
180 double maxA, double tolerance, size_t maxSize) {
181 if (tolerance == 0.0)
183
184 double tol = tolerance;
185 while (tol < 0.1) {
186 auto base = bestFit(start, end, f, p, a, maxA, tol, maxSize);
187 if (base) {
188 return base;
189 }
190 tol *= 100.0;
191 }
192 return ChebfunBase_sptr();
193}
194
195} // namespace Functions
196} // namespace CurveFitting
197} // namespace Mantid
double tolerance
This is an interface to a fitting function - a semi-abstarct class.
Definition: IFunction.h:163
The ChebfunBase class provides a base for function approximation with Chebyshev polynomials.
Definition: ChebfunBase.h:66
size_t size() const
Get the size of the base which is the number of x-points.
Definition: ChebfunBase.h:76
double m_start
Start of the interval.
Definition: ChebfunBase.h:159
ChebfunBase(ChebfunBase &&) noexcept=default
Move constructor.
ChebfunBase & operator=(const ChebfunBase &other)=delete
Private assingment operator to stress the immutability of ChebfunBase.
std::vector< double > m_x
The x-points.
Definition: ChebfunBase.h:163
std::vector< double > m_bw
The barycentric weights.
Definition: ChebfunBase.h:165
static const size_t g_maxNumberPoints
Maximum number of (x) points in a base.
Definition: ChebfunBase.h:171
static const double g_tolerance
Maximum tolerance in comparing doubles.
Definition: ChebfunBase.h:169
double width() const
Get the width of the interval.
Definition: ChebfunBase.h:82
double endX() const
End of the interval.
Definition: ChebfunBase.h:80
std::vector< double > m_integrationWeights
The integration weights.
Definition: ChebfunBase.h:167
ChebfunBase & operator=(ChebfunBase &&other)=delete
double startX() const
Start of the interval.
Definition: ChebfunBase.h:78
static std::shared_ptr< ChebfunBase > bestFit(double start, double end, ChebfunFunctionType, std::vector< double > &p, std::vector< double > &a, double maxA=0.0, double tolerance=0.0, size_t maxSize=0)
Fit a function until full convergence.
const std::vector< double > & xPoints() const
Get a reference to the x-points.
Definition: ChebfunBase.h:84
static std::shared_ptr< ChebfunBase > bestFitAnyTolerance(double start, double end, FunctionType f, std::vector< double > &p, std::vector< double > &a, double maxA=0.0, double tolerance=0.0, size_t maxSize=0)
Find best fit with highest possible tolerance (to be used with noisy data).
Definition: ChebfunBase.h:178
static std::shared_ptr< ChebfunBase > bestFitTempl(double start, double end, FunctionType f, std::vector< double > &p, std::vector< double > &a, double maxA, double tolerance, size_t maxSize)
Templated implementation of bestFit method.
double tolerance()
Tolerance for comparing doubles.
Definition: ChebfunBase.h:120
ChebfunBase(const ChebfunBase &)=default
Copy constructor.
const double m_tolerance
Actual tolerance in comparing doubles.
Definition: ChebfunBase.h:155
std::shared_ptr< ChebfunBase > ChebfunBase_sptr
Definition: ChebfunBase.h:174
std::function< double(double)> ChebfunFunctionType
Type of the approximated function.
Definition: ChebfunBase.h:26
double integral(double func(const double, const double, const double), const double a, const double b, const double g, const double w0)
Helper class which provides the Collimation Length for SANS instruments.