inte.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2020, Andrew W. Steiner and Jerry Gagelman
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 #ifndef O2SCL_INTE_H
24 #define O2SCL_INTE_H
25 
26 /** \file inte.h
27  \brief File defining \ref o2scl::inte
28 */
29 
30 #include <cmath>
31 #include <o2scl/err_hnd.h>
32 #include <o2scl/funct.h>
33 
34 #ifndef DOXYGEN_NO_O2NS
35 namespace o2scl {
36 #endif
37 
38  /** \brief Base integration class [abstract base]
39 
40  \note Currently \o2 supports only types \c double and, for some
41  integration methods, \c long \c double for the floating point
42  type \c fp_t . Also, the default values of \ref tol_rel
43  and \ref tol_abs are designed for double precision and
44  likely need to be decreased for long double precision
45  integration.
46 
47  \future It might be useful to have all of the integration
48  classes report the number of function evaluations used
49  in addition to the number of iterations which were taken.
50  */
51  template<class func_t=funct, class fp_t=double> class inte {
52 
53  public:
54 
55  inte() {
56  tol_rel=1.0e-8;
57  tol_abs=1.0e-8;
58  verbose=0;
59  interror=0.0;
60  err_nonconv=true;
61  }
62 
63  virtual ~inte() {}
64 
65  /** \brief Verbosity
66  */
67  int verbose;
68 
69  /// The most recent number of iterations taken
70  size_t last_iter;
71 
72  /** \brief The maximum relative uncertainty
73  in the value of the integral (default \f$ 10^{-8} \f$)
74  */
75  fp_t tol_rel;
76 
77  /** \brief The maximum absolute uncertainty
78  in the value of the integral (default \f$ 10^{-8} \f$)
79  */
80  fp_t tol_abs;
81 
82  /** \brief If true, call the error handler if the routine does not
83  converge or reach the desired tolerance (default true)
84 
85  If this is false, the function proceeds normally and
86  may provide convergence information in the integer return value.
87  */
89 
90  /** \brief Integrate function \c func from \c a to \c b.
91  */
92  virtual fp_t integ(func_t &func, fp_t a, fp_t b) {
93  fp_t res;
94  int ret=integ_err(func,a,b,res,this->interror);
95  if (ret!=0) {
96  O2SCL_ERR2("Integration failed in inte::integ(), ",
97  "but cannot return int.",o2scl::exc_efailed);
98  }
99  return res;
100  }
101 
102  /** \brief Integrate function \c func from \c a to \c b and place
103  the result in \c res and the error in \c err
104  */
105  virtual int integ_err(func_t &func, fp_t a, fp_t b,
106  fp_t &res, fp_t &err)=0;
107 
108  /** \brief Return the numerically estimated error in the result from
109  the last call to integ()
110 
111  This will quietly return zero if no integrations have been
112  performed or if the integrator does not estimate the error.
113  */
114  fp_t get_error() { return interror; }
115 
116  /// Return string denoting type ("inte")
117  virtual const char *type() { return "inte"; }
118 
119 #ifndef DOXYGEN_INTERNAL
120 
121  protected:
122 
123  /// The uncertainty for the last integration computation
124  fp_t interror;
125 
126 #endif
127 
128  };
129 
130  /** \brief Integrate over \f$ (-\infty,b] \f$
131 
132  \note This class only works if the base integration
133  type <tt>def_inte_t</tt> avoids evaluating the function
134  at the left-hand end point.
135  */
136  template<class func_t, class def_inte_t, class fp_t=double>
137  class inte_il {
138 
139  protected:
140 
141  /// A pointer to the user-specified function
142  func_t *user_func;
143 
144  /// The upper limit
146 
147  /// Transform from \f$ t \in (0,1] \f$ to \f$ x \in (-\infty,b] \f$
148  virtual fp_t transform(fp_t t) {
149  if (t==0.0) {
150  O2SCL_ERR2("Function called with t=0 in ",
151  "inte_il::transform().",o2scl::exc_efailed);
152  }
153  fp_t x=upper_limit-(1-t)/t, y;
154  y=(*user_func)(x);
155  return y/t/t;
156  }
157 
158  public:
159 
160  inte_il() {
161  it=&def_inte;
162  fo=std::bind(std::mem_fn<fp_t(fp_t)>(&inte_il::transform),
163  this,std::placeholders::_1);
164  }
165 
166  /** \brief Internal function type based on floating-point type
167 
168  \comment
169  This type must be public so the user can change the
170  base integration object
171  \endcomment
172  */
173  typedef std::function<fp_t(fp_t)> internal_funct;
174 
175  protected:
176 
177  /// The base integration object
179 
180  /// Function object
182 
183  public:
184 
185  /** \brief Integrate function \c func from \c a to \c b
186  giving result \c res and error \c err
187  */
188  virtual int integ_err(func_t &func, fp_t b,
189  fp_t &res, fp_t &err) {
190  user_func=&func;
191  upper_limit=b;
192  int ret=it->integ_err(fo,0.0,1.0,res,err);
193  return ret;
194  }
195 
196  /// \name Integration object
197  //@{
198  /// Set the base integration object to use
200  it=&i;
201  return 0;
202  }
203 
204  /// Default integration object
205  def_inte_t def_inte;
206  //@}
207 
208  };
209 
210  /** \brief Integrate over \f$ [a,\infty) \f$
211 
212  \note This class only works if the base integration
213  type <tt>def_inte_t</tt> avoids evaluating the function
214  at the right-hand end point.
215  */
216  template<class func_t, class def_inte_t, class fp_t=double>
217  class inte_iu {
218 
219  protected:
220 
221  /// A pointer to the user-specified function
222  func_t *user_func;
223 
224  /// The lower limit
226 
227  /// Transform from \f$ t \in (0,1] \f$ to \f$ x \in [a,\infty) \f$
228  virtual fp_t transform(fp_t t) {
229  if (t==0.0) {
230  O2SCL_ERR2("Function called with t=0 in ",
231  "inte_ul::transform().",o2scl::exc_efailed);
232  }
233  fp_t x=lower_limit+(1-t)/t, y;
234  y=(*user_func)(x);
235  return y/t/t;
236  }
237 
238  public:
239 
240  inte_iu() {
241  it=&def_inte;
242  fo=std::bind(std::mem_fn<fp_t(fp_t)>(&inte_iu::transform),
243  this,std::placeholders::_1);
244  }
245 
246  /** \brief Internal function type based on floating-point type
247 
248  \comment
249  This type must be public so the user can change the
250  base integration object
251  \endcomment
252  */
253  typedef std::function<fp_t(fp_t)> internal_funct;
254 
255  protected:
256 
257  /// The base integration object
259 
260  /// Function object
262 
263  public:
264 
265  /** \brief Integrate function \c func from \c a to \c b
266  giving result \c res and error \c err
267  */
268  virtual int integ_err(func_t &func, fp_t a,
269  fp_t &res, fp_t &err) {
270  user_func=&func;
271  lower_limit=a;
272  int ret=it->integ_err(fo,0.0,1.0,res,err);
273  return ret;
274  }
275 
276  /// \name Integration object
277  //@{
278  /// Set the base integration object to use
280  it=&i;
281  return 0;
282  }
283 
284  /// Default integration object
285  def_inte_t def_inte;
286  //@}
287 
288  };
289 
290 
291 #ifndef DOXYGEN_NO_O2NS
292 }
293 #endif
294 
295 #endif
296 
297 
298 
299 
o2scl::inte_il::transform
virtual fp_t transform(fp_t t)
Transform from to .
Definition: inte.h:148
o2scl::inte_iu::transform
virtual fp_t transform(fp_t t)
Transform from to .
Definition: inte.h:228
o2scl::inte::interror
fp_t interror
The uncertainty for the last integration computation.
Definition: inte.h:124
o2scl::inte_il::internal_funct
std::function< fp_t(fp_t)> internal_funct
Internal function type based on floating-point type.
Definition: inte.h:173
o2scl::inte::type
virtual const char * type()
Return string denoting type ("inte")
Definition: inte.h:117
o2scl::inte_iu
Integrate over .
Definition: inte.h:217
o2scl::inte_il
Integrate over .
Definition: inte.h:137
o2scl::exc_efailed
@ exc_efailed
generic failure
Definition: err_hnd.h:61
O2SCL_ERR2
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
Definition: err_hnd.h:281
o2scl::inte_iu::integ_err
virtual int integ_err(func_t &func, fp_t a, fp_t &res, fp_t &err)
Integrate function func from a to b giving result res and error err.
Definition: inte.h:268
o2scl::inte_iu::def_inte
def_inte_t def_inte
Default integration object.
Definition: inte.h:285
o2scl
The main O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl names...
Definition: anneal.h:42
o2scl::inte_iu::lower_limit
fp_t lower_limit
The lower limit.
Definition: inte.h:225
o2scl::inte_iu::fo
internal_funct fo
Function object.
Definition: inte.h:261
o2scl::inte::get_error
fp_t get_error()
Return the numerically estimated error in the result from the last call to integ()
Definition: inte.h:114
o2scl::inte_il::integ_err
virtual int integ_err(func_t &func, fp_t b, fp_t &res, fp_t &err)
Integrate function func from a to b giving result res and error err.
Definition: inte.h:188
o2scl::inte::last_iter
size_t last_iter
The most recent number of iterations taken.
Definition: inte.h:70
o2scl::inte_il::user_func
func_t * user_func
A pointer to the user-specified function.
Definition: inte.h:142
o2scl::inte::tol_abs
fp_t tol_abs
The maximum absolute uncertainty in the value of the integral (default )
Definition: inte.h:80
o2scl::inte::verbose
int verbose
Verbosity.
Definition: inte.h:67
o2scl::inte_iu::internal_funct
std::function< fp_t(fp_t)> internal_funct
Internal function type based on floating-point type.
Definition: inte.h:253
o2scl::inte::tol_rel
fp_t tol_rel
The maximum relative uncertainty in the value of the integral (default )
Definition: inte.h:75
o2scl::inte::integ
virtual fp_t integ(func_t &func, fp_t a, fp_t b)
Integrate function func from a to b.
Definition: inte.h:92
o2scl::inte
Base integration class [abstract base].
Definition: inte.h:51
o2scl::inte_iu::it
inte< internal_funct, fp_t > * it
The base integration object.
Definition: inte.h:258
o2scl::inte_il::set_inte
int set_inte(inte< internal_funct, fp_t > &i)
Set the base integration object to use.
Definition: inte.h:199
o2scl::inte::err_nonconv
bool err_nonconv
If true, call the error handler if the routine does not converge or reach the desired tolerance (defa...
Definition: inte.h:88
o2scl::inte_il::it
inte< internal_funct, fp_t > * it
The base integration object.
Definition: inte.h:178
o2scl::inte_il::upper_limit
fp_t upper_limit
The upper limit.
Definition: inte.h:145
o2scl::inte_iu::set_inte
int set_inte(inte< internal_funct, fp_t > &i)
Set the base integration object to use.
Definition: inte.h:279
o2scl::inte::integ_err
virtual int integ_err(func_t &func, fp_t a, fp_t b, fp_t &res, fp_t &err)=0
Integrate function func from a to b and place the result in res and the error in err.
o2scl::inte_il::fo
internal_funct fo
Function object.
Definition: inte.h:181
o2scl::inte_il::def_inte
def_inte_t def_inte
Default integration object.
Definition: inte.h:205
o2scl::inte_iu::user_func
func_t * user_func
A pointer to the user-specified function.
Definition: inte.h:222

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).