-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re #4158. Started refactoring functions
- Loading branch information
1 parent
b06a486
commit 125eb67
Showing
13 changed files
with
663 additions
and
509 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
Code/Mantid/Framework/API/inc/MantidAPI/FunctionDomain1D.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#ifndef MANTID_API_FUNCTIONDOMAIN1D_H_ | ||
#define MANTID_API_FUNCTIONDOMAIN1D_H_ | ||
|
||
//---------------------------------------------------------------------- | ||
// Includes | ||
//---------------------------------------------------------------------- | ||
#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 © 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 FunctionDomain1D: public FunctionDomain | ||
{ | ||
public: | ||
FunctionDomain1D(const std::vector<double>& xvalues); | ||
/// get an x value | ||
/// @param i :: Index | ||
double getX(size_t i) const {return m_X.at(i);} | ||
protected: | ||
std::vector<double> m_X; ///< vector of function arguments | ||
}; | ||
|
||
} // namespace API | ||
} // namespace Mantid | ||
|
||
#endif /*MANTID_API_FUNCTIONDOMAIN1D_H_*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
#ifndef MANTID_API_IFUNCTION1D_H_ | ||
#define MANTID_API_IFUNCTION1D_H_ | ||
|
||
//---------------------------------------------------------------------- | ||
// Includes | ||
//---------------------------------------------------------------------- | ||
#include "MantidAPI/DllConfig.h" | ||
#include "MantidAPI/IFunction.h" | ||
#include "MantidAPI/FunctionDomain1D.h" | ||
|
||
namespace Mantid | ||
{ | ||
|
||
namespace CurveFitting | ||
{ | ||
class Fit; | ||
} | ||
|
||
namespace API | ||
{ | ||
|
||
//---------------------------------------------------------------------- | ||
// Forward declaration | ||
//---------------------------------------------------------------------- | ||
class MatrixWorkspace; | ||
class Jacobian; | ||
class ParameterTie; | ||
class IConstraint; | ||
class ParameterReference; | ||
class FunctionHandler; | ||
/** This is an interface to a fitting function - a semi-abstarct class. | ||
Functions derived from IFunctionMW can be used with the Fit algorithm. | ||
IFunctionMW defines the structure of a fitting funtion. | ||
A function has a number of named parameters (not arguments), type double, on which it depends. | ||
Parameters must be declared either in the constructor or in the init() method | ||
of a derived class with method declareParameter(...). Method nParams() returns | ||
the number of declared parameters. A parameter can be accessed either by its name | ||
or the index. For example in case of Gaussian the parameters can be "Height", | ||
"PeakCentre" and "Sigma". | ||
To fit a function to a set of data its parameters must be adjusted so that the difference | ||
between the data and the corresponding function values were minimized. This is the aim | ||
of the Fit algorithm. But Fit does not work with the declared parameters directly. | ||
Instead it uses other - active - parameters. The active parameters can be a subset of the | ||
declared parameters or completely different ones. The rationale for this is following. | ||
The fitting parameters sometimes need to be fixed during the fit or "tied" (expressed | ||
in terms of other parameters). In this case the active parameters will be those | ||
declared parameters which are not tied in any sence. Also some of the declared parameters | ||
can be unsuitable for the use in a fitting algorithm. In this case different active parameters | ||
can be used in place of the inefficient declared parameters. An example is Gaussian where | ||
"Sigma" makes the fit unstable. So in the fit it can be replaced with variable Weight = 1 / Sigma | ||
which is more efficient. The number of active parameters (returned by nActive()) cannot be | ||
greater than nParams(). The function which connects the active parameters with the declared ones | ||
must be monotonic so that the forward and backward transformations between the two sets are | ||
single-valued (this is my understanding). At the moment only simple one to one transformations | ||
of Weight - Sigma type are allowed. More complecated cases of simultaneous transformations of | ||
several parameters are not supported. | ||
The active parameters can be accessed by their index. The implementations of the access method | ||
for both active and declared parameters must ensure that any changes to one of them | ||
immediately reflected on the other so that the two are consistent at any moment. | ||
IFunctionMW declares method nameOfActive(size_t i) which returns the name of the declared parameter | ||
corresponding to the i-th active parameter. I am not completely sure in the usefulness of it. | ||
IFunctionMW provides methods for tying and untying parameters. Only the declared parameters can be | ||
tied. A tied parameter cannot be active. When a parameter is tied it becomes inactive. | ||
This implies that the number of active parameters is variable and can change at runtime. | ||
Method addConstraint adds constraints on possible values of a declared parameter. Constraints | ||
and ties are used only in fitting. | ||
The main method of IFunctionMW is called function(out,xValues,nData). It calculates nData output values | ||
out[i] at arguments xValues[i]. Implement functionDeriv method for the function to be used with | ||
fitting algorithms using derivatives. functionDeriv calculates patrial derivatives of the | ||
function with respect to the fitting parameters. | ||
Any non-fitting parameters can be implemented as attributes (class IFunctionMW::Attribute). | ||
An attribute can have one of three types: std::string, int, or double. The type is set at construction | ||
and cannot be changed later. To read or write the attributes there are two ways. If the type | ||
is known the type specific accessors can be used, e.g. asString(), asInt(). Otherwise the | ||
IFunctionMW::AttributeVisitor can be used. It provides alternative virtual methods to access | ||
attributes of each type. When creating a function from a string (using FunctionFactory::creaeInitialized(...)) | ||
the attributes must be set first, before any fitting parameter, as the number and names of the parameters | ||
can depend on the attributes. | ||
@author Roman Tolchenov, Tessella Support Services plc | ||
@date 16/10/2009 | ||
Copyright © 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 IFunction1D: public virtual IFunction | ||
{ | ||
public: | ||
|
||
/// Constructor | ||
IFunction1D():IFunction(){} | ||
|
||
/* Overidden methods */ | ||
|
||
virtual void function(FunctionDomain& )const; | ||
void functionDeriv(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. | ||
virtual void function1D(FunctionDomain1D&)const = 0; | ||
/// Derivatives of function with respect to active parameters | ||
virtual void functionDeriv1D(FunctionDomain1D&, Jacobian& out); | ||
|
||
/// Calculate numerical derivatives | ||
void calNumericalDeriv(FunctionDomain1D& domain, Jacobian& out); | ||
|
||
mutable std::vector<double> m_tmpFunctionOutputMinusStep; | ||
mutable std::vector<double> m_tmpFunctionOutputPlusStep; | ||
|
||
/// Static reference to the logger class | ||
static Kernel::Logger& g_log; | ||
|
||
/// Making a friend | ||
friend class CurveFitting::Fit; | ||
|
||
}; | ||
|
||
} // namespace API | ||
} // namespace Mantid | ||
|
||
#endif /*MANTID_API_IFUNCTION1D_H_*/ |
Oops, something went wrong.