00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2006, 2007, 2008, 2009, 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 #include <o2scl/collection.h> 00028 00029 #ifndef DOXYGENP 00030 namespace o2scl { 00031 #endif 00032 00033 /** 00034 \brief One-dimensional function [abstract base] 00035 00036 This class generalizes a function y(x). 00037 00038 This class is one of a large number of function object classes 00039 in \o2 designed to provide a mechanism for the user to 00040 supply functions to solvers, minimizers, integrators, etc. 00041 See \ref funct_section for a general description. 00042 00043 */ 00044 template<class param_t> class funct { 00045 00046 public: 00047 00048 /* 00049 This empty constructor is required for the child funct classes 00050 */ 00051 funct() {} 00052 00053 virtual ~funct() {} 00054 00055 /** \brief Compute the function at point \c x, with result \c y 00056 */ 00057 virtual int operator()(double x, double &y, param_t &pa)=0; 00058 00059 /** \brief Compute the function at point \c x, returning the result 00060 */ 00061 virtual double operator()(double x, param_t &pa) { 00062 double y; 00063 operator()(x,y,pa); 00064 return y; 00065 } 00066 00067 }; 00068 00069 /** \brief Function pointer to a function 00070 */ 00071 template<class param_t> class funct_fptr : public funct<param_t> { 00072 00073 public: 00074 00075 /** \brief Specify the function pointer 00076 */ 00077 funct_fptr(int (*fp)(double x, double &y, param_t &pa)) { 00078 fptr=fp; 00079 } 00080 00081 virtual ~funct_fptr() {} 00082 00083 /** \brief Compute the function at point \c x, with result \c y 00084 */ 00085 virtual int operator()(double x, double &y, param_t &pa) { 00086 return fptr(x,y,pa); 00087 } 00088 00089 /** \brief Compute the function at point \c x, returning the result 00090 00091 \comment 00092 This is here because if we overload operator()(double,double,void*), 00093 we have to reimplement all operator()'s 00094 \endcomment 00095 */ 00096 virtual double operator()(double x, param_t &pa) { 00097 double y; 00098 operator()(x,y,pa); 00099 return y; 00100 } 00101 00102 #ifndef DOXYGEN_INTERNAL 00103 00104 protected: 00105 00106 /// Storage for the function pointer 00107 int (*fptr)(double x, double &y, param_t &pa); 00108 00109 /// Copy constructor 00110 funct_fptr(const funct_fptr &f) { 00111 fptr=f.fptr; 00112 } 00113 00114 /// Copy constructor 00115 funct_fptr &operator=(const funct_fptr &f) { 00116 fptr=f.fptr; 00117 return *this; 00118 } 00119 00120 #endif 00121 00122 }; 00123 00124 /** \brief Function pointer to a function returning the function value 00125 */ 00126 template<class param_t> 00127 class funct_fptr_noerr : public funct<param_t> { 00128 public: 00129 00130 /** \brief Specify the function pointer 00131 */ 00132 funct_fptr_noerr(double (*fp)(double x, param_t &pa)) { 00133 fptr=fp; 00134 } 00135 00136 virtual ~funct_fptr_noerr() {} 00137 00138 /** \brief Compute the function at point \c x, with result \c y 00139 */ 00140 virtual int operator()(double x, double &y, param_t &pa) { 00141 y=fptr(x,pa); 00142 return 0; 00143 } 00144 00145 /** \brief Compute the function at point \c x, returning the result 00146 00147 \comment 00148 This is here because if we overload operator()(double,double,void*), 00149 we have to reimplement all operator()'s 00150 \endcomment 00151 */ 00152 virtual double operator()(double x, param_t &pa) { 00153 double y; 00154 operator()(x,y,pa); 00155 return y; 00156 } 00157 00158 #ifndef DOXYGEN_INTERNAL 00159 00160 protected: 00161 00162 /// Storage for the function pointer 00163 double (*fptr)(double x, param_t &pa); 00164 00165 /// Copy constructor 00166 funct_fptr_noerr(const funct_fptr_noerr &f) { 00167 fptr=f.fptr; 00168 } 00169 00170 /// Copy constructor 00171 funct_fptr_noerr &operator=(const funct_fptr_noerr &f) { 00172 fptr=f.fptr; 00173 return *this; 00174 } 00175 00176 #endif 00177 00178 }; 00179 00180 /** \brief Function pointer to a function with no parameters 00181 */ 00182 template<class param_t> 00183 class funct_fptr_nopar : public funct<param_t> { 00184 00185 public: 00186 00187 /** \brief Specify the function pointer 00188 */ 00189 funct_fptr_nopar(double (*fp)(double x)) { 00190 fptr=fp; 00191 } 00192 00193 virtual ~funct_fptr_nopar() {} 00194 00195 /** \brief Compute the function at point \c x, with result \c y 00196 */ 00197 virtual int operator()(double x, double &y, param_t &pa) { 00198 y=fptr(x); 00199 return 0; 00200 } 00201 00202 /** \brief Compute the function at point \c x, returning the result 00203 00204 \comment 00205 This is here because if we overload operator()(double,double,void*), 00206 we have to reimplement all operator()'s 00207 \endcomment 00208 */ 00209 virtual double operator()(double x, param_t &pa) { 00210 double y; 00211 operator()(x,y,pa); 00212 return y; 00213 } 00214 00215 #ifndef DOXYGEN_INTERNAL 00216 00217 protected: 00218 00219 /// Storage for the function pointer 00220 double (*fptr)(double x); 00221 00222 /// Copy constructor 00223 funct_fptr_nopar(const funct_fptr_nopar &f) { 00224 fptr=f.fptr; 00225 } 00226 00227 /// Copy constructor 00228 funct_fptr_nopar &operator=(const funct_fptr_nopar &f) { 00229 fptr=f.fptr; 00230 return *this; 00231 } 00232 00233 #endif 00234 00235 }; 00236 00237 /** 00238 \brief Member function pointer to a one-dimensional function 00239 */ 00240 template <class tclass, class param_t> class funct_mfptr : 00241 public funct<param_t> { 00242 public: 00243 00244 /** \brief Specify the member function pointer 00245 */ 00246 funct_mfptr(tclass *tp, int (tclass::*fp)(double x, double &y, 00247 param_t &pa)) { 00248 tptr=tp; 00249 fptr=fp; 00250 } 00251 00252 virtual ~funct_mfptr() {}; 00253 00254 /** \brief Compute the function at point \c x, with result \c y 00255 */ 00256 virtual int operator()(double x, double &y, param_t &pa) { 00257 return (*tptr.*fptr)(x,y,pa); 00258 } 00259 00260 /** \brief Compute the function at point \c x, returning the result 00261 00262 \comment 00263 This is here because if we overload operator()(double,double,void*), 00264 we have to reimplement all operator()'s 00265 \endcomment 00266 */ 00267 virtual double operator()(double x, param_t &pa) { 00268 double y; 00269 operator()(x,y,pa); 00270 return y; 00271 } 00272 00273 #ifndef DOXYGEN_INTERNAL 00274 00275 protected: 00276 00277 /// Storage for the member function pointer 00278 int (tclass::*fptr)(double x, double &y, param_t &pa); 00279 00280 /// Store the pointer to the class instance 00281 tclass *tptr; 00282 00283 /// Copy constructor 00284 funct_mfptr(const funct_mfptr &f) { 00285 fptr=f.fptr; 00286 tptr=f.tptr; 00287 } 00288 00289 /// Copy constructor 00290 funct_mfptr &operator=(const funct_mfptr &f) { 00291 fptr=f.fptr; 00292 tptr=f.tptr; 00293 return *this; 00294 } 00295 00296 #endif 00297 00298 }; 00299 00300 /** 00301 \brief Const member function pointer to a one-dimensional function 00302 00303 \note While this is designed to accept a pointer to a const 00304 member function, the choice of whether the class pointer 00305 given in the template type <tt>tclass</tt> is const or 00306 not is up to the user. 00307 */ 00308 template <class tclass, class param_t> class funct_cmfptr : 00309 public funct<param_t> { 00310 public: 00311 00312 /** \brief Specify the member function pointer 00313 */ 00314 funct_cmfptr(tclass *tp, int (tclass::*fp)(double x, double &y, 00315 param_t &pa) const) { 00316 tptr=tp; 00317 fptr=fp; 00318 } 00319 00320 virtual ~funct_cmfptr() {}; 00321 00322 /** \brief Compute the function at point \c x, with result \c y 00323 */ 00324 virtual int operator()(double x, double &y, param_t &pa) { 00325 return (*tptr.*fptr)(x,y,pa); 00326 } 00327 00328 /** \brief Compute the function at point \c x, returning the result 00329 00330 \comment 00331 This is here because if we overload operator()(double,double,void*), 00332 we have to reimplement all operator()'s 00333 \endcomment 00334 */ 00335 virtual double operator()(double x, param_t &pa) { 00336 double y; 00337 operator()(x,y,pa); 00338 return y; 00339 } 00340 00341 #ifndef DOXYGEN_INTERNAL 00342 00343 protected: 00344 00345 /// Storage for the const member function pointer 00346 int (tclass::*fptr)(double x, double &y, param_t &pa) const; 00347 00348 /// Store the pointer to the class instance 00349 tclass *tptr; 00350 00351 /// Copy constructor 00352 funct_cmfptr(const funct_cmfptr &f) { 00353 fptr=f.fptr; 00354 tptr=f.tptr; 00355 } 00356 00357 /// Copy constructor 00358 funct_cmfptr &operator=(const funct_cmfptr &f) { 00359 fptr=f.fptr; 00360 tptr=f.tptr; 00361 return *this; 00362 } 00363 00364 #endif 00365 00366 }; 00367 00368 /** \brief Member function pointer to a one-dimensional function 00369 returning the function value 00370 */ 00371 template <class tclass, class param_t> class funct_mfptr_noerr : 00372 public funct<param_t> { 00373 public: 00374 00375 /** \brief Specify the member function pointer 00376 */ 00377 funct_mfptr_noerr(tclass *tp, 00378 double (tclass::*fp)(double x, param_t &pa)) { 00379 tptr=tp; 00380 fptr=fp; 00381 } 00382 00383 virtual ~funct_mfptr_noerr() {}; 00384 00385 /** \brief Compute the function at point \c x, with result \c y 00386 */ 00387 virtual int operator()(double x, double &y, param_t &pa) { 00388 y=(*tptr.*fptr)(x,pa); 00389 return 0; 00390 } 00391 00392 /** \brief Compute the function at point \c x, returning the result 00393 00394 \comment 00395 This is here because if we overload operator()(double,double,void*), 00396 we have to reimplement all operator()'s 00397 \endcomment 00398 */ 00399 virtual double operator()(double x, param_t &pa) { 00400 double y; 00401 operator()(x,y,pa); 00402 return y; 00403 } 00404 00405 #ifndef DOXYGEN_INTERNAL 00406 00407 protected: 00408 00409 /// Storage for the member function pointer 00410 double (tclass::*fptr)(double x, param_t &pa); 00411 00412 /// Store the pointer to the class instance 00413 tclass *tptr; 00414 00415 /// Copy constructor 00416 funct_mfptr_noerr(const funct_mfptr_noerr &f) { 00417 fptr=f.fptr; 00418 tptr=f.tptr; 00419 } 00420 00421 /// Copy constructor 00422 funct_mfptr_noerr &operator=(const funct_mfptr_noerr &f) { 00423 fptr=f.fptr; 00424 tptr=f.tptr; 00425 return *this; 00426 } 00427 00428 #endif 00429 00430 }; 00431 00432 /** \brief Const member function pointer to a one-dimensional function 00433 returning the function value 00434 00435 \note While this is designed to accept a pointer to a const 00436 member function, the choice of whether the class pointer 00437 given in the template type <tt>tclass</tt> is const or 00438 not is up to the user. 00439 */ 00440 template <class tclass, class param_t> class funct_cmfptr_noerr : 00441 public funct<param_t> { 00442 public: 00443 00444 /** \brief Specify the member function pointer 00445 */ 00446 funct_cmfptr_noerr(tclass *tp, 00447 double (tclass::*fp)(double x, param_t &pa) const) { 00448 tptr=tp; 00449 fptr=fp; 00450 } 00451 00452 virtual ~funct_cmfptr_noerr() {}; 00453 00454 /** \brief Compute the function at point \c x, with result \c y 00455 */ 00456 virtual int operator()(double x, double &y, param_t &pa) { 00457 y=(*tptr.*fptr)(x,pa); 00458 return 0; 00459 } 00460 00461 /** \brief Compute the function at point \c x, returning the result 00462 00463 \comment 00464 This is here because if we overload operator()(double,double,void*), 00465 we have to reimplement all operator()'s 00466 \endcomment 00467 */ 00468 virtual double operator()(double x, param_t &pa) { 00469 double y; 00470 operator()(x,y,pa); 00471 return y; 00472 } 00473 00474 #ifndef DOXYGEN_INTERNAL 00475 00476 protected: 00477 00478 /// Storage for the const member function pointer 00479 double (tclass::*fptr)(double x, param_t &pa) const; 00480 00481 /// Store the pointer to the class instance 00482 tclass *tptr; 00483 00484 /// Copy constructor 00485 funct_cmfptr_noerr(const funct_cmfptr_noerr &f) { 00486 fptr=f.fptr; 00487 tptr=f.tptr; 00488 } 00489 00490 /// Copy constructor 00491 funct_cmfptr_noerr &operator=(const funct_cmfptr_noerr &f) { 00492 fptr=f.fptr; 00493 tptr=f.tptr; 00494 return *this; 00495 } 00496 00497 #endif 00498 00499 }; 00500 00501 /** \brief Member function pointer to a one-dimensional function 00502 with no parameters 00503 */ 00504 template <class tclass, class param_t> class funct_mfptr_nopar : 00505 public funct<param_t> { 00506 public: 00507 00508 /** \brief Specify the member function pointer 00509 */ 00510 funct_mfptr_nopar(tclass *tp, double (tclass::*fp)(double x)) { 00511 tptr=tp; 00512 fptr=fp; 00513 } 00514 00515 virtual ~funct_mfptr_nopar() {}; 00516 00517 /** \brief Compute the function at point \c x, with result \c y 00518 */ 00519 virtual int operator()(double x, double &y, param_t &pa) { 00520 y=(*tptr.*fptr)(x); 00521 return 0; 00522 } 00523 00524 /** \brief Compute the function at point \c x, returning the result 00525 00526 \comment 00527 This is here because if we overload operator()(double,double,void*), 00528 we have to reimplement all operator()'s 00529 \endcomment 00530 */ 00531 virtual double operator()(double x, param_t &pa) { 00532 double y; 00533 operator()(x,y,pa); 00534 return y; 00535 } 00536 00537 #ifndef DOXYGEN_INTERNAL 00538 00539 protected: 00540 00541 /// Storage for the member function pointer 00542 double (tclass::*fptr)(double x); 00543 /// Store the pointer to the class instance 00544 tclass *tptr; 00545 00546 /// Copy constructor 00547 funct_mfptr_nopar(const funct_mfptr_nopar &f) { 00548 fptr=f.fptr; 00549 tptr=f.tptr; 00550 } 00551 00552 /// Copy constructor 00553 funct_mfptr_nopar &operator=(const funct_mfptr_nopar &f) { 00554 fptr=f.fptr; 00555 tptr=f.tptr; 00556 return *this; 00557 } 00558 00559 #endif 00560 00561 }; 00562 00563 /** \brief Const member function pointer to a one-dimensional function 00564 with no parameters 00565 00566 \note While this is designed to accept a pointer to a const 00567 member function, the choice of whether the class pointer 00568 given in the template type <tt>tclass</tt> is const or 00569 not is up to the user. 00570 */ 00571 template <class tclass, class param_t> class funct_cmfptr_nopar : 00572 public funct<param_t> { 00573 public: 00574 00575 /** \brief Specify the member function pointer 00576 */ 00577 funct_cmfptr_nopar(tclass *tp, double (tclass::*fp)(double x) const) { 00578 tptr=tp; 00579 fptr=fp; 00580 } 00581 00582 virtual ~funct_cmfptr_nopar() {}; 00583 00584 /** \brief Compute the function at point \c x, with result \c y 00585 */ 00586 virtual int operator()(double x, double &y, param_t &pa) { 00587 y=(*tptr.*fptr)(x); 00588 return 0; 00589 } 00590 00591 /** \brief Compute the function at point \c x, returning the result 00592 00593 \comment 00594 This is here because if we overload operator()(double,double,void*), 00595 we have to reimplement all operator()'s 00596 \endcomment 00597 */ 00598 virtual double operator()(double x, param_t &pa) { 00599 double y; 00600 operator()(x,y,pa); 00601 return y; 00602 } 00603 00604 #ifndef DOXYGEN_INTERNAL 00605 00606 protected: 00607 00608 /// Storage for the const member function pointer 00609 double (tclass::*fptr)(double x) const; 00610 /// Store the pointer to the class instance 00611 tclass *tptr; 00612 00613 /// Copy constructor 00614 funct_cmfptr_nopar(const funct_cmfptr_nopar &f) { 00615 fptr=f.fptr; 00616 tptr=f.tptr; 00617 } 00618 00619 /// Copy constructor 00620 funct_cmfptr_nopar &operator=(const funct_cmfptr_nopar &f) { 00621 fptr=f.fptr; 00622 tptr=f.tptr; 00623 return *this; 00624 } 00625 00626 #endif 00627 00628 }; 00629 00630 #ifndef DOXYGENP 00631 } 00632 #endif 00633 00634 #endif
Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.
Project hosting provided by
,
O2scl Sourceforge Project Page