ccgsl 2.7.2
C++wrappersforGnuScientificLibrary
monte_miser.hpp
Go to the documentation of this file.
1/*
2 * $Id: monte_miser.hpp 293 2012-12-17 20:27:36Z 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_MISER_HPP
21#define CCGSL_MONTE_MISER_HPP
22
23#include<new>
24#include<gsl/gsl_monte_miser.h>
25#include"monte.hpp"
26
27namespace gsl {
31 namespace monte {
35 namespace miser {
39 class state {
40 public:
45 ccgsl_pointer = 0;
46 count = 0; // initially nullptr will do
47 }
48 // Refines random access container
49 // Refines assignable
54 explicit state( size_t const dim ){
55 ccgsl_pointer = gsl_monte_miser_alloc( dim );
56 // just plausibly we could allocate state but not count
57 try { count = new size_t; } catch( std::bad_alloc& e ){
58 // try to tidy up before rethrowing
59 gsl_monte_miser_free( ccgsl_pointer );
60 throw e;
61 }
62 *count = 1; // initially there is just one reference to ccgsl_pointer
63 }
70 explicit state( gsl_monte_miser_state* v ){
71 ccgsl_pointer = v;
72 // just plausibly we could fail to allocate count: no further action needed.
73 count = new size_t;
74 *count = 1; // initially there is just one reference to ccgsl_pointer
75 }
76 // copy constructor
82 count = v.count; if( count != 0 ) ++*count; }
87 int init(){ return gsl_monte_miser_init( ccgsl_pointer ); }
88 // assignment operator
93 state& operator=( state const& v ){
94 // first, possibly delete anything pointed to by this
95 if( count == 0 or --*count == 0 ){
96 if( ccgsl_pointer != 0 ) gsl_monte_miser_free( ccgsl_pointer );
97 delete count;
98 } // Then copy
99 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count; return *this;
100 }
101 // destructor
106 if( count == 0 or --*count == 0 ){
107 // could have allocated null pointer
108 if( ccgsl_pointer != 0 ) gsl_monte_miser_free( ccgsl_pointer );
109 delete count;
110 }
111 }
112#ifdef __GXX_EXPERIMENTAL_CXX0X__
117 state( state&& v ) : ccgsl_pointer( v.ccgsl_pointer ), count( nullptr ){
118 std::swap( count, v.count );
119 v.ccgsl_pointer = nullptr;
120 }
127 state( std::move( v ) ).swap( *this );
128 return *this;
129 }
130#endif
131 // Refines equality comparable
132 // == operator
139 bool operator==( state const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
140 // != operator
147 bool operator!=( state const& v ) const { return not operator==( v ); }
148 // Refines forward container
149 // Refines less than comparable
150 // operator<
159 bool operator<( state const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
160 // operator>
169 bool operator>( state const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
170 // operator<=
179 bool operator<=( state const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
180 // operator>=
189 bool operator>=( state const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
194 bool empty() const { return ccgsl_pointer == 0; }
195 // swap() --- should work even if sizes don't match
201 void swap( state& v ){
202 std::swap( ccgsl_pointer, v.ccgsl_pointer );
203 std::swap( count, v.count );
204 }
205 private:
209 gsl_monte_miser_state* ccgsl_pointer;
213 size_t* count;
214 public:
215 // shared reference functions
220 gsl_monte_miser_state* get() const { return ccgsl_pointer; }
226 bool unique() const { return count != 0 and *count == 1; }
231 size_t use_count() const { return count == 0 ? 0 : *count; }
237#ifdef __GXX_EXPERIMENTAL_CXX0X__
238 explicit
239#endif
240 operator bool() const { return ccgsl_pointer != 0; }
241 // Access params
246 double get_estimate_frac() const { return ccgsl_pointer->estimate_frac; }
251 size_t get_min_calls() const { return ccgsl_pointer->min_calls; }
257 { return ccgsl_pointer->min_calls_per_bisection; }
262 double get_alpha() const { return ccgsl_pointer->alpha; }
267 double get_dither() const { return ccgsl_pointer->dither; }
272 void set_estimate_frac( double const estimate_frac )
273 { ccgsl_pointer->estimate_frac = estimate_frac; }
278 void set_min_calls( size_t const min_calls ){ ccgsl_pointer->min_calls = min_calls; }
283 void set_min_calls_per_bisection( size_t const min_calls_per_bisection )
284 { ccgsl_pointer->min_calls_per_bisection = min_calls_per_bisection; }
289 void set_alpha( double const alpha ){ ccgsl_pointer->alpha = alpha; }
294 void set_dither( double const dither ){ ccgsl_pointer->dither = dither; }
295 };
301 inline int init( gsl::monte::miser::state& state ){ return gsl_monte_miser_init( state.get() ); }
302#ifndef DOXYGEN_SKIP
316 inline int integrate( gsl::monte::function* f, double const xl[],
317 double const xu[], size_t const dim, size_t const calls,
318 gsl::rng& r, state& state, double* result, double* abserr ){
319 return gsl_monte_miser_integrate( f, xl, xu, dim, calls, r.get(), state.get(),
320 result, abserr ); }
334 template<typename L, typename U>
335 inline int integrate( gsl::monte::function* f, L const& xl,
336 U const& xu, size_t const calls,
337 gsl::rng& r, state& state, double* result, double* abserr ){
338 size_t const dim = xl.size();
339 if( dim != xu.size() )
340 GSL_ERROR( "Mismatch in array lengths", GSL_EBADLEN );
341 if( dim != state.get()->dim )
342 GSL_ERROR( "Array lengths must match dimension of state", GSL_EBADLEN );
343 return gsl_monte_miser_integrate( f, xl.data(), xu.data(), dim, calls,
344 r.get(), state.get(), result, abserr ); }
345#endif
359 template<typename L, typename U>
360 inline int integrate( gsl::monte::function& f, L const& xl,
361 U const& xu, size_t const calls,
362 gsl::rng& r, state& state, double& result, double& abserr ){
363 size_t const dim = xl.size();
364 if( dim != xu.size() )
365 GSL_ERROR( "Mismatch in array lengths", GSL_EBADLEN );
366 if( dim != state.get()->dim )
367 GSL_ERROR( "Array lengths must match dimension of state", GSL_EBADLEN );
368 return gsl_monte_miser_integrate( &f, xl.data(), xu.data(), dim, calls,
369 r.get(), state.get(), &result, &abserr ); }
370 }
371 }
372}
373#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 MISER algorithm.
Definition: monte_miser.hpp:39
bool operator<(state const &v) const
A container needs to define an ordering for sorting.
state(gsl_monte_miser_state *v)
Could construct from a gsl_monte_miser_state.
Definition: monte_miser.hpp:70
void set_estimate_frac(double const estimate_frac)
Set estimate_frac.
state & operator=(state &&v)
Move operator.
size_t get_min_calls_per_bisection() const
Get min_calls_per_bisection.
gsl_monte_miser_state * get() const
Get the gsl_monte_miser_state.
bool empty() const
Find if the state is empty.
bool operator!=(state const &v) const
Two state are different if their elements are not identical.
double get_alpha() const
Get alpha.
~state()
The destructor only deletes the pointers if count reaches zero.
double get_estimate_frac() const
Get estimate_frac.
void set_min_calls_per_bisection(size_t const min_calls_per_bisection)
Set min_calls_per_bisection.
state(size_t const dim)
The default constructor creates a new state with n elements.
Definition: monte_miser.hpp:54
size_t use_count() const
Find how many state objects share this pointer.
int init()
Initialise.
Definition: monte_miser.hpp:87
bool operator>(state const &v) const
A container needs to define an ordering for sorting.
void set_min_calls(size_t const min_calls)
Set min_calls.
bool unique() const
Find if this is the only object sharing the gsl_monte_miser_state.
void set_dither(double const dither)
Set dither.
bool operator<=(state const &v) const
A container needs to define an ordering for sorting.
double get_dither() const
Get dither.
state & operator=(state const &v)
The assignment operator.
Definition: monte_miser.hpp:93
gsl_monte_miser_state * ccgsl_pointer
The shared pointer.
state()
The default constructor is only really useful for assigning to.
Definition: monte_miser.hpp:44
state(state &&v)
Move constructor.
bool operator>=(state const &v) const
A container needs to define an ordering for sorting.
void swap(state &v)
Swap two state objects.
size_t * count
The shared reference count.
size_t get_min_calls() const
Get min_calls.
void set_alpha(double const alpha)
Set alpha.
bool operator==(state const &v) const
Two state are identically equal if their elements are identical.
state(state const &v)
The copy constructor.
Definition: monte_miser.hpp:81
Random number generator.
Definition: rng.hpp:31
gsl_rng * get() const
Get the gsl_rng.
Definition: rng.hpp:525
int init(gsl::monte::miser::state &state)
C++ version of gsl_monte_miser_init().
int integrate(gsl::monte::function &f, L const &xl, U const &xu, size_t const calls, gsl::rng &r, state &state, double &result, double &abserr)
C++ version of gsl_monte_miser_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