Object-oriented Scientific Computing Library: Version 0.910
funct.h
00001 /*
00002   -------------------------------------------------------------------
00003   
00004   Copyright (C) 2006-2012, Andrew W. Steiner
00005   
00006   This file is part of O2scl.
00007   
00008   O2scl is free software; you can redistribute it and/or modify
00009   it under the terms of the GNU General Public License as published by
00010   the Free Software Foundation; either version 3 of the License, or
00011   (at your option) any later version.
00012   
00013   O2scl is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016   GNU General Public License for more details.
00017   
00018   You should have received a copy of the GNU General Public License
00019   along with O2scl. If not, see <http://www.gnu.org/licenses/>.
00020 
00021   -------------------------------------------------------------------
00022 */
00023 #ifndef O2SCL_FUNCT_H
00024 #define O2SCL_FUNCT_H
00025 
00026 #include <string>
00027 
00028 #ifndef DOXYGENP
00029 namespace o2scl {
00030 #endif
00031 
00032   /** \brief One-dimensional function [abstract base]
00033       
00034       This class generalizes a one-dimensional function y(x).
00035 
00036       This class is one of a large number of function object classes
00037       in \o2 designed to provide a mechanism for the user to 
00038       supply functions to solvers, minimizers, integrators, etc.
00039       See \ref funct_section for a general description.
00040   */
00041   class funct {
00042     
00043   public:  
00044     
00045     /*
00046       This (undocumented) empty constructor is required for the 
00047       child funct classes
00048     */
00049     funct() {}
00050 
00051     virtual ~funct() {}
00052     
00053     /** \brief Compute the function at point \c x, with result \c y
00054      */
00055     virtual double operator()(double x)=0;
00056 
00057   };
00058   
00059   /** \brief Function pointer to a function
00060    */
00061   class funct_fptr : public funct {
00062 
00063   public:
00064     
00065     /** \brief Specify the function pointer
00066      */
00067     funct_fptr(double (*fp)(double)) {
00068       fptr=fp;
00069     }
00070     
00071     virtual ~funct_fptr() {}
00072 
00073     /** \brief Compute the function at point \c x, with result \c y
00074      */
00075     virtual double operator()(double x) {
00076       return fptr(x);
00077     }
00078 
00079 #ifndef DOXYGEN_INTERNAL
00080 
00081   protected:
00082 
00083     /// Function pointer
00084     double (*fptr)(double x);
00085     
00086     /// Copy constructor
00087   funct_fptr(const funct_fptr &f) : funct() {
00088       fptr=f.fptr;
00089     }
00090     
00091     /// Copy constructor
00092     funct_fptr &operator=(const funct_fptr &f) {
00093       fptr=f.fptr;
00094       return *this;
00095     }
00096 
00097 #endif
00098 
00099   };
00100 
00101   /** \brief Function pointer to a function with a parameter
00102    */
00103   template<class param_t> class funct_fptr_param : public funct {
00104     
00105   public:
00106     
00107     /** \brief Specify the function pointer
00108      */
00109     funct_fptr_param(double (*fp)(double, param_t &), param_t &pa) {
00110       fptr=fp;
00111       pp=&pa;
00112     }
00113     
00114     virtual ~funct_fptr_param() {}
00115 
00116     /** \brief Compute the function at point \c x, with result \c y
00117      */
00118     virtual double operator()(double x) {
00119       return fptr(x,*pp);
00120     }
00121     
00122 #ifndef DOXYGEN_INTERNAL
00123 
00124   protected:
00125 
00126     /// The function pointer
00127     double (*fptr)(double x, param_t &pa);
00128 
00129     /// The parameter
00130     param_t *pp;
00131 
00132     /// Copy constructor
00133     funct_fptr_param(const funct_fptr_param &f) {
00134       fptr=f.fptr;
00135     }
00136     
00137     /// Copy constructor
00138     funct_fptr_param &operator=(const funct_fptr_param &f) {
00139       fptr=f.fptr;
00140       return *this;
00141     }
00142 
00143 #endif
00144 
00145   };
00146 
00147   /** \brief Member function pointer to a one-dimensional function
00148    */
00149   template <class tclass> class funct_mfptr : public funct {
00150 
00151   public:
00152   
00153     /** \brief Specify the member function pointer
00154      */
00155     funct_mfptr(tclass *tp, double (tclass::*fp)(double x)) {
00156       tptr=tp;
00157       fptr=fp;
00158     }
00159   
00160     virtual ~funct_mfptr() {};
00161   
00162     /** \brief Compute the function at point \c x, with result \c y
00163      */
00164     virtual double operator()(double x) {
00165       return (*tptr.*fptr)(x);
00166     }
00167 
00168 #ifndef DOXYGEN_INTERNAL
00169 
00170   protected:
00171   
00172     /// Storage for the member function pointer
00173     double (tclass::*fptr)(double x);
00174 
00175     /// Store the pointer to the class instance
00176     tclass *tptr;
00177 
00178     /// Copy constructor
00179     funct_mfptr(const funct_mfptr &f) {
00180       fptr=f.fptr;
00181       tptr=f.tptr;
00182     }
00183     
00184     /// Copy constructor
00185     funct_mfptr &operator=(const funct_mfptr &f) {
00186       fptr=f.fptr;
00187       tptr=f.tptr;
00188       return *this;
00189     }
00190 
00191 #endif
00192 
00193   };
00194 
00195   /** \brief Member function pointer to a one-dimensional function
00196       with a parameter
00197   */
00198   template <class tclass, class param_t> class funct_mfptr_param : 
00199   public funct {
00200 
00201   public:
00202   
00203     /** \brief Specify the member function pointer
00204      */
00205     funct_mfptr_param(tclass *tp, 
00206                       double (tclass::*fp)(double x, param_t &pa), 
00207                       param_t &pa) {
00208       tptr=tp;
00209       fptr=fp;
00210       pp=&pa;
00211     }
00212   
00213     virtual ~funct_mfptr_param() {};
00214   
00215     /** \brief Compute the function at point \c x, with result \c y
00216      */
00217     virtual double operator()(double x) {
00218       return (*tptr.*fptr)(x,*pp);
00219     }
00220 
00221 #ifndef DOXYGEN_INTERNAL
00222 
00223   protected:
00224   
00225     /// Storage for the member function pointer
00226     double (tclass::*fptr)(double x, param_t &pa);
00227 
00228     /// Store the pointer to the class instance
00229     tclass *tptr;
00230 
00231     /// The parameter
00232     param_t *pp;
00233 
00234     /// Copy constructor
00235     funct_mfptr_param(const funct_mfptr_param &f) {
00236       fptr=f.fptr;
00237       tptr=f.tptr;
00238       pp=f.pp;
00239     }
00240     
00241     /// Copy constructor
00242     funct_mfptr_param &operator=(const funct_mfptr_param &f) {
00243       fptr=f.fptr;
00244       tptr=f.tptr;
00245       pp=f.pp;
00246       return *this;
00247     }
00248 
00249 #endif
00250 
00251   };
00252 
00253   /** \brief Const member function pointer to a one-dimensional function
00254 
00255       \note While this is designed to accept a pointer to a const
00256       member function, the choice of whether the class pointer
00257       given in the template type <tt>tclass</tt> is const or 
00258       not is up to the user.
00259   */
00260   template <class tclass> class funct_cmfptr : 
00261   public funct {
00262 
00263   public:
00264   
00265     /** \brief Specify the member function pointer
00266      */
00267     funct_cmfptr(tclass *tp, double (tclass::*fp)(double x) const) {
00268       tptr=tp;
00269       fptr=fp;
00270     }
00271   
00272     virtual ~funct_cmfptr() {};
00273   
00274     /** \brief Compute the function at point \c x, with result \c y
00275      */
00276     virtual double operator()(double x) {
00277       return (*tptr.*fptr)(x);
00278     }
00279 
00280 #ifndef DOXYGEN_INTERNAL
00281 
00282   protected:
00283   
00284     /// Storage for the const member function pointer
00285     double (tclass::*fptr)(double x) const;
00286 
00287     /// Store the pointer to the class instance
00288     tclass *tptr;
00289 
00290     /// Copy constructor
00291     funct_cmfptr(const funct_cmfptr &f) {
00292       fptr=f.fptr;
00293       tptr=f.tptr;
00294     }
00295     
00296     /// Copy constructor
00297     funct_cmfptr &operator=(const funct_cmfptr &f) {
00298       fptr=f.fptr;
00299       tptr=f.tptr;
00300       return *this;
00301     }
00302 
00303 #endif
00304 
00305   };
00306 
00307   /** \brief Const member function pointer to a one-dimensional function 
00308       with a parameter
00309 
00310       \note While this is designed to accept a pointer to a const
00311       member function, the choice of whether the class pointer
00312       given in the template type <tt>tclass</tt> is const or 
00313       not is up to the user.
00314   */
00315   template <class tclass, class param_t> class funct_cmfptr_param : 
00316   public funct {
00317 
00318   public:
00319   
00320     /** \brief Specify the member function pointer
00321      */
00322     funct_cmfptr_param(tclass *tp, 
00323                        double (tclass::*fp)(double x, param_t &pa) const,
00324                        param_t &pa) {
00325       tptr=tp;
00326       fptr=fp;
00327       pp=&pa;
00328     }
00329   
00330     virtual ~funct_cmfptr_param() {};
00331   
00332     /** \brief Compute the function at point \c x, with result \c y
00333      */
00334     virtual double operator()(double x, param_t &pa) {
00335       return (*tptr.*fptr)(x,*pp);
00336     }
00337 
00338 #ifndef DOXYGEN_INTERNAL
00339 
00340   protected:
00341   
00342     /// Storage for the const member function pointer
00343     double (tclass::*fptr)(double x, param_t &pa) const;
00344 
00345     /// Store the pointer to the class instance
00346     tclass *tptr;
00347 
00348     /// The parameter
00349     param_t *pp;
00350 
00351     /// Copy constructor
00352     funct_cmfptr_param(const funct_cmfptr_param &f) {
00353       fptr=f.fptr;
00354       tptr=f.tptr;
00355       pp=f.pp;
00356     }
00357     
00358     /// Copy constructor
00359     funct_cmfptr_param &operator=(const funct_cmfptr_param &f) {
00360       fptr=f.fptr;
00361       tptr=f.tptr;
00362       pp=f.pp;
00363       return *this;
00364     }
00365 
00366 #endif
00367 
00368   };
00369 
00370 #ifndef DOXYGENP
00371 }
00372 #endif
00373 
00374 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).

Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads.