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 [abstract 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 For functions with C-style arrays, use 00041 the corresponding children of \ref multi_vfunct . 00042 */ 00043 00044 template<class param_t, class vec_t=ovector_view> class multi_funct { 00045 00046 public: 00047 00048 multi_funct() {} 00049 00050 virtual ~multi_funct() {} 00051 00052 /** \brief Compute a function \c y of \c nv variables stored in \c x 00053 with parameter \c pa. 00054 */ 00055 virtual int operator()(size_t nv, const vec_t &x, double &y, 00056 param_t &pa)=0; 00057 00058 00059 /** \brief Return the value of a function of \c nv variables 00060 stored in \c x with parameter \c pa. 00061 00062 Note that this is reimplemented in all children because 00063 if one member function operator() is reimplemented, all must be. 00064 */ 00065 virtual double operator()(size_t nv, const vec_t &x, param_t &pa) { 00066 double y; 00067 operator()(nv,x,y,pa); 00068 return y; 00069 } 00070 00071 #ifndef DOXYGENP 00072 00073 private: 00074 00075 multi_funct(const multi_funct &); 00076 multi_funct& operator=(const multi_funct&); 00077 00078 #endif 00079 00080 }; 00081 00082 /** \brief Function pointer to a multi-dimensional function 00083 */ 00084 template<class param_t, class vec_t=ovector_view> 00085 class multi_funct_fptr : public multi_funct<param_t,vec_t> { 00086 00087 public: 00088 00089 /** \brief Specify the function pointer 00090 */ 00091 multi_funct_fptr(int (*fp)(size_t nv, const vec_t &x, double &y, 00092 param_t &pa)) { 00093 fptr=fp; 00094 } 00095 00096 00097 virtual ~multi_funct_fptr() {}; 00098 00099 /** \brief Compute a function \c y of \c nv variables stored in \c x 00100 with parameter \c pa. 00101 */ 00102 virtual int operator()(size_t nv, const vec_t &x, double &y, param_t &pa) { 00103 return fptr(nv,x,y,pa); 00104 } 00105 00106 /** \brief Return the value of a function of \c nv variables 00107 stored in \c x with parameter \c pa. 00108 00109 Note that this is reimplemented in all children because 00110 if one member function operator() is reimplemented, all must be. 00111 */ 00112 virtual double operator()(size_t nv, const vec_t &x, param_t &pa) { 00113 double y; 00114 operator()(nv,x,y,pa); 00115 return y; 00116 } 00117 00118 #ifndef DOXYGEN_INTERNAL 00119 00120 protected: 00121 00122 friend class io_tlate<multi_funct_fptr>; 00123 00124 /// Store the function pointer 00125 int (*fptr)(size_t nv, const vec_t &x, double &y, param_t &pa); 00126 00127 multi_funct_fptr() {} 00128 00129 #ifndef DOXYGENP 00130 #endif 00131 00132 private: 00133 00134 multi_funct_fptr(const multi_funct_fptr &); 00135 multi_funct_fptr& operator=(const multi_funct_fptr&); 00136 00137 #endif 00138 00139 }; 00140 00141 /** \brief Function pointer to a gsl_multimin_function 00142 */ 00143 template<class param_t, class vec_t=ovector_view> 00144 class multi_funct_gsl : public multi_funct<param_t,vec_t> { 00145 public: 00146 00147 /** \brief Specify the function pointer 00148 */ 00149 multi_funct_gsl(double (*fp)(const gsl_vector *x, param_t &pa)) { 00150 fptr=fp; 00151 } 00152 00153 virtual ~multi_funct_gsl() {} 00154 00155 /** \brief Compute a function \c y of \c nv variables stored in \c x 00156 with parameter \c pa. 00157 */ 00158 virtual int operator()(size_t nv, const vec_t &x, double &y, param_t &pa) { 00159 const gsl_vector *gx=(const gsl_vector *)(&x); 00160 y=fptr(gx,pa); 00161 return 0; 00162 } 00163 00164 /** \brief Return the value of a function of \c nv variables 00165 stored in \c x with parameter \c pa. 00166 00167 Note that this is reimplemented in all children because 00168 if one member function operator() is reimplemented, all must be. 00169 */ 00170 virtual double operator()(size_t nv, const vec_t &x, param_t &pa) { 00171 double y; 00172 operator()(nv,x,y,pa); 00173 return y; 00174 } 00175 00176 #ifndef DOXYGEN_INTERNAL 00177 00178 protected: 00179 00180 friend class io_tlate<multi_funct_gsl>; 00181 00182 /// Store the function pointer 00183 double (*fptr)(const gsl_vector *x, param_t &pa); 00184 00185 multi_funct_gsl() {} 00186 00187 #ifndef DOXYGENP 00188 #endif 00189 00190 private: 00191 00192 multi_funct_gsl(const multi_funct_gsl &); 00193 multi_funct_gsl& operator=(const multi_funct_gsl&); 00194 00195 #endif 00196 00197 }; 00198 00199 /** \brief Function pointer to a multi-dimensional function without 00200 error control 00201 */ 00202 template<class param_t, class vec_t=ovector_view> 00203 class multi_funct_fptr_noerr : public multi_funct<param_t,vec_t> { 00204 public: 00205 00206 /** \brief Specify the function pointer 00207 */ 00208 multi_funct_fptr_noerr(double (*fp)(size_t nv, const vec_t &x, 00209 param_t &pa)) { 00210 fptr=fp; 00211 } 00212 00213 virtual ~multi_funct_fptr_noerr() {} 00214 00215 /** \brief Compute a function \c y of \c nv variables stored in \c x 00216 with parameter \c pa. 00217 */ 00218 virtual int operator()(size_t nv, const vec_t &x, double &y, param_t &pa) { 00219 y=fptr(nv,x,pa); 00220 return 0; 00221 } 00222 00223 /** \brief Return the value of a function of \c nv variables 00224 stored in \c x with parameter \c pa. 00225 00226 Note that this is reimplemented in all children because 00227 if one member function operator() is reimplemented, all must be. 00228 */ 00229 virtual double operator()(size_t nv, const vec_t &x, param_t &pa) { 00230 double y; 00231 operator()(nv,x,y,pa); 00232 return y; 00233 } 00234 00235 #ifndef DOXYGEN_INTERNAL 00236 00237 protected: 00238 00239 friend class io_tlate<multi_funct_fptr_noerr>; 00240 00241 multi_funct_fptr_noerr() {} 00242 00243 /// Store the function pointer 00244 double (*fptr)(size_t nv, const vec_t &x, param_t &pa); 00245 00246 #ifndef DOXYGENP 00247 #endif 00248 00249 private: 00250 00251 multi_funct_fptr_noerr(const multi_funct_fptr_noerr &); 00252 multi_funct_fptr_noerr& operator=(const multi_funct_fptr_noerr&); 00253 00254 #endif 00255 00256 }; 00257 00258 /** \brief Member function pointer to a multi-dimensional function 00259 */ 00260 template<class tclass, class param_t, class vec_t=ovector_view> 00261 class multi_funct_mfptr : public multi_funct<param_t,vec_t> { 00262 public: 00263 00264 /** \brief Specify the member function pointer 00265 */ 00266 multi_funct_mfptr(tclass *tp, int (tclass::*fp) 00267 (size_t nv, const vec_t &x, double &y, param_t &pa)) { 00268 tptr=tp; 00269 fptr=fp; 00270 } 00271 00272 virtual ~multi_funct_mfptr() {} 00273 00274 /** \brief Compute a function \c y of \c nv variables stored in \c x 00275 with parameter \c pa. 00276 */ 00277 virtual int operator()(size_t nv, const vec_t &x, double &y, param_t &pa) { 00278 return (*tptr.*fptr)(nv,x,y,pa); 00279 } 00280 00281 /** \brief Return the value of a function of \c nv variables 00282 stored in \c x with parameter \c pa. 00283 00284 Note that this is reimplemented in all children because 00285 if one member function operator() is reimplemented, all must be. 00286 */ 00287 virtual double operator()(size_t nv, const vec_t &x, param_t &pa) { 00288 double y; 00289 operator()(nv,x,y,pa); 00290 return y; 00291 } 00292 00293 #ifndef DOXYGEN_INTERNAL 00294 00295 protected: 00296 00297 /// Store the function pointer 00298 int (tclass::*fptr)(size_t nv, const vec_t &x, double &y, param_t &pa); 00299 /// Store a pointer to the class instance 00300 tclass *tptr; 00301 00302 #ifndef DOXYGENP 00303 #endif 00304 00305 private: 00306 00307 multi_funct_mfptr(const multi_funct_mfptr &); 00308 multi_funct_mfptr& operator=(const multi_funct_mfptr&); 00309 00310 #endif 00311 00312 }; 00313 00314 /** \brief Member function pointer to a multi-dimensional function 00315 */ 00316 template<class tclass, class param_t, class vec_t=ovector_view> 00317 class multi_funct_mfptr_noerr : public multi_funct<param_t,vec_t> { 00318 public: 00319 00320 /** \brief Specify the member function pointer 00321 */ 00322 multi_funct_mfptr_noerr(tclass *tp, double (tclass::*fp) 00323 (size_t nv, const vec_t &x, param_t &pa)) { 00324 tptr=tp; 00325 fptr=fp; 00326 } 00327 00328 virtual ~multi_funct_mfptr_noerr() {} 00329 00330 /** \brief Compute a function \c y of \c nv variables stored in \c x 00331 with parameter \c pa. 00332 */ 00333 virtual int operator()(size_t nv, const vec_t &x, double &y, param_t &pa) { 00334 y=(*tptr.*fptr)(nv,x,pa); 00335 return 0; 00336 } 00337 00338 /** \brief Return the value of a function of \c nv variables 00339 stored in \c x with parameter \c pa. 00340 00341 Note that this is reimplemented in all children because 00342 if one member function operator() is reimplemented, all must be. 00343 */ 00344 virtual double operator()(size_t nv, const vec_t &x, param_t &pa) { 00345 double y; 00346 operator()(nv,x,y,pa); 00347 return y; 00348 } 00349 00350 #ifndef DOXYGEN_INTERNAL 00351 00352 protected: 00353 00354 /// Store the function pointer 00355 double (tclass::*fptr)(size_t nv, const vec_t &x, param_t &pa); 00356 /// Store a pointer to the class instance 00357 tclass *tptr; 00358 00359 #ifndef DOXYGENP 00360 #endif 00361 00362 private: 00363 00364 multi_funct_mfptr_noerr(const multi_funct_mfptr_noerr &); 00365 multi_funct_mfptr_noerr& operator=(const multi_funct_mfptr_noerr&); 00366 00367 #endif 00368 00369 }; 00370 00371 // --------------------------------------------------------------------- 00372 // --------------------------------------------------------------------- 00373 00374 /** 00375 \brief Multi-dimensional function base with arrays [abstract base] 00376 00377 This class generalizes one function of several variables, 00378 i.e. \f$ y(x_0,x_1,...,x_{nv-1}) \f$ where \c nv is the number 00379 of variables in the function y. 00380 */ 00381 template<class param_t, size_t nvar> class multi_vfunct { 00382 00383 public: 00384 00385 multi_vfunct() {} 00386 00387 virtual ~multi_vfunct() {} 00388 00389 /** \brief Compute a function \c y of \c nv variables stored in \c x 00390 with parameter \c pa. 00391 */ 00392 virtual int operator()(size_t nv, const double x[nvar], double &y, 00393 param_t &pa)=0; 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
,
O2scl Sourceforge Project Page