Mantid
Loading...
Searching...
No Matches
IFunction1D.hxx
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2016 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#pragma once
8
10#include <cmath>
11
12namespace Mantid {
13namespace API {
14
15template <typename EvaluationMethod>
16void IFunction1D::calcNumericalDerivative1D(Jacobian *jacobian, EvaluationMethod eval1D, const double *xValues,
17 const size_t nData) {
18 /*
19 * Uses the knowledge that this is a simple 1D domain to calculate the
20 * derivative. The domains are not constructed and instead function1D
21 * is called directly.
22 *
23 * There is a similar more generic method for all functions in IFunction
24 * but the method takes different parameters and uses slightly different
25 * function calls in places making it difficult to share code. Please also
26 * consider that method when updating this.
27 */
28 using std::fabs;
29
30 applyTies(); // just in case
31 std::vector<double> minusStep(nData), plusStep(nData);
32 eval1D(minusStep.data(), xValues, nData);
33
34 double step;
35 const size_t nParam = nParams();
36 for (size_t iP = 0; iP < nParam; iP++) {
37 if (isActive(iP)) {
38 const double val = activeParameter(iP);
39 step = calculateStepSize(val);
40
41 const double paramPstep = val + step;
42 setActiveParameter(iP, paramPstep);
43 applyTies();
44 eval1D(plusStep.data(), xValues, nData);
45 setActiveParameter(iP, val);
46 applyTies();
47
48 step = paramPstep - val;
49 for (size_t i = 0; i < nData; i++) {
50 jacobian->set(i, iP, (plusStep[i] - minusStep[i]) / step);
51 }
52 }
53 }
54}
55} // namespace API
56} // namespace Mantid
void calcNumericalDerivative1D(Jacobian *jacobian, EvaluationMethod func1D, const double *xValues, const size_t nData)
Calculate a numerical derivative for the 1D data.
virtual size_t nParams() const =0
Total number of parameters.
bool isActive(size_t i) const
Check if an active parameter i is actually active.
double calculateStepSize(const double parameterValue) const
Calculate step size for the given parameter value.
virtual void applyTies()
Apply the ties.
virtual double activeParameter(size_t i) const
Value of i-th active parameter.
virtual void setActiveParameter(size_t i, double value)
Set new value of i-th active parameter.
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.
Helper class which provides the Collimation Length for SANS instruments.