12#include "MantidAPI/DllConfig.h"
24#include <boost/variant.hpp>
34#pragma warning(disable : 4250)
57 using Ts::operator()...;
186 if (validator !=
nullptr) {
187 error = validator->isValid(inputData);
216 T
operator()(std::vector<double> &v)
const {
return apply(v); }
220 virtual T
apply(std::string &)
const = 0;
222 virtual T
apply(
double &)
const = 0;
228 virtual T
apply(std::vector<double> &)
const = 0;
233 ValidatorEvaluator::evaluate(inputData,
m_validator);
249 T
operator()(
const std::string &str)
const {
return apply(str); }
257 T
operator()(
const std::vector<double> &v)
const {
return apply(v); }
261 virtual T
apply(
const std::string &str)
const = 0;
263 virtual T
apply(
const double &d)
const = 0;
265 virtual T
apply(
const int &i)
const = 0;
267 virtual T
apply(
const bool &i)
const = 0;
269 virtual T
apply(
const std::vector<double> &)
const = 0;
274 ValidatorEvaluator::evaluate(inputData,
m_validator);
290 explicit Attribute(
const std::string &str,
bool quoteValue =
false) :
m_data(str), m_quoteValue(quoteValue) {}
312 void evaluateValidator()
const;
316 ValidatorEvaluator::evaluate(inputData,
m_validator);
323 std::string type()
const;
325 std::string
value()
const;
328 std::string asString()
const;
331 std::string asQuotedString()
const;
333 std::string asUnquotedString()
const;
338 double asDouble()
const;
342 std::vector<double> asVector()
const;
344 bool isEmpty()
const;
347 void setString(
const std::string &str);
349 void setDouble(
const double &);
351 void setInt(
const int &);
353 void setBool(
const bool &);
355 void setVector(
const std::vector<double> &);
358 evaluateValidator(v);
363 void fromString(
const std::string &str);
367 mutable boost::variant<std::string, int, double, bool, std::vector<double>>
m_data;
369 bool m_quoteValue =
false;
385 T
operator()(std::string &str)
const { m_attrToValidate->evaluateValidator(str); }
387 T
operator()(
double &d)
const { m_attrToValidate->evaluateValidator(
d); }
389 T
operator()(
int &i)
const { m_attrToValidate->evaluateValidator(i); }
391 T
operator()(
bool &b)
const { m_attrToValidate->evaluateValidator(b); }
393 T
operator()(std::vector<double> &v)
const { m_attrToValidate->evaluateValidator(v); }
409 virtual std::string
name()
const = 0;
411 std::string asString()
const;
413 virtual std::shared_ptr<IFunction> clone()
const;
417 virtual void registerFunctionUsage(
bool internal);
422 virtual void setMatrixWorkspace(std::shared_ptr<const API::MatrixWorkspace>
workspace,
size_t wi,
double startX,
431 void setProgressReporter(std::shared_ptr<Kernel::ProgressBase> reporter);
433 void reportProgress(
const std::string &msg =
"")
const;
436 bool cancellationRequestReceived()
const;
443 virtual const std::string
category()
const {
return "General"; }
445 virtual const std::vector<std::string> categories()
const;
501 [[nodiscard]]
bool isFixed(
size_t i)
const;
503 [[nodiscard]]
bool isFixedByDefault(
size_t i)
const;
505 void fix(
size_t i,
bool isDefault =
false);
507 void unfix(
size_t i);
509 void fixParameter(
const std::string &
name,
bool isDefault =
false);
511 void unfixParameter(
const std::string &
name);
513 void fixAll(
bool isDefault =
false);
517 void unfixAllDefault();
519 void fixAllActive(
bool isDefault =
false);
525 [[nodiscard]] std::vector<std::string> getParameterNames()
const;
532 [[nodiscard]]
virtual double activeParameter(
size_t i)
const;
535 virtual void setActiveParameter(
size_t i,
double value);
537 [[nodiscard]]
virtual std::string nameOfActive(
size_t i)
const;
539 [[nodiscard]]
virtual std::string descriptionOfActive(
size_t i)
const;
541 [[nodiscard]]
bool isActive(
size_t i)
const;
547 virtual void tie(
const std::string &parName,
const std::string &expr,
bool isDefault =
false);
549 virtual void addTies(
const std::string &ties,
bool isDefault =
false);
551 virtual void applyTies();
553 virtual void removeTie(
const std::string &parName);
555 virtual void clearTies();
557 virtual bool removeTie(
size_t i);
563 void sortTies(
const bool checkOnly =
false);
565 [[nodiscard]] std::string writeTies()
const;
571 virtual void addConstraints(
const std::string &str,
bool isDefault =
false);
573 virtual void addConstraint(std::unique_ptr<IConstraint> ic);
575 [[nodiscard]]
virtual IConstraint *getConstraint(
size_t i)
const;
577 virtual void removeConstraint(
const std::string &parName);
578 virtual void setConstraintPenaltyFactor(
const std::string &parName,
const double &c);
580 [[nodiscard]] std::string writeConstraints()
const;
582 virtual void clearConstraints();
588 [[nodiscard]]
virtual size_t nAttributes()
const;
590 [[nodiscard]]
virtual std::vector<std::string> getAttributeNames()
const;
592 [[nodiscard]]
virtual std::string attributeName(
size_t index)
const;
594 [[nodiscard]]
virtual Attribute getAttribute(
const std::string &
name)
const;
596 virtual void setAttribute(
const std::string &
name,
const Attribute &);
598 [[nodiscard]]
virtual bool hasAttribute(
const std::string &
name)
const;
601 template <typename T>
void setAttributeValue(const
std::
string &attName, const T &
value) {
604 auto attr = getAttribute(attName);
605 attr.setValue(
value);
606 setAttribute(attName, attr);
609 void setAttributeValue(const
std::
string &attName, const
char *
value);
610 void setAttributeValue(const
std::
string &attName, const
std::
string &
value);
614 [[nodiscard]] virtual
std::shared_ptr<
IFunction> getFunction(
size_t i) const;
616 [[nodiscard]] virtual
std::
size_t nFunctions()
const {
return 0; }
618 virtual void setUpForFit();
620 [[nodiscard]]
virtual size_t getValuesSize(
const FunctionDomain &domain)
const;
622 [[nodiscard]]
virtual size_t getNumberDomains()
const;
624 [[nodiscard]]
virtual std::vector<std::shared_ptr<IFunction>> createEquivalentFunctions()
const;
628 [[nodiscard]]
double calculateStepSize(
const double parameterValue)
const;
641 [[nodiscard]]
bool isParallel()
const {
return m_isParallel; }
644 void setHandler(std::unique_ptr<FunctionHandler> handler);
665 virtual void setStepSizeMethod(
const StepSizeMethod method);
671 virtual void declareParameter(
const std::string &
name,
double initValue = 0,
const std::string &description =
"") = 0;
675 const std::shared_ptr<const MatrixWorkspace> &ws,
size_t wsIndex)
const;
678 const std::shared_ptr<const MatrixWorkspace> &ws,
size_t wsIndex)
const;
691 void checkAttributeName(
const std::string &
name);
697 virtual void addTie(std::unique_ptr<ParameterTie> tie);
698 [[nodiscard]]
bool hasOrderedTies()
const;
699 void applyOrderedTies();
701 [[nodiscard]]
virtual std::string writeToString(
const std::string &parentLocalAttributesStr =
"")
const;
719 std::map<std::string, API::IFunction::Attribute>
m_attrs;
721 std::shared_ptr<Kernel::Matrix<double>>
m_covar;
725 std::vector<std::unique_ptr<ParameterTie>>
m_ties;
731 bool m_isRegistered{
false};
736 std::unique_ptr<ParameterTie> createAndProcessTie(
const std::string &parName,
const std::string &expr,
739 std::pair<std::size_t, std::string> insertTie(std::unique_ptr<ParameterTie> tie);
Kernel::IValidator_sptr m_validator
double value
The value of the point.
IPeaksWorkspace_sptr workspace
std::map< DeltaEMode::Type, std::string > index
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
const std::vector< Type > & m_data
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
A composite function is a function containing other functions.
Base class that represents the domain of a function.
FunctionGenerator is a partial implementation of IFunction that defines a function consisting of two ...
Classes inherited from FunctionHandler will handle the function.
FunctionHandler & operator=(const FunctionHandler &)=delete
virtual ~FunctionHandler()=default
Virtual destructor.
IFunction_sptr function() const
Return the handled function.
FunctionHandler(const FunctionHandler &)=delete
Delete copy constructor and copy assignment operator.
virtual void init()=0
abstract init method. It is called after setting handler to the function
IFunction_sptr m_fun
pointer to the handled function
FunctionHandler(IFunction_sptr fun)
Constructor.
FunctionParameterDecorator.
A class to store values calculated by a function.
An interface to a constraint.
Atribute validator visitor class.
T operator()(int &i) const
implements static_visitor's operator() for int
T operator()(std::vector< double > &v) const
implements static_visitor's operator() for vector
AttributeValidatorVisitor(const IFunction::Attribute *attrToValidate)
const IFunction::Attribute * m_attrToValidate
T operator()(double &d) const
implements static_visitor's operator() for double
T operator()(std::string &str) const
implements static_visitor's operator() for std::string
T operator()(bool &b) const
implements static_visitor's operator() for bool
virtual T apply(bool &) const =0
Implement this method to access attribute as bool.
virtual ~AttributeVisitor()=default
Virtual destructor.
T operator()(bool &b) const
implements static_visitor's operator() for bool
virtual T apply(int &) const =0
Implement this method to access attribute as int.
T operator()(double &d) const
implements static_visitor's operator() for double
virtual T apply(std::vector< double > &) const =0
Implement this method to access attribute as vector.
void evaluateValidator(T1 &inputData) const
Evaluates the validator associated with attribute this visitor is to visit.
T operator()(std::string &str) const
implements static_visitor's operator() for std::string
virtual T apply(std::string &) const =0
Implement this method to access attribute as string.
T operator()(std::vector< double > &v) const
implements static_visitor's operator() for vector
virtual T apply(double &) const =0
Implement this method to access attribute as double.
T operator()(int &i) const
implements static_visitor's operator() for int
Attribute is a non-fitting parameter.
Attribute(const bool &b)
Create bool attribute.
Attribute(const int &i)
Create int attribute.
void setValue(const T &v)
Attribute(const double &d)
Create double attribute.
Kernel::IValidator_sptr getValidator()
Return a clone of the attribute validator;.
T apply(AttributeVisitor< T > &v)
Apply an attribute visitor.
Attribute()
Create empty string attribute.
void evaluateValidator(T &inputData) const
Evaluates the validator associated with this attribute with regards to input value....
Kernel::IValidator_sptr m_validator
Associated Validator.
boost::variant< std::string, int, double, bool, std::vector< double > > m_data
The data holder as boost variant.
Attribute(const std::string &str, bool quoteValue=false)
Create string attribute.
void apply(AttributeLambdaVisitor< Ts... > &v)
Apply a lambda visitor.
Attribute(const char *c)
Create string attribute.
T apply(ConstAttributeVisitor< T > &v) const
Apply a const attribute visitor.
Attribute(const std::vector< double > &v)
Create vector attribute.
Const version of AttributeVisitor.
T operator()(const int &i) const
implements static_visitor's operator() for int
virtual T apply(const double &d) const =0
Implement this mathod to access attribute as double.
virtual T apply(const std::string &str) const =0
Implement this mathod to access attribute as string.
T operator()(const double &d) const
implements static_visitor's operator() for double
virtual T apply(const bool &i) const =0
Implement this mathod to access attribute as bool.
T operator()(const std::vector< double > &v) const
implements static_visitor's operator() for vector
T operator()(const std::string &str) const
implements static_visitor's operator() for std::string
T operator()(const bool &b) const
implements static_visitor's operator() for bool
virtual ~ConstAttributeVisitor()=default
Virtual destructor.
void evaluateValidator(T1 &inputData) const
Evaluates the validator associated with attribute this visitor is to visit.
virtual T apply(const int &i) const =0
Implement this mathod to access attribute as int.
virtual T apply(const std::vector< double > &) const =0
Implement this mathod to access attribute as vector.
This is an interface to a fitting function - a semi-abstarct class.
virtual size_t nParams() const =0
Total number of parameters.
std::function< double(const double)> m_stepSizeFunction
The function used to calculate the step size.
virtual void setParameterDescription(size_t, const std::string &description)=0
Set i-th parameter description.
virtual void setWorkspace(std::shared_ptr< const Workspace > ws)
Set the workspace.
std::shared_ptr< const Kernel::Matrix< double > > getCovarianceMatrix() const
Get the covariance matrix.
virtual double getParameter(size_t i) const =0
Get i-th parameter.
bool m_isParallel
Flag to hint that the function is being used in parallel computations.
virtual void initialize()
Iinialize the function.
virtual void iterationFinished()
Called at the end of an iteration.
IFunction & operator=(const IFunction &)=delete
No copying.
ParameterStatus
Describe parameter status in relation to fitting: Active: Fit varies such parameter directly.
FunctionHandler * getHandler() const
Return the handler.
std::map< std::string, API::IFunction::Attribute > m_attrs
The declared attributes.
double getReducedChiSquared() const
Get the reduced chi^2.
virtual void declareAttributes()
Override to declare function attributes.
virtual ParameterStatus getParameterStatus(size_t i) const =0
Get status of parameter.
virtual bool hasParameter(const std::string &name) const =0
Check if function has a parameter with this name.
virtual void setParameter(size_t, const double &value, bool explicitlySet=true)=0
Set i-th parameter.
virtual const std::string category() const
The categories the Fit function belong to.
virtual int64_t estimateNoProgressCalls() const
Returns an estimate of the number of progress reports a single evaluation of the function will have.
virtual const std::string categorySeparator() const
Function to return the sperator token for the category string.
void setParallel(bool on)
Set the parallel hint.
virtual void setParameterDescription(const std::string &name, const std::string &description)=0
Set description of parameter by name.
virtual size_t getParameterIndex(const ParameterReference &ref) const =0
Return parameter index from a parameter reference.
void setReducedChiSquared(double chi2)
Set the reduced chi^2.
virtual void setError(size_t i, double err)=0
Set the fitting error for a parameter.
virtual std::string parameterDescription(size_t i) const =0
Returns the description of parameter i.
virtual void iterationStarting()
Called at the start of each iteration.
std::vector< ParameterTie * > m_orderedTies
Ties ordered in order of correct application.
virtual std::string parameterName(size_t i) const =0
Returns the name of parameter i.
virtual std::string name() const =0
Returns the function's name.
std::vector< std::unique_ptr< ParameterTie > > m_ties
Holds parameter ties.
std::shared_ptr< Kernel::ProgressBase > m_progReporter
Pointer to the progress handler.
bool isParallel() const
Get the parallel hint.
std::vector< std::unique_ptr< IConstraint > > m_constraints
Holds the constraints added to function.
virtual void setParameterStatus(size_t i, ParameterStatus status)=0
Change status of parameter.
virtual void function(const FunctionDomain &domain, FunctionValues &values) const =0
Evaluates the function for all arguments in the domain.
virtual size_t parameterIndex(const std::string &name) const =0
Returns the index of parameter name.
virtual double getError(size_t i) const =0
Get the fitting error for a parameter.
virtual void declareParameters()
Override to declare function parameters.
virtual bool ignoreTie(const ParameterTie &) const
Ignore a tie.
virtual bool isExplicitlySet(size_t i) const =0
Checks if a parameter has been set explicitly.
virtual double getError(const std::string &name) const =0
Get the fitting error for a parameter by name.
std::unique_ptr< FunctionHandler > m_handler
Pointer to a function handler.
virtual double getParameter(const std::string &name) const =0
Get parameter by name.
virtual void declareParameter(const std::string &name, double initValue=0, const std::string &description="")=0
Declare a new parameter.
virtual void setError(const std::string &name, double err)=0
Set the fitting error for a parameter by name.
double m_chiSquared
The chi-squared of the last fit.
StepSizeMethod
Describes the method in which the step size will be calculated: DEFAULT: Uses the traditional Mantid ...
IFunction(const IFunction &)=delete
No copying.
virtual void setParameter(const std::string &name, const double &value, bool explicitlySet=true)=0
Set parameter by name.
std::shared_ptr< Kernel::Matrix< double > > m_covar
The covariance matrix of the fitting parameters.
Represents the Jacobian in IFitFunction::functionDeriv.
A reference to a parameter in a function.
IValidator is the basic interface for all validators for properties.
std::shared_ptr< IFunction > IFunction_sptr
shared pointer to the function base class
std::shared_ptr< const IFunction > IFunction_const_sptr
shared pointer to the function base class (const version)
std::shared_ptr< Unit > Unit_sptr
Shared pointer to the Unit base class.
std::shared_ptr< IValidator > IValidator_sptr
A shared_ptr to an IValidator.
Helper class which provides the Collimation Length for SANS instruments.
Attribute visitor structure supporting lambda expressions Example usage: AttributeLambdaVisitor{[](co...
Simple Exception Struct to differentiate validation error from other exceptions.
ValidationException(const std::string &ErrorMsg)
Simple struct to constain function to evaluate attribute validators that is required in both attribut...
static void evaluate(T1 &inputData, Mantid::Kernel::IValidator_sptr validator)