multi_funct.h

00001 /*
00002   -------------------------------------------------------------------
00003   
00004   Copyright (C) 2006, 2007, 2008, 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_MULTI_FUNCT_H
00024 #define O2SCL_MULTI_FUNCT_H
00025 
00026 #include <string>
00027 #include <o2scl/collection.h>
00028 
00029 #ifndef DOXYGENP
00030 namespace o2scl {
00031 #endif
00032 
00033   /** 
00034       \brief Multi-dimensional function base
00035 
00036       This class generalizes one function of several variables,
00037       i.e. \f$ y(x_0,x_1,...,x_{nv-1}) \f$ where \c nv is the number
00038       of variables in the function y. 
00039   */
00040 
00041   template<class param_t, class vec_t=ovector_view> class multi_funct {
00042 
00043     public:  
00044 
00045     multi_funct() {}
00046 
00047     virtual ~multi_funct() {}
00048 
00049     /** \brief Compute a function \c y of \c nv variables stored in \c x
00050         with parameter \c pa.
00051     */
00052     virtual int operator()(size_t nv, const vec_t &x, double &y, param_t &pa) {
00053       set_err_ret("Missing base in mm_funct::operator().",gsl_nobase);
00054     }
00055     
00056 
00057     /** \brief Return the value of a function of \c nv variables
00058         stored in \c x with parameter \c pa.
00059 
00060         Note that this is reimplemented in all children because 
00061         if one member function operator() is reimplemented, all must be.
00062     */
00063     virtual double operator()(size_t nv, const vec_t &x, param_t &pa) {
00064       double y;
00065       operator()(nv,x,y,pa);
00066       return y;
00067     }
00068   
00069 #ifndef DOXYGENP
00070 
00071     private:
00072 
00073     multi_funct(const multi_funct &);
00074     multi_funct& operator=(const multi_funct&);
00075 
00076 #endif
00077 
00078   };
00079 
00080   /** \brief Function pointer to a multi-dimensional function
00081    */
00082   template<class param_t, class vec_t=ovector_view>
00083     class multi_funct_fptr : public multi_funct<param_t,vec_t> {
00084 
00085     public:
00086 
00087     /** \brief Specify the function pointer
00088      */
00089     multi_funct_fptr(int (*fp)(size_t nv, const vec_t &x, double &y, 
00090                                param_t &pa)) {
00091       fptr=fp;
00092     }
00093     
00094 
00095     virtual ~multi_funct_fptr() {};
00096 
00097     /** \brief Compute a function \c y of \c nv variables stored in \c x
00098         with parameter \c pa.
00099     */
00100     virtual int operator()(size_t nv, const vec_t &x, double &y, param_t &pa) {
00101       return fptr(nv,x,y,pa);
00102     }
00103 
00104     /** \brief Return the value of a function of \c nv variables
00105         stored in \c x with parameter \c pa.
00106 
00107         Note that this is reimplemented in all children because 
00108         if one member function operator() is reimplemented, all must be.
00109     */
00110     virtual double operator()(size_t nv, const vec_t &x, param_t &pa) {
00111       double y;
00112       operator()(nv,x,y,pa);
00113       return y;
00114     }
00115 
00116 #ifndef DOXYGEN_INTERNAL
00117 
00118     protected:
00119     
00120     friend class io_tlate<multi_funct_fptr>;
00121 
00122     /// Store the function pointer
00123     int (*fptr)(size_t nv, const vec_t &x, double &y, param_t &pa);
00124 
00125     multi_funct_fptr() {}
00126 
00127 #ifndef DOXYGENP
00128 #endif
00129 
00130     private:
00131 
00132     multi_funct_fptr(const multi_funct_fptr &);
00133     multi_funct_fptr& operator=(const multi_funct_fptr&);
00134 
00135 #endif
00136 
00137   };
00138 
00139   /** \brief Function pointer to a gsl_multimin_function
00140    */
00141   template<class param_t, class vec_t=ovector_view>
00142     class multi_funct_gsl : public multi_funct<param_t,vec_t> {
00143     public:
00144 
00145     /** \brief Specify the function pointer
00146      */
00147     multi_funct_gsl(double (*fp)(const gsl_vector *x, param_t &pa)) {
00148       fptr=fp;
00149     }
00150     
00151     virtual ~multi_funct_gsl() {}
00152 
00153     /** \brief Compute a function \c y of \c nv variables stored in \c x
00154         with parameter \c pa.
00155     */
00156     virtual int operator()(size_t nv, const vec_t &x, double &y, param_t &pa) {
00157       const gsl_vector *gx=(const gsl_vector *)(&x);
00158       y=fptr(gx,pa);
00159       return 0;
00160     }
00161 
00162     /** \brief Return the value of a function of \c nv variables
00163         stored in \c x with parameter \c pa.
00164 
00165         Note that this is reimplemented in all children because 
00166         if one member function operator() is reimplemented, all must be.
00167     */
00168     virtual double operator()(size_t nv, const vec_t &x, param_t &pa) {
00169       double y;
00170       operator()(nv,x,y,pa);
00171       return y;
00172     }
00173 
00174 #ifndef DOXYGEN_INTERNAL
00175 
00176     protected:
00177   
00178     friend class io_tlate<multi_funct_gsl>;
00179     
00180     /// Store the function pointer
00181     double (*fptr)(const gsl_vector *x, param_t &pa);
00182     
00183     multi_funct_gsl() {}
00184 
00185 #ifndef DOXYGENP
00186 #endif
00187 
00188     private:
00189 
00190     multi_funct_gsl(const multi_funct_gsl &);
00191     multi_funct_gsl& operator=(const multi_funct_gsl&);
00192 
00193 #endif
00194 
00195   };
00196 
00197   /** \brief Function pointer to a multi-dimensional function without
00198       error control
00199   */
00200   template<class param_t, class vec_t=ovector_view>
00201     class multi_funct_fptr_noerr : public multi_funct<param_t,vec_t> {
00202     public:
00203 
00204     /** \brief Specify the function pointer
00205      */
00206     multi_funct_fptr_noerr(double (*fp)(size_t nv, const vec_t &x, 
00207                                         param_t &pa)) {
00208       fptr=fp;
00209     }
00210 
00211     virtual ~multi_funct_fptr_noerr() {}
00212     
00213     /** \brief Compute a function \c y of \c nv variables stored in \c x
00214         with parameter \c pa.
00215     */
00216     virtual int operator()(size_t nv, const vec_t &x, double &y, param_t &pa) {
00217       y=fptr(nv,x,pa);
00218       return 0;
00219     }
00220 
00221     /** \brief Return the value of a function of \c nv variables
00222         stored in \c x with parameter \c pa.
00223 
00224         Note that this is reimplemented in all children because 
00225         if one member function operator() is reimplemented, all must be.
00226     */
00227     virtual double operator()(size_t nv, const vec_t &x, param_t &pa) {
00228       double y;
00229       operator()(nv,x,y,pa);
00230       return y;
00231     }
00232 
00233 #ifndef DOXYGEN_INTERNAL
00234 
00235     protected:
00236 
00237     friend class io_tlate<multi_funct_fptr_noerr>;
00238   
00239     multi_funct_fptr_noerr() {}
00240 
00241     /// Store the function pointer
00242     double (*fptr)(size_t nv, const vec_t &x, param_t &pa);
00243 
00244 #ifndef DOXYGENP
00245 #endif
00246 
00247     private:
00248 
00249     multi_funct_fptr_noerr(const multi_funct_fptr_noerr &);
00250     multi_funct_fptr_noerr& operator=(const multi_funct_fptr_noerr&);
00251 
00252 #endif
00253 
00254   };
00255 
00256   /** \brief Member function pointer to a multi-dimensional function
00257    */
00258   template<class tclass, class param_t, class vec_t=ovector_view>
00259     class multi_funct_mfptr : public multi_funct<param_t,vec_t> {
00260     public:
00261   
00262     /** \brief Specify the member function pointer
00263      */
00264     multi_funct_mfptr(tclass *tp, int (tclass::*fp)
00265                       (size_t nv, const vec_t &x, double &y, param_t &pa)) {
00266       tptr=tp;
00267       fptr=fp;
00268     }
00269   
00270     virtual ~multi_funct_mfptr() {}
00271   
00272     /** \brief Compute a function \c y of \c nv variables stored in \c x
00273         with parameter \c pa.
00274     */
00275     virtual int operator()(size_t nv, const vec_t &x, double &y, param_t &pa) {
00276       return (*tptr.*fptr)(nv,x,y,pa);
00277     }
00278 
00279     /** \brief Return the value of a function of \c nv variables
00280         stored in \c x with parameter \c pa.
00281 
00282         Note that this is reimplemented in all children because 
00283         if one member function operator() is reimplemented, all must be.
00284     */
00285     virtual double operator()(size_t nv, const vec_t &x, param_t &pa) {
00286       double y;
00287       operator()(nv,x,y,pa);
00288       return y;
00289     }
00290   
00291 #ifndef DOXYGEN_INTERNAL
00292   
00293     protected:
00294   
00295     /// Store the function pointer
00296     int (tclass::*fptr)(size_t nv, const vec_t &x, double &y, param_t &pa);
00297     /// Store a pointer to the class instance
00298     tclass *tptr;
00299   
00300 #ifndef DOXYGENP
00301 #endif
00302 
00303     private:
00304 
00305     multi_funct_mfptr(const multi_funct_mfptr &);
00306     multi_funct_mfptr& operator=(const multi_funct_mfptr&);
00307 
00308 #endif
00309 
00310   };
00311 
00312   /** \brief Member function pointer to a multi-dimensional function
00313    */
00314   template<class tclass, class param_t, class vec_t=ovector_view>
00315     class multi_funct_mfptr_noerr : public multi_funct<param_t,vec_t> {
00316     public:
00317   
00318     /** \brief Specify the member function pointer
00319      */
00320     multi_funct_mfptr_noerr(tclass *tp, double (tclass::*fp)
00321                             (size_t nv, const vec_t &x, param_t &pa)) {
00322       tptr=tp;
00323       fptr=fp;
00324     }
00325   
00326     virtual ~multi_funct_mfptr_noerr() {}
00327   
00328     /** \brief Compute a function \c y of \c nv variables stored in \c x
00329         with parameter \c pa.
00330     */
00331     virtual int operator()(size_t nv, const vec_t &x, double &y, param_t &pa) {
00332       y=(*tptr.*fptr)(nv,x,pa);
00333       return 0;
00334     }
00335 
00336     /** \brief Return the value of a function of \c nv variables
00337         stored in \c x with parameter \c pa.
00338 
00339         Note that this is reimplemented in all children because 
00340         if one member function operator() is reimplemented, all must be.
00341     */
00342     virtual double operator()(size_t nv, const vec_t &x, param_t &pa) {
00343       double y;
00344       operator()(nv,x,y,pa);
00345       return y;
00346     }
00347   
00348 #ifndef DOXYGEN_INTERNAL
00349   
00350     protected:
00351   
00352     /// Store the function pointer
00353     double (tclass::*fptr)(size_t nv, const vec_t &x, param_t &pa);
00354     /// Store a pointer to the class instance
00355     tclass *tptr;
00356   
00357 #ifndef DOXYGENP
00358 #endif
00359 
00360     private:
00361 
00362     multi_funct_mfptr_noerr(const multi_funct_mfptr_noerr &);
00363     multi_funct_mfptr_noerr& operator=(const multi_funct_mfptr_noerr&);
00364 
00365 #endif
00366 
00367   };
00368   
00369   // ---------------------------------------------------------------------
00370   // ---------------------------------------------------------------------
00371 
00372   /** 
00373       \brief Multi-dimensional function base with arrays
00374 
00375       This class generalizes one function of several variables,
00376       i.e.  \f$ y(x_0,x_1,...,x_{nv-1}) \f$  where \c nv  is the number
00377       of variables in the function y. 
00378   */
00379   template<class param_t, size_t nvar> class multi_vfunct {
00380 
00381     public:  
00382 
00383     multi_vfunct() {}
00384 
00385     virtual ~multi_vfunct() {}
00386     
00387     /** \brief Compute a function \c y of \c nv variables stored in \c x
00388         with parameter \c pa.
00389     */
00390     virtual int operator()(size_t nv, const double x[nvar], double &y, 
00391                            param_t &pa) {
00392       set_err_ret("Missing base in mm_vfunct::operator().",gsl_nobase);
00393     }
00394     
00395     /** \brief Return the value of a function of \c nv variables
00396         stored in \c x with parameter \c pa.
00397 
00398         Note that this is reimplemented in all children because 
00399         if one member function operator() is reimplemented, all must be.
00400     */
00401     virtual double operator()(size_t nv, const double x[nvar], param_t &pa) {
00402       double y;
00403       operator()(nv,x,y,pa);
00404       return y;
00405     }
00406   
00407 #ifndef DOXYGENP
00408 
00409     private:
00410 
00411     multi_vfunct(const multi_vfunct &);
00412     multi_vfunct& operator=(const multi_vfunct&);
00413 
00414 #endif
00415 
00416   };
00417 
00418   /** \brief Function pointer to a multi-dimensional function with arrays
00419    */
00420   template<class param_t, size_t nvar>
00421     class multi_vfunct_fptr : public multi_vfunct<param_t,nvar> {
00422 
00423     public:
00424 
00425     /** \brief Specify the function pointer
00426      */
00427     multi_vfunct_fptr(int (*fp)(size_t nv, const double x[nvar], double &y, 
00428                                param_t &pa)) {
00429       fptr=fp;
00430     }
00431     
00432 
00433     virtual ~multi_vfunct_fptr() {}
00434 
00435     /** \brief Compute a function \c y of \c nv variables stored in \c x
00436         with parameter \c pa.
00437     */
00438     virtual int operator()(size_t nv, const double x[nvar], double &y, 
00439                            param_t &pa) {
00440       return fptr(nv,x,y,pa);
00441     }
00442     
00443     /** \brief Return the value of a function of \c nv variables
00444         stored in \c x with parameter \c pa.
00445 
00446         Note that this is reimplemented in all children because 
00447         if one member function operator() is reimplemented, all must be.
00448     */
00449     virtual double operator()(size_t nv, const double x[nvar], param_t &pa) {
00450       double y;
00451       operator()(nv,x,y,pa);
00452       return y;
00453     }
00454 
00455 #ifndef DOXYGEN_INTERNAL
00456 
00457     protected:
00458     
00459     friend class io_tlate<multi_vfunct_fptr>;
00460 
00461     /// Store the function pointer
00462     int (*fptr)(size_t nv, const double x[nvar], double &y, param_t &pa);
00463 
00464     multi_vfunct_fptr() {}
00465 
00466 #ifndef DOXYGENP
00467 #endif
00468 
00469     private:
00470 
00471     multi_vfunct_fptr(const multi_vfunct_fptr &);
00472     multi_vfunct_fptr& operator=(const multi_vfunct_fptr&);
00473 
00474 #endif
00475 
00476   };
00477 
00478   /** \brief Function pointer to a gsl_multimin_function with arrays
00479    */
00480   template<class param_t, size_t nvar>
00481     class multi_vfunct_gsl : public multi_vfunct<param_t,nvar> {
00482     public:
00483 
00484     /** \brief Specify the function pointer
00485      */
00486     multi_vfunct_gsl(double (*fp)(const gsl_vector *x, param_t &pa)) {
00487       fptr=fp;
00488     }
00489     
00490     virtual ~multi_vfunct_gsl() {}
00491 
00492     /** \brief Compute a function \c y of \c nv variables stored in \c x
00493         with parameter \c pa.
00494     */
00495     virtual int operator()(size_t nv, const double x[nvar], double &y, 
00496                            param_t &pa) {
00497       const gsl_vector *gx=(const gsl_vector *)(&x);
00498       y=fptr(gx,pa);
00499       return 0;
00500     }
00501 
00502     /** \brief Return the value of a function of \c nv variables
00503         stored in \c x with parameter \c pa.
00504 
00505         Note that this is reimplemented in all children because 
00506         if one member function operator() is reimplemented, all must be.
00507     */
00508     virtual double operator()(size_t nv, const double x[nvar], param_t &pa) {
00509       double y;
00510       operator()(nv,x,y,pa);
00511       return y;
00512     }
00513 
00514 #ifndef DOXYGEN_INTERNAL
00515 
00516     protected:
00517   
00518     friend class io_tlate<multi_vfunct_gsl>;
00519     
00520     /// Store the function pointer
00521     double (*fptr)(const gsl_vector *x, param_t &pa);
00522     
00523     multi_vfunct_gsl() {}
00524 
00525 #ifndef DOXYGENP
00526 #endif
00527 
00528     private:
00529 
00530     multi_vfunct_gsl(const multi_vfunct_gsl &);
00531     multi_vfunct_gsl& operator=(const multi_vfunct_gsl&);
00532 
00533 #endif
00534 
00535   };
00536 
00537   /** \brief Function pointer to a multi-dimensional function with
00538       arrays and without error control
00539   */
00540   template<class param_t, size_t nvar>
00541     class multi_vfunct_fptr_noerr : public multi_vfunct<param_t,nvar> {
00542     public:
00543     
00544     /** \brief Specify the function pointer
00545      */
00546     multi_vfunct_fptr_noerr(double (*fp)(size_t nv, const double x[nvar], 
00547                                          param_t &pa)) {
00548       fptr=fp;
00549     }
00550     
00551     virtual ~multi_vfunct_fptr_noerr() {}
00552 
00553     /** \brief Compute a function \c y of \c nv variables stored in \c x
00554         with parameter \c pa.
00555     */
00556     virtual int operator()(size_t nv, const double x[nvar], double &y, 
00557                            param_t &pa) {
00558       y=fptr(nv,x,pa);
00559       return 0;
00560     }
00561 
00562     /** \brief Return the value of a function of \c nv variables
00563         stored in \c x with parameter \c pa.
00564 
00565         Note that this is reimplemented in all children because 
00566         if one member function operator() is reimplemented, all must be.
00567     */
00568     virtual double operator()(size_t nv, const double x[nvar], param_t &pa) {
00569       double y;
00570       operator()(nv,x,y,pa);
00571       return y;
00572     }
00573 
00574 #ifndef DOXYGEN_INTERNAL
00575 
00576     protected:
00577 
00578     friend class io_tlate<multi_vfunct_fptr_noerr>;
00579   
00580     multi_vfunct_fptr_noerr() {}
00581 
00582     /// Store the function pointer
00583     double (*fptr)(size_t nv, const double x[nvar], param_t &pa);
00584 
00585 #ifndef DOXYGENP
00586 #endif
00587 
00588     private:
00589 
00590     multi_vfunct_fptr_noerr(const multi_vfunct_fptr_noerr &);
00591     multi_vfunct_fptr_noerr& operator=(const multi_vfunct_fptr_noerr&);
00592 
00593 #endif
00594 
00595   };
00596 
00597   /** \brief Member function pointer to a multi-dimensional function
00598       with arrays
00599    */
00600   template<class tclass, class param_t, size_t nvar>
00601     class multi_vfunct_mfptr : public multi_vfunct<param_t,nvar> {
00602     public:
00603   
00604     /** \brief Specify the member function pointer
00605      */
00606     multi_vfunct_mfptr(tclass *tp, int (tclass::*fp)
00607                        (size_t nv, const double x[nvar], double &y, 
00608                         param_t &pa)) {
00609       tptr=tp;
00610       fptr=fp;
00611     }
00612     
00613     virtual ~multi_vfunct_mfptr() {}
00614     
00615     /** \brief Compute a function \c y of \c nv variables stored in \c x
00616         with parameter \c pa.
00617     */
00618     virtual int operator()(size_t nv, const double x[nvar], double &y, 
00619                            param_t &pa) {
00620       return (*tptr.*fptr)(nv,x,y,pa);
00621     }
00622 
00623     /** \brief Return the value of a function of \c nv variables
00624         stored in \c x with parameter \c pa.
00625 
00626         Note that this is reimplemented in all children because 
00627         if one member function operator() is reimplemented, all must be.
00628     */
00629     virtual double operator()(size_t nv, const double x[nvar], param_t &pa) {
00630       double y;
00631       operator()(nv,x,y,pa);
00632       return y;
00633     }
00634   
00635 #ifndef DOXYGEN_INTERNAL
00636   
00637     protected:
00638   
00639     /// Store the function pointer
00640     int (tclass::*fptr)(size_t nv, const double x[nvar], double &y, 
00641                         param_t &pa);
00642 
00643     /// Store a pointer to the class instance
00644     tclass *tptr;
00645   
00646 #ifndef DOXYGENP
00647 #endif
00648 
00649     private:
00650 
00651     multi_vfunct_mfptr(const multi_vfunct_mfptr &);
00652     multi_vfunct_mfptr& operator=(const multi_vfunct_mfptr&);
00653 
00654 #endif
00655 
00656   };
00657 
00658   /** \brief Member function pointer to a multi-dimensional function
00659       with arrays
00660    */
00661   template<class tclass, class param_t, size_t nvar>
00662     class multi_vfunct_mfptr_noerr : public multi_vfunct<param_t,nvar> {
00663     public:
00664   
00665     /** \brief Specify the member function pointer
00666      */
00667     multi_vfunct_mfptr_noerr(tclass *tp, double (tclass::*fp)
00668                             (size_t nv, const double x[nvar], param_t &pa)) {
00669       tptr=tp;
00670       fptr=fp;
00671     }
00672   
00673     virtual ~multi_vfunct_mfptr_noerr() {}
00674   
00675     /** \brief Compute a function \c y of \c nv variables stored in \c x
00676         with parameter \c pa.
00677     */
00678     virtual int operator()(size_t nv, const double x[nvar], double &y, 
00679                            param_t &pa) {
00680       y=(*tptr.*fptr)(nv,x,pa);
00681       return 0;
00682     }
00683 
00684     /** \brief Return the value of a function of \c nv variables
00685         stored in \c x with parameter \c pa.
00686 
00687         Note that this is reimplemented in all children because 
00688         if one member function operator() is reimplemented, all must be.
00689     */
00690     virtual double operator()(size_t nv, const double x[nvar], param_t &pa) {
00691       double y;
00692       operator()(nv,x,y,pa);
00693       return y;
00694     }
00695   
00696 #ifndef DOXYGEN_INTERNAL
00697   
00698     protected:
00699   
00700     /// Store the function pointer
00701     double (tclass::*fptr)(size_t nv, const double x[nvar], param_t &pa);
00702 
00703     /// Store a pointer to the class instance
00704     tclass *tptr;
00705   
00706 #ifndef DOXYGENP
00707 #endif
00708 
00709     private:
00710 
00711     multi_vfunct_mfptr_noerr(const multi_vfunct_mfptr_noerr &);
00712     multi_vfunct_mfptr_noerr& operator=(const multi_vfunct_mfptr_noerr&);
00713 
00714 #endif
00715 
00716   };
00717   
00718 #ifndef DOXYGENP
00719 }
00720 #endif
00721 
00722 #endif

Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.

Project hosting provided by SourceForge.net Logo, O2scl Sourceforge Project Page