All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mcarlo_plain.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 /* monte/plain.c
24  *
25  * Copyright (C) 1996, 1997, 1998, 1999, 2000 Michael Booth
26  *
27  * This program is free software; you can redistribute it and/or modify
28  * it under the terms of the GNU General Public License as published by
29  * the Free Software Foundation; either version 3 of the License, or (at
30  * your option) any later version.
31  *
32  * This program is distributed in the hope that it will be useful, but
33  * WITHOUT ANY WARRANTY; without even the implied warranty of
34  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35  * General Public License for more details.
36  *
37  * You should have received a copy of the GNU General Public License
38  * along with this program; if not, write to the Free Software
39  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
40  * 02110-1301, USA.
41  */
42 #ifndef O2SCL_MCARLO_PLAIN_H
43 #define O2SCL_MCARLO_PLAIN_H
44 
45 /** \file mcarlo_plain.h
46  \brief File defining \ref o2scl::mcarlo_plain
47 */
48 #include <iostream>
49 
50 #include <boost/numeric/ublas/vector.hpp>
51 
52 #include <o2scl/multi_funct.h>
53 #include <o2scl/mcarlo.h>
54 #include <o2scl/rng_gsl.h>
55 
56 #ifndef DOXYGEN_NO_O2NS
57 namespace o2scl {
58 #endif
59 
60  /** \brief Multidimensional integration using plain Monte Carlo (GSL)
61  */
62  template<class func_t=multi_funct11,
64  class rng_t=int, class rng_dist_t=rng_gsl>
65  //class rng_t=std::mt19937,
66  // class rng_dist_t=std::uniform_real_distribution<double> >
67  class mcarlo_plain : public mcarlo<func_t,vec_t,rng_t,rng_dist_t> {
68 
69  public:
70 
71  virtual ~mcarlo_plain() {}
72 
73  /// Integrate function \c func from x=a to x=b.
74  virtual int minteg_err(func_t &func, size_t ndim, const vec_t &a,
75  const vec_t &b, double &res, double &err) {
76 
77  double r;
78 
79  vec_t cc(ndim);
80 
81  double vol=1.0, m=0.0, q=0.0;
82  for(size_t i=0;i<ndim;i++) vol*=b[i]-a[i];
83 
84  for(size_t n=0;n<this->n_points;n++) {
85  for(size_t i=0;i<ndim;i++) {
86  do {
87  r=this->rng_dist(this->rng);
88  } while (r==0.0);
89  cc[i]=a[i]+r*(b[i]-a[i]);
90  }
91  double fval;
92  fval=func(ndim,cc);
93  double d=fval-m;
94  m+=d/(n+1.0);
95  q+=d*d*(n/(n+1.0));
96  }
97 
98  res=vol*m;
99 
100  if (this->n_points<2) {
101  err=0.0;
102  } else {
103  err=vol*sqrt(q/(this->n_points*(this->n_points-1.0)));
104  }
105 
106  return 0;
107 
108  }
109 
110  /** \brief Integrate function \c func over the hypercube from
111  \f$ x_i=a_i \f$ to \f$ x_i=b_i \f$ for \f$ 0<i< \f$ ndim-1
112  */
113  virtual double minteg(func_t &func, size_t ndim, const vec_t &a,
114  const vec_t &b) {
115  double res;
116  minteg_err(func,ndim,a,b,res,this->interror);
117  return res;
118  }
119 
120  /// Return string denoting type ("mcarlo_plain")
121  virtual const char *type() { return "mcarlo_plain"; }
122 
123  };
124 
125 #ifndef DOXYGEN_NO_O2NS
126 }
127 #endif
128 
129 #endif
130 
rng_dist_t rng_dist
The random number distribution.
Definition: mcarlo.h:68
unsigned long n_points
Number of integration points (default 1000)
Definition: mcarlo.h:65
virtual int minteg_err(func_t &func, size_t ndim, const vec_t &a, const vec_t &b, double &res, double &err)
Integrate function func from x=a to x=b.
Definition: mcarlo_plain.h:74
rng_t rng
The random number generator.
Definition: mcarlo.h:71
virtual double minteg(func_t &func, size_t ndim, const vec_t &a, const vec_t &b)
Integrate function func over the hypercube from to for ndim-1.
Definition: mcarlo_plain.h:113
virtual const char * type()
Return string denoting type ("mcarlo_plain")
Definition: mcarlo_plain.h:121
Monte-Carlo integration [abstract base].
Definition: mcarlo.h:53
Multidimensional integration using plain Monte Carlo (GSL)
Definition: mcarlo_plain.h:67
double interror
The uncertainty for the last integration computation.
Definition: inte_multi.h:110
std::function< double(size_t, const boost::numeric::ublas::vector< double > &)> multi_funct11
Multi-dimensional function typedef.
Definition: multi_funct.h:45

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..