12#include "MantidAPI/DllConfig.h"
25#include <boost/variant.hpp>
35#pragma warning(disable : 4250)
58 using Ts::operator()...;
187 if (validator !=
nullptr) {
188 error = validator->isValid(inputData);
217 T
operator()(std::vector<double> &v)
const {
return apply(v); }
221 virtual T
apply(std::string &)
const = 0;
223 virtual T
apply(
double &)
const = 0;
229 virtual T
apply(std::vector<double> &)
const = 0;
234 ValidatorEvaluator::evaluate(inputData,
m_validator);
250 T
operator()(
const std::string &str)
const {
return apply(str); }
258 T
operator()(
const std::vector<double> &v)
const {
return apply(v); }
262 virtual T
apply(
const std::string &str)
const = 0;
264 virtual T
apply(
const double &d)
const = 0;
266 virtual T
apply(
const int &i)
const = 0;
268 virtual T
apply(
const bool &i)
const = 0;
270 virtual T
apply(
const std::vector<double> &)
const = 0;
275 ValidatorEvaluator::evaluate(inputData,
m_validator);
291 explicit Attribute(
const std::string &str,
bool quoteValue =
false) :
m_data(str), m_quoteValue(quoteValue) {}
313 void evaluateValidator()
const;
317 ValidatorEvaluator::evaluate(inputData,
m_validator);
324 std::string type()
const;
326 std::string
value()
const;
329 std::string asString()
const;
332 std::string asQuotedString()
const;
334 std::string asUnquotedString()
const;
339 double asDouble()
const;
343 std::vector<double> asVector()
const;
345 bool isEmpty()
const;
348 void setString(
const std::string &str);
350 void setDouble(
const double &);
352 void setInt(
const int &);
354 void setBool(
const bool &);
356 void setVector(
const std::vector<double> &);
359 evaluateValidator(v);
364 void fromString(
const std::string &str);
368 mutable boost::variant<std::string, int, double, bool, std::vector<double>>
m_data;
370 bool m_quoteValue =
false;
386 T
operator()(std::string &str)
const { m_attrToValidate->evaluateValidator(str); }
388 T
operator()(
double &d)
const { m_attrToValidate->evaluateValidator(
d); }
390 T
operator()(
int &i)
const { m_attrToValidate->evaluateValidator(i); }
392 T
operator()(
bool &b)
const { m_attrToValidate->evaluateValidator(b); }
394 T
operator()(std::vector<double> &v)
const { m_attrToValidate->evaluateValidator(v); }
410 virtual std::string
name()
const = 0;
412 std::string asString()
const;
414 virtual std::shared_ptr<IFunction> clone()
const;
418 virtual void registerFunctionUsage(
bool internal);
421 virtual void setWorkspace([[maybe_unused]] std::shared_ptr<const Workspace> ws) {}
423 virtual void setMatrixWorkspace(std::shared_ptr<const API::MatrixWorkspace>
workspace,
size_t wi,
double startX,
432 void setProgressReporter(std::shared_ptr<Kernel::ProgressBase> reporter);
434 void reportProgress(
const std::string &msg =
"")
const;
437 bool cancellationRequestReceived()
const;
444 virtual const std::string
category()
const {
return "General"; }
446 virtual const std::vector<std::string> categories()
const;
502 [[nodiscard]]
bool isFixed(
size_t i)
const;
504 [[nodiscard]]
bool isFixedByDefault(
size_t i)
const;
506 void fix(
size_t i,
bool isDefault =
false);
508 void unfix(
size_t i);
510 void fixParameter(
const std::string &
name,
bool isDefault =
false);
512 void unfixParameter(
const std::string &
name);
514 void fixAll(
bool isDefault =
false);
518 void unfixAllDefault();
520 void fixAllActive(
bool isDefault =
false);
526 [[nodiscard]] std::vector<std::string> getParameterNames()
const;
533 [[nodiscard]]
virtual double activeParameter(
size_t i)
const;
536 virtual void setActiveParameter(
size_t i,
double value);
538 [[nodiscard]]
virtual std::string nameOfActive(
size_t i)
const;
540 [[nodiscard]]
virtual std::string descriptionOfActive(
size_t i)
const;
542 [[nodiscard]]
bool isActive(
size_t i)
const;
548 virtual void tie(
const std::string &parName,
const std::string &expr,
bool isDefault =
false);
550 virtual void addTies(
const std::string &ties,
bool isDefault =
false);
552 virtual void applyTies();
554 virtual void removeTie(
const std::string &parName);
556 virtual void clearTies();
558 virtual bool removeTie(
size_t i);
564 void sortTies(
const bool checkOnly =
false);
566 [[nodiscard]] std::string writeTies()
const;
572 virtual void addConstraints(
const std::string &str,
bool isDefault =
false);
574 virtual void addConstraint(std::unique_ptr<IConstraint> ic);
576 [[nodiscard]]
virtual IConstraint *getConstraint(
size_t i)
const;
578 virtual void removeConstraint(
const std::string &parName);
579 virtual void setConstraintPenaltyFactor(
const std::string &parName,
const double &c);
581 [[nodiscard]] std::string writeConstraints()
const;
583 virtual void clearConstraints();
589 [[nodiscard]]
virtual size_t nAttributes()
const;
591 [[nodiscard]]
virtual std::vector<std::string> getAttributeNames()
const;
593 [[nodiscard]]
virtual std::string attributeName(
size_t index)
const;
595 [[nodiscard]]
virtual Attribute getAttribute(
const std::string &
name)
const;
597 virtual void setAttribute(
const std::string &
name,
const Attribute &);
599 [[nodiscard]]
virtual bool hasAttribute(
const std::string &
name)
const;
602 template <typename T>
void setAttributeValue(const
std::
string &attName, const T &
value) {
605 auto attr = getAttribute(attName);
606 attr.setValue(
value);
607 setAttribute(attName, attr);
610 void setAttributeValue(const
std::
string &attName, const
char *
value);
611 void setAttributeValue(const
std::
string &attName, const
std::
string &
value);
615 [[nodiscard]] virtual
std::shared_ptr<
IFunction> getFunction(
size_t i) const;
617 [[nodiscard]] virtual
std::
size_t nFunctions()
const {
return 0; }
619 virtual void setUpForFit();
621 [[nodiscard]]
virtual size_t getValuesSize(
const FunctionDomain &domain)
const;
623 [[nodiscard]]
virtual size_t getNumberDomains()
const;
625 [[nodiscard]]
virtual std::vector<std::shared_ptr<IFunction>> createEquivalentFunctions()
const;
629 [[nodiscard]]
double calculateStepSize(
size_t parameterIndex,
double parameterValue)
const;
642 [[nodiscard]]
bool isParallel()
const {
return m_isParallel; }
645 void setHandler(std::unique_ptr<FunctionHandler> handler);
667 virtual void setStepSizeMethod(
const StepSizeMethod method);
669 void setCustomStepSizes(
const std::vector<double> &stepSizes);
675 virtual void declareParameter(
const std::string &
name,
double initValue = 0,
const std::string &description =
"") = 0;
679 const std::shared_ptr<const MatrixWorkspace> &ws,
size_t wsIndex)
const;
682 const std::shared_ptr<const MatrixWorkspace> &ws,
size_t wsIndex)
const;
695 void checkAttributeName(
const std::string &
name);
701 virtual void addTie(std::unique_ptr<ParameterTie> tie);
702 [[nodiscard]]
bool hasOrderedTies()
const;
703 void applyOrderedTies();
705 [[nodiscard]]
virtual std::string writeToString(
const std::string &parentLocalAttributesStr =
"")
const;
723 std::map<std::string, API::IFunction::Attribute>
m_attrs;
725 std::shared_ptr<Kernel::Matrix<double>>
m_covar;
727 double m_chiSquared{0.0};
729 std::vector<std::unique_ptr<ParameterTie>>
m_ties;
735 bool m_isRegistered{
false};
742 std::unique_ptr<ParameterTie> createAndProcessTie(
const std::string &parName,
const std::string &expr,
745 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.
MANTID_API_EXTERN_TEMPLATE(Mantid::Kernel::PropertyWithValue< std::shared_ptr< Mantid::API::IFunction > >)
IPeaksWorkspace_sptr workspace
std::map< DeltaEMode::Type, std::string > index
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.
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.
std::vector< double > m_stepSizes
The custom step sizes for the derivative.
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.
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.
The concrete, templated class 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)