All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
inte_gen_comp.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2014, Andrew W. Steiner
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_COMP_GEN_INTE_H
24 #define O2SCL_COMP_GEN_INTE_H
25 
26 /** \file inte_gen_comp.h
27  \brief File defining \ref o2scl::inte_gen_comp
28 */
29 
30 #include <iostream>
31 
32 #include <boost/numeric/ublas/vector.hpp>
33 
34 #include <o2scl/funct.h>
35 #include <o2scl/inte.h>
36 #include <o2scl/inte_gen.h>
37 
38 #ifndef DOXYGEN_NO_O2NS
39 namespace o2scl {
40 #endif
41 
42  /** \brief Naive generalized multi-dimensional integration
43 
44  Naively combine several one-dimensional integration objects from
45  class inte in order to perform a multi-dimensional integration.
46  The integration routines are specified in the function
47  set_ptrs().
48 
49  The integration routines are called in order of the index
50  specified in the function set_oned_inte(). For
51  <tt>n-</tt>dimensional integration, <tt>n</tt> one-dimensional
52  integration objects should be specified, with indexes <tt>0</tt>
53  through <tt>n-1</tt>. The integration routines are called in
54  order of their index, so that the outermost integration is done
55  by the routine specified with index 0. The integral
56  performed is:
57  \f[
58  \int_{x_0=a_0}^{x_0=b_0} f(x_0) \int_{x_1=a_1(x_0)}^{x_1=b_1(x_0)}
59  f(x_0, x_1) ...
60  \int_{x_{\mathrm{n}-1}=a_{\mathrm{n}-1}(x_0,x_1,..,x_{\mathrm{n}-2})}^
61  {x_{\mathrm{n}-1}=b_{\mathrm{n}-1}(x_0,x_1,..,x_{\mathrm{n}-2})}
62  f(x_0,x_1,...,x_{\mathrm{n-1}})~d x_{\mathrm{n}-1}~...~d x_1~d x_0
63  \f]
64 
65  This class is particularly useful if \f$ f_0 \f$ is time-consuming
66  to evaluate, and separable from \f$ f_{n-1} \f$ .
67 
68  See the discussion about the functions \c func, \c lower and \c
69  upper in the documentation for the class inte_gen.
70 
71  No error estimate is performed. Error estimation for multiple
72  dimension integrals is provided by the Monte Carlo integration
73  classes (see \ref mcarlo_section).
74 
75  \future Provide an example of usage for this class.
76  */
77  template<class func_t, class lfunc_t=func_t,
78  class ufunc_t=func_t, class vec_t=boost::numeric::ublas::vector<double> >
79  class inte_gen_comp :
80  public inte_gen<func_t,lfunc_t,ufunc_t,vec_t> {
81 
82 #ifndef DOXYGEN_INTERNAL
83 
84  protected:
85 
86  /// The independent variable vector
87  vec_t *cx;
88  /// The function specifying the lower limits
89  lfunc_t *lowerp;
90  /// The function specifying the upper limits
91  ufunc_t *upperp;
92  /// The function to be integrated
93  func_t *mf;
94  /// The number of dimensions
95  size_t ndim;
96 
97 #endif
98 
99  public:
100 
101  inte_gen_comp() {
102 
103  nint=0;
104  max_dim=100;
105  }
106 
107  virtual ~inte_gen_comp() {
108  }
109 
110  /** \brief Set the one-dimensional integration object with
111  index \c i.
112  */
113  int set_oned_inte(inte<> &it, size_t i) {
114 
115  if (i>=max_dim) {
116  O2SCL_ERR("Index >= max_dim in inte_multi_comp::set_oned_inte().",
117  exc_einval);
118  }
119 
120  if (nint==0) {
121 
122  // Create new space
123  nint=i+1;
124  iptrs=new inte<> *[nint];
125  tptrs=new bool[nint];
126 
127  } else if (i>nint-1) {
128 
129  // Create new space and copy old info over
130  size_t nint_new=i+1;
131 
132  inte<> **iptrs_new=
133  new inte<> *[nint_new];
134  bool *tptrs_new=new bool[nint_new];
135 
136  for(size_t j=0;j<nint;j++) {
137  iptrs_new[j]=iptrs[j];
138  tptrs_new[j]=tptrs[j];
139  }
140 
141  delete[] iptrs;
142  delete[] tptrs;
143 
144  iptrs=iptrs_new;
145  tptrs=tptrs_new;
146  nint=nint_new;
147 
148  }
149 
150  iptrs[i]=&it;
151  tptrs[i]=true;
152 
153  return success;
154  }
155 
156  /** \brief Integrate function \c func from
157  \f$ {\ell}_i=f_i(x_i) \f$ to \f$ u_i=g_i(x_i) \f$ for
158  \f$ 0<i<\mathrm{n}-1 \f$.
159  */
160  virtual double ginteg(func_t &func, size_t n, func_t &lower,
161  func_t &upper) {
162 
163  // Test to make sure the 1-d integrators were set
164  bool enough_oned=true;
165  if (n>nint) enough_oned=false;
166  for(size_t i=0;i<n;i++) {
167  if (tptrs[i]==false) enough_oned=false;
168  }
169 
170  if (enough_oned==false) {
171  O2SCL_ERR("Too few objects specified with set_ptrs() in minteg().",
172  exc_einval);
173  return 0.0;
174  }
175 
176  // Perform integration
177  vec_t c(n);
178 
179  cx=&c;
180  lowerp=&lower;
181  upperp=&upper;
182  mf=&func;
183  ndim=n;
184  size_t ix=0;
185 
186  funct11 fmn=
187  std::bind(std::mem_fn<double(double,size_t &)>
189  this,std::placeholders::_1,ix);
190  double res=iptrs[0]->integ(fmn,lower(0,c),upper(0,c));
191 
192  return res;
193  }
194 
195  /// Return string denoting type ("inte_gen_comp")
196  virtual const char *type() { return "inte_gen_comp"; }
197 
198  /// The maxiumum number of integration dimensions (default 100)
199  size_t max_dim;
200 
201 #ifndef DOXYGEN_INTERNAL
202 
203  protected:
204 
205  /// The one-dimensional integration function
206  double odfunc(double x, size_t &ix) {
207 
208  double res;
209 
210  (*cx)[ix]=x;
211  if (ix==ndim-1) {
212  res=(*mf)(ndim,(*cx));
213  } else {
214  size_t ix_next=ix+1;
215  res=(*mf)(ix_next,*cx);
216 
217  funct11 fmn=
218  std::bind(std::mem_fn<double(double,size_t &)>
220  this,std::placeholders::_1,ix_next);
221 
222  res*=iptrs[ix]->integ(fmn,(*lowerp)(ix_next,*cx),
223  (*upperp)(ix_next,*cx));
224  }
225  return res;
226  }
227 
228  /// The size of the integration object arrays
229  size_t nint;
230 
231  /// Pointers to the integration objects
233 
234  /// Flag indicating if integration object has been set
235  bool *tptrs;
236 
237 #endif
238 
239 
240  };
241 
242 #ifndef DOXYGEN_NO_O2NS
243 }
244 #endif
245 
246 #endif
247 
size_t max_dim
The maxiumum number of integration dimensions (default 100)
std::function< double(double)> funct11
One-dimensional function typedef.
Definition: funct.h:44
size_t nint
The size of the integration object arrays.
invalid argument supplied by user
Definition: err_hnd.h:59
Generalized multi-dimensional integration [abstract base].
Definition: inte_gen.h:67
vec_t * cx
The independent variable vector.
Definition: inte_gen_comp.h:87
size_t ndim
The number of dimensions.
Definition: inte_gen_comp.h:95
virtual double integ(func_t &func, double a, double b)
Integrate function func from a to b.
Definition: inte.h:85
lfunc_t * lowerp
The function specifying the lower limits.
Definition: inte_gen_comp.h:89
ufunc_t * upperp
The function specifying the upper limits.
Definition: inte_gen_comp.h:91
int set_oned_inte(inte<> &it, size_t i)
Set the one-dimensional integration object with index i.
Base integration class [abstract base].
Definition: inte.h:44
#define O2SCL_ERR(d, n)
Set an error with message d and code n.
Definition: err_hnd.h:273
double odfunc(double x, size_t &ix)
The one-dimensional integration function.
inte ** iptrs
Pointers to the integration objects.
virtual const char * type()
Return string denoting type ("inte_gen_comp")
virtual double ginteg(func_t &func, size_t n, func_t &lower, func_t &upper)
Integrate function func from to for .
Naive generalized multi-dimensional integration.
Definition: inte_gen_comp.h:79
func_t * mf
The function to be integrated.
Definition: inte_gen_comp.h:93
bool * tptrs
Flag indicating if integration object has been set.
Success.
Definition: err_hnd.h:47

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).
Hosted at Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads..