gsl_miser.h

00001 /*
00002   -------------------------------------------------------------------
00003   
00004   Copyright (C) 2006, 2007, 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_MISER_H
00024 #define O2SCL_GSL_MISER_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_miser.h>
00032 
00033 #ifndef DOXYGENP
00034 namespace o2scl {
00035 #endif
00036 
00037   /** 
00038       \brief Multidimensional integration using plain Miser Carlo (GSL)
00039 
00040   */
00041   template<class param_t, class func_t, class rng_t=gsl_rnga, 
00042     class vec_t=ovector_view> class gsl_miser : 
00043     public mcarlo_inte<param_t,func_t,rng_t,vec_t> {
00044 
00045       public:
00046 
00047       virtual ~gsl_miser() {}
00048   
00049       /// Integrate function \c func from x=a to x=b.
00050       virtual int minteg_err(func_t &func, size_t ndim, 
00051                              const vec_t &a, const vec_t &b, param_t &pa,
00052                              double &res, double &err) 
00053       {
00054         gm_parms gmp={&func,(void *)&pa};
00055         gsl_monte_function gfunc={gsl_func,ndim,&gmp};
00056 
00057         int ncalls=this->n_points;
00058 
00059         double *aa=new double[ndim], *bb=new double[ndim];
00060         for(size_t i=0;i<ndim;i++) {
00061           aa[i]=a[i];
00062           bb[i]=b[i];
00063         }
00064   
00065         gsl_monte_miser_state *s=gsl_monte_miser_alloc(ndim);
00066         gsl_monte_miser_integrate(&gfunc,aa,bb,ndim,ncalls,
00067                                   this->def_rng.get_gsl_rng(),s,&res,&err);
00068         gsl_monte_miser_free(s);
00069         
00070         delete[] aa;
00071         delete[] bb;
00072 
00073         return 0;
00074 
00075       }
00076       
00077       /// Return string denoting type ("gsl_miser")
00078       virtual const char *type() { return "gsl_miser"; }
00079       
00080       protected:
00081       
00082 #ifndef DOXYGEN_INTERNAL
00083       
00084       /// The GSL function pointer
00085       static double gsl_func(double *x, size_t dim, void *pa) {
00086         gm_parms *gp=(gm_parms *)pa;
00087         ovector_array aa(dim,x);
00088         return (*gp->func)(dim,aa,*((param_t *)(gp->vp)));
00089       }
00090       
00091       /// The parameters for the GSL function pointer
00092       typedef struct gm_parms_s {
00093         func_t *func;
00094         void *vp;
00095       } gm_parms;
00096       
00097 #endif
00098       
00099     };
00100   
00101 #ifndef DOXYGENP
00102 }
00103 #endif
00104 
00105 #endif
00106 

Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.

Project hosting provided by SourceForge.net Logo, O2scl Sourceforge Project Page