Mantid
Loading...
Searching...
No Matches
StretchExp.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#include <cmath>
13
15
16using namespace CurveFitting;
17
18using namespace Kernel;
19
20using namespace API;
21
22DECLARE_FUNCTION(StretchExp)
23
25 declareParameter("Height", 1.0, "Height at time zero");
26 declareParameter("Lifetime", 1.0, "Relaxation time of the standard exponential");
27 declareParameter("Stretching", 1.0, "Stretching exponent");
28}
29
36void StretchExp::function1D(double *out, const double *xValues, const size_t nData) const {
37 const double h = getParameter("Height");
38 const double t = getParameter("Lifetime");
39 const double b = getParameter("Stretching");
40
41 for (size_t i = 0; i < nData; i++) {
42 double x = xValues[i];
43 if (x < 0.0) {
44 // although it is defined for integer b's we don't allow negative x in
45 // fitting
46 throw std::runtime_error("StretchExp is undefined for negative argument.");
47 }
48 out[i] = h * exp(-pow(x / t, b));
49 }
50}
51
58void StretchExp::functionDeriv1D(API::Jacobian *out, const double *xValues, const size_t nData) {
59 const double h = getParameter("Height");
60 const double t = getParameter("Lifetime");
61 const double b = getParameter("Stretching");
62
63 for (size_t i = 0; i < nData; i++) {
64 double x = xValues[i];
65 double a = pow(x / t, b);
66 double e = exp(-a);
67 out->set(i, 0, e); // derivative with respect to h
68 out->set(i, 1, h * a * b * e / t); // derivative with respect to t
69 if (x == 0.0) {
70 // derivative with respect to b at x = 0 is undefined, set to 0
71 out->set(i, 2, 0.0);
72 } else {
73 out->set(i, 2, -h * a * e * log(x / t)); // derivative with respect to b
74 }
75 }
76}
77
78} // namespace Mantid::CurveFitting::Functions
#define DECLARE_FUNCTION(classname)
Macro for declaring a new type of function to be used with the FunctionFactory.
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.
double getParameter(size_t i) const override
Get i-th parameter.
Provide Streteched Exponential fitting function: h*exp(-(x/t)^b )
Definition: StretchExp.h:25
void function1D(double *out, const double *xValues, const size_t nData) const override
Function you want to fit to.
void functionDeriv1D(API::Jacobian *out, const double *xValues, const size_t nData) override
Derivatives of function with respect to active parameters.