00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2006, 2007, 2008, 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_GSL_MONTE_H 00024 #define O2SCL_GSL_MONTE_H 00025 00026 #include <iostream> 00027 #include <o2scl/collection.h> 00028 #include <o2scl/mcarlo_inte.h> 00029 #include <o2scl/gsl_rnga.h> 00030 #include <gsl/gsl_monte.h> 00031 #include <gsl/gsl_monte_plain.h> 00032 00033 #ifndef DOXYGENP 00034 namespace o2scl { 00035 #endif 00036 00037 /** 00038 \brief Multidimensional integration using plain Monte Carlo (GSL) 00039 00040 */ 00041 template<class param_t, class func_t, class rng_t=gsl_rnga, 00042 class vec_t=ovector_view, class alloc_vec_t=ovector, 00043 class alloc_t=ovector_alloc> class gsl_monte : 00044 public mcarlo_inte<param_t,func_t,rng_t,vec_t> { 00045 00046 public: 00047 00048 virtual ~gsl_monte() {} 00049 00050 /// Integrate function \c func from x=a to x=b. 00051 virtual int minteg_err(func_t &func, size_t ndim, const vec_t &a, 00052 const vec_t &b, param_t &pa, double &res, 00053 double &err) 00054 { 00055 00056 double r; 00057 00058 alloc_vec_t cc; 00059 alloc_t ao; 00060 ao.allocate(cc,ndim); 00061 00062 double vol=1.0, m=0.0, q=0.0; 00063 for(size_t i=0;i<ndim;i++) vol*=b[i]-a[i]; 00064 00065 for(int n=0;n<this->n_points;n++) { 00066 for(size_t i=0;i<ndim;i++) { 00067 do { 00068 r=this->def_rng.random(); 00069 } while (r==0.0); 00070 cc[i]=a[i]+r*(b[i]-a[i]); 00071 } 00072 double fval; 00073 func(ndim,cc,fval,pa); 00074 double d=fval-m; 00075 m+=d/(n+1.0); 00076 q+=d*d*(n/(n+1.0)); 00077 } 00078 00079 res=vol*m; 00080 00081 if (this->n_points<2) { 00082 err=0.0; 00083 } else { 00084 err=vol*sqrt(q/(this->n_points*(this->n_points-1.0))); 00085 } 00086 00087 ao.free(cc); 00088 00089 return 0; 00090 00091 } 00092 00093 /** \brief Integrate function \c func over the hypercube from 00094 \f$ x_i=a_i \f$ to \f$ x_i=b_i \f$ for 00095 \f$ 0<i< \f$ ndim-1 00096 */ 00097 virtual double minteg(func_t &func, size_t ndim, const vec_t &a, 00098 const vec_t &b, param_t &pa) { 00099 double res; 00100 minteg_err(func,ndim,a,b,pa,res,this->interror); 00101 return res; 00102 } 00103 00104 /// Return string denoting type ("gsl_monte") 00105 virtual const char *type() { return "gsl_monte"; } 00106 00107 }; 00108 00109 #ifndef DOXYGENP 00110 } 00111 #endif 00112 00113 #endif 00114
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