mm_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_MM_FUNCT_H
00024 #define O2SCL_MM_FUNCT_H
00025 
00026 #include <string>
00027 #include <o2scl/collection.h>
00028 #include <o2scl/ovector_tlate.h>
00029 
00030 #ifndef DOXYGENP
00031 namespace o2scl {
00032 #endif
00033 
00034   /** 
00035       \brief Array of multi-dimensional functions
00036       
00037       This class generalizes \c nv functions of \c nv variables, i.e.
00038       \f$ y_j(x_0,x_1,...,x_{nv-1}) \f$ for \f$ 0\leq j \leq 
00039       nv-1 \f$ .
00040 
00041       To use C-style arrays, use \ref mm_vfunct.
00042 
00043   */
00044   template<class param_t, class vec_t=ovector_view>
00045     class mm_funct {
00046     public:  
00047 
00048     mm_funct() {}
00049     
00050     virtual ~mm_funct() {}
00051     
00052     /** \brief Compute \c nv functions, \c y, of \c nv variables
00053         stored in \c x with parameter \c pa.
00054     */
00055     virtual int operator()(size_t nv, const vec_t &x, vec_t &y, param_t &pa) {
00056       set_err_ret("Missing base in mm_funct::operator().",gsl_nobase);
00057     }
00058     
00059 #ifndef DOXYGENP
00060 
00061     private:
00062 
00063     mm_funct(const mm_funct &);
00064     mm_funct& operator=(const mm_funct&);
00065 
00066 #endif
00067 
00068   };
00069   
00070   /** \brief Function pointer to array of multi-dimensional functions
00071    */
00072   template<class param_t, class vec_t=ovector_view>
00073     class mm_funct_fptr : public mm_funct<param_t,vec_t> {
00074 
00075     public:
00076     
00077     mm_funct_fptr() {}
00078 
00079     virtual ~mm_funct_fptr() {}
00080 
00081     /** \brief Specify the function pointer
00082      */
00083     mm_funct_fptr(int (*fp)(size_t nv, const vec_t &x, vec_t &y, 
00084                             param_t &pa)) {
00085       fptr=fp;
00086     }
00087 
00088     /** \brief Specify the function pointer
00089      */
00090     int set_function(int (*fp)(size_t nv, const vec_t &x, vec_t &y, 
00091                                param_t &pa)) {
00092       fptr=fp;
00093       return 0;
00094     }
00095     
00096     /** \brief Compute \c nv functions, \c y, of \c nv variables
00097         stored in \c x with parameter \c pa.
00098     */
00099     virtual int operator()(size_t nv, const vec_t &x, vec_t &y, param_t &pa) {
00100       return fptr(nv,x,y,pa);
00101     }
00102     
00103     
00104 #ifndef DOXYGEN_INTERNAL
00105     
00106     protected:
00107     
00108     friend class io_tlate<mm_funct_fptr>;
00109 
00110     /// The function pointer to the user-supplied function
00111     int (*fptr)(size_t nv, const vec_t &x, vec_t &y, param_t &pa);
00112     
00113     private:
00114     
00115     mm_funct_fptr(const mm_funct_fptr &);
00116     mm_funct_fptr& operator=(const mm_funct_fptr&);
00117     
00118 #endif
00119     
00120   };
00121   
00122   /** \brief Function pointer to array of multi-dimensional functions with
00123       no parameters
00124   */
00125   template<class param_t, class vec_t=ovector_view>
00126     class mm_funct_fptr_nopar : public mm_funct<param_t,vec_t> {
00127     
00128     public:
00129     
00130     mm_funct_fptr_nopar() {}
00131 
00132     virtual ~mm_funct_fptr_nopar() {}
00133 
00134     /** \brief Specify the function pointer
00135      */
00136     mm_funct_fptr_nopar(int (*fp)(size_t nv, const vec_t &x, vec_t &y)) {
00137       fptr=fp;
00138     }
00139 
00140     /** \brief Specify the function pointer
00141      */
00142     int set_function(int (*fp)(size_t nv, const vec_t &x, vec_t &y)) {
00143       fptr=fp;
00144       return 0;
00145     }
00146     
00147     /** \brief Compute \c nv functions, \c y, of \c nv variables
00148         stored in \c x with parameter \c pa.
00149     */
00150     virtual int operator()(size_t nv, const vec_t &x, vec_t &y, param_t &pa) {
00151       return fptr(nv,x,y);
00152     }
00153     
00154     
00155 #ifndef DOXYGEN_INTERNAL
00156     
00157     protected:
00158     
00159     friend class io_tlate<mm_funct_fptr_nopar>;
00160 
00161     /// The function pointer to the user-supplied function
00162     int (*fptr)(size_t nv, const vec_t &x, vec_t &y);
00163     
00164     private:
00165     
00166     mm_funct_fptr_nopar(const mm_funct_fptr_nopar &);
00167     mm_funct_fptr_nopar& operator=(const mm_funct_fptr_nopar&);
00168     
00169 #endif
00170     
00171   };
00172   
00173   /** 
00174       \brief Function pointer to a gsl_multiroot_function
00175 
00176       This works because with the template parameter \c vec_t as an
00177       \ref ovector_view class because \ref ovector_view is inherited
00178       from \c gsl_vector.
00179   */
00180   template<class param_t, class vec_t=ovector_view>
00181     class mm_funct_gsl : public mm_funct<param_t,vec_t> {
00182     public:
00183     
00184     /** \brief Specify the function pointer
00185      */
00186     mm_funct_gsl(int (*fp)(const gsl_vector *x, param_t &pa, gsl_vector *f)) {
00187       fptr=fp;
00188     }
00189     
00190     /** \brief Compute \c nv functions, \c y, of \c nv variables
00191         stored in \c x with parameter \c pa.
00192     */
00193     virtual int operator()(size_t nv, const vec_t &x, vec_t &y, param_t &pa) {
00194       const gsl_vector *gx=(const gsl_vector *)(&x);
00195       gsl_vector *gy=(gsl_vector *)(&y);
00196       return fptr(gx,pa,gy);
00197     }
00198     
00199 #ifndef DOXYGEN_INTERNAL
00200     
00201     protected:
00202     
00203     friend class io_tlate<mm_funct_gsl>;
00204     
00205     /// The function pointer to the user-supplied function
00206     int (*fptr)(const gsl_vector *x, param_t &pa, gsl_vector *f);
00207     
00208     private:
00209     
00210     mm_funct_gsl(const mm_funct_gsl &);
00211     mm_funct_gsl& operator=(const mm_funct_gsl&);
00212     
00213 #endif
00214     
00215   };
00216   
00217   /** \brief Member function pointer to an array of 
00218       multi-dimensional functions
00219   */
00220   template<class tclass, class param_t, class vec_t=ovector_view>
00221     class mm_funct_mfptr : public mm_funct<param_t,vec_t> {
00222     public:
00223     
00224     /** \brief Empty constructor
00225      */
00226     mm_funct_mfptr() {
00227     }
00228 
00229     /** \brief Specify the member function pointer
00230      */
00231     mm_funct_mfptr(tclass *tp, int (tclass::*fp)
00232                    (size_t nv, const vec_t &x, vec_t &y, param_t &pa)) {
00233       tptr=tp;
00234       fptr=fp;
00235     }
00236 
00237     /** \brief Specify the member function pointer
00238      */
00239     int set_function(tclass *tp, int (tclass::*fp)
00240                      (size_t nv, const vec_t &x, vec_t &y, param_t &pa)) 
00241     {
00242       tptr=tp;
00243       fptr=fp;
00244       return 0;
00245     }
00246     
00247     virtual ~mm_funct_mfptr() {};
00248     
00249     /** \brief Compute \c nv functions, \c y, of \c nv variables
00250         stored in \c x with parameter \c pa.
00251     */
00252     virtual int operator()(size_t nv, const vec_t &x, vec_t &y, param_t &pa) {
00253       return (*tptr.*fptr)(nv,x,y,pa);
00254     }
00255     
00256 #ifndef DOXYGEN_INTERNAL
00257     
00258     protected:
00259     
00260     /// The member function pointer
00261     int (tclass::*fptr)(size_t nv, const vec_t &x, vec_t &y, param_t &pa);
00262 
00263     /// The class pointer
00264     tclass *tptr;
00265     
00266     private:
00267     
00268     mm_funct_mfptr(const mm_funct_mfptr &);
00269     mm_funct_mfptr& operator=(const mm_funct_mfptr&);
00270     
00271 #endif
00272     
00273   };
00274 
00275   /** \brief Member function pointer to an array of 
00276       multi-dimensional functions
00277   */
00278   template<class tclass, class param_t, class vec_t=ovector_view>
00279     class mm_funct_mfptr_nopar : public mm_funct<param_t,vec_t> {
00280     public:
00281     
00282     /** \brief Empty constructor
00283      */
00284     mm_funct_mfptr_nopar() {
00285     }
00286 
00287     /** \brief Specify the member function pointer
00288      */
00289     mm_funct_mfptr_nopar(tclass *tp, int (tclass::*fp)
00290                          (size_t nv, const vec_t &x, vec_t &y)) {
00291       tptr=tp;
00292       fptr=fp;
00293     }
00294     
00295     /** \brief Specify the member function pointer
00296      */
00297     int set_function(tclass *tp, int (tclass::*fp)
00298                      (size_t nv, const vec_t &x, vec_t &y)) 
00299     {
00300       tptr=tp;
00301       fptr=fp;
00302       return 0;
00303     }
00304     
00305     virtual ~mm_funct_mfptr_nopar() {};
00306     
00307     /** \brief Compute \c nv functions, \c y, of \c nv variables
00308         stored in \c x with parameter \c pa.
00309     */
00310     virtual int operator()(size_t nv, const vec_t &x, vec_t &y, param_t &pa) {
00311       return (*tptr.*fptr)(nv,x,y);
00312     }
00313     
00314 #ifndef DOXYGEN_INTERNAL
00315     
00316     protected:
00317     
00318     /// The member function pointer
00319     int (tclass::*fptr)(size_t nv, const vec_t &x, vec_t &y);
00320 
00321     /// The class pointer
00322     tclass *tptr;
00323     
00324     private:
00325     
00326     mm_funct_mfptr_nopar(const mm_funct_mfptr_nopar &);
00327     mm_funct_mfptr_nopar& operator=(const mm_funct_mfptr_nopar&);
00328     
00329 #endif
00330     
00331   };
00332 
00333   /** 
00334       \brief Array of multi-dimensional functions with arrays
00335       
00336       This class generalizes \c nv functions of \c nv variables, i.e.
00337       \f$ y_j(x_0,x_1,...,x_{nv-1}) \f$ for \f$ 0\leq j \leq 
00338       nv-1 \f$.
00339 
00340       To use ovector_view objects instead of C-style arrays, 
00341       use \ref mm_funct.
00342       
00343   */
00344   template<class param_t, size_t nv>
00345     class mm_vfunct {
00346   public:  
00347     
00348     mm_vfunct() {}
00349 
00350     virtual ~mm_vfunct() {}
00351     
00352     /** \brief Compute \c nv functions, \c y, of \c nv variables
00353         stored in \c x with parameter \c pa.
00354     */
00355     virtual int operator()(size_t nvar, const double x[nv], 
00356                            double y[nv], param_t &pa) {
00357       set_err_ret("Missing base in mm_vfunct::operator().",gsl_nobase);
00358     }
00359     
00360 #ifndef DOXYGENP
00361 
00362   private:
00363 
00364     mm_vfunct(const mm_vfunct &);
00365     mm_vfunct& operator=(const mm_vfunct&);
00366 
00367 #endif
00368 
00369   };
00370   
00371   /** \brief Function pointer to array of multi-dimensional functions
00372       with arrays
00373   */
00374   template<class param_t, size_t nv> class mm_vfunct_fptr : 
00375   public mm_vfunct<param_t,nv> {
00376   public:
00377     
00378     /** \brief Specify the function pointer
00379      */
00380     mm_vfunct_fptr(int (*fp)(size_t nvar, const double x[nv], double y[nv], 
00381                              param_t &pa)) {
00382       fptr=fp;
00383     }
00384     
00385     
00386     virtual ~mm_vfunct_fptr() {};
00387     
00388     /** \brief Compute \c nv functions, \c y, of \c nv variables
00389         stored in \c x with parameter \c pa.
00390     */
00391     virtual int operator()(size_t nvar, const double x[nv], double y[nv], 
00392                            param_t &pa) {
00393       return fptr(nv,x,y,pa);
00394     }
00395     
00396     
00397 #ifndef DOXYGEN_INTERNAL
00398     
00399   protected:
00400     
00401     friend class io_tlate<mm_vfunct_fptr>;
00402     
00403     mm_vfunct_fptr() {};
00404     
00405     /// The function pointer
00406     int (*fptr)(size_t nvar, const double x[nv], double y[nv], param_t &pa);
00407     
00408   private:
00409     
00410     mm_vfunct_fptr(const mm_vfunct_fptr &);
00411     mm_vfunct_fptr& operator=(const mm_vfunct_fptr&);
00412     
00413 #endif
00414     
00415   };
00416     
00417   /** \brief Function pointer to array of multi-dimensional functions
00418       with arrays and no parameters
00419   */
00420   template<class param_t, size_t nv> class mm_vfunct_fptr_nopar : 
00421   public mm_vfunct<param_t,nv> 
00422     {
00423     public:
00424         
00425       /** \brief Specify the function pointer
00426        */
00427       mm_vfunct_fptr_nopar(int (*fp)(size_t nvar, const double x[nv], 
00428                                      double y[nv])) {
00429         fptr=fp;
00430       }
00431       
00432       
00433       virtual ~mm_vfunct_fptr_nopar() {};
00434       
00435       /** \brief Compute \c nv functions, \c y, of \c nv variables
00436           stored in \c x with parameter \c pa.
00437       */
00438       virtual int operator()(size_t nvar, const double x[nv], double y[nv],
00439                              param_t &pa) {
00440         return fptr(nv,x,y);
00441       }
00442     
00443     
00444 #ifndef DOXYGEN_INTERNAL
00445     
00446     protected:
00447     
00448       friend class io_tlate<mm_vfunct_fptr_nopar>;
00449     
00450       mm_vfunct_fptr_nopar() {};
00451     
00452       /// The function pointer
00453       int (*fptr)(size_t nvar, const double x[nv], double y[nv]);
00454     
00455     private:
00456     
00457       mm_vfunct_fptr_nopar(const mm_vfunct_fptr_nopar &);
00458       mm_vfunct_fptr_nopar& operator=(const mm_vfunct_fptr_nopar&);
00459     
00460 #endif
00461     
00462     };
00463   
00464   /** \brief Function pointer to a gsl_multiroot_function with arrays
00465    */
00466   template<class param_t, size_t nv>
00467     class mm_vfunct_gsl : public mm_vfunct<param_t,nv> {
00468   public:
00469     
00470     /** \brief Specify the function pointer
00471      */
00472     mm_vfunct_gsl(int (*fp)(const gsl_vector *x, param_t &pa, gsl_vector *f)) {
00473       fptr=fp;
00474     }
00475     
00476     /** \brief Compute \c nv functions, \c y, of \c nv variables
00477         stored in \c x with parameter \c pa.
00478     */
00479     virtual int operator()(size_t nvar, const double x[nv], 
00480                            double y[nv], param_t &pa) {
00481       const gsl_vector *gx=(const gsl_vector *)(&x);
00482       gsl_vector *gy=(gsl_vector *)(&y);
00483       return fptr(gx,pa,gy);
00484     }
00485     
00486 #ifndef DOXYGEN_INTERNAL
00487     
00488   protected:
00489     
00490     friend class io_tlate<mm_vfunct_gsl>;
00491     
00492     /// The function pointer
00493     int (*fptr)(const gsl_vector *x, param_t &pa, gsl_vector *f);
00494     
00495   private:
00496     
00497     mm_vfunct_gsl(const mm_vfunct_gsl &);
00498     mm_vfunct_gsl& operator=(const mm_vfunct_gsl&);
00499     
00500 #endif
00501     
00502   };
00503   
00504   /** \brief Member function pointer to an array of 
00505       multi-dimensional functions with arrays
00506   */
00507   template<class tclass, class param_t, size_t nv>
00508     class mm_vfunct_mfptr : public mm_vfunct<param_t,nv> {
00509   public:
00510     
00511     /** \brief Specify the member function pointer
00512      */
00513     mm_vfunct_mfptr(tclass *tp, int (tclass::*fp)
00514                     (size_t nvar, const double x[nv], double y[nv], 
00515                      param_t &pa)) {
00516       tptr=tp;
00517       fptr=fp;
00518     }
00519     
00520     virtual ~mm_vfunct_mfptr() {};
00521     
00522     /** \brief Compute \c nv functions, \c y, of \c nv variables
00523         stored in \c x with parameter \c pa.
00524     */
00525     virtual int operator()(size_t nvar, const double x[nv], double y[nv], 
00526                            param_t &pa) {
00527       return (*tptr.*fptr)(nv,x,y,pa);
00528     }
00529     
00530 #ifndef DOXYGEN_INTERNAL
00531     
00532   protected:
00533     
00534     /// The member function pointer
00535     int (tclass::*fptr)(size_t nvar, const double x[nv], double y[nv], 
00536                         param_t &pa);
00537 
00538     /// The class pointer
00539     tclass *tptr;
00540     
00541   private:
00542     
00543     mm_vfunct_mfptr(const mm_vfunct_mfptr &);
00544     mm_vfunct_mfptr& operator=(const mm_vfunct_mfptr&);
00545     
00546 #endif
00547     
00548   };
00549 
00550   /** \brief Member function pointer to an array of 
00551       multi-dimensional functions with arrays
00552   */
00553   template<class tclass, class param_t, size_t nv>
00554     class mm_vfunct_mfptr_nopar : public mm_vfunct<param_t,nv> {
00555 
00556   public:
00557     
00558     /** \brief Specify the member function pointer
00559      */
00560     mm_vfunct_mfptr_nopar(tclass *tp, int (tclass::*fp)
00561                           (size_t nvar, const double x[nv], double y[nv])) {
00562       tptr=tp;
00563       fptr=fp;
00564     }
00565     
00566     virtual ~mm_vfunct_mfptr_nopar() {};
00567     
00568     /** \brief Compute \c nv functions, \c y, of \c nv variables
00569         stored in \c x with parameter \c pa.
00570     */
00571     virtual int operator()(size_t nvar, const double x[nv], double y[nv], 
00572                            param_t &pa) {
00573       return (*tptr.*fptr)(nv,x,y);
00574     }
00575     
00576 #ifndef DOXYGEN_INTERNAL
00577     
00578   protected:
00579     
00580     /// The member function pointer
00581     int (tclass::*fptr)(size_t nvar, const double x[nv], double y[nv]);
00582 
00583     /// The class pointer
00584     tclass *tptr;
00585     
00586   private:
00587     
00588     mm_vfunct_mfptr_nopar(const mm_vfunct_mfptr_nopar &);
00589     mm_vfunct_mfptr_nopar& operator=(const mm_vfunct_mfptr_nopar&);
00590     
00591 #endif
00592     
00593   };
00594 
00595 #ifndef DOXYGENP
00596 }
00597 #endif
00598 
00599 #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