21using namespace Functions;
28std::recursive_mutex FUNCTION_MAP_MUTEX;
39 assert(functionMapType.empty());
41 functionMapType[
"Gaussian"] = std::make_pair(2,
Gaussian);
42 functionMapType[
"Lorentzian"] = std::make_pair(2,
Lorentzian);
49 std::lock_guard<std::recursive_mutex> lock(FUNCTION_MAP_MUTEX);
53 if (functionMapType.empty())
55 return functionMapType;
62 auto index = functionMap.find(function.
name());
63 if (
index != functionMap.end()) {
65 return index->second.second;
75 for (
size_t i = 0; i < cf->nFunctions(); ++i) {
92 std::vector<double> &
y) {
94 size_t n = domain.
size();
95 double start = domain[0];
96 double end = domain[
n - 1];
98 auto startIter = std::lower_bound(dBegin, dBegin +
n, start);
99 auto istart =
static_cast<size_t>(std::distance(dBegin, startIter));
105 auto endIter = std::lower_bound(startIter, dBegin +
n, end);
106 auto iend =
static_cast<size_t>(std::distance(dBegin, endIter));
107 if (iend <= istart) {
115 for (
size_t i = istart; i < iend; ++i) {
135 double left = centre;
136 double right = centre;
140 if (!roots.empty()) {
141 auto iright = std::upper_bound(roots.begin(), roots.end(), centre);
142 if (iright == roots.end()) {
146 if (
static_cast<size_t>(std::distance(roots.begin(), iright)) <
n) {
150 left = *(iright -
n);
151 if (
static_cast<size_t>(std::distance(iright, roots.end())) <
n) {
154 right = *(iright +
n - 1);
181 return std::make_pair(
left - centre,
right - centre);
182 }
else if (roots.size() == 1) {
183 if (roots.front() < centre) {
184 left = roots.front();
186 right = roots.front();
189 auto iright = std::upper_bound(roots.begin(), roots.end(), centre);
190 if (iright == roots.end()) {
192 }
else if (iright == roots.begin()) {
193 right = roots.front();
195 left = *(iright - 1);
199 return std::make_pair(
left - centre,
right - centre);
208 return fabs(leftRight.second - leftRight.first) / 2;
218 if (!roots.empty()) {
219 double minDiff = der1.
width();
220 size_t n = roots.size();
222 for (
size_t i = 0; i <
n; ++i) {
223 auto dx =
fabs(roots[i] - centre);
261 g_log.
debug() <<
"Estimating parameters of BackToBackExponential\n";
262 g_log.
debug() <<
"centre= " << centre <<
'\n';
267 g_log.
debug() <<
"extent: " << xlr.first - centre <<
' ' << xlr.second - centre <<
'\n';
268 double yl = fun(xlr.first);
269 double yr = fun(xlr.second);
270 double slope = (yr - yl) / (xlr.second - xlr.first);
271 double background = yl + slope * (centre - xlr.first);
272 double height = fun(centre) - background;
274 g_log.
debug() <<
"background= " << background <<
'\n';
283 g_log.
debug() <<
"HWHM: " << hwhm.first <<
' ' << hwhm.second <<
'\n';
290 if (leftX < 3 *
sigma) {
295 if (rightX < 3 *
sigma) {
298 rightX = x0 + rightX;
307 double height1 = b2b(centre1);
309 g_log.
debug() <<
"new HWHM: " << hwhm1.first <<
' ' << hwhm1.second <<
'\n';
311 double denom = hwhm.first +
sigma;
312 double aCorr = denom > 0 ? (hwhm1.first +
sigma) / denom : 100.0;
317 denom = hwhm.second -
sigma;
318 double bCorr = denom > 0 ? (hwhm1.second -
sigma) / denom : 100.0;
323 g_log.
debug() <<
"corrections: " << aCorr <<
' ' << bCorr <<
'\n';
336 double height1 = b2b(centre1);
337 x0 += centre - centre1;
359 for (
size_t i = 0; i < cf->nFunctions(); ++i) {
360 setValues(*cf->getFunction(i), fun, der1, der2);
394 std::vector<double>
x;
395 std::vector<double>
y;
std::map< DeltaEMode::Type, std::string > index
A composite function is a function containing other functions.
Represent a domain for functions of one real argument.
size_t size() const override
Return the number of arguments in the domain.
const double * getPointerAt(size_t i) const
Get a pointer to i-th value.
A class to store values calculated by a function.
double getFitData(size_t i) const
Get a fitting data value.
This is an interface to a fitting function - a semi-abstarct class.
virtual double getParameter(size_t i) const =0
Get i-th parameter.
virtual void setParameter(size_t, const double &value, bool explicitlySet=true)=0
Set i-th parameter.
virtual std::string name() const =0
Returns the function's name.
void fix(size_t i, bool isDefault=false)
Removes a parameter i from the list of active.
virtual bool isExplicitlySet(size_t i) const =0
Checks if a parameter has been set explicitly.
Provide BackToBackExponential peak shape function interface to IPeakFunction.
Provide gaussian peak shape function interface to IPeakFunction.
Provide lorentzian peak shape function interface to IPeakFunction.
SimpleChebfun : approximates smooth 1d functions and provides methods to manipulate them.
std::vector< double > roughRoots(double level=0.0) const
Get rough estimates of the roots.
SimpleChebfun derivative() const
Create a derivative of this function.
double width() const
Get the width of the interval.
double startX() const
Start of the interval.
const std::vector< double > & xPoints() const
Get a reference to the x-points.
double endX() const
End of the interval.
size_t order() const
Order of the approximating polynomial.
LinearFunction(double a0, double a1)
double operator()(double x) const
The Logger class is in charge of the publishing messages from the framework through various channels.
void debug(const std::string &msg)
Logs at debug level.
Kernel::Logger g_log("ExperimentInfo")
static logger object
Function whichFunction(const API::IFunction &function)
Return a function code for a function if it needs setting values or None otherwise.
void setValues(API::IFunction &function, const SimpleChebfun &fun, const SimpleChebfun &der1, const SimpleChebfun &der2)
Set initial values to a function if it needs to.
bool needSettingInitialValues(const API::IFunction &function)
Test if initial values need to be set before fitting.
void MANTID_CURVEFITTING_DLL estimate(API::IFunction &function, const API::FunctionDomain1D &domain, const API::FunctionValues &values)
ParameterEstimator estimates parameter values of some fitting functions from fitting data.
void extractValues(const API::FunctionDomain1D &domain, const API::FunctionValues &values, std::vector< double > &x, std::vector< double > &y)
Extract values from domain and values objects to vectors.
std::pair< double, double > getPeakHWHM(double centre, double height, const SimpleChebfun &fun)
Get displacements from peak centre where peak reaches half the maximum.
double getPeakCentre(double centre, const SimpleChebfun &der1)
Improve an estimate of a peak centre.
std::pair< double, double > getPeakLeftRightExtent(double centre, const SimpleChebfun &der2)
Get the peak's extent on either side of the centre.
void initFunctionLookup(FunctionMapType &functionMapType)
Initializes a FunctionMapType object.
std::pair< double, double > getPeakLeftRightWidth(double centre, const SimpleChebfun &der2, size_t n=1)
Get a measure of peak's width.
const FunctionMapType & getFunctionMapType()
Returns a reference to the static functionMapType.
std::map< std::string, std::pair< size_t, Function > > FunctionMapType
void setBackToBackExponential(API::IFunction &function, const SimpleChebfun &fun, const SimpleChebfun &der1, const SimpleChebfun &der2)
Set initial values to a BackToBackExponential.
double getPeakWidth(double centre, const SimpleChebfun &der2)
Estimate a peak width from a second derivative of the data.