00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2006, 2007, 2008, 2009, 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 /* monte/plain.c 00024 * 00025 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Michael Booth 00026 * 00027 * This program is free software; you can redistribute it and/or modify 00028 * it under the terms of the GNU General Public License as published by 00029 * the Free Software Foundation; either version 3 of the License, or (at 00030 * your option) any later version. 00031 * 00032 * This program is distributed in the hope that it will be useful, but 00033 * WITHOUT ANY WARRANTY; without even the implied warranty of 00034 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00035 * General Public License for more details. 00036 * 00037 * You should have received a copy of the GNU General Public License 00038 * along with this program; if not, write to the Free Software 00039 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00040 * 02110-1301, USA. 00041 */ 00042 #ifndef O2SCL_GSL_MONTE_H 00043 #define O2SCL_GSL_MONTE_H 00044 00045 #include <iostream> 00046 #include <o2scl/collection.h> 00047 #include <o2scl/mcarlo_inte.h> 00048 #include <o2scl/gsl_rnga.h> 00049 #include <gsl/gsl_monte.h> 00050 #include <gsl/gsl_monte_plain.h> 00051 00052 #ifndef DOXYGENP 00053 namespace o2scl { 00054 #endif 00055 00056 /** 00057 \brief Multidimensional integration using plain Monte Carlo (GSL) 00058 00059 */ 00060 template<class param_t, class func_t, class rng_t=gsl_rnga, 00061 class vec_t=ovector_base, class alloc_vec_t=ovector, 00062 class alloc_t=ovector_alloc> class gsl_monte : 00063 public mcarlo_inte<param_t,func_t,rng_t,vec_t> { 00064 00065 public: 00066 00067 virtual ~gsl_monte() {} 00068 00069 /// Integrate function \c func from x=a to x=b. 00070 virtual int minteg_err(func_t &func, size_t ndim, const vec_t &a, 00071 const vec_t &b, param_t &pa, double &res, 00072 double &err) 00073 { 00074 00075 double r; 00076 00077 alloc_vec_t cc; 00078 alloc_t ao; 00079 ao.allocate(cc,ndim); 00080 00081 double vol=1.0, m=0.0, q=0.0; 00082 for(size_t i=0;i<ndim;i++) vol*=b[i]-a[i]; 00083 00084 for(int n=0;n<this->n_points;n++) { 00085 for(size_t i=0;i<ndim;i++) { 00086 do { 00087 r=this->def_rng.random(); 00088 } while (r==0.0); 00089 cc[i]=a[i]+r*(b[i]-a[i]); 00090 } 00091 double fval; 00092 func(ndim,cc,fval,pa); 00093 double d=fval-m; 00094 m+=d/(n+1.0); 00095 q+=d*d*(n/(n+1.0)); 00096 } 00097 00098 res=vol*m; 00099 00100 if (this->n_points<2) { 00101 err=0.0; 00102 } else { 00103 err=vol*sqrt(q/(this->n_points*(this->n_points-1.0))); 00104 } 00105 00106 ao.free(cc); 00107 00108 return 0; 00109 00110 } 00111 00112 /** \brief Integrate function \c func over the hypercube from 00113 \f$ x_i=a_i \f$ to \f$ x_i=b_i \f$ for 00114 \f$ 0<i< \f$ ndim-1 00115 */ 00116 virtual double minteg(func_t &func, size_t ndim, const vec_t &a, 00117 const vec_t &b, param_t &pa) { 00118 double res; 00119 minteg_err(func,ndim,a,b,pa,res,this->interror); 00120 return res; 00121 } 00122 00123 /// Return string denoting type ("gsl_monte") 00124 virtual const char *type() { return "gsl_monte"; } 00125 00126 }; 00127 00128 #ifndef DOXYGENP 00129 } 00130 #endif 00131 00132 #endif 00133
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