Skip to content
Snippets Groups Projects
Commit 9ef89b88 authored by Lorenzo Moneta's avatar Lorenzo Moneta
Browse files

change for new function interface in MathCore

git-svn-id: http://root.cern.ch/svn/root/trunk@16812 27541ba8-7e3a-0410-8455-c3a389f83636
parent 7c0c5dbc
No related branches found
No related tags found
No related merge requests found
Showing
with 556 additions and 480 deletions
...@@ -29,10 +29,6 @@ MATHMOREDH := $(MATHMOREDS:.cxx=.h) ...@@ -29,10 +29,6 @@ MATHMOREDH := $(MATHMOREDS:.cxx=.h)
MATHMOREDH1 := $(MODDIRI)/Math/ProbFuncMathMore.h \ MATHMOREDH1 := $(MODDIRI)/Math/ProbFuncMathMore.h \
$(MODDIRI)/Math/ProbFuncInv.h \ $(MODDIRI)/Math/ProbFuncInv.h \
$(MODDIRI)/Math/SpecFuncMathMore.h \ $(MODDIRI)/Math/SpecFuncMathMore.h \
$(MODDIRI)/Math/IGenFunction.h \
$(MODDIRI)/Math/IParamFunction.h \
$(MODDIRI)/Math/ParamFunction.h \
$(MODDIRI)/Math/WrappedFunction.h \
$(MODDIRI)/Math/Polynomial.h \ $(MODDIRI)/Math/Polynomial.h \
$(MODDIRI)/Math/Derivator.h \ $(MODDIRI)/Math/Derivator.h \
$(MODDIRI)/Math/Interpolator.h \ $(MODDIRI)/Math/Interpolator.h \
......
// @(#)root/mathmore:$Name: $:$Id: Chebyshev.h,v 1.2 2005/09/19 15:17:08 brun Exp $ // @(#)root/mathmore:$Name: $:$Id: Chebyshev.h,v 1.3 2005/09/19 15:50:04 brun Exp $
// Authors: L. Moneta, A. Zsenei 08/2005 // Authors: L. Moneta, A. Zsenei 08/2005
/********************************************************************** /**********************************************************************
...@@ -35,15 +35,22 @@ ...@@ -35,15 +35,22 @@
@defgroup FuncApprox Function Approximation (Chebyshev) @defgroup FuncApprox Function Approximation (Chebyshev)
*/ */
#include <memory>
#ifndef ROOT_Math_IFunctionfwd
#include "Math/IFunctionfwd.h"
#endif
#ifndef ROOT_Math_GSLFunctionAdapter
#include "Math/GSLFunctionAdapter.h" #include "Math/GSLFunctionAdapter.h"
#endif
#include <memory>
namespace ROOT { namespace ROOT {
namespace Math { namespace Math {
class IGenFunction;
class GSLChebSeries; class GSLChebSeries;
class GSLFunctionWrapper; class GSLFunctionWrapper;
......
// @(#)root/mathmore:$Name: $:$Id: Derivator.h,v 1.1 2005/09/18 17:33:47 brun Exp $ // @(#)root/mathmore:$Name: $:$Id: Derivator.h,v 1.2 2006/06/08 16:36:17 moneta Exp $
// Authors: L. Moneta, A. Zsenei 08/2005 // Authors: L. Moneta, A. Zsenei 08/2005
/********************************************************************** /**********************************************************************
...@@ -37,8 +37,10 @@ ...@@ -37,8 +37,10 @@
@defgroup Deriv Numerical Differentiation @defgroup Deriv Numerical Differentiation
*/ */
#ifndef ROOT_Math_IFunctionfwd
#include "Math/IFunctionfwd.h"
#endif
#include "Math/IGenFunction.h"
namespace ROOT { namespace ROOT {
namespace Math { namespace Math {
...@@ -106,7 +108,7 @@ namespace Math { ...@@ -106,7 +108,7 @@ namespace Math {
adaptive central difference algorithm with a step size h adaptive central difference algorithm with a step size h
*/ */
double Eval(const IGenFunction & f, double x, double h = 1E-8); double Eval(const IGenFunction & f, double x, double h = 1E-8) const;
...@@ -116,7 +118,7 @@ namespace Math { ...@@ -116,7 +118,7 @@ namespace Math {
adaptive central difference algorithm with a step size h adaptive central difference algorithm with a step size h
*/ */
double Eval(double x, double h = 1E-8); double Eval(double x, double h = 1E-8) const;
...@@ -125,7 +127,7 @@ namespace Math { ...@@ -125,7 +127,7 @@ namespace Math {
difference algorithm with a step size h difference algorithm with a step size h
*/ */
double EvalCentral(const IGenFunction & f, double x, double h = 1E-8); double EvalCentral(const IGenFunction & f, double x, double h = 1E-8) const;
// template <class UserFunc> // template <class UserFunc>
// inline double EvalCentral(const UserFunc & f, double x, double h) { // inline double EvalCentral(const UserFunc & f, double x, double h) {
...@@ -140,7 +142,7 @@ namespace Math { ...@@ -140,7 +142,7 @@ namespace Math {
The function is evaluated only at points greater than x and at x itself The function is evaluated only at points greater than x and at x itself
*/ */
double EvalForward(const IGenFunction & f, double x, double h = 1E-8); double EvalForward(const IGenFunction & f, double x, double h = 1E-8) const;
/** /**
Computes the numerical derivative of a function f at a point x using an adaptive backward Computes the numerical derivative of a function f at a point x using an adaptive backward
...@@ -148,28 +150,28 @@ namespace Math { ...@@ -148,28 +150,28 @@ namespace Math {
The function is evaluated only at points less than x and at x itself The function is evaluated only at points less than x and at x itself
*/ */
double EvalBackward(const IGenFunction & f, double x, double h = 1E-8); double EvalBackward(const IGenFunction & f, double x, double h = 1E-8) const;
/** /**
Computes the numerical derivative at a point x using an adaptive central Computes the numerical derivative at a point x using an adaptive central
difference algorithm with a step size h. difference algorithm with a step size h.
*/ */
double EvalCentral( double x, double h = 1E-8); double EvalCentral( double x, double h = 1E-8) const;
/** /**
Computes the numerical derivative at a point x using an adaptive forward Computes the numerical derivative at a point x using an adaptive forward
difference algorithm with a step size h. difference algorithm with a step size h.
The function is evaluated only at points greater than x and at x itself. The function is evaluated only at points greater than x and at x itself.
*/ */
double EvalForward( double x, double h = 1E-8); double EvalForward( double x, double h = 1E-8) const;
/** /**
Computes the numerical derivative at a point x using an adaptive backward Computes the numerical derivative at a point x using an adaptive backward
difference algorithm with a step size h. difference algorithm with a step size h.
The function is evaluated only at points less than x and at x itself. The function is evaluated only at points less than x and at x itself.
*/ */
double EvalBackward( double x, double h = 1E-8); double EvalBackward( double x, double h = 1E-8) const;
/** /**
return the error status of the last integral calculation return the error status of the last integral calculation
...@@ -193,7 +195,7 @@ protected: ...@@ -193,7 +195,7 @@ protected:
private: private:
GSLDerivator * fDerivator; mutable GSLDerivator * fDerivator;
}; };
......
// @(#)root/mathmore:$Name: $:$Id: GSLFunctionAdapter.h,v 1.1 2005/09/08 07:14:56 brun Exp $ // @(#)root/mathmore:$Name: $:$Id: GSLFunctionAdapter.h,v 1.1 2005/09/18 17:33:47 brun Exp $
// Authors: L. Moneta, A. Zsenei 08/2005 // Authors: L. Moneta, A. Zsenei 08/2005
/********************************************************************** /**********************************************************************
...@@ -77,14 +77,14 @@ namespace Math { ...@@ -77,14 +77,14 @@ namespace Math {
static double Df( double x, void * p) { static double Df( double x, void * p) {
UserFunc * function = reinterpret_cast< UserFunc *> (p); UserFunc * function = reinterpret_cast< UserFunc *> (p);
return (*function).Gradient( x ); return (*function).Derivative( x );
} }
static void Fdf( double x, void * p, double *f, double *df ) { static void Fdf( double x, void * p, double *f, double *df ) {
UserFunc * function = reinterpret_cast< UserFunc *> (p); UserFunc * function = reinterpret_cast< UserFunc *> (p);
*f = (*function) ( x ); *f = (*function) ( x );
*df = (*function).Gradient( x ); *df = (*function).Derivative( x );
} }
}; };
......
// @(#)root/mathmore:$Name: $:$Id: GSLRootFinder.h,v 1.1 2005/09/08 07:14:56 brun Exp $ // @(#)root/mathmore:$Name: $:$Id: GSLRootFinder.h,v 1.1 2005/09/18 17:33:47 brun Exp $
// Authors: L. Moneta, A. Zsenei 08/2005 // Authors: L. Moneta, A. Zsenei 08/2005
/********************************************************************** /**********************************************************************
...@@ -31,8 +31,16 @@ ...@@ -31,8 +31,16 @@
#ifndef ROOT_Math_GSLRootFinder #ifndef ROOT_Math_GSLRootFinder
#define ROOT_Math_GSLRootFinder #define ROOT_Math_GSLRootFinder
#include "Math/IGenFunction.h"
#ifndef ROOT_Math_GSLFunctionAdapter
#include "Math/GSLFunctionAdapter.h" #include "Math/GSLFunctionAdapter.h"
#endif
#ifndef ROOT_Math_IFunctionfwd
#include "Math/IFunctionfwd.h"
#endif
#include <iostream>
namespace ROOT { namespace ROOT {
namespace Math { namespace Math {
...@@ -67,8 +75,9 @@ namespace Math { ...@@ -67,8 +75,9 @@ namespace Math {
public: public:
void SetFunction( const IGenFunction & f, double Root) { void SetFunction( const IGradFunction & , double ) {
SetFunction( f, Root -1, Root + 1); // temporary std::cerr <<"GSLRootFinder - Error : use other Setfunction" << std::endl;
//SetFunction( f, Root -1, Root + 1); // temporary
} }
/** /**
......
// @(#)root/mathmore:$Name: $:$Id: GSLRootFinderDeriv.h,v 1.1 2005/09/08 07:14:56 brun Exp $ // @(#)root/mathmore:$Name: $:$Id: GSLRootFinderDeriv.h,v 1.1 2005/09/18 17:33:47 brun Exp $
// Authors: L. Moneta, A. Zsenei 08/2005 // Authors: L. Moneta, A. Zsenei 08/2005
/********************************************************************** /**********************************************************************
...@@ -31,8 +31,19 @@ ...@@ -31,8 +31,19 @@
#ifndef ROOT_Math_GSL_RootFinderDeriv #ifndef ROOT_Math_GSL_RootFinderDeriv
#define ROOT_Math_GSL_RootFinderDeriv #define ROOT_Math_GSL_RootFinderDeriv
#include "Math/IGenFunction.h"
#ifndef ROOT_Math_GSLFunctionAdapter
#include "Math/GSLFunctionAdapter.h" #include "Math/GSLFunctionAdapter.h"
#endif
#ifndef ROOT_Math_IFunctionfwd
#include "Math/IFunctionfwd.h"
#endif
#ifndef ROOT_Math_IFunction
#include "Math/IFunction.h"
#endif
#include <iostream>
namespace ROOT { namespace ROOT {
namespace Math { namespace Math {
...@@ -67,14 +78,14 @@ namespace Math { ...@@ -67,14 +78,14 @@ namespace Math {
public: public:
void SetFunction( const IGenFunction & f, double xlow, double xup) { void SetFunction( const IGenFunction & , double , double ) {
SetFunction( f, (xlow + xup)/2. ); std::cerr <<"GSLRootFinderDeriv - Error : Algorithm requirs derivatives" << std::endl;
} }
void SetFunction( const IGenFunction & f, double Root) { void SetFunction( const IGradFunction & f, double Root) {
const void * p = &f; const void * p = &f;
SetFunction( &GSLFunctionAdapter<IGenFunction>::F, &GSLFunctionAdapter<IGenFunction>::Df, &GSLFunctionAdapter<IGenFunction>::Fdf, const_cast<void *>(p), Root ); SetFunction( &GSLFunctionAdapter<IGradFunction>::F, &GSLFunctionAdapter<IGradFunction>::Df, &GSLFunctionAdapter<IGradFunction>::Fdf, const_cast<void *>(p), Root );
} }
......
// @(#)root/mathmore:$Name: $:$Id: IGenFunction.h,v 1.1 2005/09/18 17:33:47 brun Exp $
// Authors: L. Moneta, A. Zsenei 08/2005
/**********************************************************************
* *
* Copyright (c) 2004 ROOT Foundation, CERN/PH-SFT *
* *
* This library 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 2 *
* of the License, or (at your option) any later version. *
* *
* This library 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 library (see file COPYING); if not, write *
* to the Free Software Foundation, Inc., 59 Temple Place, Suite *
* 330, Boston, MA 02111-1307 USA, or contact the author. *
* *
**********************************************************************/
// Header file for class IGenFunction
//
// Generic interface for one-dimensional functions
//
// Created by: Lorenzo Moneta at Wed Nov 10 16:15:35 2004
//
// Last update: Wed Nov 10 16:15:35 2004
//
#ifndef ROOT_Math_IGenFunction
#define ROOT_Math_IGenFunction
/**
@defgroup CppFunctions Function Classes and Interfaces
*/
namespace ROOT {
namespace Math {
/**
Interface for generic 1 Dimensional Functions.
@ingroup CppFunctions
*/
class IGenFunction {
public:
virtual ~IGenFunction() {}
/**
Evaluate the function at a point x.
It is a pure virtual method and must be implemented by sub-classes
*/
virtual double operator() (double x) = 0;
//virtual double operator() (double x) {return 2.0;}
/**
Evaluate the function derivative at a point x.
Functions which do not implement it, can use the dummy implementation of this class
which returns zero.
The method IgenFunction::ProvidesGradient() is used to query this information
*/
virtual double Gradient(double /* x */ ) { return 0; }
/**
Optimized method to evaluate at the same time the function value and derivative at a point x.
Often both value and derivatives are needed and it is often more efficient to compute them at the same time.
Derived class should implement this method if performances play an important role and if it is faster to
evaluate value and derivative at the same time
*/
virtual void Fdf(double x, double & f, double & df) {
f = operator()(x);
df = Gradient(x);
}
// should return a reference ? The returned function has no sense if this disappears
//virtual GenFunction * gradientFunc();
/**
Clone a function.
Pure virtual method needed to perform deep copy of the derived classes.
*/
virtual IGenFunction * Clone() const = 0;
/**
Return \a true if the calculation of derivatives is implemented
*/
// overwrite by derived classes
virtual bool ProvidesGradient() const { return false; }
};
} // namespace Math
} // namespace ROOT
#endif /* ROOT_Math_IGenFunction */
// @(#)root/mathmore:$Name: $:$Id: IParamFunction.h,v 1.1 2005/09/18 17:33:47 brun Exp $
// Authors: L. Moneta, A. Zsenei 08/2005
/**********************************************************************
* *
* Copyright (c) 2004 ROOT Foundation, CERN/PH-SFT *
* *
* This library 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 2 *
* of the License, or (at your option) any later version. *
* *
* This library 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 library (see file COPYING); if not, write *
* to the Free Software Foundation, Inc., 59 Temple Place, Suite *
* 330, Boston, MA 02111-1307 USA, or contact the author. *
* *
**********************************************************************/
// Header file for class IParamFunction
//
// interface for parameteric functions
//
// Created by: Lorenzo Moneta at Wed Nov 10 16:23:49 2004
//
// Last update: Wed Nov 10 16:23:49 2004
//
#ifndef ROOT_Math_IParamFunction
#define ROOT_Math_IParamFunction
#include "Math/IGenFunction.h"
#include <vector>
namespace ROOT {
namespace Math {
/**
Interface for 1 Dimensional Parametric Functions.
A parameteric function is a Generic Function with Parameters, so
it is a function object which carries a state, the Parameters.
The Parameters are described with a standard vector of doubles.
@ingroup CppFunctions
*/
class IParamFunction : public IGenFunction {
public:
virtual ~IParamFunction() {}
/**
Access the parameter values
*/
virtual const std::vector<double> & Parameters() const = 0;
// set params values (can user change number of params ? )
/**
Set the parameter values
@param p vector of doubles containing the parameter values.
*/
virtual void SetParameters(const std::vector<double> & p) = 0;
/**
Return the number of Parameters
*/
virtual unsigned int NumberOfParameters() const = 0;
/**
Evaluate the derivatives of the function with respect to the parameters at a point x.
It is optional to be implemented by the derived classes
*/
virtual const std::vector<double> & ParameterGradient(double /* x */ ) {
// in case of no Gradient provided return Parameters
return Parameters();
}
// user override if implements parameter Gradient
/**
Return \a true if the calculation of the derivatives with respect to the Parameters is implemented
*/
virtual bool ProvidesParamGradient() const { return false; }
};
} // namespace Math
} // namespace ROOT
#endif /* MATHLIB_IPARAMFUNCTION */
// @(#)root/mathmore:$Name: $:$Id: Integrator.h,v 1.1 2005/09/18 17:33:47 brun Exp $ // @(#)root/mathmore:$Name: $:$Id: Integrator.h,v 1.2 2006/06/16 10:34:08 moneta Exp $
// Authors: L. Moneta, A. Zsenei 08/2005 // Authors: L. Moneta, A. Zsenei 08/2005
/********************************************************************** /**********************************************************************
...@@ -31,8 +31,15 @@ ...@@ -31,8 +31,15 @@
#ifndef ROOT_Math_Integrator #ifndef ROOT_Math_Integrator
#define ROOT_Math_Integrator #define ROOT_Math_Integrator
#include "Math/IGenFunction.h" #ifndef ROOT_Math_IntegrationTypes
#include "Math/IntegrationTypes.h" #include "Math/IntegrationTypes.h"
#endif
#ifndef ROOT_Math_IFunctionfwd
#include "Math/IFunctionfwd.h"
#endif
#include <vector> #include <vector>
......
// @(#)root/mathmore:$Name: $:$Id: LinkDef.h,v 1.5 2006/10/05 15:23:42 moneta Exp $ // @(#)root/mathmore:$Name: $:$Id: LinkDef.h,v 1.6 2006/11/10 16:28:42 moneta Exp $
// Authors: L. Moneta, A. Zsenei 08/2005 // Authors: L. Moneta, A. Zsenei 08/2005
...@@ -6,12 +6,16 @@ ...@@ -6,12 +6,16 @@
#include "LinkDef_SpecFunc.h" #include "LinkDef_SpecFunc.h"
#include "LinkDef_StatFunc.h" #include "LinkDef_StatFunc.h"
#include "LinkDef_Func.h" #include "LinkDef_Func.h"
#include "TF1.h"
#include "LinkDef_RootFinding.h" #include "LinkDef_RootFinding.h"
#ifdef __CINT__ #ifdef __CINT__
#pragma extra_include "Math/IFunctionfwd.h";
#pragma extra_include "Math/IFunction.h";
#pragma link C++ nestedclasses; #pragma link C++ nestedclasses;
#pragma link C++ nestedtypedef; #pragma link C++ nestedtypedef;
...@@ -20,7 +24,7 @@ ...@@ -20,7 +24,7 @@
#pragma link C++ class ROOT::Math::ParamFunction+; #pragma link C++ class ROOT::Math::ParamFunction+;
#pragma link C++ class ROOT::Math::Polynomial+; #pragma link C++ class ROOT::Math::Polynomial+;
#pragma link C++ class ROOT::Math::WrappedFunction<ROOT::Math::Polynomial>+; //#pragma link C++ class ROOT::Math::WrappedFunction<ROOT::Math::Polynomial>+;
#pragma link C++ class ROOT::Math::Chebyshev+; #pragma link C++ class ROOT::Math::Chebyshev+;
......
// @(#)root/mathmore:$Name: $:$Id: ParamFunction.h,v 1.2 2005/09/19 13:06:53 brun Exp $ // @(#)root/mathmore:$Name: $:$Id: ParamFunction.h,v 1.3 2006/11/10 16:28:42 moneta Exp $
// Authors: L. Moneta, A. Zsenei 08/2005 // Authors: L. Moneta, A. Zsenei 08/2005
/********************************************************************** /**********************************************************************
...@@ -34,7 +34,9 @@ ...@@ -34,7 +34,9 @@
#define ROOT_Math_ParamFunction #define ROOT_Math_ParamFunction
#ifndef ROOT_Math_IParamFunction
#include "Math/IParamFunction.h" #include "Math/IParamFunction.h"
#endif
namespace ROOT { namespace ROOT {
namespace Math { namespace Math {
...@@ -55,86 +57,76 @@ namespace Math { ...@@ -55,86 +57,76 @@ namespace Math {
*/ */
class ParamFunction : virtual public IParamGradFunction<ROOT::Math::OneDim> {
class ParamFunction : public IParamFunction { public:
public: typedef IParamGradFunction<ROOT::Math::OneDim> BaseFunc;
/** /**
Construct a parameteric function with npar parameters Construct a parameteric function with npar parameters
@param npar number of parameters (default is zero) @param npar number of parameters (default is zero)
@param providesGrad flag to specify if function implements the calculation of the derivative @param providesGrad flag to specify if function implements the calculation of the derivative
@param providesParamGrad flag to specify if function implements the calculation of the derivatives with respect to the Parameters @param providesParamGrad flag to specify if function implements the calculation of the derivatives with respect to the Parameters
*/ */
ParamFunction(unsigned int npar = 0, bool providesGrad = false, bool providesParamGrad = false); ParamFunction(unsigned int npar = 0, bool providesGrad = false, bool providesParamGrad = false);
// destructor // destructor
virtual ~ParamFunction() {} virtual ~ParamFunction() {}
// copying constructors // copying constructors
// use defaults one ?? // use defaults one ??
//ParamFunction(const ParamFunction & pf); //ParamFunction(const ParamFunction & pf);
//ParamFunction & operator = (const ParamFunction &); //ParamFunction & operator = (const ParamFunction &);
// cloning // cloning
/** /**
Deep copy of function (to be implemented by the derived classes) Deep copy of function (to be implemented by the derived classes)
*/ */
virtual IGenFunction * Clone() const = 0; //virtual ParamFunction * Clone() const = 0;
/** /**
Access the parameter values Access the parameter values
*/ */
const std::vector<double> & Parameters() const { return fParams; } virtual const double * Parameters() const { return &fParams.front(); }
/** /**
Set the parameter values Set the parameter values
@param p vector of doubles containing the parameter values. @param p vector of doubles containing the parameter values.
*/ */
void SetParameters(const std::vector<double> & p) virtual void SetParameters(const double * p)
// pending : can user change number of Parameters ? {
{ fParams = std::vector<double>(p,p+fNpar);
fParams = p; }
fNpar = fParams.size();
} /**
Return the number of parameters
/** */
Return the number of parameters unsigned int NPar() const { return fNpar; }
*/
unsigned int NumberOfParameters() const { return fNpar; }
//using BaseFunc::operator();
// this needs to be implemented in derived classes
/** /**
Evaluate function at a point x (to be implemented by the derived classes) Return \a true if the calculation of derivatives is implemented
*/ */
virtual double operator() (double x ) = 0; // bool ProvidesGradient() const { return fProvGrad; }
// user may re-implement this for better efficiency /**
// this method is NOT required to change internal values of parameters. confusing ?? Return \a true if the calculation of derivatives with respect to the Parameters is implemented
/** */
Evaluate function at a point x and for parameters p. bool ProvidesParameterGradient() const { return fProvParGrad; }
This method mey be needed for better efficiencies when for each function evaluation the parameters are changed.
*/ const std::vector<double> & GetParGradient( double x) {
virtual double operator() (double x, const std::vector<double> & p ) BaseFunc::ParameterGradient(x,&fParGradient[0]);
{ return fParGradient;
SetParameters(p); }
return operator() (x);
}
/**
Return \a true if the calculation of derivatives is implemented
*/
bool ProvidesGradient() const { return fProvGrad; }
/**
Return \a true if the calculation of derivatives with respect to the Parameters is implemented
*/
bool ProvidesParameterGradient() const { return fProvParGrad; }
public: public:
...@@ -142,20 +134,20 @@ public: ...@@ -142,20 +134,20 @@ public:
protected: protected:
// Parameters (make protected to be accessible directly by derived classes) // Parameters (make protected to be accessible directly by derived classes)
std::vector<double> fParams; std::vector<double> fParams;
// cache paramGradient for better efficiency (to be used by derived classes) // cache paramGradient for better efficiency (to be used by derived classes)
mutable std::vector<double> fParGradient; mutable std::vector<double> fParGradient;
private: private:
// cache number of Parameters for speed efficiency // cache number of Parameters for speed efficiency
unsigned int fNpar; unsigned int fNpar;
bool fProvGrad; bool fProvGrad;
bool fProvParGrad; bool fProvParGrad;
}; };
......
// @(#)root/mathmore:$Name: $:$Id: Polynomial.h,v 1.3 2006/05/26 14:26:08 moneta Exp $ // @(#)root/mathmore:$Name: $:$Id: Polynomial.h,v 1.4 2006/11/10 16:28:42 moneta Exp $
// Authors: L. Moneta, A. Zsenei 08/2005 // Authors: L. Moneta, A. Zsenei 08/2005
/********************************************************************** /**********************************************************************
...@@ -51,106 +51,108 @@ namespace Math { ...@@ -51,106 +51,108 @@ namespace Math {
@ingroup CppFunctions @ingroup CppFunctions
*/ */
class Polynomial : public ParamFunction { class Polynomial : public ParamFunction {
public: public:
/** /**
Construct a Polynomial function of order n. Construct a Polynomial function of order n.
The number of Parameters is n+1. The number of Parameters is n+1.
*/ */
Polynomial(unsigned int n); Polynomial(unsigned int n);
/** /**
Construct a Polynomial of degree 1 : a*x + b Construct a Polynomial of degree 1 : a*x + b
*/ */
Polynomial(double a, double b); Polynomial(double a, double b);
/** /**
Construct a Polynomial of degree 2 : a*x**2 + b*x + c Construct a Polynomial of degree 2 : a*x**2 + b*x + c
*/ */
Polynomial(double a, double b, double c); Polynomial(double a, double b, double c);
/** /**
Construct a Polynomial of degree 3 : a*x**3 + b*x**2 + c*x + d Construct a Polynomial of degree 3 : a*x**3 + b*x**2 + c*x + d
*/ */
Polynomial(double a, double b, double c, double d); Polynomial(double a, double b, double c, double d);
/** /**
Construct a Polynomial of degree 4 : a*x**4 + b*x**3 + c*x**2 + dx + e Construct a Polynomial of degree 4 : a*x**4 + b*x**3 + c*x**2 + dx + e
*/ */
Polynomial(double a, double b, double c, double d, double e); Polynomial(double a, double b, double c, double d, double e);
virtual ~Polynomial(); virtual ~Polynomial();
/** /**
Copy constructor Copy constructor
*/ */
//Polynomial(const Polynomial &); //Polynomial(const Polynomial &);
/** /**
Copy operator Copy operator
*/ */
//Polynomial & operator = (const Polynomial &); //Polynomial & operator = (const Polynomial &);
double operator() ( double x );
double operator() ( double x, const std::vector<double> & p ); double operator() ( double x, const std::vector<double> & p );
double Gradient (double x); using ParamFunction::operator();
const std::vector<double> & ParameterGradient( double x);
/**
Find the polynomial roots.
For n <= 4, the roots are found analytically while for larger order an iterative numerical method is used
The numerical method used is from GSL (see <A HREF="http://www.gnu.org/software/gsl/manual/gsl-ref_6.html#SEC53" )
*/
const std::vector<std::complex <double> > & FindRoots();
/**
Find the polynomial roots.
For n <= 4, the roots are found analytically while for larger order an iterative numerical method is used
The numerical method used is from GSL (see <A HREF="http://www.gnu.org/software/gsl/manual/gsl-ref_6.html#SEC53" )
*/
const std::vector<std::complex <double> > & FindRoots();
/**
Find the only the real polynomial roots.
For n <= 4, the roots are found analytically while for larger order an iterative numerical method is used
The numerical method used is from GSL (see <A HREF="http://www.gnu.org/software/gsl/manual/gsl-ref_6.html#SEC53" )
*/
std::vector<double > FindRealRoots();
/**
Find the only the real polynomial roots.
For n <= 4, the roots are found analytically while for larger order an iterative numerical method is used
The numerical method used is from GSL (see <A HREF="http://www.gnu.org/software/gsl/manual/gsl-ref_6.html#SEC53" )
*/
std::vector<double > FindRealRoots();
/**
Find the polynomial roots using always an iterative numerical methods
The numerical method used is from GSL (see <A HREF="http://www.gnu.org/software/gsl/manual/gsl-ref_6.html#SEC53" )
*/
const std::vector<std::complex <double> > & FindNumRoots();
/** /**
Order of Polynomial Find the polynomial roots using always an iterative numerical methods
*/ The numerical method used is from GSL (see <A HREF="http://www.gnu.org/software/gsl/manual/gsl-ref_6.html#SEC53" )
unsigned int Order() const { return fOrder; } */
const std::vector<std::complex <double> > & FindNumRoots();
/**
Order of Polynomial
*/
unsigned int Order() const { return fOrder; }
IGenFunction * Clone() const;
IGenFunction * Clone() const;
private:
protected: double DoEval ( double x ) const ;
double DoDerivative (double x) const ;
private: void DoParameterGradient(double x, double * g) const;
// cache order = number of params - 1) // cache order = number of params - 1)
unsigned int fOrder; unsigned int fOrder;
// cache Parameters for Gradient // cache Parameters for Gradient
std::vector<double> fDerived_params; mutable std::vector<double> fDerived_params;
// roots // roots
std::vector< std::complex < double > > fRoots; std::vector< std::complex < double > > fRoots;
}; };
......
// @(#)root/mathmore:$Name: $:$Id: RootFinder.h,v 1.1 2005/09/08 07:14:56 brun Exp $ // @(#)root/mathmore:$Name: $:$Id: RootFinder.h,v 1.1 2005/09/18 17:33:47 brun Exp $
// Authors: L. Moneta, A. Zsenei 08/2005 // Authors: L. Moneta, A. Zsenei 08/2005
/********************************************************************** /**********************************************************************
...@@ -31,8 +31,14 @@ ...@@ -31,8 +31,14 @@
#ifndef ROOT_Math_RootFinder #ifndef ROOT_Math_RootFinder
#define ROOT_Math_RootFinder #define ROOT_Math_RootFinder
#ifndef ROOT_Math_GSLRootHelper
#include "Math/GSLRootHelper.h" #include "Math/GSLRootHelper.h"
#include "Math/IGenFunction.h" #endif
#ifndef ROOT_Math_IFunctionfwd
#include "Math/IFunctionfwd.h"
#endif
/** /**
@defgroup RootFinders Root-Finding algorithms @defgroup RootFinders Root-Finding algorithms
...@@ -109,7 +115,7 @@ public: ...@@ -109,7 +115,7 @@ public:
<em> double operator() ( double x ) </em> <em> double operator() ( double x ) </em>
*/ */
void SetFunction( const IGenFunction & f, double Root) { void SetFunction( const IGradFunction & f, double Root) {
fSolver.SetFunction( f, Root); fSolver.SetFunction( f, Root);
} }
......
// @(#)root/mathmore:$Name: $:$Id: RootFinderAlgorithms.h,v 1.1 2005/09/08 07:14:56 brun Exp $ // @(#)root/mathmore:$Name: $:$Id: RootFinderAlgorithms.h,v 1.1 2005/09/18 17:33:47 brun Exp $
// Authors: L. Moneta, A. Zsenei 08/2005 // Authors: L. Moneta, A. Zsenei 08/2005
/********************************************************************** /**********************************************************************
...@@ -32,10 +32,13 @@ ...@@ -32,10 +32,13 @@
#define ROOT_Math_GSLRootFinderAlgorithms #define ROOT_Math_GSLRootFinderAlgorithms
#ifndef ROOT_Math_GSLRootFinder
#include "Math/GSLRootFinder.h" #include "Math/GSLRootFinder.h"
#endif
#ifndef ROOT_Math_GSLRootFinderDeriv
#include "Math/GSLRootFinderDeriv.h" #include "Math/GSLRootFinderDeriv.h"
#endif
namespace ROOT { namespace ROOT {
namespace Math { namespace Math {
......
// @(#)root/mathmore:$Name: $:$Id: WrappedFunction.h,v 1.2 2005/09/19 13:06:53 brun Exp $
// Authors: L. Moneta, A. Zsenei 08/2005
/**********************************************************************
* *
* Copyright (c) 2004 ROOT Foundation, CERN/PH-SFT *
* *
* This library 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 2 *
* of the License, or (at your option) any later version. *
* *
* This library 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 library (see file COPYING); if not, write *
* to the Free Software Foundation, Inc., 59 Temple Place, Suite *
* 330, Boston, MA 02111-1307 USA, or contact the author. *
* *
**********************************************************************/
#ifndef ROOT_Math_WrappedFunction
#define ROOT_Math_WrappedFunction
#include "IGenFunction.h"
#include <iostream>
namespace ROOT {
namespace Math {
/**
Template class to wrap any C++ callable object which takes one argument
i.e. implementing operator() (double x).
It provides a ROOT::Math::IGenFunction-like signature
*/
template< class CALLABLE >
class WrappedFunction : public IGenFunction {
public:
explicit WrappedFunction( CALLABLE f ) : fFunc( f ) { /**/ }
virtual WrappedFunction * Clone() const {
//return new WrappedFunction( *this );
return new WrappedFunction<CALLABLE>(fFunc);
}
virtual double operator() (double x) {
return fFunc( x );
}
virtual ~WrappedFunction() { /**/ }
private:
CALLABLE fFunc;
}; // WrappedFunction
} // namespace Math
} // namespace ROOT
#endif // ROOT_Math_WrappedFunction
// @(#)root/fit:$Name: $:$Id: inc/Fit/WrappedTF1.h,v 1.0 2006/01/01 12:00:00 moneta Exp $
// Author: L. Moneta Wed Sep 6 09:52:26 2006
/**********************************************************************
* *
* Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
* *
* *
**********************************************************************/
// Header file for class WrappedTFunction
#ifndef ROOT_Math_WrappedMultiTF1
#define ROOT_Math_WrappedMultiTF1
#ifndef ROOT_Math_IParamFunction
#include "Math/IParamFunction.h"
#endif
#include "TF1.h"
namespace ROOT {
namespace Math {
/**
Class to Wrap a ROOT Function class (like TF1) in a IParamFunction interface
in order to be used in fitting
*/
class WrappedMultiTF1 : public ROOT::Math::IParamFunction<ROOT::Math::MultiDim> {
public:
typedef ROOT::Math::IParamFunction<ROOT::Math::MultiDim> BaseParamFunc;
typedef ROOT::Math::IParamFunction<ROOT::Math::MultiDim>::BaseFunc BaseFunc;
/**
constructor from a function pointer. A flag (default false) is specified if
class owns the pointer
*/
WrappedMultiTF1 (TF1 & f ) :
fFunc(&f)
{ }
/**
Destructor (no operations). Function pointer is not owned
*/
virtual ~WrappedMultiTF1 () {}
/**
Copy constructor
*/
WrappedMultiTF1(const WrappedMultiTF1 & rhs) :
BaseFunc(),
BaseParamFunc(),
fFunc(rhs.fFunc)
{}
/**
Assignment operator
*/
WrappedMultiTF1 & operator = (const WrappedMultiTF1 & rhs) {
if (this == &rhs) return *this; // time saving self-test
fFunc = rhs.fFunc;
}
/** @name interface inherited from IFunction */
/**
Clone the wrapper but not the original function
*/
IMultiGenFunction * Clone() const {
return new WrappedMultiTF1(*fFunc);
}
/// function dimension
unsigned int NDim() const {
return fFunc->GetNdim();
}
/** @name interface inherited from IParamFunction */
/// access the parameter values
const double * Parameters() const {
return fFunc->GetParameters();
}
/// set parameter values
void SetParameters(const double * p) {
fFunc->SetParameters(p);
}
/// return number of parameters
unsigned int NPar() const {
return static_cast<unsigned int>(fFunc->GetNpar() );
}
/// return parameter name
std::string ParameterName(unsigned int i) const {
return std::string(fFunc->GetParName(i));
}
/// evaluate function passing coordinates x and vector of parameters
double operator() (const double * x, const double * p ) {
fFunc->InitArgs(x,p); // needed for interpreted functions
return fFunc->EvalPar(x,p);
}
/// evaluate integral between x1 and x2
//double Integral(double * x1, double * x2) const;
using BaseFunc::operator();
private:
/// evaluate function using parameter values cached in the TF1
double DoEval (const double * x) const {
fFunc->InitArgs(x, 0 );
return fFunc->EvalPar(x,0);
}
// /// return the function derivatives w.r.t. x
// double DoDerivative(const double * x, unsigned int icoord ) const {
// std::cerr << "WrappedMultiTF1:: gradient for multidim functions not implemented" << std::endl;
// }
// /// evaluate the derivative of the function with respect to the parameters
// void DoParameterGradient(const double * x, double * grad ) const {
// static const double kEps = 0.001;
// fFunc->GradientPar(x,grad,kEps);
// }
// pointer to ROOT function
TF1 * fFunc;
};
} // end namespace Fit
} // end namespace ROOT
#endif /* ROOT_Fit_WrappedMultiTF1 */
// @(#)root/fit:$Name: $:$Id: inc/Fit/WrappedTF1.h,v 1.0 2006/01/01 12:00:00 moneta Exp $
// Author: L. Moneta Wed Sep 6 09:52:26 2006
/**********************************************************************
* *
* Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
* *
* *
**********************************************************************/
// Header file for class WrappedTFunction
#ifndef ROOT_Math_WrappedTF1
#define ROOT_Math_WrappedTF1
#ifndef ROOT_Math_IParamFunction
#include "Math/IParamFunction.h"
#endif
#include "TF1.h"
namespace ROOT {
namespace Math {
/**
Class to Wrap a ROOT Function class (like TF1) in a IParamFunction interface
in order to be used in fitting
*/
class WrappedTF1 : public ROOT::Math::IParamGradFunction<ROOT::Math::OneDim> {
public:
typedef ROOT::Math::IParamGradFunction<ROOT::Math::OneDim> BaseGradFunc;
typedef ROOT::Math::IParamGradFunction<ROOT::Math::OneDim>::BaseFunc BaseFunc;
WrappedTF1() {}
/**
constructor from a function pointer. A flag (default false) is specified if
class owns the pointer
*/
WrappedTF1 ( TF1 & f ) :
fFunc(&f)
{
fFunc->InitArgs(fX, 0 );
}
/**
Destructor (no operations). Function pointer is not owned
*/
virtual ~WrappedTF1 () {}
/**
Copy constructor
*/
WrappedTF1(const WrappedTF1 & rhs) :
BaseFunc(),
BaseGradFunc(),
fFunc(rhs.fFunc)
{
fFunc->InitArgs(fX, 0 );
}
/**
Assignment operator
*/
WrappedTF1 & operator = (const WrappedTF1 & rhs) {
if (this == &rhs) return *this; // time saving self-test
fFunc = rhs.fFunc;
fFunc->InitArgs(fX, 0 );
}
/** @name interface inherited from IFunction */
/**
Clone the wrapper but not the original function
*/
ROOT::Math::IGenFunction * Clone() const {
return new WrappedTF1(*fFunc);
}
/** @name interface inherited from IParamFunction */
/// access the parameter values
const double * Parameters() const {
return fFunc->GetParameters();
}
/// set parameter values
void SetParameters(const double * p) {
fFunc->SetParameters(p);
// need to re-initialize it
fFunc->InitArgs(fX, p );
}
/// return number of parameters
unsigned int NPar() const {
return static_cast<unsigned int>(fFunc->GetNpar() );
}
/// return parameter name
std::string ParameterName(unsigned int i) const {
return std::string(fFunc->GetParName(i));
}
/// evaluate function passing coordinates x and vector of parameters
double operator() (const double * x, const double * p ) {
fFunc->InitArgs(x,p); // needed for interpreted functions
return fFunc->EvalPar(x,p);
}
/// evaluate integral between x1 and x2
//double Integral(double * x1, double * x2) const;
using BaseGradFunc::operator();
private:
/// evaluate function using parameter values cached in the TF1
double DoEval (double x) const {
// no need to InitArg
fX[0] = x;
return fFunc->EvalPar(fX,0);
}
/// return the function derivatives w.r.t. x
double DoDerivative( double x ) const {
static const double kEps = 0.001;
return fFunc->Derivative(x,0,kEps);
}
/// evaluate the derivative of the function with respect to the parameters
void DoParameterGradient(double x, double * grad ) const {
static const double kEps = 0.001;
fFunc->GradientPar(&x,grad,kEps);
}
// pointer to ROOT function
TF1 * fFunc;
mutable double fX[1];
};
} // end namespace Fit
} // end namespace ROOT
#endif /* ROOT_Fit_WrappedTF1 */
// @(#)root/mathmore:$Name: $:$Id: Chebyshev.cxx,v 1.4 2005/10/18 10:11:51 moneta Exp $ // @(#)root/mathmore:$Name: $:$Id: Chebyshev.cxx,v 1.5 2006/06/16 10:34:08 moneta Exp $
// Authors: L. Moneta, A. Zsenei 08/2005 // Authors: L. Moneta, A. Zsenei 08/2005
...@@ -30,15 +30,17 @@ ...@@ -30,15 +30,17 @@
// Last update: Thu Dec 2 14:51:15 2004 // Last update: Thu Dec 2 14:51:15 2004
// //
#include <cassert>
#include "Math/IFunction.h"
#include "Math/Chebyshev.h" #include "Math/Chebyshev.h"
#include "Math/IGenFunction.h"
#include "GSLFunctionWrapper.h" #include "GSLFunctionWrapper.h"
#include "GSLChebSeries.h" #include "GSLChebSeries.h"
#include "gsl/gsl_chebyshev.h" #include "gsl/gsl_chebyshev.h"
#include <cassert>
namespace ROOT { namespace ROOT {
namespace Math { namespace Math {
......
// @(#)root/mathmore:$Name: $:$Id: Derivator.cxx,v 1.2 2005/09/18 20:41:25 brun Exp $ // @(#)root/mathmore:$Name: $:$Id: Derivator.cxx,v 1.3 2006/06/16 10:34:08 moneta Exp $
// Authors: L. Moneta, A. Zsenei 08/2005 // Authors: L. Moneta, A. Zsenei 08/2005
/********************************************************************** /**********************************************************************
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
// Last update: Sat Nov 13 14:46:00 2004 // Last update: Sat Nov 13 14:46:00 2004
// //
#include "Math/IFunction.h"
#include "Math/Derivator.h" #include "Math/Derivator.h"
#include "GSLDerivator.h" #include "GSLDerivator.h"
...@@ -82,36 +83,36 @@ void Derivator::SetFunction( const GSLFuncPointer &f) { ...@@ -82,36 +83,36 @@ void Derivator::SetFunction( const GSLFuncPointer &f) {
} }
double Derivator::Eval(const IGenFunction & f, double x, double h ) { double Derivator::Eval(const IGenFunction & f, double x, double h ) const {
return fDerivator->Eval(f, x, h); return fDerivator->Eval(f, x, h);
} }
double Derivator::EvalCentral(const IGenFunction & f, double x, double h) { double Derivator::EvalCentral(const IGenFunction & f, double x, double h) const {
return fDerivator->EvalCentral(f, x, h); return fDerivator->EvalCentral(f, x, h);
} }
double Derivator::EvalForward(const IGenFunction & f, double x, double h) { double Derivator::EvalForward(const IGenFunction & f, double x, double h) const {
return fDerivator->EvalForward(f, x, h); return fDerivator->EvalForward(f, x, h);
} }
double Derivator::EvalBackward(const IGenFunction & f, double x, double h) { double Derivator::EvalBackward(const IGenFunction & f, double x, double h) const {
return fDerivator->EvalBackward(f, x, h); return fDerivator->EvalBackward(f, x, h);
} }
double Derivator::Eval( double x, double h) { double Derivator::Eval( double x, double h) const {
return fDerivator->EvalCentral(x, h); return fDerivator->EvalCentral(x, h);
} }
double Derivator::EvalCentral( double x, double h) { double Derivator::EvalCentral( double x, double h) const {
return fDerivator->EvalCentral(x, h); return fDerivator->EvalCentral(x, h);
} }
double Derivator::EvalForward( double x, double h) { double Derivator::EvalForward( double x, double h) const {
return fDerivator->EvalForward(x, h); return fDerivator->EvalForward(x, h);
} }
double Derivator::EvalBackward( double x, double h) { double Derivator::EvalBackward( double x, double h) const {
return fDerivator->EvalBackward(x, h); return fDerivator->EvalBackward(x, h);
} }
......
// @(#)root/mathmore:$Name: $:$Id: GSLDerivator.h,v 1.1 2005/09/08 07:14:56 brun Exp $ // @(#)root/mathmore:$Name: $:$Id: GSLDerivator.h,v 1.2 2005/09/18 20:41:25 brun Exp $
// Authors: L. Moneta, A. Zsenei 08/2005 // Authors: L. Moneta, A. Zsenei 08/2005
/********************************************************************** /**********************************************************************
...@@ -39,7 +39,8 @@ ...@@ -39,7 +39,8 @@
#include "Math/GSLFunctionAdapter.h" #include "Math/GSLFunctionAdapter.h"
#include "Math/IGenFunction.h" #include "Math/IFunctionfwd.h"
#include "Math/IFunction.h"
namespace ROOT { namespace ROOT {
namespace Math { namespace Math {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment