19#include <boost/lexical_cast.hpp>
29Kernel::Logger
g_log(
"CompositeFunction");
30constexpr char *ATTNUMDERIV =
"NumDeriv";
31constexpr int NUMDEFAULTATTRIBUTES = 1;
42void replaceVariableIndexRange(std::vector<T> &variableFunctionIndexList,
const size_t oldSize,
const size_t newSize,
43 const T functionIndex) {
44 auto itFun = std::find(variableFunctionIndexList.begin(), variableFunctionIndexList.end(), functionIndex);
45 if (itFun != variableFunctionIndexList.end()) {
46 if (oldSize > newSize) {
47 variableFunctionIndexList.erase(itFun, itFun + oldSize - newSize);
48 }
else if (oldSize < newSize) {
49 variableFunctionIndexList.insert(itFun, newSize - oldSize, functionIndex);
51 }
else if (newSize > 0) {
52 using std::placeholders::_1;
53 itFun = std::find_if(variableFunctionIndexList.begin(), variableFunctionIndexList.end(),
54 std::bind(std::greater<size_t>(), _1, functionIndex));
55 variableFunctionIndexList.insert(itFun, newSize, functionIndex);
67 createDefaultGlobalAttributes();
96 std::ostringstream ostr;
100 return "name=" +
name();
104 getAttribute(ATTNUMDERIV).asBool() || !parentLocalAttributesStr.empty()) {
105 ostr <<
"composite=" <<
name();
107 for (
const auto &attName : attr) {
109 if (!attValue.empty()) {
110 ostr <<
',' << attName <<
'=' << attValue;
113 ostr << parentLocalAttributesStr <<
';';
118 bool isComp = std::dynamic_pointer_cast<CompositeFunction>(fun) !=
nullptr;
121 std::ostringstream localAttributesStr;
122 for (
const auto &localAttName : localAttr) {
124 if (!localAttValue.empty()) {
126 localAttributesStr <<
',' <<
'$' << localAttName <<
'=' << localAttValue;
129 ostr << fun->writeToString(localAttributesStr.str());
140 if (!constraints.empty()) {
141 ostr <<
";constraints=(" << constraints <<
")";
148 ostr <<
";ties=(" << ties <<
")";
160 for (
auto it =
m_functions.begin(); it != iend; ++it) {
161 (*it)->setWorkspace(ws);
173 for (
size_t iFun = 0; iFun <
nFunctions(); ++iFun) {
183 [&stepSizeMethod](
const auto &
function) { function->setStepSizeMethod(stepSizeMethod); });
194 for (
size_t iFun = 0; iFun <
nFunctions(); ++iFun) {
209 for (
size_t iFun = 0; iFun <
nFunctions(); ++iFun) {
261 }
catch (std::invalid_argument &) {
278 }
catch (std::invalid_argument &) {
326 }
catch (std::invalid_argument &) {
327 throw std::invalid_argument(
"ParamFunctionAttributeHolder::getAttribute - Unknown attribute '" +
name +
"'");
349 return std::accumulate(
351 [](
const size_t accumulator,
const auto &
function) ->
size_t {
return accumulator +
function->nAttributes(); });
368 std::ostringstream ostr;
378 std::ostringstream ostr;
379 ostr <<
'f' << i <<
'.' <<
m_functions[i]->parameterName(j);
393 std::ostringstream ostr;
403 std::ostringstream ostr;
471 std::ostringstream ostr;
479 std::ostringstream ostr;
508 for (
auto &f : functions) {
559 for (
size_t j = 0; j <
nParams();) {
561 if (
tie &&
tie->findParametersOf(fun.get())) {
590 for (
size_t j = i + 1; j <
nFunctions(); j++) {
605 std::vector<IFunction_sptr>::const_iterator it = std::find(
m_functions.begin(),
m_functions.end(), f_old);
608 std::vector<IFunction_sptr>::difference_type iFun = it -
m_functions.begin();
623 const size_t np_old = fun->nParams();
624 const size_t np_new = f->nParams();
625 const size_t at_old = fun->nAttributes();
626 const size_t at_new = f->nAttributes();
633 const size_t dnp = np_new - np_old;
634 const size_t dna = at_new - at_old;
653 return function->name() == functionName;
683 throw std::invalid_argument(
"A function with name '" + functionName +
"' does not exist in this composite function.");
693 throw std::out_of_range(
"Function parameter index (" +
std::to_string(i) +
") out of range (" +
705 throw std::out_of_range(
"Function attribute index (" +
std::to_string(i) +
") out of range (" +
717 const size_t i = varName.find(
'.');
718 if (i == std::string::npos) {
719 throw std::invalid_argument(
"Variable " + varName +
" not found.");
721 if (varName[0] !=
'f')
722 throw std::invalid_argument(
"External function variable name must start with 'f'");
724 const std::string sindex = varName.substr(1, i - 1);
725 const size_t index = boost::lexical_cast<size_t>(sindex);
727 if (i == varName.size() - 1)
728 throw std::invalid_argument(
"Name cannot be empty");
730 return std::make_pair(varName.substr(i + 1),
index);
768 return localFunction->parameterName(localIndex);
801 if (!foundAndRemovedTie) {
806 return foundAndRemovedTie;
815 if (
tie ==
nullptr) {
878 for (
size_t i = 0; i <
nParams(); ++i) {
880 if (
tie && !
tie->isConstant()) {
881 g_log.
warning() <<
"Numeric derivatives should be used when "
882 "non-constant ties defined.\n";
894 if (constraint ==
nullptr) {
907 if (constraint !=
nullptr) {
935 for (
size_t iFun = 0; iFun <
nFunctions(); iFun++) {
937 size_t iLocalIndex = fun->getParameterIndex(ref);
938 if (iLocalIndex < fun->
nParams()) {
951 for (
size_t iFun = 0; iFun <
nFunctions(); iFun++) {
953 if (fun->getParameterIndex(ref) < fun->nParams()) {
967 for (
size_t iFun = 1; iFun <
n; ++iFun) {
969 throw std::runtime_error(
"CompositeFunction has members with "
970 "inconsistent domain numbers.");
988 std::vector<std::vector<IFunction_sptr>> equiv;
990 for (
size_t i = 0; i < nf; ++i) {
994 std::vector<IFunction_sptr> funs;
996 for (
size_t i = 0; i < nd; ++i) {
999 for (
size_t j = 0; j < nf; ++j) {
1000 comp->addFunction(equiv[j][i]);
double value
The value of the point.
#define DECLARE_FUNCTION(classname)
Macro for declaring a new type of function to be used with the FunctionFactory.
IPeaksWorkspace_sptr workspace
std::map< DeltaEMode::Type, std::string > index
A composite function is a function containing other functions.
size_t nAttributes() const override
Returns the number of attributes associated with the function.
std::string nameOfActive(size_t i) const override
Returns the name of active parameter i.
void clearTies() override
Remove all ties.
static std::pair< std::string, size_t > parseName(const std::string &varName)
Extract function index and parameter name from a variable name.
ParameterTie * getTie(size_t i) const override
Get the tie of i-th parameter.
void setUpForFit() override
Prepare function for a fit.
std::string parameterLocalName(size_t i, bool recursive=false) const
Returns the name of parameter i as it declared in its function.
size_t m_nParams
Total number of parameters.
std::string descriptionOfActive(size_t i) const override
Returns the name of active parameter i.
void functionDeriv(const FunctionDomain &domain, Jacobian &jacobian) override
Derivatives of function with respect to active parameters.
bool hasParameter(const std::string &name) const override
Check if function has a parameter with this name.
virtual std::vector< std::string > getLocalAttributeNames() const
Returns a list of attribute names.
virtual size_t addFunction(IFunction_sptr f)
Add a function at the back of the internal function list.
void setActiveParameter(size_t i, double value) override
Set new value of i-th active parameter.
void function(const FunctionDomain &domain, FunctionValues &values) const override
Function you want to fit to.
std::string attributeName(size_t i) const override
Returns the name of attribute i.
size_t nGlobalAttributes() const noexcept
std::vector< size_t > m_attributeIndex
void setParameter(size_t, const double &value, bool explicitlySet=true) override
Set i-th parameter.
void declareAttribute(const std::string &name, const API::IFunction::Attribute &defaultValue)
Declare a single attribute.
size_t paramOffset(size_t i) const
std::vector< std::shared_ptr< IFunction > > createEquivalentFunctions() const override
Split this function (if needed) into a list of independent functions.
size_t m_nAttributes
Total number of attributes.
void replaceFunctionPtr(const IFunction_sptr &f_old, const IFunction_sptr &f_new)
Replace a function.
bool removeTie(size_t i) override
Removes i-th parameter's tie.
void setWorkspace(std::shared_ptr< const Workspace > ws) override
Sets the workspace for each member function.
size_t getParameterIndex(const ParameterReference &ref) const override
Return parameter index from a parameter reference.
std::vector< size_t > m_paramOffsets
Individual function parameter offsets (function index in m_functions) e.g.
size_t parameterIndex(const std::string &name) const override
Returns the index of parameter name.
void setParameterDescription(size_t, const std::string &description) override
Set i-th parameter description.
bool hasAttribute(const std::string &name) const override
Check if a function has an attribute with this name.
std::size_t nFunctions() const override
Number of functions.
bool isExplicitlySet(size_t i) const override
Checks if a parameter has been set explicitly.
size_t nParams() const override
Total number of parameters.
void replaceFunction(size_t functionIndex, const IFunction_sptr &f)
Replace a function.
size_t getNumberDomains() const override
Get number of domains required by this function.
double getParameter(size_t i) const override
Get i-th parameter.
double getError(size_t i) const override
Get the fitting error for a parameter.
Attribute getAttribute(const std::string &name) const override
Return a value of attribute attName.
std::vector< std::string > m_globalAttributeNames
void init() override
Function initialization. Declare function parameters in this method.
IFunction_sptr getContainingFunction(const ParameterReference &ref) const
Get the containing function.
void removeConstraint(const std::string &parName) override
Remove a constraint.
void createDefaultGlobalAttributes()
std::string name() const override
Returns the function's name.
std::string parameterDescription(size_t i) const override
Returns the description of parameter i.
std::string writeToString(const std::string &parentLocalAttributesStr="") const override
Writes itself into a string.
void setMatrixWorkspace(std::shared_ptr< const API::MatrixWorkspace > workspace, size_t wi, double startX, double endX) override
Set matrix workspace.
std::size_t attributeFunctionIndex(std::size_t i) const
Get the index of the function to which parameter i belongs.
void removeFunction(size_t i)
Remove a function.
std::vector< size_t > m_IFunction
Keeps the function index for each declared parameter (parameter declared index)
virtual Attribute getLocalAttribute(size_t i, const std::string &attName) const
Return a value of attribute attName.
CompositeFunction()
Default constructor.
size_t parameterLocalIndex(size_t i, bool recursive=false) const
Returns the index of parameter i as it declared in its function.
double activeParameter(size_t i) const override
Value of i-th active parameter.
void declareParameter(const std::string &name, double initValue=0, const std::string &description="") override
Declare a new parameter.
std::string parameterName(size_t i) const override
Returns the name of parameter i.
IFunction_sptr getFunction(std::size_t i) const override
Returns the pointer to i-th function.
size_t getAttributeOffset(size_t attributeIndex) const
void setError(size_t i, double err) override
Set the fitting error for a parameter.
IConstraint * getConstraint(size_t i) const override
Get constraint of i-th parameter.
void setStepSizeMethod(const StepSizeMethod stepSizeMethod) override
Sets the function to use when calculating the step size.
void setAttribute(const std::string &name, const API::IFunction::Attribute &value) override
Set a value of a named attribute.
void applyTies() override
Apply the ties.
ParameterStatus getParameterStatus(size_t i) const override
Get status of parameter.
std::vector< IFunction_sptr > m_functions
Pointers to the included functions.
void registerFunctionUsage(bool internal) override
Registers the usage of the function with the UsageService.
std::size_t functionIndex(const std::string &functionName) const
Get the first function index with a matching function name.
void checkFunction()
Check the function.
bool hasFunction(const std::string &functionName) const
Returns true if the composite has at least one of this function.
void clear()
Remove all member functions.
void setParameterStatus(size_t i, ParameterStatus status) override
Change status of parameter.
Base class that represents the domain of a function.
A class to store values calculated by a function.
void zeroCalculated()
Set all calculated values to zero.
An interface to a constraint.
Attribute is a non-fitting parameter.
std::string value() const
Returns the attribute value as a string.
This is an interface to a fitting function - a semi-abstarct class.
bool hasOrderedTies() const
virtual void removeConstraint(const std::string &parName)
Remove a constraint.
std::string writeTies() const
Write a parameter tie to a string.
virtual Attribute getAttribute(const std::string &name) const
Return a value of attribute attName.
virtual void clearTies()
Remove all ties.
void declareAttribute(const std::string &name, const API::IFunction::Attribute &defaultValue)
Declare a single attribute.
ParameterStatus
Describe parameter status in relation to fitting: Active: Fit varies such parameter directly.
virtual void tie(const std::string &parName, const std::string &expr, bool isDefault=false)
Tie a parameter to other parameters (or a constant)
virtual void applyTies()
Apply the ties.
virtual ParameterTie * getTie(size_t i) const
Get the tie of i-th parameter.
virtual void setAttribute(const std::string &name, const Attribute &)
Set a value to attribute attName.
virtual void setUpForFit()
Set up the function for a fit.
virtual std::string attributeName(size_t index) const
Get name of ith attribute.
std::string asString() const
Writes itself into a string.
void calNumericalDeriv(const FunctionDomain &domain, Jacobian &jacobian)
Calculate numerical derivatives.
std::string writeConstraints() const
Write a parameter constraint to a string.
virtual void removeTie(const std::string &parName)
Removes the tie off a parameter.
virtual IConstraint * getConstraint(size_t i) const
Get constraint of i-th parameter.
StepSizeMethod
Describes the method in which the step size will be calculated: DEFAULT: Uses the traditional Mantid ...
Represents the Jacobian in IFitFunction::functionDeriv.
A reference to a parameter in a function.
std::size_t getLocalIndex() const
Return parameter index in the local function.
IFunction * getLocalFunction() const
Return pointer to the local function.
A Jacobian for individual functions.
Marks code as not implemented yet.
IValidator is the basic interface for all validators for properties.
void warning(const std::string &msg)
Logs at warning level.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
Kernel::Logger g_log("ExperimentInfo")
static logger object
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< CompositeFunction > CompositeFunction_sptr
shared pointer to the composite function base class
std::string to_string(const wide_integer< Bits, Signed > &n)