Mantid
Loading...
Searching...
No Matches
IFunctionAdapter.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2011 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
9//-----------------------------------------------------------------------------
10// Includes
11//-----------------------------------------------------------------------------
12#include "MantidAPI/IFunction.h"
13
14#include <boost/python/list.hpp>
15#include <boost/python/object.hpp>
16
17namespace Mantid {
18namespace PythonInterface {
23class IFunctionAdapter : virtual public API::IFunction {
24public:
26 IFunctionAdapter(PyObject *self, std::string functionMethod, std::string derivMethod);
27
30
33
35 std::string name() const override;
37 const std::string category() const override;
39 void init() override;
40
42 void declareAttribute(const std::string &name, const boost::python::object &defaultValue);
44 void declareAttribute(const std::string &name, const boost::python::object &defaultValue,
45 const boost::python::object &validator);
47 static PyObject *getAttributeValue(const IFunction &self, const std::string &name);
49 static PyObject *getAttributeValue(const IFunction &self, const API::IFunction::Attribute &attr);
51 static void setAttributePythonValue(IFunction &self, const std::string &name, const boost::python::object &value);
53 void setAttribute(const std::string &attName, const API::IFunction::Attribute &attr) override;
55 static boost::python::list createPythonEquivalentFunctions(const IFunction &self);
56
57 // Each overload of declareParameter requires a different name as we
58 // can't use a function pointer with a virtual base class
59
66 inline void declareFitParameter(const std::string &name, double initValue, const std::string &description) {
67 this->declareParameter(name, initValue, description);
68 }
69
75 inline void declareFitParameterNoDescr(const std::string &name, double initValue) {
76 this->declareFitParameter(name, initValue, "");
77 }
78
83 inline void declareFitParameterZeroInit(const std::string &name) { this->declareFitParameter(name, 0.0, ""); }
84
87 double activeParameter(size_t i) const override;
89 void setActiveParameter(size_t i, double value) override;
90
91protected:
93 inline PyObject *getSelf() const { return m_self; }
95 inline bool derivativeOverridden() const { return m_derivOveridden; }
97 void evaluateFunction(double *out, const double *xValues, const size_t nData) const;
99 void evaluateDerivative(API::Jacobian *out, const double *xValues, const size_t nData) const;
100
101private:
103 PyObject *m_self;
105 std::string m_functionName;
107 std::string m_derivName;
110};
111} // namespace PythonInterface
112} // namespace Mantid
double value
The value of the point.
Definition: FitMW.cpp:51
Attribute is a non-fitting parameter.
Definition: IFunction.h:282
This is an interface to a fitting function - a semi-abstarct class.
Definition: IFunction.h:163
virtual void declareParameter(const std::string &name, double initValue=0, const std::string &description="")=0
Declare a new parameter.
Represents the Jacobian in IFitFunction::functionDeriv.
Definition: Jacobian.h:22
Provides a layer to hook into the protected functions of IFunction.
void init() override
Declare all attributes & parameters.
IFunctionAdapter & operator=(const IFunctionAdapter &)=delete
Disable assignment operator.
void declareFitParameter(const std::string &name, double initValue, const std::string &description)
Declare a named parameter with initial value & description.
static void setAttributePythonValue(IFunction &self, const std::string &name, const boost::python::object &value)
Set the attribute's value.
static boost::python::list createPythonEquivalentFunctions(const IFunction &self)
Split this function (if needed) into a list of independent functions.
void declareFitParameterZeroInit(const std::string &name)
Declare a named parameter with initial value = 0.0.
std::string name() const override
Returns the name of the function.
PyObject * m_self
The Python portion of the object.
void evaluateFunction(double *out, const double *xValues, const size_t nData) const
Evaluate the function by calling the overridden method.
void evaluateDerivative(API::Jacobian *out, const double *xValues, const size_t nData) const
Evaluate the derivative by calling the overridden method.
const std::string category() const override
Specify a category for the function.
void setAttribute(const std::string &attName, const API::IFunction::Attribute &attr) override
Called by the framework when an attribute has been set.
std::string m_functionName
The name of the method to evaluate the function.
IFunctionAdapter(const IFunctionAdapter &)=delete
The PyObject must be supplied to construct the object.
bool m_derivOveridden
Flag if the derivateive method is overridden (avoids multiple checks)
std::string m_derivName
The name of the method to evaluate the derivative.
void declareFitParameterNoDescr(const std::string &name, double initValue)
Declare a named parameter with initial value.
void setActiveParameter(size_t i, double value) override
Override this method to make fitted parameters different from the declared.
void declareAttribute(const std::string &name, const boost::python::object &defaultValue)
Declare an attribute with an initial value.
double activeParameter(size_t i) const override
Override this method to make fitted parameters different from the declared.
static PyObject * getAttributeValue(const IFunction &self, const std::string &name)
Get a named attribute value.
Helper class which provides the Collimation Length for SANS instruments.