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 template<class param_t, class func_t, class rng_t=gsl_rnga, 00060 class vec_t=ovector_base, class alloc_vec_t=ovector, 00061 class alloc_t=ovector_alloc> class gsl_monte : 00062 public mcarlo_inte<param_t,func_t,rng_t,vec_t> { 00063 00064 public: 00065 00066 virtual ~gsl_monte() {} 00067 00068 /// Integrate function \c func from x=a to x=b. 00069 virtual int minteg_err(func_t &func, size_t ndim, const vec_t &a, 00070 const vec_t &b, param_t &pa, double &res, 00071 double &err) 00072 { 00073 00074 double r; 00075 00076 alloc_vec_t cc; 00077 alloc_t ao; 00078 ao.allocate(cc,ndim); 00079 00080 double vol=1.0, m=0.0, q=0.0; 00081 for(size_t i=0;i<ndim;i++) vol*=b[i]-a[i]; 00082 00083 for(int n=0;n<this->n_points;n++) { 00084 for(size_t i=0;i<ndim;i++) { 00085 do { 00086 r=this->def_rng.random(); 00087 } while (r==0.0); 00088 cc[i]=a[i]+r*(b[i]-a[i]); 00089 } 00090 double fval; 00091 func(ndim,cc,fval,pa); 00092 double d=fval-m; 00093 m+=d/(n+1.0); 00094 q+=d*d*(n/(n+1.0)); 00095 } 00096 00097 res=vol*m; 00098 00099 if (this->n_points<2) { 00100 err=0.0; 00101 } else { 00102 err=vol*sqrt(q/(this->n_points*(this->n_points-1.0))); 00103 } 00104 00105 ao.free(cc); 00106 00107 return 0; 00108 00109 } 00110 00111 /** \brief Integrate function \c func over the hypercube from 00112 \f$ x_i=a_i \f$ to \f$ x_i=b_i \f$ for 00113 \f$ 0<i< \f$ ndim-1 00114 */ 00115 virtual double minteg(func_t &func, size_t ndim, const vec_t &a, 00116 const vec_t &b, param_t &pa) { 00117 double res; 00118 minteg_err(func,ndim,a,b,pa,res,this->interror); 00119 return res; 00120 } 00121 00122 /// Return string denoting type ("gsl_monte") 00123 virtual const char *type() { return "gsl_monte"; } 00124 00125 }; 00126 00127 #ifndef DOXYGENP 00128 } 00129 #endif 00130 00131 #endif 00132
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