diff --git a/mathmore/Module.mk b/mathmore/Module.mk index 0e35aa6434a7e492abd3cff9826db1b24f9b9277..348d6a65983a133adb84d82e54988cb200b884d1 100644 --- a/mathmore/Module.mk +++ b/mathmore/Module.mk @@ -29,10 +29,6 @@ MATHMOREDH := $(MATHMOREDS:.cxx=.h) MATHMOREDH1 := $(MODDIRI)/Math/ProbFuncMathMore.h \ $(MODDIRI)/Math/ProbFuncInv.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/Derivator.h \ $(MODDIRI)/Math/Interpolator.h \ diff --git a/mathmore/inc/Math/Chebyshev.h b/mathmore/inc/Math/Chebyshev.h index 0d17eab3d06f242d87c5df8dad0d25e8e82f02a3..caa34a1994fb118b6e9352d5d8902c2dace1c54d 100644 --- a/mathmore/inc/Math/Chebyshev.h +++ b/mathmore/inc/Math/Chebyshev.h @@ -1,4 +1,4 @@ -// @(#)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 /********************************************************************** @@ -35,15 +35,22 @@ @defgroup FuncApprox Function Approximation (Chebyshev) */ -#include <memory> +#ifndef ROOT_Math_IFunctionfwd +#include "Math/IFunctionfwd.h" +#endif + +#ifndef ROOT_Math_GSLFunctionAdapter #include "Math/GSLFunctionAdapter.h" +#endif + +#include <memory> + namespace ROOT { namespace Math { -class IGenFunction; class GSLChebSeries; class GSLFunctionWrapper; diff --git a/mathmore/inc/Math/Derivator.h b/mathmore/inc/Math/Derivator.h index bc3443e20d2b6f9a2ecdacb8fea7a4b6c7371a9c..9efd6568bc70336e938c083f32eabeea0b02885f 100644 --- a/mathmore/inc/Math/Derivator.h +++ b/mathmore/inc/Math/Derivator.h @@ -1,4 +1,4 @@ -// @(#)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 /********************************************************************** @@ -37,8 +37,10 @@ @defgroup Deriv Numerical Differentiation */ +#ifndef ROOT_Math_IFunctionfwd +#include "Math/IFunctionfwd.h" +#endif -#include "Math/IGenFunction.h" namespace ROOT { namespace Math { @@ -106,7 +108,7 @@ namespace Math { 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 { 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 { 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> // inline double EvalCentral(const UserFunc & f, double x, double h) { @@ -140,7 +142,7 @@ namespace Math { 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 @@ -148,28 +150,28 @@ namespace Math { 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 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 difference algorithm with a step size h. 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 difference algorithm with a step size h. 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 @@ -193,7 +195,7 @@ protected: private: - GSLDerivator * fDerivator; + mutable GSLDerivator * fDerivator; }; diff --git a/mathmore/inc/Math/GSLFunctionAdapter.h b/mathmore/inc/Math/GSLFunctionAdapter.h index b5632a26dff06ba089732374a98790404577036c..4ba1ada94d0b5e8be7bb4c647dcca34fc60c4af3 100644 --- a/mathmore/inc/Math/GSLFunctionAdapter.h +++ b/mathmore/inc/Math/GSLFunctionAdapter.h @@ -1,4 +1,4 @@ -// @(#)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 /********************************************************************** @@ -77,14 +77,14 @@ namespace Math { static double Df( double x, void * 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 ) { UserFunc * function = reinterpret_cast< UserFunc *> (p); *f = (*function) ( x ); - *df = (*function).Gradient( x ); + *df = (*function).Derivative( x ); } }; diff --git a/mathmore/inc/Math/GSLRootFinder.h b/mathmore/inc/Math/GSLRootFinder.h index 5776af2f9bbb447074aca69c28e2398f458da711..67fe803acf43fa0ade99e8f5548ef7e75f7a8611 100644 --- a/mathmore/inc/Math/GSLRootFinder.h +++ b/mathmore/inc/Math/GSLRootFinder.h @@ -1,4 +1,4 @@ -// @(#)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 /********************************************************************** @@ -31,8 +31,16 @@ #ifndef ROOT_Math_GSLRootFinder #define ROOT_Math_GSLRootFinder -#include "Math/IGenFunction.h" + +#ifndef ROOT_Math_GSLFunctionAdapter #include "Math/GSLFunctionAdapter.h" +#endif + +#ifndef ROOT_Math_IFunctionfwd +#include "Math/IFunctionfwd.h" +#endif + +#include <iostream> namespace ROOT { namespace Math { @@ -67,8 +75,9 @@ namespace Math { public: - void SetFunction( const IGenFunction & f, double Root) { - SetFunction( f, Root -1, Root + 1); // temporary + void SetFunction( const IGradFunction & , double ) { + std::cerr <<"GSLRootFinder - Error : use other Setfunction" << std::endl; + //SetFunction( f, Root -1, Root + 1); // temporary } /** diff --git a/mathmore/inc/Math/GSLRootFinderDeriv.h b/mathmore/inc/Math/GSLRootFinderDeriv.h index d7baf4a0a6628cd629fd1ccb35de7fba43b10663..55bca4ad05597a73d24f328ad9ce9c554a14060e 100644 --- a/mathmore/inc/Math/GSLRootFinderDeriv.h +++ b/mathmore/inc/Math/GSLRootFinderDeriv.h @@ -1,4 +1,4 @@ -// @(#)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 /********************************************************************** @@ -31,8 +31,19 @@ #ifndef ROOT_Math_GSL_RootFinderDeriv #define ROOT_Math_GSL_RootFinderDeriv -#include "Math/IGenFunction.h" + +#ifndef ROOT_Math_GSLFunctionAdapter #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 Math { @@ -67,14 +78,14 @@ namespace Math { public: - void SetFunction( const IGenFunction & f, double xlow, double xup) { - SetFunction( f, (xlow + xup)/2. ); + void SetFunction( const IGenFunction & , double , double ) { + 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; - 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 ); } diff --git a/mathmore/inc/Math/IGenFunction.h b/mathmore/inc/Math/IGenFunction.h deleted file mode 100644 index ce97b99b9145b278655ce6f446d77aaa31a9f906..0000000000000000000000000000000000000000 --- a/mathmore/inc/Math/IGenFunction.h +++ /dev/null @@ -1,107 +0,0 @@ -// @(#)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 */ diff --git a/mathmore/inc/Math/IParamFunction.h b/mathmore/inc/Math/IParamFunction.h deleted file mode 100644 index fa94270af8d72728185cf4fefc1a5da18e9b6ed1..0000000000000000000000000000000000000000 --- a/mathmore/inc/Math/IParamFunction.h +++ /dev/null @@ -1,99 +0,0 @@ -// @(#)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 */ diff --git a/mathmore/inc/Math/Integrator.h b/mathmore/inc/Math/Integrator.h index 3d0599fa66e05a666d31d72c1575b6c3d7cad705..081d81c79eb019004b251ce2b6abdf83b839e763 100644 --- a/mathmore/inc/Math/Integrator.h +++ b/mathmore/inc/Math/Integrator.h @@ -1,4 +1,4 @@ -// @(#)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 /********************************************************************** @@ -31,8 +31,15 @@ #ifndef ROOT_Math_Integrator #define ROOT_Math_Integrator -#include "Math/IGenFunction.h" +#ifndef ROOT_Math_IntegrationTypes #include "Math/IntegrationTypes.h" +#endif + +#ifndef ROOT_Math_IFunctionfwd +#include "Math/IFunctionfwd.h" +#endif + + #include <vector> diff --git a/mathmore/inc/Math/LinkDef.h b/mathmore/inc/Math/LinkDef.h index e932cecfc93ef9d1422d1f9a8ce2ec29be2a4bbc..63dad521e3ada6ecc085fcfb97fc4c0acd2627c2 100644 --- a/mathmore/inc/Math/LinkDef.h +++ b/mathmore/inc/Math/LinkDef.h @@ -1,4 +1,4 @@ -// @(#)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 @@ -6,12 +6,16 @@ #include "LinkDef_SpecFunc.h" #include "LinkDef_StatFunc.h" #include "LinkDef_Func.h" -#include "TF1.h" + #include "LinkDef_RootFinding.h" #ifdef __CINT__ +#pragma extra_include "Math/IFunctionfwd.h"; +#pragma extra_include "Math/IFunction.h"; + + #pragma link C++ nestedclasses; #pragma link C++ nestedtypedef; @@ -20,7 +24,7 @@ #pragma link C++ class ROOT::Math::ParamFunction+; #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+; diff --git a/mathmore/inc/Math/ParamFunction.h b/mathmore/inc/Math/ParamFunction.h index afd5b2e8428f236c86c9776af0981db424b8383c..40ab105eac34ddf0a31c272e56db52f448fc2ad4 100644 --- a/mathmore/inc/Math/ParamFunction.h +++ b/mathmore/inc/Math/ParamFunction.h @@ -1,4 +1,4 @@ -// @(#)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 /********************************************************************** @@ -34,7 +34,9 @@ #define ROOT_Math_ParamFunction +#ifndef ROOT_Math_IParamFunction #include "Math/IParamFunction.h" +#endif namespace ROOT { 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 - @param npar number of parameters (default is zero) - @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 - */ - ParamFunction(unsigned int npar = 0, bool providesGrad = false, bool providesParamGrad = false); + /** + Construct a parameteric function with npar parameters + @param npar number of parameters (default is zero) + @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 + */ + ParamFunction(unsigned int npar = 0, bool providesGrad = false, bool providesParamGrad = false); - // destructor - virtual ~ParamFunction() {} + // destructor + virtual ~ParamFunction() {} - // copying constructors + // copying constructors - // use defaults one ?? - //ParamFunction(const ParamFunction & pf); - //ParamFunction & operator = (const ParamFunction &); + // use defaults one ?? + //ParamFunction(const ParamFunction & pf); + //ParamFunction & operator = (const ParamFunction &); - // cloning - /** - Deep copy of function (to be implemented by the derived classes) - */ - virtual IGenFunction * Clone() const = 0; + // cloning + /** + Deep copy of function (to be implemented by the derived classes) + */ + //virtual ParamFunction * Clone() const = 0; - /** - Access the parameter values - */ - const std::vector<double> & Parameters() const { return fParams; } - - /** - Set the parameter values - @param p vector of doubles containing the parameter values. - */ - void SetParameters(const std::vector<double> & p) - // pending : can user change number of Parameters ? - { - fParams = p; - fNpar = fParams.size(); - } - - /** - Return the number of parameters - */ - unsigned int NumberOfParameters() const { return fNpar; } - - // this needs to be implemented in derived classes - /** - Evaluate function at a point x (to be implemented by the derived classes) - */ - virtual double operator() (double x ) = 0; - - // user may re-implement this for better efficiency - // this method is NOT required to change internal values of parameters. confusing ?? - /** - Evaluate function at a point x and for parameters p. - This method mey be needed for better efficiencies when for each function evaluation the parameters are changed. - */ - virtual double operator() (double x, const std::vector<double> & p ) - { - 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; } + /** + Access the parameter values + */ + virtual const double * Parameters() const { return &fParams.front(); } + + /** + Set the parameter values + @param p vector of doubles containing the parameter values. + */ + virtual void SetParameters(const double * p) + { + fParams = std::vector<double>(p,p+fNpar); + } + + /** + Return the number of parameters + */ + unsigned int NPar() const { return fNpar; } + + + //using BaseFunc::operator(); + + /** + 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; } + + const std::vector<double> & GetParGradient( double x) { + BaseFunc::ParameterGradient(x,&fParGradient[0]); + return fParGradient; + } public: @@ -142,20 +134,20 @@ public: protected: - // Parameters (make protected to be accessible directly by derived classes) - std::vector<double> fParams; + // Parameters (make protected to be accessible directly by derived classes) + std::vector<double> fParams; - // cache paramGradient for better efficiency (to be used by derived classes) - mutable std::vector<double> fParGradient; + // cache paramGradient for better efficiency (to be used by derived classes) + mutable std::vector<double> fParGradient; private: - // cache number of Parameters for speed efficiency - unsigned int fNpar; + // cache number of Parameters for speed efficiency + unsigned int fNpar; - bool fProvGrad; - bool fProvParGrad; + bool fProvGrad; + bool fProvParGrad; }; diff --git a/mathmore/inc/Math/Polynomial.h b/mathmore/inc/Math/Polynomial.h index b5770bd9c9e2f8e573d5b97fc6ce99493cb7a40e..a4a7256e7a1b67a064d9013f3adc3cfa23237f1d 100644 --- a/mathmore/inc/Math/Polynomial.h +++ b/mathmore/inc/Math/Polynomial.h @@ -1,4 +1,4 @@ -// @(#)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 /********************************************************************** @@ -51,106 +51,108 @@ namespace Math { @ingroup CppFunctions */ - class Polynomial : public ParamFunction { +class Polynomial : public ParamFunction { - public: +public: - /** - Construct a Polynomial function of order n. - The number of Parameters is n+1. - */ + /** + Construct a Polynomial function of order n. + The number of Parameters is n+1. + */ - Polynomial(unsigned int n); + Polynomial(unsigned int n); - /** - Construct a Polynomial of degree 1 : a*x + b - */ - Polynomial(double a, double b); + /** + Construct a Polynomial of degree 1 : a*x + b + */ + Polynomial(double a, double b); - /** - Construct a Polynomial of degree 2 : a*x**2 + b*x + c - */ - Polynomial(double a, double b, double c); + /** + Construct a Polynomial of degree 2 : a*x**2 + b*x + c + */ + Polynomial(double a, double b, double c); - /** - Construct a Polynomial of degree 3 : a*x**3 + b*x**2 + c*x + d - */ - Polynomial(double a, double b, double c, double 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); - /** - 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); + /** + 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); - virtual ~Polynomial(); + virtual ~Polynomial(); - /** - Copy constructor - */ - //Polynomial(const Polynomial &); + /** + Copy constructor + */ + //Polynomial(const Polynomial &); - /** - Copy operator - */ - //Polynomial & operator = (const Polynomial &); + /** + Copy operator + */ + //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 - */ - unsigned int Order() const { return fOrder; } + /** + 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 + */ + 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) - unsigned int fOrder; + // cache order = number of params - 1) + unsigned int fOrder; - // cache Parameters for Gradient - std::vector<double> fDerived_params; + // cache Parameters for Gradient + mutable std::vector<double> fDerived_params; - // roots + // roots - std::vector< std::complex < double > > fRoots; + std::vector< std::complex < double > > fRoots; }; diff --git a/mathmore/inc/Math/RootFinder.h b/mathmore/inc/Math/RootFinder.h index 017d5b2dbd5edb0b9d4eaa3e42380396073e0cae..f266517f089381421b7e75eab856078e78e6e6a2 100644 --- a/mathmore/inc/Math/RootFinder.h +++ b/mathmore/inc/Math/RootFinder.h @@ -1,4 +1,4 @@ -// @(#)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 /********************************************************************** @@ -31,8 +31,14 @@ #ifndef ROOT_Math_RootFinder #define ROOT_Math_RootFinder +#ifndef ROOT_Math_GSLRootHelper #include "Math/GSLRootHelper.h" -#include "Math/IGenFunction.h" +#endif + +#ifndef ROOT_Math_IFunctionfwd +#include "Math/IFunctionfwd.h" +#endif + /** @defgroup RootFinders Root-Finding algorithms @@ -109,7 +115,7 @@ public: <em> double operator() ( double x ) </em> */ - void SetFunction( const IGenFunction & f, double Root) { + void SetFunction( const IGradFunction & f, double Root) { fSolver.SetFunction( f, Root); } diff --git a/mathmore/inc/Math/RootFinderAlgorithms.h b/mathmore/inc/Math/RootFinderAlgorithms.h index 85d7296c7216154e6987a4eab8f9b26a267f84eb..e39bf6c5cf51ccdbf0755d4b3d39ad4b323c6489 100644 --- a/mathmore/inc/Math/RootFinderAlgorithms.h +++ b/mathmore/inc/Math/RootFinderAlgorithms.h @@ -1,4 +1,4 @@ -// @(#)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 /********************************************************************** @@ -32,10 +32,13 @@ #define ROOT_Math_GSLRootFinderAlgorithms +#ifndef ROOT_Math_GSLRootFinder #include "Math/GSLRootFinder.h" +#endif +#ifndef ROOT_Math_GSLRootFinderDeriv #include "Math/GSLRootFinderDeriv.h" - +#endif namespace ROOT { namespace Math { diff --git a/mathmore/inc/Math/WrappedFunction.h b/mathmore/inc/Math/WrappedFunction.h deleted file mode 100644 index 8a03414c920008a2414271c20763d077ab697a65..0000000000000000000000000000000000000000 --- a/mathmore/inc/Math/WrappedFunction.h +++ /dev/null @@ -1,74 +0,0 @@ -// @(#)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 diff --git a/mathmore/inc/Math/WrappedMultiTF1.h b/mathmore/inc/Math/WrappedMultiTF1.h new file mode 100644 index 0000000000000000000000000000000000000000..a9687cdbd1cc257a24c071975826084f30a43078 --- /dev/null +++ b/mathmore/inc/Math/WrappedMultiTF1.h @@ -0,0 +1,154 @@ +// @(#)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 */ diff --git a/mathmore/inc/Math/WrappedTF1.h b/mathmore/inc/Math/WrappedTF1.h new file mode 100644 index 0000000000000000000000000000000000000000..43ff2d5f20883e82e1adab24194aa1119f2e6c6a --- /dev/null +++ b/mathmore/inc/Math/WrappedTF1.h @@ -0,0 +1,159 @@ +// @(#)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 */ diff --git a/mathmore/src/Chebyshev.cxx b/mathmore/src/Chebyshev.cxx index 3cc3a348a7eef41fba1f261fc5286dda4b9f2386..ed8bb0ab0d05eef162a5ce8adf5cfbee0c707ac7 100644 --- a/mathmore/src/Chebyshev.cxx +++ b/mathmore/src/Chebyshev.cxx @@ -1,4 +1,4 @@ -// @(#)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 @@ -30,15 +30,17 @@ // Last update: Thu Dec 2 14:51:15 2004 // -#include <cassert> + +#include "Math/IFunction.h" #include "Math/Chebyshev.h" -#include "Math/IGenFunction.h" #include "GSLFunctionWrapper.h" #include "GSLChebSeries.h" #include "gsl/gsl_chebyshev.h" +#include <cassert> + namespace ROOT { namespace Math { diff --git a/mathmore/src/Derivator.cxx b/mathmore/src/Derivator.cxx index 49b88d9c8f0d63e8e5f4007bf2835eb0fb1815c2..32764d0724dd0dd6d36a56523dec5cb24bde850c 100644 --- a/mathmore/src/Derivator.cxx +++ b/mathmore/src/Derivator.cxx @@ -1,4 +1,4 @@ -// @(#)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 /********************************************************************** @@ -29,6 +29,7 @@ // Last update: Sat Nov 13 14:46:00 2004 // +#include "Math/IFunction.h" #include "Math/Derivator.h" #include "GSLDerivator.h" @@ -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); } -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); } -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); } -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); } -double Derivator::Eval( double x, double h) { +double Derivator::Eval( double x, double h) const { 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); } -double Derivator::EvalForward( double x, double h) { +double Derivator::EvalForward( double x, double h) const { 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); } diff --git a/mathmore/src/GSLDerivator.h b/mathmore/src/GSLDerivator.h index f967024b3b2c8749db64851be8c139d3e84a22e9..f5badcb7cfc5e1aa8843286ab23a3851879970ea 100644 --- a/mathmore/src/GSLDerivator.h +++ b/mathmore/src/GSLDerivator.h @@ -1,4 +1,4 @@ -// @(#)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 /********************************************************************** @@ -39,7 +39,8 @@ #include "Math/GSLFunctionAdapter.h" -#include "Math/IGenFunction.h" +#include "Math/IFunctionfwd.h" +#include "Math/IFunction.h" namespace ROOT { namespace Math { diff --git a/mathmore/src/GSLFunctionWrapper.h b/mathmore/src/GSLFunctionWrapper.h index eabb918a0667a5907e3585ef3b3d42bb10bb623a..53eede85f4e7409b469ed3e784fc937ee81e065b 100644 --- a/mathmore/src/GSLFunctionWrapper.h +++ b/mathmore/src/GSLFunctionWrapper.h @@ -1,4 +1,4 @@ -// @(#)root/mathmore:$Name: $:$Id: GSLFunctionWrapper.h,v 1.3 2005/09/19 13:06:53 brun Exp $ +// @(#)root/mathmore:$Name: $:$Id: GSLFunctionWrapper.h,v 1.4 2006/06/08 16:36:17 moneta Exp $ // Authors: L. Moneta, A. Zsenei 08/2005 /********************************************************************** @@ -33,7 +33,8 @@ #include "gsl/gsl_math.h" -#include "Math/IGenFunction.h" +#include "Math/IFunctionfwd.h" +#include "Math/IFunction.h" namespace ROOT { @@ -100,7 +101,7 @@ namespace Math { // evaluate the function and derivatives double operator() (double x) { return GSL_FN_FDF_EVAL_F(&fFunc, x); } - double Gradient (double x) { return GSL_FN_FDF_EVAL_DF(&fFunc, x); } + double Derivative (double x) { return GSL_FN_FDF_EVAL_DF(&fFunc, x); } void Fdf(double x, double & f, double & df) { return GSL_FN_FDF_EVAL_F_DF(&fFunc, x, &f, &df); diff --git a/mathmore/src/GSLIntegrator.cxx b/mathmore/src/GSLIntegrator.cxx index 7f48b00dd6cf4920153d762a4f0cd240fcfce41f..dc1e7086a47fb21548a3a99f1b93e5738c805b80 100644 --- a/mathmore/src/GSLIntegrator.cxx +++ b/mathmore/src/GSLIntegrator.cxx @@ -1,4 +1,4 @@ -// @(#)root/mathmore:$Name: $:$Id: GSLIntegrator.cxx,v 1.4 2006/06/16 10:34:08 moneta Exp $ +// @(#)root/mathmore:$Name: $:$Id: GSLIntegrator.cxx,v 1.5 2006/06/19 08:44:08 moneta Exp $ // Authors: L. Moneta, A. Zsenei 08/2005 /********************************************************************** @@ -31,7 +31,7 @@ #include "gsl/gsl_integration.h" #include "GSLIntegrator.h" -//#include "MathMore/IGenFunction.h" +#include "Math/IFunction.h" #include "GSLIntegrationWorkspace.h" diff --git a/mathmore/src/GSLIntegrator.h b/mathmore/src/GSLIntegrator.h index d248ffa53ba4509f0eb2406f4cd108de972d2adf..469e5a6d325abbc7918512fb242439c0836d6e3f 100644 --- a/mathmore/src/GSLIntegrator.h +++ b/mathmore/src/GSLIntegrator.h @@ -1,4 +1,4 @@ -// @(#)root/mathmore:$Name: $:$Id: GSLIntegrator.h,v 1.4 2006/06/19 08:44:08 moneta Exp $ +// @(#)root/mathmore:$Name: $:$Id: GSLIntegrator.h,v 1.5 2006/06/22 08:39:19 moneta Exp $ // Authors: L. Moneta, A. Zsenei 08/2005 /********************************************************************** @@ -31,11 +31,13 @@ #ifndef ROOT_Math_GSLIntegrator #define ROOT_Math_GSLIntegrator -#include "Math/IGenFunction.h" -#include "Math/Integrator.h" +#include "Math/IFunctionfwd.h" +#include "Math/IFunction.h" #include <vector> +#include "Math/Integrator.h" + #include "Math/GSLFunctionAdapter.h" diff --git a/mathmore/src/GSLRootFinder.cxx b/mathmore/src/GSLRootFinder.cxx index af011f410faa775e3c675d533cebec783e304d2c..a5a2f8263fe3d260285070f7d8dd64af7fc3c27e 100644 --- a/mathmore/src/GSLRootFinder.cxx +++ b/mathmore/src/GSLRootFinder.cxx @@ -1,4 +1,4 @@ -// @(#)root/mathmore:$Name: $:$Id: GSLRootFinder.cxx,v 1.2 2005/09/18 20:41:25 brun Exp $ +// @(#)root/mathmore:$Name: $:$Id: GSLRootFinder.cxx,v 1.3 2006/06/16 10:34:08 moneta Exp $ // Authors: L. Moneta, A. Zsenei 08/2005 /********************************************************************** @@ -29,6 +29,7 @@ // Last update: Sun Nov 14 11:27:11 2004 // +#include "Math/IFunction.h" #include "Math/GSLRootFinder.h" #include "Math/GSLRootHelper.h" diff --git a/mathmore/src/GSLRootFinderDeriv.cxx b/mathmore/src/GSLRootFinderDeriv.cxx index 9cb2f23fc5fbbedcf27b38527c686c8972cc7737..1c9e79217c99547c214b99ecb368c20d5e1dd71b 100644 --- a/mathmore/src/GSLRootFinderDeriv.cxx +++ b/mathmore/src/GSLRootFinderDeriv.cxx @@ -1,4 +1,4 @@ -// @(#)root/mathmore:$Name: $:$Id: GSLRootFinderDeriv.cxx,v 1.3 2006/06/16 10:34:08 moneta Exp $ +// @(#)root/mathmore:$Name: $:$Id: GSLRootFinderDeriv.cxx,v 1.4 2006/06/19 08:44:08 moneta Exp $ // Authors: L. Moneta, A. Zsenei 08/2005 /********************************************************************** @@ -29,6 +29,8 @@ // Last update: Sun Nov 21 16:26:03 2004 // +#include "Math/IFunction.h" + #include "Math/GSLRootFinderDeriv.h" #include "Math/GSLRootHelper.h" #include "GSLRootFdFSolver.h" diff --git a/mathmore/src/Integrator.cxx b/mathmore/src/Integrator.cxx index ecd87a0e328274ee81a0941f7f2160668f0eee20..9891edfb0e5260807cdbb533fae57499dcb13842 100644 --- a/mathmore/src/Integrator.cxx +++ b/mathmore/src/Integrator.cxx @@ -1,4 +1,4 @@ -// @(#)root/mathmore:$Name: $:$Id: Integrator.cxx,v 1.3 2005/09/18 20:41:25 brun Exp $ +// @(#)root/mathmore:$Name: $:$Id: Integrator.cxx,v 1.4 2006/06/16 10:34:08 moneta Exp $ // Authors: L. Moneta, A. Zsenei 08/2005 /********************************************************************** @@ -29,6 +29,7 @@ // Last update: Thu Nov 11 14:22:32 2004 // +#include "Math/IFunction.h" #include "Math/Integrator.h" #include "GSLIntegrator.h" diff --git a/mathmore/src/Polynomial.cxx b/mathmore/src/Polynomial.cxx index bdcbc3e595426416525192ce425862bfb8d58a42..87f6d5d54be11b1d52e17845fdbd8bcd0a1f27f7 100644 --- a/mathmore/src/Polynomial.cxx +++ b/mathmore/src/Polynomial.cxx @@ -1,4 +1,4 @@ -// @(#)root/mathmore:$Name: $:$Id: Polynomial.cxx,v 1.3 2005/09/19 13:06:53 brun Exp $ +// @(#)root/mathmore:$Name: $:$Id: Polynomial.cxx,v 1.4 2006/05/26 14:26:08 moneta Exp $ // Authors: L. Moneta, A. Zsenei 08/2005 /********************************************************************** @@ -121,7 +121,7 @@ Polynomial::~Polynomial() // } -double Polynomial::operator() (double x) { +double Polynomial::DoEval (double x) const { return gsl_poly_eval( &fParams.front(), fOrder + 1, x); @@ -135,7 +135,7 @@ double Polynomial::operator() (double x, const std::vector<double> & p) { } -double Polynomial::Gradient(double x) { +double Polynomial::DoDerivative(double x) const{ for ( unsigned int i = 0; i < fOrder; ++i ) fDerived_params[i] = (i + 1) * Parameters()[i+1]; @@ -144,12 +144,12 @@ double Polynomial::Gradient(double x) { } -const std::vector<double> & Polynomial::ParameterGradient (double x) { +void Polynomial::DoParameterGradient (double x, double * grad) const { - for (unsigned int i = 0; i < fParGradient.size(); ++i) - fParGradient[i] = gsl_pow_int(x, i); - - return fParGradient; + unsigned int npar = NPar(); + for (unsigned int i = 0; i < npar; ++i) + grad[i] = gsl_pow_int(x, i); + // return fParGradient; } @@ -277,7 +277,7 @@ const std::vector< std::complex <double> > & Polynomial::FindNumRoots(){ gsl_poly_complex_workspace * w = gsl_poly_complex_workspace_alloc( n + 1); std::vector<double> z(2*n); - int status = gsl_poly_complex_solve (&Parameters().front(), n+1, w, &z.front() ); + int status = gsl_poly_complex_solve ( Parameters(), n+1, w, &z.front() ); gsl_poly_complex_workspace_free(w); if (status != GSL_SUCCESS) return fRoots; for (unsigned int i = 0; i < n; ++i) diff --git a/mathmore/test/testChebyshev.cxx b/mathmore/test/testChebyshev.cxx index 0b7fdaecc89cc801941c3b821aa98c691e48478f..ccfc355ec5731fc7f96df8a17f1280c4e3fb19ab 100644 --- a/mathmore/test/testChebyshev.cxx +++ b/mathmore/test/testChebyshev.cxx @@ -1,6 +1,7 @@ #include "Math/Chebyshev.h" -#include "Math/IGenFunction.h" +#include "Math/IFunction.h" +#include "Math/Functor.h" #include "Math/SpecFunc.h" //#include "MathCore/GSLIntegrator.h" @@ -13,7 +14,7 @@ typedef double ( * FP ) ( double, void * ); // function is a step function -double myfunc ( double x, void * params) { +double myfunc ( double x, void * /* params */) { //double * p = reinterpret_cast<double *>( params); if (x < 0.5) return 0.25; @@ -31,14 +32,17 @@ class GammaFunction : public ROOT::Math::IGenFunction { public: - double operator() ( double x) { - return ROOT::Math::tgamma(x); - } ROOT::Math::IGenFunction * Clone() const { return new GammaFunction(); } +private: + + double DoEval ( double x) const { + return ROOT::Math::tgamma(x); + } + }; diff --git a/mathmore/test/testDerivation.cxx b/mathmore/test/testDerivation.cxx index 145bbb2ba358ce6b12ff0e24685513c86f76fedc..b055cbc1961bb6a267ae50c68c72b1956ec0b62f 100644 --- a/mathmore/test/testDerivation.cxx +++ b/mathmore/test/testDerivation.cxx @@ -1,10 +1,16 @@ #include "Math/Polynomial.h" #include "Math/Derivator.h" -#include "Math/WrappedFunction.h" -//#include "TF1.h" +#include "Math/IFunction.h" +#include "Math/Functor.h" +//#include "Math/WrappedTF1.h" #include <iostream> +#include <vector> - +#define HAVE_LIBHIST +#ifdef HAVE_LIBHIST +#include "TF1.h" +#include "TF2.h" +#endif typedef double ( * FP ) ( double, void * ); typedef double ( * FP2 ) ( double ); @@ -19,6 +25,41 @@ double myfunc2 ( double x) { return std::pow( x, 1.5); } +#ifdef LATER +// class WrappedFunc : public ROOT::Math::IGenFunction { + +// public: + +// WrappedFunc(TF1 & f, unsigned int icoord, double * x, double * par = 0) : +// fIndex(icoord), +// fFunc(f), +// fX(x), +// fParams(par) +// { +// assert(int(fIndex) < fFunc.GetNdim() ); +// } + + +// double operator() (double x) { +// //std::cout << " x = " << x << " fIndex " << fIndex << std::endl; +// assert( fX != 0 ); +// fX[fIndex] = x; +// return fFunc.EvalPar(fX, fParams); +// } + +// WrappedFunc * Clone() const { +// WrappedFunc * f = new WrappedFunc(fFunc,fIndex, fX, fParams); +// return f; +// } + +// private: + +// unsigned int fIndex; +// TF1 & fFunc; +// double * fX; +// double * fParams; +// }; +#endif void testDerivation() { @@ -34,20 +75,21 @@ void testDerivation() { p[0] = 2; p[1] = 3; p[2] = 4; - f1->SetParameters(p); + f1->SetParameters(&p[0]); + ROOT::Math::Functor1D<ROOT::Math::IGenFunction> polyf(*f1); ROOT::Math::Derivator *der = new ROOT::Math::Derivator(*f1); double step = 1E-8; double x0 = 2; - der->SetFunction(*f1); + der->SetFunction(polyf); double result = der->Eval(x0); std::cout << "Derivative of function inheriting from IGenFunction f(x) = 2 + 3x + 4x^2 at x = 2" << std::endl; std::cout << "Return code: " << der->Status() << std::endl; std::cout << "Result: " << result << " +/- " << der->Error() << std::endl; - std::cout << "Exact result: " << f1->Gradient(x0) << std::endl; - std::cout << "EvalForward: " << der->EvalForward(*f1, x0) << std::endl; + std::cout << "Exact result: " << f1->Derivative(x0) << std::endl; + std::cout << "EvalForward: " << der->EvalForward(polyf, x0) << std::endl; std::cout << "EvalBackward: " << der->EvalBackward(x0, step) << std::endl << std::endl;; @@ -70,19 +112,28 @@ void testDerivation() { // Derivative of a free function wrapped in an IGenFunction // Works when compiled c++, compiled ACLiC, does not work when interpreted by CINT - ROOT::Math::WrappedFunction<FP2> *f3 = new ROOT::Math::WrappedFunction<FP2>(myfunc2); + ROOT::Math::Functor1D<ROOT::Math::IGenFunction> *f3 = new ROOT::Math::Functor1D<ROOT::Math::IGenFunction>(myfunc2); - std::cout << "Derivative of a free function wrapped in WrappedFunction f(x) = x^(3/2) at x = 2" << std::endl; + std::cout << "Derivative of a free function wrapped in a Functor f(x) = x^(3/2) at x = 2" << std::endl; std::cout << "EvalCentral: " << der->EvalCentral( *f3, x0) << std::endl; der->SetFunction(*f3); std::cout << "EvalForward: " << der->EvalForward(x0) << std::endl; std::cout << "EvalBackward: " << der->EvalBackward(x0) << std::endl; std::cout << "Exact result: " << 1.5*sqrt(x0) << std::endl << std::endl; - + // Derivative of a multidim TF1 function - - +#ifdef LATER + TF2 * f2d = new TF2("f2d","x*x + y*y",-10,10,-10,10); + // find gradient at x={1,1} + double vx[2] = {1.,2.}; + ROOT::Math::WrappedTF1 fx(*f2d); + + std::cout << "Derivative of a f(x,y) = x^2 + y^2 at x = 1,y=2" << std::endl; + std::cout << "df/dx = " << der->EvalCentral(fx,1.) << std::endl; + WrappedFunc fy(*f2d,0,vx); + std::cout << "df/dy = " << der->EvalCentral(fy,2.) << std::endl; +#endif } diff --git a/mathmore/test/testIntegration.cxx b/mathmore/test/testIntegration.cxx index 1cd6fc6a6430f85ba76aa34a0df72277c15ff036..0c7a7bdbfb6396136f6f33ef9647ddd0e9488c85 100644 --- a/mathmore/test/testIntegration.cxx +++ b/mathmore/test/testIntegration.cxx @@ -1,6 +1,6 @@ #include "Math/Polynomial.h" #include "Math/Integrator.h" -#include "Math/WrappedFunction.h" +#include "Math/Functor.h" //#include "TF1.h" #include <iostream> @@ -17,7 +17,7 @@ double exactIntegral ( const std::vector<double> & par, double a, double b) { for (unsigned int i = 1; i < p.size() ; ++i) { p[i] = par[i-1]/double(i); } - func->SetParameters(p); + func->SetParameters(&p.front()); return (*func)(b)-(*func)(a); } @@ -30,20 +30,21 @@ double singularFunction(double x) { void testIntegration() { - ROOT::Math::Polynomial *f = new ROOT::Math::Polynomial(2); + ROOT::Math::Polynomial * f = new ROOT::Math::Polynomial(2); std::vector<double> p(3); p[0] = 4; p[1] = 2; p[2] = 6; - f->SetParameters(p); + f->SetParameters(&p[0]); + ROOT::Math::IGenFunction &func = *f; - double exactresult = exactIntegral(f->Parameters(), 0,3); + double exactresult = exactIntegral(p, 0,3); std::cout << "Exact value " << exactresult << std::endl << std::endl; - ROOT::Math::Integrator ig(*f, 0.001, 0.01, 100 ); + ROOT::Math::Integrator ig(func, 0.001, 0.01, 100 ); double value = ig.Integral( 0, 3); // or ig.Integral(*f, 0, 10); if new function @@ -56,7 +57,7 @@ void testIntegration() { // integrate again ADAPTIve, with different rule - ROOT::Math::Integrator ig2(*f, ROOT::Math::Integration::ADAPTIVE, ROOT::Math::Integration::GAUSS61, 0.001, 0.01, 100 ); + ROOT::Math::Integrator ig2(func, ROOT::Math::Integration::ADAPTIVE, ROOT::Math::Integration::GAUSS61, 0.001, 0.01, 100 ); value = ig2.Integral(0, 3); // or ig2.Integral(*f, 0, 10); if different function @@ -67,16 +68,20 @@ void testIntegration() { std::cout << "Testing SetFunction member function" << std::endl; ROOT::Math::Polynomial *pol = new ROOT::Math::Polynomial(2); - pol->SetParameters(p); - ig.SetFunction(*pol); + + pol->SetParameters(&p.front()); + ROOT::Math::IGenFunction &func2 = *pol; + ig.SetFunction(func2); std::cout << "Result " << ig.Integral( 0, 3) << " +/- " << ig.Error() << std::endl; // test error - typedef double ( * FreeFunc ) ( double); + //typedef double ( * FreeFunc ) ( double); std::cout << "Testing a singular function" << std::endl; - ROOT::Math::WrappedFunction<FreeFunc> wf(&singularFunction); + //ROOT::Math::WrappedFunction<FreeFunc> wf(&singularFunction); + ROOT::Math::Functor1D<ROOT::Math::IGenFunction> wf(&singularFunction); + ig.SetFunction(wf); double r = ig.Integral(0,1); if (ig.Status() != 0) diff --git a/mathmore/test/testMinimization1D.cxx b/mathmore/test/testMinimization1D.cxx index 064e47f7efa0ecc0aad1c5f3a7d9f11d36c021a1..fe498841cb63b935ed15e023d52086d8d22771bd 100644 --- a/mathmore/test/testMinimization1D.cxx +++ b/mathmore/test/testMinimization1D.cxx @@ -1,6 +1,6 @@ #include "Math/Polynomial.h" #include "Math/Minimizer1D.h" -#include "Math/WrappedFunction.h" +#include "Math/Functor.h" //#include "TF1.h" #include <iostream> @@ -10,19 +10,21 @@ void testMinimization1D() { - ROOT::Math::Polynomial *f = new ROOT::Math::Polynomial(2); + ROOT::Math::Polynomial *polyf = new ROOT::Math::Polynomial(2); std::vector<double> p(3); p[0] = 1; p[1] = -4; p[2] = 1; - f->SetParameters(p); + polyf->SetParameters(&p[0]); + //ROOT::Math::Functor1D<ROOT::Math::Base> func(*polyf); + ROOT::Math::IGenFunction & func = *polyf; { // default (Brent) ROOT::Math::Minimizer1D min; - min.SetFunction(*f,1,-10,10); + min.SetFunction(func,1,-10,10); int status = min.Minimize(100,0.01,0.01); std::cout << "test Min1D " << min.Name() << " Return code " << status << std::endl; @@ -34,7 +36,7 @@ void testMinimization1D() { { // Golden Section ROOT::Math::Minimizer1D min(ROOT::Math::Minim1D::GOLDENSECTION); - min.SetFunction(*f,1,-10,10); + min.SetFunction(func,1,-10,10); int status = min.Minimize(100,0.01,0.01); std::cout << "test Min1D " << min.Name() << " Return code " << status << std::endl; diff --git a/mathmore/test/testRandomDist.cxx b/mathmore/test/testRandomDist.cxx index d4d5a9da091c3b7b97af58bca03ddc19086a4b85..b4a6fa921284a672459331d4e1907108ddbc9820 100644 --- a/mathmore/test/testRandomDist.cxx +++ b/mathmore/test/testRandomDist.cxx @@ -38,7 +38,7 @@ void testDiff(TH1D & h1, TH1D & h2, const std::string & name="") { int ndf; if (h1.GetEntries() == 0 && h2.GetEntries() == 0) return; int igood; - double prob = h1.Chi2TestX(&h2,chi2,ndf,igood,""); + double prob = h1.Chi2TestX(&h2,chi2,ndf,igood,"UU"); std::cout << " Test " << name << " chi2 = " << chi2 << " ndf " << ndf << " prob = " << prob << std::endl << " igood = " << igood; std::string cname="c1_" + name; diff --git a/mathmore/test/testRootFinder.cxx b/mathmore/test/testRootFinder.cxx index d5e5ce040a9e33df7fb6f49c81dcf1971f52d0f8..1bb98e05826cf76bbaf7e2133f8e983a4724cd25 100644 --- a/mathmore/test/testRootFinder.cxx +++ b/mathmore/test/testRootFinder.cxx @@ -1,4 +1,5 @@ #include "Math/Polynomial.h" +#include "Math/Functor.h" #include "Math/RootFinder.h" #include "Math/RootFinderAlgorithms.h" @@ -49,15 +50,15 @@ int findRoot( RF * r ) { void testRootFinder() { - ROOT::Math::Polynomial func(2); + ROOT::Math::Polynomial polyf(2); std::vector<double> p(3); p[0] = -5; p[1] = 0; p[2] = 1; - func.SetParameters(p); - + polyf.SetParameters(&p[0]); + ROOT::Math::IGenFunction & func = polyf; ROOT::Math::RootFinder<ROOT::Math::Roots::Bisection> *rf1 = new ROOT::Math::RootFinder<ROOT::Math::Roots::Bisection>; rf1->SetFunction( func, 0, 5); @@ -68,15 +69,15 @@ void testRootFinder() { rf2->SetFunction( func, 0, 5); findRoot(rf2); - + // methods using derivatives ROOT::Math::RootFinder<ROOT::Math::Roots::Secant> *rf3 = new ROOT::Math::RootFinder<ROOT::Math::Roots::Secant>; - rf3->SetFunction( func, 0, 5); + rf3->SetFunction( polyf, 1); findRoot(rf3); ROOT::Math::RootFinder<ROOT::Math::Roots::Steffenson> *rf4 = new ROOT::Math::RootFinder<ROOT::Math::Roots::Steffenson>; - rf4->SetFunction( func, 0, 5); + rf4->SetFunction( polyf, 1); findRoot(rf4);