Skip to content

Commit

Permalink
Re #4158. Changed functions in CurveFitting
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Apr 2, 2012
1 parent 1b02431 commit cd16b57
Show file tree
Hide file tree
Showing 56 changed files with 705 additions and 190 deletions.
6 changes: 5 additions & 1 deletion Code/Mantid/Framework/API/inc/MantidAPI/FunctionDomain.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/DllConfig.h"
#include "MantidKernel/PropertyManager.h"

#include <stdexcept>

Expand Down Expand Up @@ -41,7 +42,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 FunctionDomain
class MANTID_API_DLL FunctionDomain: public Kernel::PropertyManager
{
public:
/// Virtual destructor
Expand All @@ -52,6 +53,9 @@ class MANTID_API_DLL FunctionDomain
virtual void reset() const {}
};

/// typedef for a shared pointer
typedef boost::shared_ptr<FunctionDomain> FunctionDomain_sptr;

} // namespace API
} // namespace Mantid

Expand Down
7 changes: 7 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/FunctionValues.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ namespace API
class MANTID_API_DLL FunctionValues
{
public:
/// Default constructor.
FunctionValues(){}
/// Constructor.
FunctionValues(const FunctionDomain& domain);
/// Copy constructor.
FunctionValues(const FunctionValues& values);
/// Reset the values to match a new domain.
void reset(const FunctionDomain& domain);
/// 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()
Expand Down Expand Up @@ -77,6 +81,9 @@ class MANTID_API_DLL FunctionValues
std::vector<double> m_weights; ///< buffer for fitting weights (reciprocal errors)
};

/// typedef for a shared pointer
typedef boost::shared_ptr<FunctionValues> FunctionValues_sptr;

} // namespace API
} // namespace Mantid

Expand Down
32 changes: 25 additions & 7 deletions Code/Mantid/Framework/API/inc/MantidAPI/ICostFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,35 @@ class MANTID_API_DLL ICostFunction
virtual std::string name() const = 0;

/// Get short name of minimizer - useful for say labels in guis
virtual std::string shortName() const {return "Quality";};

/// Calculate value of cost function from observed
/// and calculated values
virtual double val(const double* yData, const double* inverseError, double* yCal, const size_t& n) = 0;
virtual std::string shortName() const {return "Quality";}

/// Get i-th parameter
/// @param i :: Index of a parameter
/// @return :: Value of the parameter
virtual double getParameter(size_t i)const = 0;
/// Set i-th parameter
/// @param i :: Index of a parameter
/// @param value :: New value of the parameter
virtual void setParameter(size_t i, const double& value) = 0;
/// Number of parameters
virtual size_t nParams()const = 0;

/// Calculate value of cost function
virtual double val() const = 0;

/// Calculate the derivatives of the cost function
virtual void deriv(const double* yData, const double* inverseError, const double* yCal,
const double* jacobian, double* outDerivs, const size_t& p, const size_t& n) = 0;
/// @param der :: Container to output the derivatives
virtual void deriv(std::vector<double>& der) const = 0;

/// Calculate the value and the derivatives of the cost function
/// @param der :: Container to output the derivatives
/// @return :: The value of the function
virtual double valAndDeriv(std::vector<double>& der) const = 0;
};

/// define a shared pointer to a cost function
typedef boost::shared_ptr<ICostFunction> ICostFunction_sptr;

/**
* Macro for declaring a new type of cost functions to be used with the CostFunctionFactory
*/
Expand Down
7 changes: 6 additions & 1 deletion Code/Mantid/Framework/API/inc/MantidAPI/IFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ class MANTID_API_DLL IFunction
virtual void declareParameter(const std::string& name, double initValue = 0, const std::string& description="") = 0;

/// Calculate numerical derivatives
void calNumericalDeriv(const FunctionDomain& domain, Jacobian& out){}
void calNumericalDeriv(const FunctionDomain& domain, Jacobian& out);

/// Create an instance of a tie without actually tying it to anything
//virtual ParameterTie* createTie(const std::string& parName);
Expand All @@ -368,6 +368,11 @@ class MANTID_API_DLL IFunction
friend class ParameterTie;
friend class CompositeFunction;

/// Values storage for numeric derivatives
FunctionValues m_minusStep;
/// Values storage for numeric derivatives
FunctionValues m_plusStep;

/// Pointer to a function handler
FunctionHandler* m_handler;

Expand Down
6 changes: 0 additions & 6 deletions Code/Mantid/Framework/API/inc/MantidAPI/IFunction1D.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,6 @@ class MANTID_API_DLL IFunction1D: public virtual IFunction
virtual void function(const FunctionDomain& domain,FunctionValues& values)const;
void functionDeriv(const FunctionDomain& domain, Jacobian& jacobian);

//boost::shared_ptr<API::MatrixWorkspace> createCalculatedWorkspace(
// boost::shared_ptr<const API::MatrixWorkspace> inWS,
// size_t wi,
// const std::vector<double>& sd = std::vector<double>()
// );

protected:

/// Function you want to fit to.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//----------------------------------------------------------------------
#include "MantidAPI/DllConfig.h"
#include "MantidAPI/ParamFunction.h"
#include "MantidAPI/IFunctionMW.h"
#include "MantidAPI/IFunction1D.h"

namespace Mantid
{
Expand Down Expand Up @@ -40,7 +40,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 IFunctionWithLocation : public ParamFunction, public IFunctionMW
class MANTID_API_DLL IFunctionWithLocation : public ParamFunction, public IFunction1D
{
public:
/// Returns the centre of the function, which may be something as simple as the centre of
Expand Down
16 changes: 11 additions & 5 deletions Code/Mantid/Framework/API/src/FunctionValues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ namespace API
*/
FunctionValues::FunctionValues(const FunctionDomain& domain)
{
if (domain.size() == 0)
{
throw std::invalid_argument("FunctionValues cannot have zero size.");
}
m_calculated.resize(domain.size());
reset(domain);
}

/** Copy constructor.
Expand All @@ -33,6 +29,16 @@ namespace API
{
}

/// Reset the values to match a new domain.
void FunctionValues::reset(const FunctionDomain& domain)
{
if (domain.size() == 0)
{
throw std::invalid_argument("FunctionValues cannot have zero size.");
}
m_calculated.resize(domain.size());
}

/**
* Get a pointer to calculated data at index i
* @param i :: Index.
Expand Down
53 changes: 53 additions & 0 deletions Code/Mantid/Framework/API/src/IFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,5 +409,58 @@ void IFunction::Attribute::fromString(const std::string& str)
apply(tmp);
}

/** Calculate numerical derivatives.
* @param out :: Derivatives
* @param xValues :: X values for data points
* @param nData :: Number of data points
*/
void IFunction::calNumericalDeriv(const FunctionDomain& domain, Jacobian& jacobian)
{
const double minDouble = std::numeric_limits<double>::min();
const double epsilon = std::numeric_limits<double>::epsilon();
double stepPercentage = 0.001; // step percentage
double step; // real step
double cutoff = 100.0*minDouble/stepPercentage;
size_t nParam = nParams();
size_t nData = domain.size();

// allocate memory if not already done
if (m_minusStep.size() != domain.size())
{
m_minusStep.reset(domain);
m_plusStep.reset(domain);
}

function(domain,m_minusStep);

for (size_t iP = 0; iP < nParam; iP++)
{
if ( !isFixed(iP) )
{
const double& val = getParameter(iP);
if (fabs(val) < cutoff)
{
step = epsilon;
}
else
{
step = val*stepPercentage;
}

double paramPstep = val + step;
setParameter(iP, paramPstep);
function(domain,m_plusStep);

step = paramPstep - val;
setParameter(iP, val);

for (size_t i = 0; i < nData; i++) {
jacobian.set(i,iP,
(m_plusStep.getCalculated(i) - m_minusStep.getCalculated(i))/step);
}
}
}
}

} // namespace API
} // namespace Mantid
57 changes: 0 additions & 57 deletions Code/Mantid/Framework/API/src/IFunction1D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,63 +179,6 @@ void IFunction1D::functionDeriv1D(Jacobian*, const double*, const size_t)
// return ws;
//}

/** Calculate numerical derivatives.
* @param out :: Derivatives
* @param xValues :: X values for data points
* @param nData :: Number of data points
*/
//void IFunction1D::calNumericalDeriv(FunctionDomain1D& domain, Jacobian& jacobian)
//{
//const double minDouble = std::numeric_limits<double>::min();
//const double epsilon = std::numeric_limits<double>::epsilon();
//double stepPercentage = 0.001; // step percentage
//double step; // real step
//double cutoff = 100.0*minDouble/stepPercentage;
//size_t nParam = nParams();

//// allocate memory if not already done
//if (m_tmpFunctionOutputMinusStep.size() != domain.size())
//{
// m_tmpFunctionOutputMinusStep.resize(domain.size());
// m_tmpFunctionOutputPlusStep.resize(domain.size());
//}

//functionMW(m_tmpFunctionOutputMinusStep.get(), xValues, nData);

//for (size_t iP = 0; iP < nParam; iP++)
//{
// if ( isActive(iP) )
// {
// const double& val = getParameter(iP);
// if (fabs(val) < cutoff)
// {
// step = epsilon;
// }
// else
// {
// step = val*stepPercentage;
// }

// //double paramMstep = val - step;
// //setParameter(iP, paramMstep);
// //function(m_tmpFunctionOutputMinusStep.get(), xValues, nData);

// double paramPstep = val + step;
// setParameter(iP, paramPstep);
// functionMW(m_tmpFunctionOutputPlusStep.get(), xValues, nData);

// step = paramPstep - val;
// setParameter(iP, val);

// for (size_t i = 0; i < nData; i++) {
// // out->set(i,iP,
// // (m_tmpFunctionOutputPlusStep[i]-m_tmpFunctionOutputMinusStep[i])/(2.0*step));
// out->set(i,iP,
// (m_tmpFunctionOutputPlusStep[i]-m_tmpFunctionOutputMinusStep[i])/step);
// }
// }
//}
//}

} // namespace API
} // namespace Mantid
5 changes: 3 additions & 2 deletions Code/Mantid/Framework/API/test/CompositeFunctionTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/ParamFunction.h"
#include "MantidAPI/IFunction1D.h"

using namespace Mantid;
using namespace Mantid::API;
Expand Down Expand Up @@ -148,7 +149,7 @@ class Gauss: public IPeakFunction
};


class Linear: public ParamFunction, public IFunctionMW
class Linear: public ParamFunction, public IFunction1D
{
public:
Linear()
Expand Down Expand Up @@ -180,7 +181,7 @@ class Linear: public ParamFunction, public IFunctionMW

};

class Cubic: public ParamFunction, public IFunctionMW
class Cubic: public ParamFunction, public IFunction1D
{
public:
Cubic()
Expand Down
5 changes: 5 additions & 0 deletions Code/Mantid/Framework/CurveFitting/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ set ( SRC_FILES
src/BoundaryConstraint.cpp
src/Chebyshev.cpp
src/Convolution.cpp
src/CostFuncFitting.cpp
src/CostFuncIgnorePosPeaks.cpp
src/CostFuncLeastSquares.cpp
src/DeltaFunction.cpp
src/DiffSphere.cpp
src/DerivMinimizer.cpp
src/ExpDecay.cpp
src/ExpDecayMuon.cpp
src/ExpDecayOsc.cpp
Expand Down Expand Up @@ -64,10 +66,12 @@ set ( INC_FILES
inc/MantidCurveFitting/BoundaryConstraint.h
inc/MantidCurveFitting/Chebyshev.h
inc/MantidCurveFitting/Convolution.h
inc/MantidCurveFitting/CostFuncFitting.h
inc/MantidCurveFitting/CostFuncIgnorePosPeaks.h
inc/MantidCurveFitting/CostFuncLeastSquares.h
inc/MantidCurveFitting/DeltaFunction.h
inc/MantidCurveFitting/DiffSphere.h
inc/MantidCurveFitting/DerivMinimizer.h
inc/MantidCurveFitting/DllConfig.h
inc/MantidCurveFitting/ExpDecay.h
inc/MantidCurveFitting/ExpDecayMuon.h
Expand All @@ -78,6 +82,7 @@ set ( INC_FILES
inc/MantidCurveFitting/FlatBackground.h
inc/MantidCurveFitting/FuncMinimizerFactory.h
inc/MantidCurveFitting/GSLFunctions.h
inc/MantidCurveFitting/GSLJacobian.h
inc/MantidCurveFitting/GausDecay.h
inc/MantidCurveFitting/GausOsc.h
inc/MantidCurveFitting/Gaussian.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ namespace Mantid
/// overwrite IFunction base class methods
std::string name()const{return "BackToBackExponential";}
virtual const std::string category() const { return "Peak";}
virtual void functionMW(double* out, const double* xValues, const size_t nData)const;
virtual void functionDerivMW(API::Jacobian* out, const double* xValues, const size_t nData);
virtual void function1D(double* out, const double* xValues, const size_t nData)const;
virtual void functionDeriv1D(API::Jacobian* out, const double* xValues, const size_t nData);

protected:
/// overwrite IFunction base class method, which declare function parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------

#include "MantidCurveFitting/UserFunction.h"
#include "MantidAPI/IFitFunction.h"
#include "MantidAPI/IFunction1D.h"
#include "MantidKernel/Logger.h"
#include "MantidKernel/cow_ptr.h"
#include "MantidCurveFitting/BoundaryConstraint.h"
Expand Down Expand Up @@ -87,9 +86,9 @@ namespace Mantid

virtual const std::string category() const { return "Peak";}

void functionMW (double *out, const double *xValues, const size_t nData)const ;
void function1D (double *out, const double *xValues, const size_t nData)const ;

void functionDerivMW (API::Jacobian *out, const double *xValues, const size_t nData);
void functionDeriv1D (API::Jacobian *out, const double *xValues, const size_t nData);


size_t nAttributes () const
Expand Down

0 comments on commit cd16b57

Please sign in to comment.