function.h 3.14 KB
Newer Older
1 2
#pragma once

3 4 5
#include <functional>
#include <string>

6 7
#include <QtPlugin>

8
#include "common.h"
9
#include "args.h"
10 11

class data ;
12

Laurent Belcour's avatar
Laurent Belcour committed
13
/*! \brief A representation of an analytical function.
14
 *  \ingroup core
Laurent Belcour's avatar
Laurent Belcour committed
15
 *
16 17 18 19 20 21
 *  function are functors with a domain of definition specified by a vector 
 *  interval \f$[\vec{min} .. \vec{max}]\f$ where \f$\vec{min}\f$ and 
 *  \f$\vec{max}\f$ have the size of the input domain.
 *
 *  Any function used by the fitting algorithm should overload publicly this
 *  interface.
22 23
 *
 *  \todo how-to write a plugin here ?
Laurent Belcour's avatar
Laurent Belcour committed
24
 */
Laurent Belcour's avatar
Laurent Belcour committed
25
class function 
26 27 28 29
{
	public: // methods

		// Overload the function operator
30
		virtual vec operator()(const vec& x) const = 0 ;
Laurent Belcour's avatar
Laurent Belcour committed
31
		virtual vec value(const vec& x) const = 0 ;
32

33
		//! Load function specific files
34
		virtual void load(const std::string& filename) = 0 ;
35 36 37 38 39

		//! \brief Save the current function to a specific file type, args can 
		//! be used to differenciate the type of export.
		//!
		//! \see rational_function.cpp for an example
40
		virtual void save(const std::string& filename, const arguments& args) const = 0 ;
41

42
		//! Provide the dimension of the input space of the function
43
		virtual int dimX() const { return _nX ; }
44
		//! Provide the dimension of the output space of the function
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
		virtual int dimY() const { return _nY ; }

		virtual void setDimX(int nX) { _nX = nX ; }
		virtual void setDimY(int nY) { _nY = nY ; }

		// Acces to the domain of definition of the function
		virtual void setMin(const vec& min) 
		{
			assert(min.size() == _nX) ;
		  	_min = min ; 
		}
		virtual void setMax(const vec& max) 
		{
			assert(max.size() == _nX) ;
			_max = max ; 
		}
		virtual vec getMin() const { return _min ; }
		virtual vec getMax() const { return _max ; }

64

65
	protected: //data
66

67
		// Dimension of the function & domain of definition.
68
		int _nX, _nY ;
69
		vec _min, _max ;
70 71
};

72 73 74 75 76 77 78
/*! \brief Non-linear function interface
 *
 * Provide a way to obtain the dérivative of the function with respect to its
 * parameters. If the function \f$f(\vec{x})\f$ is defined for a vector of
 * parameters \f$\vec{a}\f$, the resulting vector is \f$df_i = {df \over 
 * da_i}\f$. 
 *
79
 * \note It is not necessary to have an analytical formulation
80 81
 * of the derivative and a numerical evaluation of it can be provided.
 *
82
 * \todo Add a Levenberg-Marquardt fitter for this generic kind of function
83
 */
84 85 86 87
class nonlinear_function: public function
{
	public: // methods

88 89 90
		//! Number of parameters to this non-linear function
		virtual int nbParameters() const = 0;

91
		//! Get the vector of parameters for the function
92
		virtual vec parameters() const = 0;
93 94

		//! Update the vector of parameters for the function
95 96
		virtual void setParameters(const vec& p) = 0;

97 98 99 100
		//! \brief Obtain the derivatives of the function with respect to the 
		//! parameters. 
		//
		// The x input of this function is the position in the input space and 
101 102
		// has size dimX(), the resulting vector has the size of the parameters
		// times the size of the output domain.
103 104 105
		//
		// The result vector should be orderer as res[i + dimY()*j], output
		// dimension first, then parameters.
106
		virtual vec parametersJacobian(const vec& x) const = 0;
107
};
108

Laurent Belcour's avatar
Laurent Belcour committed
109
Q_DECLARE_INTERFACE(function, "Fitter.Function")