Mantid
Loading...
Searching...
No Matches
LogNormal.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(LogNormal)
23
25 declareParameter("Height", 1.0, "Overall scaling factor");
26 declareParameter("Location", 1.0, "Natural logarithm of the geometric mean");
27 declareParameter("Scale", 1.0, "Natural logarithm of the geometric standard deviation");
28}
29
36void LogNormal::function1D(double *out, const double *xValues, const size_t nData) const {
37 const double h = getParameter("Height");
38 const double t = getParameter("Location");
39 const double b = getParameter("Scale");
40
41 for (size_t i = 0; i < nData; i++) {
42 double x = xValues[i];
43 if (x == 0.0) {
44 out[i] = 0.0; // limit of the distribution as x approaches to zero
45 } else {
46 double c = (log(x) - t) / b;
47 out[i] = h / x * exp(-c * c / 2);
48 }
49 }
50}
51
58void LogNormal::functionDeriv1D(API::Jacobian *out, const double *xValues, const size_t nData) {
59 const double h = getParameter("Height");
60 const double t = getParameter("Location");
61 const double b = getParameter("Scale");
62
63 for (size_t i = 0; i < nData; i++) {
64 double x = xValues[i];
65 if (x == 0.0) {
66 out->set(i, 0,
67 0.0); // all partial derivatives approach to 0 as x goes to 0
68 out->set(i, 1, 0.0);
69 out->set(i, 2, 0.0);
70 } else {
71 double c = (log(x) - t) / b;
72 double e = exp(-c * c / 2) / x;
73 out->set(i, 0, e); // partial derivative with respect to Height
74 out->set(i, 1,
75 h * e * (c / b)); // partial derivative with respect to Location parameter
76 out->set(i, 2,
77 h * e * (c * c / b)); // partial derivative with respect to Scale parameter
78 }
79 }
80}
81
82} // 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 Log Normal function: h*exp(-(log(x)-t)^2 / (2*b^2) )/x.
Definition: LogNormal.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.