12#include "MantidAPI/DllConfig.h"
23#include <boost/variant.hpp>
33#pragma warning(disable : 4250)
183 if (validator !=
nullptr) {
184 error = validator->isValid(inputData);
213 T
operator()(std::vector<double> &v)
const {
return apply(v); }
217 virtual T
apply(std::string &)
const = 0;
219 virtual T
apply(
double &)
const = 0;
225 virtual T
apply(std::vector<double> &)
const = 0;
230 ValidatorEvaluator::evaluate(inputData,
m_validator);
246 T
operator()(
const std::string &str)
const {
return apply(str); }
254 T
operator()(
const std::vector<double> &v)
const {
return apply(v); }
258 virtual T
apply(
const std::string &str)
const = 0;
260 virtual T
apply(
const double &d)
const = 0;
262 virtual T
apply(
const int &i)
const = 0;
264 virtual T
apply(
const bool &i)
const = 0;
266 virtual T
apply(
const std::vector<double> &)
const = 0;
271 ValidatorEvaluator::evaluate(inputData,
m_validator);
287 explicit Attribute(
const std::string &str,
bool quoteValue =
false) :
m_data(str), m_quoteValue(quoteValue) {}
309 void evaluateValidator()
const;
313 ValidatorEvaluator::evaluate(inputData,
m_validator);
320 std::string type()
const;
322 std::string
value()
const;
325 std::string asString()
const;
328 std::string asQuotedString()
const;
330 std::string asUnquotedString()
const;
335 double asDouble()
const;
339 std::vector<double> asVector()
const;
341 bool isEmpty()
const;
344 void setString(
const std::string &str);
346 void setDouble(
const double &);
348 void setInt(
const int &);
350 void setBool(
const bool &);
352 void setVector(
const std::vector<double> &);
355 evaluateValidator(v);
360 void fromString(
const std::string &str);
364 mutable boost::variant<std::string, int, double, bool, std::vector<double>>
m_data;
366 bool m_quoteValue =
false;
382 T
operator()(std::string &str)
const { m_attrToValidate->evaluateValidator(str); }
384 T
operator()(
double &d)
const { m_attrToValidate->evaluateValidator(
d); }
386 T
operator()(
int &i)
const { m_attrToValidate->evaluateValidator(i); }
388 T
operator()(
bool &b)
const { m_attrToValidate->evaluateValidator(b); }
390 T
operator()(std::vector<double> &v)
const { m_attrToValidate->evaluateValidator(v); }
406 virtual std::string
name()
const = 0;
408 std::string asString()
const;
410 virtual std::shared_ptr<IFunction> clone()
const;
414 virtual void registerFunctionUsage(
bool internal);
419 virtual void setMatrixWorkspace(std::shared_ptr<const API::MatrixWorkspace>
workspace,
size_t wi,
double startX,
428 void setProgressReporter(std::shared_ptr<Kernel::ProgressBase> reporter);
430 void reportProgress(
const std::string &msg =
"")
const;
433 bool cancellationRequestReceived()
const;
440 virtual const std::string
category()
const {
return "General"; }
442 virtual const std::vector<std::string> categories()
const;
471 virtual void setParameter(
const std::string &name,
const double &
value,
bool explicitlySet =
true) = 0;
491 virtual double getError(
const std::string &name)
const = 0;
495 virtual void setError(
const std::string &name,
double err) = 0;
498 [[nodiscard]]
bool isFixed(
size_t i)
const;
500 [[nodiscard]]
bool isFixedByDefault(
size_t i)
const;
502 void fix(
size_t i,
bool isDefault =
false);
504 void unfix(
size_t i);
506 void fixParameter(
const std::string &name,
bool isDefault =
false);
508 void unfixParameter(
const std::string &name);
510 void fixAll(
bool isDefault =
false);
514 void unfixAllDefault();
516 void fixAllActive(
bool isDefault =
false);
522 [[nodiscard]] std::vector<std::string> getParameterNames()
const;
529 [[nodiscard]]
virtual double activeParameter(
size_t i)
const;
532 virtual void setActiveParameter(
size_t i,
double value);
534 [[nodiscard]]
virtual std::string nameOfActive(
size_t i)
const;
536 [[nodiscard]]
virtual std::string descriptionOfActive(
size_t i)
const;
538 [[nodiscard]]
bool isActive(
size_t i)
const;
544 virtual void tie(
const std::string &parName,
const std::string &expr,
bool isDefault =
false);
546 virtual void addTies(
const std::string &ties,
bool isDefault =
false);
548 virtual void applyTies();
550 virtual void removeTie(
const std::string &parName);
552 virtual void clearTies();
554 virtual bool removeTie(
size_t i);
562 [[nodiscard]] std::string writeTies()
const;
568 virtual void addConstraints(
const std::string &str,
bool isDefault =
false);
570 virtual void addConstraint(std::unique_ptr<IConstraint> ic);
572 [[nodiscard]]
virtual IConstraint *getConstraint(
size_t i)
const;
574 virtual void removeConstraint(
const std::string &parName);
575 virtual void setConstraintPenaltyFactor(
const std::string &parName,
const double &c);
577 [[nodiscard]] std::string writeConstraints()
const;
579 virtual void clearConstraints();
585 [[nodiscard]]
virtual size_t nAttributes()
const;
587 [[nodiscard]]
virtual std::vector<std::string> getAttributeNames()
const;
589 [[nodiscard]]
virtual std::string attributeName(
size_t index)
const;
591 [[nodiscard]]
virtual Attribute getAttribute(
const std::string &name)
const;
593 virtual void setAttribute(
const std::string &name,
const Attribute &);
595 [[nodiscard]]
virtual bool hasAttribute(
const std::string &name)
const;
600 auto attr = getAttribute(attName);
601 attr.setValue(
value);
602 setAttribute(attName, attr);
604 void setAttributeValue(
const std::string &attName,
const char *
value);
605 void setAttributeValue(
const std::string &attName,
const std::string &
value);
609 [[nodiscard]]
virtual std::shared_ptr<IFunction> getFunction(
size_t i)
const;
611 [[nodiscard]]
virtual std::size_t
nFunctions()
const {
return 0; }
613 virtual void setUpForFit();
615 [[nodiscard]]
virtual size_t getValuesSize(
const FunctionDomain &domain)
const;
617 [[nodiscard]]
virtual size_t getNumberDomains()
const;
619 [[nodiscard]]
virtual std::vector<std::shared_ptr<IFunction>> createEquivalentFunctions()
const;
623 [[nodiscard]]
double calculateStepSize(
const double parameterValue)
const;
636 [[nodiscard]]
bool isParallel()
const {
return m_isParallel; }
639 void setHandler(std::unique_ptr<FunctionHandler> handler);
660 virtual void setStepSizeMethod(
const StepSizeMethod method);
666 virtual void declareParameter(
const std::string &name,
double initValue = 0,
const std::string &description =
"") = 0;
670 const std::shared_ptr<const MatrixWorkspace> &ws,
size_t wsIndex)
const;
673 const std::shared_ptr<const MatrixWorkspace> &ws,
size_t wsIndex)
const;
686 void checkAttributeName(
const std::string &name);
692 virtual void addTie(std::unique_ptr<ParameterTie> tie);
693 [[nodiscard]]
bool hasOrderedTies()
const;
694 void applyOrderedTies();
696 [[nodiscard]]
virtual std::string writeToString(
const std::string &parentLocalAttributesStr =
"")
const;
714 std::map<std::string, API::IFunction::Attribute>
m_attrs;
716 std::shared_ptr<Kernel::Matrix<double>>
m_covar;
720 std::vector<std::unique_ptr<ParameterTie>>
m_ties;
726 bool m_isRegistered{
false};
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
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.
void setAttributeValue(const std::string &attName, const T &value)
Set an attribute value.
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.
virtual std::size_t nFunctions() const
Number of child functions.
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)