Commit 683bb84c authored by Roman Tolchenov's avatar Roman Tolchenov
Browse files

Various changes to IFunction, Function, CompositeFunction and ParameterTie to...

Various changes to IFunction, Function, CompositeFunction and ParameterTie to improve function editing. re #1033
parent 84161e4a
......@@ -220,6 +220,10 @@
RelativePath=".\src\FrameworkManager.cpp"
>
</File>
<File
RelativePath=".\src\Function.cpp"
>
</File>
<File
RelativePath=".\src\FunctionFactory.cpp"
>
......@@ -394,6 +398,10 @@
RelativePath=".\inc\MantidAPI\CompositeFunction.h"
>
</File>
<File
RelativePath=".\test\CompositeFunctionTest.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\DeclareUserAlg.h"
>
......@@ -410,10 +418,18 @@
RelativePath=".\test\FrameworkManagerTest.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\Function.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\FunctionFactory.h"
>
</File>
<File
RelativePath=".\test\FunctionTest.h"
>
</File>
<File
RelativePath=".\test\GaussianErrorHelperTest.h"
>
......@@ -438,6 +454,10 @@
RelativePath=".\inc\MantidAPI\IFunction.h"
>
</File>
<File
RelativePath=".\test\IFunctionTest.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\IFunctionWithLocation.h"
>
......@@ -506,6 +526,10 @@
RelativePath=".\inc\MantidAPI\ParameterTie.h"
>
</File>
<File
RelativePath=".\test\ParameterTieTest.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\ParInstrument.h"
>
......
......@@ -47,6 +47,8 @@ public:
///Destructor
virtual ~CompositeFunction();
/* Overriden methods */
/// Set the workspace
void setWorkspace(boost::shared_ptr<const DataObjects::Workspace2D> workspace,int spec,int xMin,int xMax);
/// Returns the function's name
......@@ -73,6 +75,8 @@ public:
int nParams()const;
/// Returns the index of parameter name
int parameterIndex(const std::string& name)const;
/// Returns the index of a parameter
int parameterIndex(const double* p)const;
/// Returns the name of parameter i
std::string parameterName(int i)const;
......@@ -91,21 +95,45 @@ public:
/// Check if a parameter is active
bool isActive(int i)const;
/// Removes a parameter from the list of active
void removeActive(int i);
/// Get active index for a declared parameter i
int activeIndex(int i)const;
/// Removes a parameter from the list of active
void removeActive(int i);
/// Restores a declared parameter i to the active status
void restoreActive(int i);
/// Apply the ties
void applyTies();
/// Remove all ties
void clearTies();
/// Removes i-th parameter's tie
bool removeTie(int i);
/// Get the tie of i-th parameter
ParameterTie* getTie(int i)const;
/// Add a constraint to function
virtual void addConstraint(IConstraint* ic);
/* CompositeFunction own methods */
/// Add a function
/// Add a function at the back of the internal function list
void addFunction(IFunction* f);
/// Returns the pointer to i-th function
IFunction* getFunction(int i)const;
/// Number of functions
int nFunctions()const{return m_functions.size();}
/// Remove a function
void removeFunction(int i);
/// Replace a function
void replaceFunction(int i,IFunction* f);
protected:
/// Function initialization. Declare function parameters in this method.
void init();
/// Declare a new parameter
virtual void declareParameter(const std::string& name,double initValue = 0);
/// Add a new tie
virtual void addTie(ParameterTie* tie);
private:
......@@ -118,13 +146,15 @@ private:
/// Pointers to the included funtions
std::vector<IFunction*> m_functions;
/// Individual function parameter offsets
/// Individual function parameter offsets <function index in m_functions>
/// e.g. m_functions[i]->activeParameter(m_activeOffsets[i]+1) gives second active parameter of i-th function
std::vector<int> m_activeOffsets;
/// Individual function parameter offsets
/// Individual function parameter offsets <function index in m_functions>
/// e.g. m_functions[i]->parameter(m_paramOffsets[i]+1) gives second declared parameter of i-th function
std::vector<int> m_paramOffsets;
/// Keeps the function number for each declared parameter
/// Keeps the function index for each declared parameter <parameter declared index>
std::vector<int> m_iFunction;
/// Keeps the function number for each active parameter
/// Keeps the function index for each active parameter <parameter active index>
std::vector<int> m_iFunctionActive;
/// Number of active parameters
int m_nActive;
......
#ifndef MANTID_API_FUNCTION_H_
#define MANTID_API_FUNCTION_H_
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidKernel/System.h"
#include "MantidKernel/Unit.h"
#include "MantidAPI/IFunction.h"
#include "boost/shared_ptr.hpp"
#include <string>
#include <vector>
#ifndef HAS_UNORDERED_MAP_H
#include <map>
#else
#include <tr1/unordered_map>
#endif
namespace Mantid
{
namespace DataObjects
{
class Workspace2D;
}
namespace API
{
//----------------------------------------------------------------------
// Forward declaration
//----------------------------------------------------------------------
class Jacobian;
class ParameterTie;
class IConstraint;
/** An interface to a function.
@author Roman Tolchenov, Tessella Support Services plc
@date 16/10/2009
Copyright &copy; 2009 STFC Rutherford Appleton Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport Function : public IFunction
{
public:
/// Default constructor
Function(){}
/// Copy contructor
Function(const Function&);
/// Assignment operator
Function& operator=(const Function&);
/// Virtual destructor
virtual ~Function();
/// Address of i-th parameter
virtual double& parameter(int);
/// Address of i-th parameter
virtual double parameter(int i)const;
/// Get parameter by name.
virtual double& getParameter(const std::string& name);
/// Get parameter by name.
virtual double getParameter(const std::string& name)const;
/// Total number of parameters
virtual int nParams()const{return m_parameters.size();}
/// Returns the index of parameter name
virtual int parameterIndex(const std::string& name)const;
/// Returns the index of a parameter
virtual int parameterIndex(const double* p)const;
/// Returns the name of parameter i
virtual std::string parameterName(int i)const;
/// Number of active (in terms of fitting) parameters
virtual int nActive()const{return m_indexMap.size();}
/// Returns "global" index of active parameter i
virtual int indexOfActive(int i)const;
/// Returns the name of active parameter i
virtual std::string nameOfActive(int i)const;
/// Check if a declared parameter i is active
virtual bool isActive(int i)const;
/// Get active index for a declared parameter i
virtual int activeIndex(int i)const;
/// Removes a declared parameter i from the list of active
virtual void removeActive(int i);
/// Restores a declared parameter i to the active status
virtual void restoreActive(int i);
/// Apply the ties
virtual void applyTies();
/// Remove all ties
virtual void clearTies();
/// Add a constraint to function
virtual void addConstraint(IConstraint* ic);
using IFunction::removeTie;
protected:
/// Function initialization. Declare function parameters in this method.
virtual void init(){};
/// Declare a new parameter
virtual void declareParameter(const std::string& name,double initValue = 0);
/// Add a new tie
virtual void addTie(ParameterTie* tie);
/// Removes i-th parameter's tie
virtual bool removeTie(int i);
/// Get the tie of i-th parameter
virtual ParameterTie* getTie(int i)const;
private:
/// The index map. m_indexMap[i] gives the total index for active parameter i
std::vector<int> m_indexMap;
/// Keeps parameter names
std::vector<std::string> m_parameterNames;
/// Keeps parameter values
std::vector<double> m_parameters;
/// Holds parameter ties as <parameter index,tie pointer>
std::vector<ParameterTie*> m_ties;
/// Holds the constraints added to function
std::vector<IConstraint*> m_constraints;
};
} // namespace API
} // namespace Mantid
#endif /*MANTID_API_FUNCTION_H_*/
......@@ -11,12 +11,6 @@
#include <string>
#include <vector>
#ifndef HAS_UNORDERED_MAP_H
#include <map>
#else
#include <tr1/unordered_map>
#endif
namespace Mantid
{
......@@ -61,15 +55,11 @@ class IConstraint;
class DLLExport IFunction
{
public:
/// Default constructor
IFunction(){}
/// Copy contructor
IFunction(const IFunction&);
/// Assignment operator
IFunction& operator=(const IFunction&);
/// Virtual destructor
virtual ~IFunction();
virtual ~IFunction(){}
/// Returns the function's name
virtual std::string name()const = 0;
/// Writes itself into a string
virtual std::string asString()const;
/// The string operator
......@@ -79,8 +69,6 @@ public:
/// Iinialize the function
virtual void initialize(){this->init();}
/// Returns the function's name
virtual std::string name()const = 0;
/// Function you want to fit to.
virtual void function(double* out, const double* xValues, const int& nData) = 0;
/// Derivatives of function with respect to active parameters
......@@ -90,22 +78,24 @@ public:
virtual void calJacobianForCovariance(Jacobian* out, const double* xValues, const int& nData);
/// Address of i-th parameter
virtual double& parameter(int);
virtual double& parameter(int) = 0;
/// Address of i-th parameter
virtual double parameter(int i)const;
virtual double parameter(int i)const = 0;
/// Get parameter by name.
virtual double& getParameter(const std::string& name);
virtual double& getParameter(const std::string& name) = 0;
/// Get parameter by name.
virtual double getParameter(const std::string& name)const;
virtual double getParameter(const std::string& name)const = 0;
/// Total number of parameters
virtual int nParams()const{return m_parameters.size();}
virtual int nParams()const = 0;
/// Returns the index of parameter name
virtual int parameterIndex(const std::string& name)const;
virtual int parameterIndex(const std::string& name)const = 0;
/// Returns the index of a parameter
virtual int parameterIndex(const double* p)const = 0;
/// Returns the name of parameter i
virtual std::string parameterName(int i)const;
virtual std::string parameterName(int i)const = 0;
/// Number of active (in terms of fitting) parameters
virtual int nActive()const{return m_indexMap.size();}
virtual int nActive()const = 0;
/// Value of i-th active parameter. Override this method to make fitted parameters different from the declared
virtual double activeParameter(int i)const;
/// Set new value of i-th active parameter. Override this method to make fitted parameters different from the declared
......@@ -113,24 +103,30 @@ public:
/// Update parameters after a fitting iteration
virtual void updateActive(const double* in);
/// Returns "global" index of active parameter i
virtual int indexOfActive(int i)const;
virtual int indexOfActive(int i)const = 0;
/// Returns the name of active parameter i
virtual std::string nameOfActive(int i)const;
virtual std::string nameOfActive(int i)const = 0;
/// Check if a declared parameter i is active
virtual bool isActive(int i)const;
/// Removes a declared parameter i from the list of active
virtual void removeActive(int i);
virtual bool isActive(int i)const = 0;
/// Get active index for a declared parameter i
virtual int activeIndex(int i)const;
virtual int activeIndex(int i)const = 0;
/// Removes a declared parameter i from the list of active
virtual void removeActive(int i) = 0;
/// Restores a declared parameter i to the active status
virtual void restoreActive(int i) = 0;
/// Tie a parameter to other parameters (or a constant)
virtual void tie(const std::string& parName,const std::string& expr);
/// Apply the ties
virtual void applyTies();
virtual void applyTies() = 0;
/// Removes the tie off a parameter
virtual void removeTie(const std::string& parName);
/// Remove all ties
virtual void clearTies() = 0;
/// Add a constraint to function
virtual void addConstraint(IConstraint* ic);
virtual void addConstraint(IConstraint* ic) = 0;
/// This method calls function() and add any penalty to its output if constraints are violated
void functionWithConstraint(double* out, const double* xValues, const int& nData);
......@@ -138,11 +134,23 @@ public:
void functionDerivWithConstraint(Jacobian* out, const double* xValues, const int& nData);
protected:
/// Function initialization. Declare function parameters in this method.
virtual void init(){};
/// Declare a new parameter
virtual void declareParameter(const std::string& name,double initValue = 0);
virtual void declareParameter(const std::string& name,double initValue = 0) = 0;
/// Create an instance of a tie without actually tying it to anything
virtual ParameterTie* createTie(const std::string& parName);
/// Add a new tie
virtual void addTie(ParameterTie* tie) = 0;
/// Removes i-th parameter's tie
virtual bool removeTie(int i) = 0;
/// Get the tie of i-th parameter
virtual ParameterTie* getTie(int i)const = 0;
friend class CompositeFunction;
/// Shared pointer to the workspace
boost::shared_ptr<const DataObjects::Workspace2D> m_workspace;
/// Spectrum index
......@@ -152,17 +160,6 @@ protected:
/// Upper bin index
int m_xMaxIndex;
private:
/// The index map. m_indexMap[i] gives the total index for active parameter i
std::vector<int> m_indexMap;
/// Keeps parameter names
std::vector<std::string> m_parameterNames;
/// Keeps parameter values
std::vector<double> m_parameters;
/// Holds parameter ties
std::vector<std::pair<int,ParameterTie*> > m_ties;
/// Holds the constraints added to function
std::vector<IConstraint*> m_constraints;
};
/** Represents the Jacobian in functionDeriv.
......@@ -173,7 +170,7 @@ class Jacobian
public:
/** Set a value to a Jacobian matrix element.
* @param iY The index of a data point.
* @param iP The index of a declare parameter.
* @param iP The index of a declared parameter.
* @param value The derivative value.
*/
virtual void set(int iY, int iP, double value) = 0;
......
......@@ -4,7 +4,8 @@
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/IFunction.h"
#include "MantidKernel/System.h"
//#include "MantidAPI/IFunction.h"
namespace Mantid
{
......@@ -38,7 +39,7 @@ namespace API
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport IFunctionWithLocation : public IFunction
class DLLExport IFunctionWithLocation //: public IFunction
{
public:
/// Returns the centre of the function, which may be something as simple as the centre of
......@@ -60,4 +61,4 @@ public:
} // namespace API
} // namespace Mantid
#endif /*MANTID_API_IIFUNCTIONWITHLOCATION_H_*/
#endif /*MANTID_API_IIFUNCTIONWITHLOCATION_H_*/
......@@ -5,6 +5,7 @@
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/IFunctionWithLocation.h"
#include "MantidAPI/Function.h"
namespace Mantid
{
......@@ -36,7 +37,7 @@ namespace API
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport IPeakFunction : public IFunctionWithLocation
class DLLExport IPeakFunction : public IFunctionWithLocation, public Function
{
public:
/// Returns the peak FWHM
......
......@@ -16,7 +16,10 @@ namespace Mantid
namespace API
{
class IFunction;
/** Ties fitting parameters.
/** Ties fitting parameters. A tie is a formula that is used to
calculate the value of a function parameter based on the values of other parameters.
A tied parameter is not considered independent and doesn't take part in fitting.
Its value is always calculated with its tie's formula.
@author Roman Tolchenov, Tessella Support Services plc
@date 28/10/2009
......@@ -47,21 +50,25 @@ public:
/// Constructor
ParameterTie(IFunction* funct,const std::string& parName);
/// Destructor
~ParameterTie();
virtual ~ParameterTie();
/// Set the tie expression
void set(const std::string& expr);
virtual void set(const std::string& expr);
/// Evaluate the expression
double eval();
/// The index of the tied parameter
int index()const{return m_iPar;}
virtual double eval();
private:
/// MuParser callback function
static double* AddVariable(const char *varName, void *palg);
const double* parameter()const{return m_par;}
/// Check if the tie has any references to certain parameters
bool findParameters(const std::vector<const double*>& pars)const;
protected:
mu::Parser* m_parser; ///< math parser
IFunction* m_function; ///< Pointer to the function which parameter is to be tied
int m_iPar; ///< index of the tied parameter
double* m_par; ///< Pointer to the tied parameter
//int m_iPar; ///< index of the tied parameter
private:
/// MuParser callback function
static double* AddVariable(const char *varName, void *palg);
};
} // namespace API
......
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/CompositeFunction.h"
#include "MantidKernel/Exception.h"
#include "MantidAPI/CompositeFunction.h"
#include "MantidAPI/ParameterTie.h"
#include <boost/lexical_cast.hpp>
#include <boost/shared_array.hpp>
......@@ -188,6 +189,24 @@ int CompositeFunction::parameterIndex(const std::string& name)const
return m_paramOffsets[index] + getFunction(index)->parameterIndex(pname);
}
/**
* Checks that a pointer points to a parameter of this function and returns its index.
* @param p A pointer to a double variable.
* @retrun The index of the parameter or -1 if p is not a pointer to any of the function's parameters.
*/
int CompositeFunction::parameterIndex(const double* p)const
{
for(int iFun=0;iFun<nFunctions();iFun++)
{
int i = m_functions[iFun]->parameterIndex(p);
if (i >= 0)
{
return m_paramOffsets[iFun] + i;
}
}
return -1;
}
/// Returns the name of parameter i
std::string CompositeFunction::parameterName(int i)const
{
......@@ -263,11 +282,30 @@ void CompositeFunction::removeActive(int i)
m_iFunctionActive.erase(m_iFunctionActive.begin()+ia);
m_functions[ iFun ]->removeActive(i - m_paramOffsets[iFun]);
--m_nActive;
m_nActive--;
for(int j=iFun+1;j<nFunctions();j++)
m_activeOffsets[j] -= 1;
}
/** Makes a parameter active again. It doesn't change the parameter's tie.
* @param i A declared parameter index to be restored to active
*/
void CompositeFunction::restoreActive(int i)
{
int iFun = functionIndex(i);
int ia = m_activeOffsets[iFun] + m_functions[iFun]->activeIndex(i - m_paramOffsets[iFun]);
std::vector<int>::iterator itFun =
std::find_if(m_iFunctionActive.begin(),m_iFunctionActive.end(),std::bind2nd(std::greater<int>(),i));
m_iFunctionActive.insert(itFun,1,ia);
m_functions[ iFun ]->restoreActive(i - m_paramOffsets[iFun]);
m_nActive++;
for(int j=iFun+1;j<nFunctions();j++)
m_activeOffsets[j] += 1;
}
/**
* @param i The index of a declared parameter