ccgsl 2.7.2
C++wrappersforGnuScientificLibrary
monte_vegas.hpp
Go to the documentation of this file.
1/*
2 * $Id: monte_vegas.hpp 236 2012-08-08 19:47:53Z jdl3 $
3 * Copyright (C) 2010, 2011, 2012, 2020 John D Lamb
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20#ifndef CCGSL_MONTE_VEGAS_HPP
21#define CCGSL_MONTE_VEGAS_HPP
22
23#include<gsl/gsl_monte_vegas.h>
24#include"monte.hpp"
25
26namespace gsl {
30 namespace monte {
34 namespace vegas {
38 class state {
39 public:
44 ccgsl_pointer = 0;
45 count = 0; // initially nullptr will do
46 }
47 // Refines random access container
48 // Refines assignable
53 explicit state( size_t const dim ){
54 ccgsl_pointer = gsl_monte_vegas_alloc( dim );
55 // just plausibly we could allocate state but not count
56 try { count = new size_t; } catch( std::bad_alloc& e ){
57 // try to tidy up before rethrowing
58 gsl_monte_vegas_free( ccgsl_pointer );
59 throw e;
60 }
61 *count = 1; // initially there is just one reference to ccgsl_pointer
62 }
69 explicit state( gsl_monte_vegas_state* v ){
70 ccgsl_pointer = v;
71 // just plausibly we could fail to allocate count: no further action needed.
72 count = new size_t;
73 *count = 1; // initially there is just one reference to ccgsl_pointer
74 }
75 // copy constructor
81 count = v.count; if( count != 0 ) ++*count; }
86 int init(){ return gsl_monte_vegas_init( ccgsl_pointer ); }
87 // assignment operator
92 state& operator=( state const& v ){
93 // first, possibly delete anything pointed to by this
94 if( count == 0 or --*count == 0 ){
95 if( ccgsl_pointer != 0 ) gsl_monte_vegas_free( ccgsl_pointer );
96 delete count;
97 } // Then copy
98 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count; return *this;
99 }
100 // destructor
105 if( count == 0 or --*count == 0 ){
106 // could have allocated null pointer
107 if( ccgsl_pointer != 0 ) gsl_monte_vegas_free( ccgsl_pointer );
108 delete count;
109 }
110 }
111#ifdef __GXX_EXPERIMENTAL_CXX0X__
116 state( state&& v ) : ccgsl_pointer( v.ccgsl_pointer ), count( nullptr ){
117 std::swap( count, v.count );
118 v.ccgsl_pointer = nullptr;
119 }
126 state( std::move( v ) ).swap( *this );
127 return *this;
128 }
129#endif
130 // Refines equality comparable
131 // == operator
138 bool operator==( state const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
139 // != operator
146 bool operator!=( state const& v ) const { return not operator==( v ); }
147 // Refines forward container
148 // Refines less than comparable
149 // operator<
158 bool operator<( state const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
159 // operator>
168 bool operator>( state const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
169 // operator<=
178 bool operator<=( state const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
179 // operator>=
188 bool operator>=( state const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
193 bool empty() const { return ccgsl_pointer == 0; }
194 // swap() --- should work even if sizes don't match
200 void swap( state& v ){
201 std::swap( ccgsl_pointer, v.ccgsl_pointer );
202 std::swap( count, v.count );
203 }
204 private:
208 gsl_monte_vegas_state* ccgsl_pointer;
212 size_t* count;
213 public:
214 // shared reference functions
219 gsl_monte_vegas_state* get() const { return ccgsl_pointer; }
225 bool unique() const { return count != 0 and *count == 1; }
230 size_t use_count() const { return count == 0 ? 0 : *count; }
236#ifdef __GXX_EXPERIMENTAL_CXX0X__
237 explicit
238#endif
239 operator bool() const { return ccgsl_pointer != 0; }
240 // Access params
245 int get_stage() const { return ccgsl_pointer->stage; }
250 size_t get_iterations() const { return ccgsl_pointer->iterations; }
255 size_t get_mode() const { return ccgsl_pointer->mode; }
260 double get_alpha() const { return ccgsl_pointer->alpha; }
265 int get_verbose() const { return ccgsl_pointer->verbose; }
270 FILE* get_ostream() const { return ccgsl_pointer->ostream; }
275 void set_stage( int const stage ){ ccgsl_pointer->stage = stage; }
280 void set_iterations( size_t const iterations ){ ccgsl_pointer->iterations = iterations; }
285 void set_mode( int const mode ){ ccgsl_pointer->mode = mode; }
290 void set_alpha( double const alpha ){ ccgsl_pointer->alpha = alpha; }
295 void set_verbose( int const verbose ){ ccgsl_pointer->verbose = verbose; }
300 void set_ostream( FILE* ostream ){ ccgsl_pointer->ostream = ostream; }
301 // Other functions
306 double chisq() const { return gsl_monte_vegas_chisq( ccgsl_pointer ); }
313 void runval( double* result, double* sigma ){
314 gsl_monte_vegas_runval( ccgsl_pointer, result, sigma ); }
321 void runval( double& result, double& sigma ){
322 gsl_monte_vegas_runval( ccgsl_pointer, &result, &sigma ); }
323 };
329 inline int init( gsl::monte::vegas::state& state ){ return gsl_monte_vegas_init( state.get() ); }
330#ifndef DOXYGEN_SKIP
344 inline int integrate( gsl::monte::function* f, double xl[],
345 double xu[], size_t const dim, size_t const calls,
346 gsl::rng& r, state& state, double* result, double* abserr ){
347 return gsl_monte_vegas_integrate( f, xl, xu, dim, calls, r.get(), state.get(),
348 result, abserr ); }
362 template<typename L, typename U>
363 inline int integrate( gsl::monte::function* f, L& xl,
364 U& xu, size_t const calls,
365 gsl::rng& r, state& state, double* result, double* abserr ){
366 size_t const dim = xl.size();
367 if( dim != xu.size() )
368 GSL_ERROR( "Mismatch in array lengths", GSL_EBADLEN );
369 if( dim != state.get()->dim )
370 GSL_ERROR( "Array lengths must match dimension of state", GSL_EBADLEN );
371 return gsl_monte_vegas_integrate( f, xl.data(), xu.data(), dim, calls,
372 r.get(), state.get(), result, abserr ); }
373#endif
387 template<typename L, typename U>
388 inline int integrate( gsl::monte::function& f, L& xl,
389 U& xu, size_t const calls,
390 gsl::rng& r, state& state, double& result, double& abserr ){
391 size_t const dim = xl.size();
392 if( dim != xu.size() )
393 GSL_ERROR( "Mismatch in array lengths", GSL_EBADLEN );
394 if( dim != state.get()->dim )
395 GSL_ERROR( "Array lengths must match dimension of state", GSL_EBADLEN );
396 return gsl_monte_vegas_integrate( &f, xl.data(), xu.data(), dim, calls,
397 r.get(), state.get(), &result, &abserr ); }
398 }
399 }
400}
401#endif
Class that extends gsl_monte_function so that it can be constructed from arbitrary function objects.
Definition: monte.hpp:59
Workspace for Monte Carlo integration using the VEGAS algorithm.
Definition: monte_vegas.hpp:38
int get_stage() const
Get stage.
double get_alpha() const
Get alpha.
state(size_t const dim)
The default constructor creates a new state with n elements.
Definition: monte_vegas.hpp:53
bool empty() const
Find if the state is empty.
state(gsl_monte_vegas_state *v)
Could construct from a gsl_monte_vegas_state.
Definition: monte_vegas.hpp:69
bool operator!=(state const &v) const
Two state are different if their elements are not identical.
state & operator=(state const &v)
The assignment operator.
Definition: monte_vegas.hpp:92
bool operator>=(state const &v) const
A container needs to define an ordering for sorting.
void swap(state &v)
Swap two state objects.
bool operator<=(state const &v) const
A container needs to define an ordering for sorting.
state()
The default constructor is only really useful for assigning to.
Definition: monte_vegas.hpp:43
state(state &&v)
Move constructor.
state(state const &v)
The copy constructor.
Definition: monte_vegas.hpp:80
gsl_monte_vegas_state * ccgsl_pointer
The shared pointer.
size_t use_count() const
Find how many state objects share this pointer.
void set_alpha(double const alpha)
Set alpha.
FILE * get_ostream() const
Get ostream.
void set_mode(int const mode)
Set mode.
double chisq() const
Get chi-squared value: this should be close to 1.
void set_verbose(int const verbose)
Set verbose.
size_t get_mode() const
Get mode.
bool operator<(state const &v) const
A container needs to define an ordering for sorting.
~state()
The destructor only deletes the pointers if count reaches zero.
int get_verbose() const
Get verbose.
bool operator==(state const &v) const
Two state are identically equal if their elements are identical.
void runval(double &result, double &sigma)
Returns the raw (unaveraged) values of the integral result and its error sigma from the most recent i...
void runval(double *result, double *sigma)
Returns the raw (unaveraged) values of the integral result and its error sigma from the most recent i...
void set_ostream(FILE *ostream)
Set ostream.
gsl_monte_vegas_state * get() const
Get the gsl_monte_vegas_state.
void set_stage(int const stage)
Set stage.
state & operator=(state &&v)
Move operator.
size_t * count
The shared reference count.
int init()
Initialise.
Definition: monte_vegas.hpp:86
bool operator>(state const &v) const
A container needs to define an ordering for sorting.
void set_iterations(size_t const iterations)
Set iterations.
bool unique() const
Find if this is the only object sharing the gsl_monte_vegas_state.
size_t get_iterations() const
Get iterations.
Random number generator.
Definition: rng.hpp:31
gsl_rng * get() const
Get the gsl_rng.
Definition: rng.hpp:525
int init(gsl::monte::vegas::state &state)
C++ version of gsl_monte_vegas_init().
int integrate(gsl::monte::function &f, L &xl, U &xu, size_t const calls, gsl::rng &r, state &state, double &result, double &abserr)
C++ version of gsl_monte_vegas_integrate().
gsl_sf_result result
Typedef for gsl_sf_result.
Definition: sf_result.hpp:30
The gsl package creates an interface to the GNU Scientific Library for C++.
Definition: blas.hpp:34