Commit a5af5797 authored by Roman Tolchenov's avatar Roman Tolchenov
Browse files

Re #4158. Refactored function interfaces to use domains and Values.

parent 125eb674
......@@ -14,9 +14,8 @@ set ( SRC_FILES
src/CloneableAlgorithm.cpp
src/Column.cpp
src/ColumnFactory.cpp
src/CompositeDomainMD.cpp
src/CompositeFunction.cpp
src/CompositeFunctionMD.cpp
src/CompositeFunctionMW.cpp
src/ConstraintFactory.cpp
src/CoordTransform.cpp
src/CostFunctionFactory.cpp
......@@ -27,14 +26,14 @@ set ( SRC_FILES
src/FileFinder.cpp
src/FileProperty.cpp
src/FrameworkManager.cpp
src/FunctionDomain.cpp
src/FunctionDomain1D.cpp
src/FunctionDomainMD.cpp
src/FunctionFactory.cpp
src/FunctionProperty.cpp
src/FunctionValues.cpp
src/IDataFileChecker.cpp
src/IEventList.cpp
src/IEventWorkspace.cpp
src/IFitFunction.cpp
src/IFunction.cpp
src/IFunctionMD.cpp
src/IFunction1D.cpp
......@@ -78,7 +77,6 @@ set ( SRC_FILES
src/SpectraAxis.cpp
src/SpectraDetectorMap.cpp
src/TableRow.cpp
src/TempFunction.cpp
src/TextAxis.cpp
src/Workspace.cpp
src/WorkspaceFactory.cpp
......@@ -115,8 +113,8 @@ set ( INC_FILES
inc/MantidAPI/Column.h
inc/MantidAPI/ColumnFactory.h
inc/MantidAPI/CompositeFunction.h
inc/MantidAPI/CompositeFunctionMD.h
inc/MantidAPI/CompositeFunctionMW.h
inc/MantidAPI/CompositeDomain.h
inc/MantidAPI/CompositeDomainMD.h
inc/MantidAPI/ConstraintFactory.h
inc/MantidAPI/CoordTransform.h
inc/MantidAPI/CostFunctionFactory.h
......@@ -131,8 +129,10 @@ set ( INC_FILES
inc/MantidAPI/FrameworkManager.h
inc/MantidAPI/FunctionDomain.h
inc/MantidAPI/FunctionDomain1D.h
inc/MantidAPI/FunctionDomainMD.h
inc/MantidAPI/FunctionFactory.h
inc/MantidAPI/FunctionProperty.h
inc/MantidAPI/FunctionValues.h
inc/MantidAPI/IAlgorithm.h
inc/MantidAPI/IArchiveSearch.h
inc/MantidAPI/IBackgroundFunction.h
......@@ -143,7 +143,6 @@ set ( INC_FILES
inc/MantidAPI/IDataItem.h
inc/MantidAPI/IEventList.h
inc/MantidAPI/IEventWorkspace.h
inc/MantidAPI/IFitFunction.h
inc/MantidAPI/IFunction.h
inc/MantidAPI/IFunctionMD.h
inc/MantidAPI/IFunction1D.h
......@@ -197,7 +196,6 @@ set ( INC_FILES
inc/MantidAPI/SpectraAxis.h
inc/MantidAPI/SpectraDetectorMap.h
inc/MantidAPI/TableRow.h
inc/MantidAPI/TempFunction.h
inc/MantidAPI/TextAxis.h
inc/MantidAPI/VectorParameter.h
inc/MantidAPI/VectorParameterParser.h
......@@ -256,7 +254,6 @@ set ( TEST_FILES
test/SampleTest.h
test/SpectraAxisTest.h
test/SpectraDetectorMapTest.h
test/TempFunctionTest.h
test/TextAxisTest.h
test/VectorParameterParserTest.h
test/VectorParameterTest.h
......
#ifndef MANTID_API_COMPOSITEDOMAIN_H_
#define MANTID_API_COMPOSITEDOMAIN_H_
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/DllConfig.h"
#include "MantidAPI/FunctionDomain.h"
#include <stdexcept>
namespace Mantid
{
namespace API
{
/** Base class that represents the domain of a function.
A domain is a generalisation of x (argument) and y (value) arrays.
A domain consists at least of a list of function arguments for which a function should
be evaluated and a buffer for the calculated values. If used in fitting also contains
the fit data and weights.
@author Roman Tolchenov, Tessella plc
@date 15/11/2011
Copyright &copy; 2009 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National 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 MANTID_API_DLL CompositeDomain: public FunctionDomain
{
public:
/// Return the number of parts in the domain
virtual size_t getNParts() const = 0;
/// Return i-th domain
virtual const FunctionDomain& getDomain(size_t i) const = 0;
};
} // namespace API
} // namespace Mantid
#endif /*MANTID_API_COMPOSITEDOMAIN_H_*/
#ifndef MANTID_API_COMPOSITEDOMAINMD_H_
#define MANTID_API_COMPOSITEDOMAINMD_H_
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/CompositeDomain.h"
#include "MantidAPI/IMDWorkspace.h"
#include <vector>
namespace Mantid
{
namespace API
{
class FunctionDomainMD;
/** Base class that represents the domain of a function.
A domain is a generalisation of x (argument) and y (value) arrays.
A domain consists at least of a list of function arguments for which a function should
be evaluated and a buffer for the calculated values. If used in fitting also contains
the fit data and weights.
@author Roman Tolchenov, Tessella plc
@date 15/11/2011
Copyright &copy; 2009 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National 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 MANTID_API_DLL CompositeDomainMD: public CompositeDomain
{
public:
CompositeDomainMD(IMDWorkspace_const_sptr ws, size_t maxDomainSize);
~CompositeDomainMD();
/// Return the total number of arguments in the domain
virtual size_t size() const {return m_totalSize;}
/// Return the number of parts in the domain
virtual size_t getNParts() const {return m_domains.size();}
/// Return i-th domain
virtual const FunctionDomain& getDomain(size_t i) const;
protected:
mutable IMDIterator* m_iterator; ///< IMDIterator
size_t m_totalSize; ///< The total size of the domain
mutable std::vector<FunctionDomainMD*> m_domains; ///< smaller parts of the domain
};
} // namespace API
} // namespace Mantid
#endif /*MANTID_API_COMPOSITEDOMAINMD_H_*/
......@@ -4,7 +4,7 @@
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/IFitFunction.h"
#include "MantidAPI/IFunction.h"
#include "MantidAPI/Jacobian.h"
#include <boost/shared_array.hpp>
......@@ -12,7 +12,6 @@ namespace Mantid
{
namespace API
{
class MatrixWorkspace;
/** A composite function.
@author Roman Tolchenov, Tessella Support Services plc
......@@ -38,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 MANTID_API_DLL CompositeFunction : public virtual IFitFunction
class MANTID_API_DLL CompositeFunction : public virtual IFunction
{
public:
/// Default constructor
......@@ -52,8 +51,15 @@ public:
/* Overriden methods */
/// Returns the function's name
virtual std::string name()const {return "CompositeFunction";}
/// Writes itself into a string
std::string asString()const;
/// Function you want to fit to.
/// @param domain :: The buffer for writing the calculated values. Must be big enough to accept dataSize() values
virtual void function(const FunctionDomain& domain, FunctionValues& values)const;
/// Derivatives of function with respect to active parameters
virtual void functionDeriv(const FunctionDomain& domain, Jacobian& jacobian);
/// Set i-th parameter
void setParameter(size_t, const double& value, bool explicitlySet = true);
......@@ -86,21 +92,17 @@ public:
void setActiveParameter(size_t i, double value);
/// Update parameters after a fitting iteration
void updateActive(const double* in);
/// Returns "global" index of active parameter i
size_t indexOfActive(size_t i)const;
/// Returns the name of active parameter i
std::string nameOfActive(size_t i)const;
/// Returns the name of active parameter i
std::string descriptionOfActive(size_t i)const;
/// Check if a parameter is active
bool isActive(size_t i)const;
/// Get active index for a declared parameter i
size_t activeIndex(size_t i)const;
bool isFixed(size_t i)const;
/// Removes a parameter from the list of active
void removeActive(size_t i);
void fix(size_t i);
/// Restores a declared parameter i to the active status
void restoreActive(size_t i);
void unfix(size_t i);
/// Return parameter index from a parameter reference.
size_t getParameterIndex(const ParameterReference& ref)const;
......
......@@ -27,7 +27,7 @@ namespace API
// More forward declarations
//----------------------------------------------------------------------
class IConstraint;
class IFitFunction;
class IFunction;
class Expression;
/** @class Mantid::API::ConstraintFactoryImpl
......@@ -68,13 +68,13 @@ namespace API
* @param input :: The creation expression
* @return A pointer to the created Constraint
*/
IConstraint* createInitialized(IFitFunction* fun, const std::string& input) const;
IConstraint* createInitialized(IFunction* fun, const std::string& input) const;
/**Creates an instance of a Constraint
* @param fun :: The function
* @param expr :: The creation expression
* @return A pointer to the created Constraint
*/
IConstraint* createInitialized(IFitFunction* fun, const Expression& expr) const;
IConstraint* createInitialized(IFunction* fun, const Expression& expr) const;
private:
friend struct Mantid::Kernel::CreateUsingNew<ConstraintFactoryImpl>;
......
......@@ -5,9 +5,8 @@
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/DllConfig.h"
#include "MantidKernel/Exception.h"
#include <vector>
#include <stdexcept>
namespace Mantid
{
......@@ -45,31 +44,12 @@ namespace API
class MANTID_API_DLL FunctionDomain
{
public:
/// Constructor.
FunctionDomain(size_t n);
/// Virtual destructor
virtual ~FunctionDomain(){}
/// Return the number of points, values, etc in the domain
virtual size_t size() const {return m_calculated.size();}
/// store i-th calculated value. 0 <= i < size()
virtual void setCalculated(size_t i,double value) {m_calculated[i] = value;}
/// get i-th calculated value. 0 <= i < size()
virtual double getCalculated(size_t i) const {return m_calculated[i];}
/// set a fitting data value
virtual void setFitData(size_t i,double value);
virtual void setFitData(const std::vector<double>& values);
/// get a fitting data value
virtual double getFitData(size_t i) const;
/// set a fitting weight
virtual void setFitWeight(size_t i,double value);
virtual void setFitWeights(const std::vector<double>& values);
/// get a fitting weight
virtual double getFitWeight(size_t i) const;
protected:
void setDataSize();
std::vector<double> m_calculated; ///< buffer for calculated values
std::vector<double> m_data; ///< buffer for fit data
std::vector<double> m_weights; ///< buffer for fitting weights (reciprocal errors)
/// Return the number of points in the domain
virtual size_t size() const = 0;
/// Reset the the domain so it can be reused. Implement this method for domains with a state.
virtual void reset() const {}
};
} // namespace API
......
......@@ -45,9 +45,11 @@ class MANTID_API_DLL FunctionDomain1D: public FunctionDomain
{
public:
FunctionDomain1D(const std::vector<double>& xvalues);
/// get an x value
/// Return the number of arguments in the domain
virtual size_t size() const {return m_X.size();}
/// Get an x value.
/// @param i :: Index
double getX(size_t i) const {return m_X.at(i);}
const double* operator[](size_t i) const {return &m_X.at(i);}
protected:
std::vector<double> m_X; ///< vector of function arguments
};
......
#ifndef MANTID_API_FUNCTIONDOMAINMD_H_
#define MANTID_API_FUNCTIONDOMAINMD_H_
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/FunctionDomain.h"
#include "MantidAPI/IMDWorkspace.h"
namespace Mantid
{
namespace API
{
/** Base class that represents the domain of a function.
A domain is a generalisation of x (argument) and y (value) arrays.
A domain consists at least of a list of function arguments for which a function should
be evaluated and a buffer for the calculated values. If used in fitting also contains
the fit data and weights.
@author Roman Tolchenov, Tessella plc
@date 15/11/2011
Copyright &copy; 2009 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National 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 MANTID_API_DLL FunctionDomainMD: public FunctionDomain
{
public:
/// Constructor.
FunctionDomainMD(IMDWorkspace_const_sptr ws, size_t start = 0, size_t length = 0);
/// Destructor.
~FunctionDomainMD();
/// Return the number of arguments in the domain
virtual size_t size() const {return m_size;}
/// Reset the iterator to point to the start of the domain.
virtual void reset() const;
/// Next iterator.
const IMDIterator* getNextIterator() const;
protected:
/// IMDIterator
mutable IMDIterator* m_iterator;
/// start of the domain, 0 <= m_startIndex < m_iterator->getDataSize()
const size_t m_startIndex;
/// track the iterator's index, 0 <= m_currentIndex < m_size.
mutable size_t m_currentIndex;
/// The size of the domain
size_t m_size;
};
} // namespace API
} // namespace Mantid
#endif /*MANTID_API_FUNCTIONDOMAINMD_H_*/
......@@ -26,7 +26,7 @@ namespace API
//----------------------------------------------------------------------
// More forward declarations
//----------------------------------------------------------------------
class IFitFunction;
class IFunction;
class CompositeFunction;
class Expression;
......@@ -60,20 +60,20 @@ namespace API
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>
*/
class MANTID_API_DLL FunctionFactoryImpl : public Kernel::DynamicFactory<IFitFunction>
class MANTID_API_DLL FunctionFactoryImpl : public Kernel::DynamicFactory<IFunction>
{
public:
/**Creates an instance of a function
* @param type :: The function's type
* @return A pointer to the created function
*/
IFitFunction* createFunction(const std::string& type) const;
IFunction* createFunction(const std::string& type) const;
///Creates an instance of a function
IFitFunction* createInitialized(const std::string& input) const;
IFunction* createInitialized(const std::string& input) const;
///Creates an instance of a function
IFitFunction* createFitFunction(const std::string& input) const;
IFunction* createFitFunction(const std::string& input) const;
/// Query available functions based on the template type
template<typename FunctionType>
......@@ -92,26 +92,26 @@ namespace API
virtual ~FunctionFactoryImpl();
/// These methods shouldn't be used to create functions
using Kernel::DynamicFactory<IFitFunction>::create;
using Kernel::DynamicFactory<IFitFunction>::createUnwrapped;
using Kernel::DynamicFactory<IFunction>::create;
using Kernel::DynamicFactory<IFunction>::createUnwrapped;
/// Create a simple function
IFitFunction* createSimple(const Expression& expr)const;
IFunction* createSimple(const Expression& expr)const;
/// Create a composite function
CompositeFunction* createComposite(const Expression& expr)const;
///Creates an instance of a function
IFitFunction* createFitFunction(const Expression& expr) const;
IFunction* createFitFunction(const Expression& expr) const;
/// Throw an exception
void inputError(const std::string& str="")const;
/// Add constraints to the created function
void addConstraints(IFitFunction* fun,const Expression& expr)const;
void addConstraints(IFunction* fun,const Expression& expr)const;
/// Add a single constraint to the created function
void addConstraint(IFitFunction* fun,const Expression& expr)const;
void addConstraint(IFunction* fun,const Expression& expr)const;
/// Add ties to the created function
void addTies(IFitFunction* fun,const Expression& expr)const;
void addTies(IFunction* fun,const Expression& expr)const;
/// Add a tie to the created function
void addTie(IFitFunction* fun,const Expression& expr)const;
void addTie(IFunction* fun,const Expression& expr)const;
///static reference to the logger class
Kernel::Logger& g_log;
......@@ -132,7 +132,7 @@ namespace API
for( std::vector<std::string>::const_iterator it = names.begin();
it != names.end(); ++it )
{
IFitFunction *func = this->createFitFunction(*it);
IFunction *func = this->createFitFunction(*it);
if( dynamic_cast<FunctionType*>(func) )
{
typeNames.push_back(*it);
......
#ifndef MANTID_API_FUNCTIONVALUES_H_
#define MANTID_API_FUNCTIONVALUES_H_
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/DllConfig.h"
#include "MantidAPI/FunctionDomain.h"
#include <vector>
namespace Mantid
{
namespace API
{
/** Base class that represents the domain of a function.
A domain is a generalisation of x (argument) and y (value) arrays.
A domain consists at least of a list of function arguments for which a function should
be evaluated and a buffer for the calculated values. If used in fitting also contains
the fit data and weights.
@author Roman Tolchenov, Tessella plc
@date 15/11/2011
Copyright &copy; 2009 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National 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 MANTID_API_DLL FunctionValues
{
public:
/// Constructor.
FunctionValues(const FunctionDomain& domain);
/// Copy constructor.
FunctionValues(const FunctionValues& values);
/// Return the number of points, values, etc in the domain
size_t size() const {return m_calculated.size();}
/// store i-th calculated value. 0 <= i < size()
void setCalculated(size_t i,double value) {m_calculated[i] = value;}
/// get i-th calculated value. 0 <= i < size()
double getCalculated(size_t i) const {return m_calculated[i];}
/// Get a pointer to calculated data at index i
double* getPointerToCalculated(size_t i);
/// Add other calculated values
FunctionValues& operator+=(const FunctionValues& values);
/// set a fitting data value
void setFitData(size_t i,double value);
void setFitData(const std::vector<double>& values);
/// get a fitting data value
double getFitData(size_t i) const;
/// set a fitting weight
void setFitWeight(size_t i,double value);
void setFitWeights(const std::vector<double>& values);
/// get a fitting weight
double getFitWeight(size_t i) const;
protected:
void setDataSize();
std::vector<double> m_calculated; ///< buffer for calculated values
std::vector<double> m_data; ///< buffer for fit data
std::vector<double> m_weights; ///< buffer for fitting weights (reciprocal errors)
};
} // namespace API
} // namespace Mantid
#endif /*MANTID_API_FUNCTIONVALUES_H_*/
......@@ -4,7 +4,7 @@
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/IFitFunction.h"
#include "MantidAPI/IFunction.h"
#include "MantidAPI/ParameterReference.h"
namespace Mantid
......@@ -49,7 +49,7 @@ public:
virtual ~IConstraint() {}
/// Initialize the constraint from an expression
virtual void initialize(IFitFunction* fun, const Expression& expr) = 0;
virtual void initialize(IFunction* fun, const Expression& expr) = 0;
/// Returns a penalty number which is bigger than or equal to zero