Mantid
Loading...
Searching...
No Matches
PowerLaw.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2021 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
12
13#include <cmath>
14
16
17using namespace CurveFitting;
18
19using namespace Kernel;
20
21using namespace API;
22
23DECLARE_FUNCTION(PowerLaw)
24
25void PowerLaw::init() {
26 declareParameter("Magnitude", 1.0, "coefficient for linear term");
27 declareParameter("Exponent", 1.0, "exponent");
28 declareParameter("Constant", 0.0, "coefficient for constant term");
29}
30
31void PowerLaw::function1D(double *out, const double *xValues, const size_t nData) const {
32 const double a = getParameter("Magnitude");
33 const double b = getParameter("Exponent");
34 const double c = getParameter("Constant");
35
36 for (size_t i = 0; i < nData; i++) {
37 out[i] = c + a * pow(xValues[i], b);
38 }
39}
40
41void PowerLaw::functionDeriv1D(Jacobian *out, const double *xValues, const size_t nData) {
42 const double a = getParameter("Magnitude");
43 const double b = getParameter("Exponent");
44
45 for (size_t i = 0; i < nData; i++) {
46 double diffa = pow(xValues[i], b);
47 double diffb = a * pow(xValues[i], b) * log(xValues[i]);
48 out->set(i, 0, diffa);
49 out->set(i, 1, diffb);
50 out->set(i, 2, 1);
51 }
52}
53
54} // 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 Power Law function interface to IFunction.
Definition: PowerLaw.h:28
void function1D(double *out, const double *xValues, const size_t nData) const override
Function you want to fit to.
Definition: PowerLaw.cpp:31
void functionDeriv1D(API::Jacobian *out, const double *xValues, const size_t nData) override
Derivatives of function with respect to active parameters.
Definition: PowerLaw.cpp:41