CLHEP/GenericFunctions/AbsFunction.hh

00001 // -*- C++ -*-
00002 // $Id: AbsFunction.hh,v 1.6 2003/09/03 08:45:43 pvs Exp $
00003 //------------------------AbsFunction-----------------------------------//
00004 //                                                                      //
00005 //  AbsFunction, base class for function objects                        //
00006 //  Joe Boudreau, Petar Maksimovic                                      //
00007 //  Nov 1999                                                            //
00008 //                                                                      //
00009 //----------------------------------------------------------------------//
00010 #ifndef AbsFunction_h
00011 #define AbsFunction_h 1
00012 #include "CLHEP/GenericFunctions/Argument.hh"
00013 #include "CLHEP/config/CLHEP.h"
00014 
00015 namespace Genfun {
00016 
00017   class AbsParameter;
00018 
00019   //-----------------------------------------------------------------------//
00020   // Exact return type of arithmentic operations.  To the user, the return //
00021   // type is GENFUNCTION, or const AbsFunction &.                          //
00022   //-----------------------------------------------------------------------//
00023 
00024   class FunctionProduct;
00025   class FunctionSum;
00026   class FunctionDifference;
00027   class FunctionQuotient;
00028   class FunctionNegation;
00029   class FunctionConvolution;
00030   class FunctionDirectProduct;
00031   class FunctionComposition;
00032   class ConstPlusFunction;
00033   class ConstTimesFunction;
00034   class ConstMinusFunction;
00035   class ConstOverFunction;
00036   class FunctionPlusParameter;
00037   class FunctionTimesParameter;
00038   class FunctionNumDeriv;
00039   class Variable;
00040   class FunctionNoop;
00041 
00042   typedef FunctionNoop Derivative;
00043 
00048   class AbsFunction {
00049   
00050   public:
00051   
00052     // Default Constructor
00053     AbsFunction();
00054   
00055     // Copy Constructor:
00056     AbsFunction(const AbsFunction &right);
00057   
00058     // Destructor
00059     virtual ~AbsFunction();
00060   
00061     // Function value:  N-dimensional functions must override these:
00062     virtual unsigned int dimensionality() const ;      // returns 1;
00063 
00064     // Function value
00065     virtual double operator() (double argument)          const=0;   
00066     virtual double operator() (const Argument &argument) const=0; 
00067 
00068     // Every function must override this:
00069     AbsFunction * clone() const;
00070   
00071     // Function composition.  Do not attempt to override:
00072     virtual FunctionComposition operator () (const AbsFunction &f) const;
00073     
00074     // Derivative, (All functions)  (do not override)
00075     Derivative derivative(const Variable &v) const;
00076 
00077     // Derivative (1D functions only) (do not override)
00078     Derivative prime() const;
00079 
00080     // Does this function have an analytic derivative?
00081     virtual bool hasAnalyticDerivative() const {return false;}
00082 
00083     // Derivative.  Overriders may be provided, numerical method by default!
00084     virtual Derivative partial(unsigned int) const;
00085   
00086   private:
00087 
00088     virtual AbsFunction *_clone() const=0;
00089 
00090     // It is illegal to assign a function.
00091     const AbsFunction & operator=(const AbsFunction &right);
00092   
00093   };
00094 
00095 FunctionProduct           operator * (const AbsFunction &op1, const AbsFunction &op2);
00096 FunctionSum               operator + (const AbsFunction &op1, const AbsFunction &op2);
00097 FunctionDifference        operator - (const AbsFunction &op1, const AbsFunction &op2);
00098 FunctionQuotient          operator / (const AbsFunction &op1, const AbsFunction &op2);
00099 FunctionNegation          operator - (const AbsFunction &op1);
00100 
00101 ConstTimesFunction        operator * (double c, const AbsFunction &op2);
00102 ConstPlusFunction         operator + (double c, const AbsFunction &op2);
00103 ConstMinusFunction        operator - (double c, const AbsFunction &op2);
00104 ConstOverFunction         operator / (double c, const AbsFunction &op2);
00105 
00106 ConstTimesFunction        operator * (const AbsFunction &op2, double c);
00107 ConstPlusFunction         operator + (const AbsFunction &op2, double c);
00108 ConstPlusFunction         operator - (const AbsFunction &op2, double c);
00109 ConstTimesFunction        operator / (const AbsFunction &op2, double c);
00110 
00111 FunctionTimesParameter    operator * (const AbsFunction &op1, const AbsParameter &op2);
00112 FunctionPlusParameter     operator + (const AbsFunction &op1, const AbsParameter &op2);
00113 FunctionPlusParameter     operator - (const AbsFunction &op1, const AbsParameter &op2);
00114 FunctionTimesParameter    operator / (const AbsFunction &op1, const AbsParameter &op2);
00115 
00116 FunctionTimesParameter    operator * (const AbsParameter   &op1, const AbsFunction &op2);
00117 FunctionPlusParameter     operator + (const AbsParameter   &op1, const AbsFunction &op2);
00118 FunctionPlusParameter     operator - (const AbsParameter   &op1, const AbsFunction &op2);
00119 FunctionTimesParameter    operator / (const AbsParameter   &op1, const AbsFunction &op2);
00120 
00121 FunctionConvolution       convolve   (const AbsFunction &op1, const AbsFunction &op2, double x0, double x1);
00122 FunctionDirectProduct     operator % (const AbsFunction &op1, const AbsFunction &op2);
00123 
00124 typedef const AbsFunction & GENFUNCTION;
00125 
00126 } // namespace Genfun
00127 
00128 
00129 //----------------------------------------------------------------------------
00130 //
00131 // This macro does all the ugly boilerplate.  For reference I will lis what
00132 // it is doing:
00133 //
00134 // 1).  It uses the base class function composition operator.  It would be
00135 //      nice to just use the 
00136 // 
00137 //        using AbsFunction::operator();
00138 //      
00139 //      directive but unfortunately this is compiler-dependent!
00140 //
00141 // 2)  It defines the clone methods, using a poor-man's covariant return type.
00142 //     again this is for compiler independence.
00143 //
00144 
00145 #define FUNCTION_OBJECT_DEF(classname) \
00146 public:                                \
00147   virtual FunctionComposition operator()(const AbsFunction &function) const; \
00148   classname *clone() const;            \
00149 private:                               \
00150   virtual AbsFunction *_clone() const;
00151 
00152 //----------------------------------------------------------------------------
00153 //
00154 //  This macro implements the ugly boilerplate
00155 //
00156   
00157 #define FUNCTION_OBJECT_IMP(classname)       \
00158 FunctionComposition classname::operator()(const AbsFunction & function) const\
00159 {                                            \
00160   return AbsFunction::operator() (function); \
00161 }                                            \
00162 classname *classname::clone () const {       \
00163   return (classname *) _clone();             \
00164 }                                            \
00165 AbsFunction *classname::_clone () const {    \
00166   return new classname(*this);                       \
00167 }
00168 
00169 //----------------------------------------------------------------------------
00170 
00171 
00172 #include "CLHEP/GenericFunctions/FunctionProduct.hh"
00173 #include "CLHEP/GenericFunctions/FunctionSum.hh"
00174 #include "CLHEP/GenericFunctions/FunctionDifference.hh"
00175 #include "CLHEP/GenericFunctions/FunctionQuotient.hh"
00176 #include "CLHEP/GenericFunctions/FunctionConvolution.hh"
00177 #include "CLHEP/GenericFunctions/FunctionNegation.hh"
00178 #include "CLHEP/GenericFunctions/FunctionDirectProduct.hh"
00179 #include "CLHEP/GenericFunctions/FunctionComposition.hh"
00180 #include "CLHEP/GenericFunctions/ConstPlusFunction.hh"
00181 #include "CLHEP/GenericFunctions/ConstTimesFunction.hh"
00182 #include "CLHEP/GenericFunctions/ConstMinusFunction.hh"
00183 #include "CLHEP/GenericFunctions/ConstOverFunction.hh"
00184 #include "CLHEP/GenericFunctions/FunctionPlusParameter.hh"
00185 #include "CLHEP/GenericFunctions/FunctionTimesParameter.hh"
00186 #include "CLHEP/GenericFunctions/FunctionNoop.hh"
00187 
00188 #endif

Class Library for High Energy Physics (version 1.8)