00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2006, 2007, 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 /** \brief One-dimensional function base 00034 00035 This class generalizes a function y(x). 00036 */ 00037 template<class param_t> 00038 class funct { 00039 00040 public: 00041 00042 funct() {} 00043 00044 virtual ~funct() {} 00045 00046 /** \brief The overloaded operator() 00047 */ 00048 virtual int operator()(double x, double &y, param_t &pa) { 00049 set_err_ret("Empty base function called in funct::operator()", 00050 gsl_nobase); 00051 } 00052 00053 /** \brief The overloaded operator() 00054 */ 00055 virtual double operator()(double x, param_t &pa) { 00056 double y; 00057 operator()(x,y,pa); 00058 return y; 00059 } 00060 00061 #ifndef DOXYGENP 00062 00063 private: 00064 00065 funct(const funct &); 00066 00067 funct& operator=(const funct&); 00068 00069 #endif 00070 00071 }; 00072 00073 /** \brief Function pointer to a function 00074 */ 00075 template<class param_t> 00076 class funct_fptr : public funct<param_t> { 00077 public: 00078 00079 /** \brief Specify the function pointer 00080 */ 00081 funct_fptr(int (*fp)(double x, double &y, param_t &pa)) { 00082 fptr=fp; 00083 } 00084 00085 virtual ~funct_fptr() {} 00086 00087 virtual int operator()(double x, double &y, param_t &pa) { 00088 return fptr(x,y,pa); 00089 } 00090 00091 // Unfortunately, if we overload operator()(double,double,void*), 00092 // we have to reimplement all operator()'s 00093 virtual double operator()(double x, param_t &pa) { 00094 double y; 00095 operator()(x,y,pa); 00096 return y; 00097 } 00098 00099 #ifndef DOXYGEN_INTERNAL 00100 00101 protected: 00102 00103 friend class io_tlate<funct_fptr>; 00104 00105 funct_fptr() {}; 00106 00107 /// Storage for the function pointer 00108 int (*fptr)(double x, double &y, param_t &pa); 00109 00110 #endif 00111 #ifndef DOXYGENP 00112 00113 private: 00114 00115 funct_fptr(const funct_fptr &); 00116 funct_fptr& operator=(const funct_fptr&); 00117 00118 #endif 00119 00120 }; 00121 00122 /** \brief Function pointer to a function 00123 */ 00124 template<class param_t> 00125 class funct_fptr_noerr : public funct<param_t> { 00126 public: 00127 00128 /** \brief Specify the function pointer 00129 */ 00130 funct_fptr_noerr(double (*fp)(double x, param_t &pa)) { 00131 fptr=fp; 00132 } 00133 00134 virtual ~funct_fptr_noerr() {} 00135 00136 virtual int operator()(double x, double &y, param_t &pa) { 00137 y=fptr(x,pa); 00138 return 0; 00139 } 00140 00141 // Unfortunately, if we overload operator()(double,double,void*), 00142 // we have to reimplement all operator()'s 00143 virtual double operator()(double x, param_t &pa) { 00144 double y; 00145 operator()(x,y,pa); 00146 return y; 00147 } 00148 00149 #ifndef DOXYGEN_INTERNAL 00150 00151 protected: 00152 00153 /// Storage for the function pointer 00154 double (*fptr)(double x, param_t &pa); 00155 00156 #endif 00157 #ifndef DOXYGENP 00158 00159 private: 00160 00161 funct_fptr_noerr(const funct_fptr_noerr &); 00162 funct_fptr_noerr& operator=(const funct_fptr_noerr&); 00163 00164 #endif 00165 00166 }; 00167 00168 /** \brief Function pointer to a function 00169 */ 00170 template<class param_t> 00171 class funct_fptr_nopar : public funct<param_t> { 00172 public: 00173 00174 /** \brief Specify the function pointer 00175 */ 00176 funct_fptr_nopar(double (*fp)(double x)) { 00177 fptr=fp; 00178 } 00179 00180 virtual ~funct_fptr_nopar() {} 00181 00182 virtual int operator()(double x, double &y, param_t &pa) { 00183 y=fptr(x); 00184 return 0; 00185 } 00186 00187 // Unfortunately, if we overload operator()(double,double,void*), 00188 // we have to reimplement all operator()'s 00189 virtual double operator()(double x, param_t &pa) { 00190 double y; 00191 operator()(x,y,pa); 00192 return y; 00193 } 00194 00195 #ifndef DOXYGEN_INTERNAL 00196 00197 protected: 00198 00199 /// Storage for the function pointer 00200 double (*fptr)(double x); 00201 00202 #endif 00203 #ifndef DOXYGENP 00204 00205 private: 00206 00207 funct_fptr_nopar(const funct_fptr_nopar &); 00208 funct_fptr_nopar& operator=(const funct_fptr_nopar&); 00209 00210 #endif 00211 00212 }; 00213 00214 /** 00215 \brief Member function pointer to a one-dimensional function 00216 */ 00217 template <class tclass, class param_t> class funct_mfptr : 00218 public funct<param_t> { 00219 public: 00220 00221 /** \brief Specify the member function pointer 00222 */ 00223 funct_mfptr(tclass *tp, int (tclass::*fp)(double x, double &y, 00224 param_t &pa)) { 00225 tptr=tp; 00226 fptr=fp; 00227 } 00228 00229 virtual ~funct_mfptr() {}; 00230 00231 virtual int operator()(double x, double &y, param_t &pa) { 00232 return (*tptr.*fptr)(x,y,pa); 00233 } 00234 00235 // Unfortunately, if we overload operator()(double,double,void*), 00236 // we have to reimplement all operator()'s 00237 virtual double operator()(double x, param_t &pa) { 00238 double y; 00239 operator()(x,y,pa); 00240 return y; 00241 } 00242 00243 #ifndef DOXYGEN_INTERNAL 00244 00245 protected: 00246 00247 /// Storage for the member function pointer 00248 int (tclass::*fptr)(double x, double &y, param_t &pa); 00249 00250 /// Store the pointer to the class instance 00251 tclass *tptr; 00252 00253 #endif 00254 #ifndef DOXYGENP 00255 00256 private: 00257 00258 funct_mfptr(const funct_mfptr &); 00259 funct_mfptr& operator=(const funct_mfptr&); 00260 00261 #endif 00262 00263 }; 00264 00265 /** \brief Member function pointer to a one-dimensional function 00266 */ 00267 template <class tclass, class param_t> class funct_mfptr_noerr : 00268 public funct<param_t> { 00269 public: 00270 00271 /** \brief Specify the member function pointer 00272 */ 00273 funct_mfptr_noerr(tclass *tp, 00274 double (tclass::*fp)(double x, param_t &pa)) { 00275 tptr=tp; 00276 fptr=fp; 00277 } 00278 00279 virtual ~funct_mfptr_noerr() {}; 00280 00281 virtual int operator()(double x, double &y, param_t &pa) { 00282 y=(*tptr.*fptr)(x,pa); 00283 return 0; 00284 } 00285 00286 // Unfortunately, if we overload operator()(double,double,void*), 00287 // we have to reimplement all operator()'s 00288 virtual double operator()(double x, param_t &pa) { 00289 double y; 00290 operator()(x,y,pa); 00291 return y; 00292 } 00293 00294 #ifndef DOXYGEN_INTERNAL 00295 00296 protected: 00297 00298 /// Storage for the member function pointer 00299 double (tclass::*fptr)(double x, param_t &pa); 00300 00301 /// Store the pointer to the class instance 00302 tclass *tptr; 00303 00304 #endif 00305 #ifndef DOXYGENP 00306 00307 private: 00308 00309 funct_mfptr_noerr(const funct_mfptr_noerr &); 00310 funct_mfptr_noerr& operator=(const funct_mfptr_noerr&); 00311 00312 #endif 00313 00314 }; 00315 00316 /** \brief Member function pointer to a one-dimensional function 00317 */ 00318 template <class tclass, class param_t> class funct_mfptr_nopar : 00319 public funct<param_t> { 00320 public: 00321 00322 /** \brief Specify the member function pointer 00323 */ 00324 funct_mfptr_nopar(tclass *tp, double (tclass::*fp)(double x)) { 00325 tptr=tp; 00326 fptr=fp; 00327 } 00328 00329 virtual ~funct_mfptr_nopar() {}; 00330 00331 virtual int operator()(double x, double &y, param_t &pa) { 00332 y=(*tptr.*fptr)(x); 00333 return 0; 00334 } 00335 00336 // Unfortunately, if we overload operator()(double,double,void*), 00337 // we have to reimplement all operator()'s 00338 virtual double operator()(double x, param_t &pa) { 00339 double y; 00340 operator()(x,y,pa); 00341 return y; 00342 } 00343 00344 #ifndef DOXYGEN_INTERNAL 00345 00346 protected: 00347 00348 /// Storage for the member function pointer 00349 double (tclass::*fptr)(double x); 00350 /// Store the pointer to the class instance 00351 tclass *tptr; 00352 00353 #endif 00354 #ifndef DOXYGENP 00355 00356 private: 00357 00358 funct_mfptr_nopar(const funct_mfptr_nopar &); 00359 funct_mfptr_nopar& operator=(const funct_mfptr_nopar&); 00360 00361 #endif 00362 00363 }; 00364 00365 #ifndef DOXYGENP 00366 } 00367 #endif 00368 00369 #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