Mantid
Loading...
Searching...
No Matches
UserFunction.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//----------------------------------------------------------------------
14#include <boost/tokenizer.hpp>
15
17
18using namespace CurveFitting;
19
20// Register the class into the function factory
21DECLARE_FUNCTION(UserFunction)
22
24using namespace Kernel;
25using namespace API;
26
28UserFunction::UserFunction() : m_parser(new mu::Parser()), m_x(0.), m_x_set(false) { extraOneVarFunctions(*m_parser); }
29
32
37double *UserFunction::AddVariable(const char *varName, void *pufun) {
38 UserFunction &fun = *reinterpret_cast<UserFunction *>(pufun);
39
40 if (std::string(varName) != "x") {
41 try {
42 fun.declareParameter(varName, 0.0);
43 } catch (...) {
44 }
45 } else {
46 fun.m_x_set = true;
47 fun.m_x = 0.;
48 }
49
50 return &fun.m_x;
51}
52
58void UserFunction::setAttribute(const std::string &attName, const Attribute &value) {
60
61 if (attName != "Formula") {
62 return;
63 }
64
65 m_x_set = false;
67
68 try {
69 mu::Parser tmp_parser;
70 extraOneVarFunctions(tmp_parser);
71 tmp_parser.SetVarFactory(AddVariable, this);
72
73 m_formula = value.asString();
74 tmp_parser.SetExpr(m_formula);
75
76 // Call Eval() to implicitly initialize the variables
77 tmp_parser.Eval();
78
79 } catch (...) {
80 // Formula may be edited by a GUI component
81 return;
82 }
83
84 if (!m_x_set) {
85 // Formula may be edited by a GUI component
86 return;
87 }
88
89 m_parser->ClearVar();
90 m_parser->DefineVar("x", &m_x);
91 for (size_t i = 0; i < nParams(); i++) {
93 }
94
95 m_parser->SetExpr(m_formula);
96}
97
105void UserFunction::function1D(double *out, const double *xValues, const size_t nData) const {
106 if (m_formula.empty()) {
107 throw std::invalid_argument("Empty formula supplied for user function");
108 }
109 for (size_t i = 0; i < nData; i++) {
110 m_x = xValues[i];
111 try {
112 out[i] = m_parser->Eval();
113 } catch (mu::Parser::exception_type &e) {
114 throw std::invalid_argument("Error evaluating function \"" + m_formula + "\" for x=" + std::to_string(m_x) +
115 ": " + e.GetMsg());
116 }
117 }
118}
119
126 calNumericalDeriv(domain, jacobian);
127}
128
129} // namespace Mantid::CurveFitting::Functions
double value
The value of the point.
Definition: FitMW.cpp:51
#define DECLARE_FUNCTION(classname)
Macro for declaring a new type of function to be used with the FunctionFactory.
Base class that represents the domain of a function.
Attribute is a non-fitting parameter.
Definition: IFunction.h:282
virtual void setAttribute(const std::string &name, const Attribute &)
Set a value to attribute attName.
Definition: IFunction.cpp:1409
void calNumericalDeriv(const FunctionDomain &domain, Jacobian &jacobian)
Calculate numerical derivatives.
Definition: IFunction.cpp:1031
Represents the Jacobian in IFitFunction::functionDeriv.
Definition: Jacobian.h:22
void clearAllParameters()
Nonvirtual member which removes all declared parameters.
virtual double * getParameterAddress(size_t i)
Get the address of the parameter. For use in UserFunction with mu::Parser.
std::string parameterName(size_t i) const override
Returns the name of parameter i.
void declareParameter(const std::string &name, double initValue=0, const std::string &description="") override
Declare a new parameter.
size_t nParams() const override
Total number of parameters.
Definition: ParamFunction.h:53
static double * AddVariable(const char *varName, void *pufun)
mu::Parser callback function for setting variables.
void function1D(double *out, const double *xValues, const size_t nData) const override
Function you want to fit to.
void setAttribute(const std::string &attName, const Attribute &value) override
Set a value to attribute attName.
bool m_x_set
True indicates that input formula contains 'x' variable.
Definition: UserFunction.h:68
double m_x
Used as 'x' variable in m_parser.
Definition: UserFunction.h:66
void functionDeriv(const API::FunctionDomain &domain, API::Jacobian &jacobian) override
Derivatives of function with respect to active parameters.
mu::Parser * m_parser
extended muParser instance
Definition: UserFunction.h:64
void MANTID_API_DLL extraOneVarFunctions(mu::Parser &parser)
std::string to_string(const wide_integer< Bits, Signed > &n)