-
Notifications
You must be signed in to change notification settings - Fork 122
/
StretchExp.cpp
78 lines (67 loc) · 2.64 KB
/
StretchExp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidCurveFitting/Functions/StretchExp.h"
#include "MantidAPI/FunctionFactory.h"
#include <cmath>
namespace Mantid::CurveFitting::Functions {
using namespace CurveFitting;
using namespace Kernel;
using namespace API;
DECLARE_FUNCTION(StretchExp)
StretchExp::StretchExp() {
declareParameter("Height", 1.0, "Height at time zero");
declareParameter("Lifetime", 1.0, "Relaxation time of the standard exponential");
declareParameter("Stretching", 1.0, "Stretching exponent");
}
/** \relates StretchExp
* Implements the StretchExp function
* @param out :: The result of evaluating the function
* @param xValues :: function domain values
* @param nData :: size of the function domain
*/
void StretchExp::function1D(double *out, const double *xValues, const size_t nData) const {
const double h = getParameter("Height");
const double t = getParameter("Lifetime");
const double b = getParameter("Stretching");
for (size_t i = 0; i < nData; i++) {
double x = xValues[i];
if (x < 0.0) {
// although it is defined for integer b's we don't allow negative x in
// fitting
throw std::runtime_error("StretchExp is undefined for negative argument.");
}
out[i] = h * exp(-pow(x / t, b));
}
}
/** \relates StretchExp
* Calculates the derivatives of the StretchExp
* @param out :: The resulting jacobian
* @param xValues :: function domain values
* @param nData :: size of the function domain
*/
void StretchExp::functionDeriv1D(API::Jacobian *out, const double *xValues, const size_t nData) {
const double h = getParameter("Height");
const double t = getParameter("Lifetime");
const double b = getParameter("Stretching");
for (size_t i = 0; i < nData; i++) {
double x = xValues[i];
double a = pow(x / t, b);
double e = exp(-a);
out->set(i, 0, e); // derivative with respect to h
out->set(i, 1, h * a * b * e / t); // derivative with respect to t
if (x == 0.0) {
// derivative with respect to b at x = 0 is undefined, set to 0
out->set(i, 2, 0.0);
} else {
out->set(i, 2, -h * a * e * log(x / t)); // derivative with respect to b
}
}
}
} // namespace Mantid::CurveFitting::Functions