All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
inte_multi_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 /** \file inte_multi_comp.h
24  \brief File defining \ref o2scl::inte_multi_comp
25 */
26 #ifndef O2SCL_INTE_MULTI_COMP_H
27 #define O2SCL_INTE_MULTI_COMP_H
28 
29 #include <iostream>
30 
31 #include <o2scl/inte_multi.h>
32 #include <o2scl/inte.h>
33 #include <o2scl/funct.h>
34 #include <o2scl/multi_funct.h>
35 
36 #ifndef DOXYGEN_NO_O2NS
37 namespace o2scl {
38 #endif
39 
40  /** \brief Naive multi-dimensional integration over a hypercube
41 
42  Naively combine several one-dimensional integration objects from
43  class inte in order to perform a multi-dimensional integration
44  over a region defined by constant limits. For more general regions
45  of integration, use children of the class \ref inte_gen.
46 
47  The 1-dimensional integration routines are specified in the function
48  set_oned_inte().
49 
50  The integration routines are called in order of the index
51  specified in the function set_oned_inte(). For
52  <tt>n-</tt>dimensional integration, <tt>n</tt> one-dimensional
53  integration objects should be specified, with indexes <tt>0</tt>
54  through <tt>n-1</tt>. The integration routines are called in
55  order of their index, so that the outermost integration is done
56  by the routine specified with index 0.
57  \f[
58  \int_{x_0=a_0}^{x_0=b_0} \int_{x_1=a_1}^{x_1=b_1} ...
59  \int_{x_{\mathrm{n}-1}=a_{\mathrm{n}-1}}^
60  {x_{\mathrm{n}-1}=b_{\mathrm{n}-1}}
61  f(x_0,x_1,...,x_{\mathrm{n}})
62  \f]
63 
64  No error estimate is performed. Error estimation for multiple
65  dimension integrals is provided by the Monte Carlo integration
66  classes (see \ref o2scl::mcarlo).
67 
68  \future Create a function to set an entire array of
69  one-dimensional integration objects at once?
70  \future Convert the inte<funct> ** to a std::vector<inte<funct> *>
71  */
72  template<class func_t=multi_funct11,
74  class inte_multi_comp : public inte_multi<func_t,vec_t> {
75 
76 #ifndef DOXYGEN_INTERNAL
77 
78  protected:
79 
80  /// The user-specified upper limits
81  const vec_t *ax;
82  /// The user-specified lower limits
83  const vec_t *bx;
84  /// The independent variable vector
85  vec_t *cx;
86  /// The user-specified function
87  func_t *mf;
88  /// The user-specified number of dimensions
89  size_t ndim;
90 
91 #endif
92 
93  public:
94 
95  inte_multi_comp() {
96  nint=0;
97  max_dim=100;
98  }
99 
100  virtual ~inte_multi_comp() {
101  if (nint>0) {
102  delete[] iptrs;
103  delete[] tptrs;
104  }
105  }
106 
107  /** \brief Set the one-dimensional integration object with
108  index \c i.
109  */
110  int set_oned_inte(inte<> &it, size_t i) {
111 
112  if (i>=max_dim) {
113  O2SCL_ERR("Index >= max_dim in inte_multi_comp::set_oned_inte().",
114  exc_einval);
115  }
116 
117  if (nint==0) {
118 
119  // Create new space
120  nint=i+1;
121  iptrs=new inte<> *[nint];
122  tptrs=new bool[nint];
123 
124  } else if (i>nint-1) {
125 
126  // Create new space and copy old info over
127  size_t nint_new=i+1;
128 
129  inte<> **iptrs_new=new inte<> *[nint_new];
130  bool *tptrs_new=new bool[nint_new];
131 
132  for(size_t j=0;j<nint;j++) {
133  iptrs_new[j]=iptrs[j];
134  tptrs_new[j]=tptrs[j];
135  }
136 
137  delete[] iptrs;
138  delete[] tptrs;
139 
140  iptrs=iptrs_new;
141  tptrs=tptrs_new;
142  nint=nint_new;
143 
144  }
145 
146  iptrs[i]=&it;
147  tptrs[i]=true;
148 
149  return success;
150  }
151 
152  /** \brief Integrate function \c func over the hypercube from
153  \f$ x_i=a_i \f$ to \f$ x_i=b_i \f$ for
154  \f$ 0<i< \f$ ndim-1
155 
156  The function set_oned_inte() must be used first to set the
157  one-dimensional routines to use.
158  */
159  virtual int minteg_err(func_t &func, size_t n, const vec_t &a,
160  const vec_t &b, double &res, double &err) {
161  err=0.0;
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  }
174 
175  // Perform integration
176  vec_t c;
177  c.resize(n);
178 
179  ax=&a;
180  bx=&b;
181  cx=&c;
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 
191  res=iptrs[0]->integ(fmn,a[0],b[0]);
192 
193  return success;
194 
195  }
196 
197  /// Return string denoting type ("inte_multi_comp")
198  virtual const char *type() { return "inte_multi_comp"; }
199 
200  /// The maxiumum number of integration dimensions (default 100)
201  size_t max_dim;
202 
203  protected:
204 
205 #ifndef DOXYGEN_INTERNAL
206 
207  /// The one-dimensional integration function
208  double odfunc(double x, size_t &ix) {
209 
210  double res;
211 
212  (*cx)[ix]=x;
213  if (ix==ndim-1) {
214  res=(*mf)(ndim,(*cx));
215  } else {
216  size_t ix_next=ix+1;
217 
218  /// This function to send to the integrators
219  funct11 fmn=
220  std::bind(std::mem_fn<double(double,size_t &)>
222  this,std::placeholders::_1,ix_next);
223 
224  res=iptrs[ix]->integ(fmn,(*ax)[ix+1],(*bx)[ix+1]);
225  }
226  return res;
227  }
228 
229  /// The size of the integration object arrays
230  size_t nint;
231 
232  /// Pointers to the integration objects
234 
235  /// Flag indicating if integration object has been set
236  bool *tptrs;
237 
238 #endif
239 
240  };
241 
242 #ifndef DOXYGEN_NO_O2NS
243 }
244 #endif
245 
246 #endif
247 
virtual const char * type()
Return string denoting type ("inte_multi_comp")
size_t nint
The size of the integration object arrays.
double odfunc(double x, size_t &ix)
The one-dimensional integration function.
std::function< double(double)> funct11
One-dimensional function typedef.
Definition: funct.h:44
int set_oned_inte(inte<> &it, size_t i)
Set the one-dimensional integration object with index i.
invalid argument supplied by user
Definition: err_hnd.h:59
vec_t * cx
The independent variable vector.
const vec_t * ax
The user-specified upper limits.
size_t max_dim
The maxiumum number of integration dimensions (default 100)
virtual double integ(func_t &func, double a, double b)
Integrate function func from a to b.
Definition: inte.h:85
bool * tptrs
Flag indicating if integration object has been set.
Base integration class [abstract base].
Definition: inte.h:44
const vec_t * bx
The user-specified lower limits.
Multi-dimensional integration over a hypercube [abstract base].
Definition: inte_multi.h:46
inte ** iptrs
Pointers to the integration objects.
#define O2SCL_ERR(d, n)
Set an error with message d and code n.
Definition: err_hnd.h:273
size_t ndim
The user-specified number of dimensions.
virtual int minteg_err(func_t &func, size_t n, const vec_t &a, const vec_t &b, double &res, double &err)
Integrate function func over the hypercube from to for ndim-1.
func_t * mf
The user-specified function.
std::function< double(size_t, const boost::numeric::ublas::vector< double > &)> multi_funct11
Multi-dimensional function typedef.
Definition: multi_funct.h:45
Naive multi-dimensional integration over a hypercube.
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..