/
LogNormal.cpp
82 lines (71 loc) · 2.74 KB
/
LogNormal.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
79
80
81
82
// 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/LogNormal.h"
#include "MantidAPI/FunctionFactory.h"
#include <cmath>
namespace Mantid::CurveFitting::Functions {
using namespace CurveFitting;
using namespace Kernel;
using namespace API;
DECLARE_FUNCTION(LogNormal)
LogNormal::LogNormal() {
declareParameter("Height", 1.0, "Overall scaling factor");
declareParameter("Location", 1.0, "Natural logarithm of the geometric mean");
declareParameter("Scale", 1.0, "Natural logarithm of the geometric standard deviation");
}
/** \relates LogNormal
* Implements the LogNormal function
* @param out :: The result of evaluating the function
* @param xValues :: function domain values
* @param nData :: size of the function domain
*/
void LogNormal::function1D(double *out, const double *xValues, const size_t nData) const {
const double h = getParameter("Height");
const double t = getParameter("Location");
const double b = getParameter("Scale");
for (size_t i = 0; i < nData; i++) {
double x = xValues[i];
if (x == 0.0) {
out[i] = 0.0; // limit of the distribution as x approaches to zero
} else {
double c = (log(x) - t) / b;
out[i] = h / x * exp(-c * c / 2);
}
}
}
/** \relates LogNormal
* Calculates the derivatives of the LogNormal
* @param out :: The resulting jacobian
* @param xValues :: function domain values
* @param nData :: size of the function domain
*/
void LogNormal::functionDeriv1D(API::Jacobian *out, const double *xValues, const size_t nData) {
const double h = getParameter("Height");
const double t = getParameter("Location");
const double b = getParameter("Scale");
for (size_t i = 0; i < nData; i++) {
double x = xValues[i];
if (x == 0.0) {
out->set(i, 0,
0.0); // all partial derivatives approach to 0 as x goes to 0
out->set(i, 1, 0.0);
out->set(i, 2, 0.0);
} else {
double c = (log(x) - t) / b;
double e = exp(-c * c / 2) / x;
out->set(i, 0, e); // partial derivative with respect to Height
out->set(i, 1,
h * e * (c / b)); // partial derivative with respect to Location parameter
out->set(i, 2,
h * e * (c * c / b)); // partial derivative with respect to Scale parameter
}
}
}
} // namespace Mantid::CurveFitting::Functions