KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsl_qrng.h
Go to the documentation of this file.
1 
9 /*
10  * $Id: gsl_qrng.h 17393 2014-08-23 20:19:14Z predoehl $
11  */
12 
13 #ifndef GSL_QRNG_WRAP_H_KJBLIB_UARIZONAVISION
14 #define GSL_QRNG_WRAP_H_KJBLIB_UARIZONAVISION
15 
16 #include <l_cpp/l_exception.h>
17 #include <m_cpp/m_vector.h>
18 #include <gsl_cpp/gsl_util.h>
19 
20 #ifdef KJB_HAVE_GSL
21 #include "gsl/gsl_qrng.h"
22 #endif
23 
24 
25 namespace kjb {
26 
27 #ifndef KJB_HAVE_GSL
28 #warning "Compiling GNU GSL wrapper without GNU GSL; it will not run properly"
29 typedef void gsl_qrng_type;
30 typedef void gsl_qrng;
35 #endif
36 
37 
38 enum {
43 };
44 
45 
62 template< unsigned KIND >
64 
65  unsigned m_dimensions;
66 
67  gsl_qrng *m_qrng;
68 
69 public:
70 
80  unsigned dimensions,
81  const gsl_qrng_type* qtype,
82  unsigned maxdim
83  )
84 #ifdef KJB_HAVE_GSL
85  : m_dimensions( dimensions ),
86  m_qrng( m_dimensions ? gsl_qrng_alloc( qtype, m_dimensions ) : 00 )
87  {
88  ETX_2( 0 == m_dimensions, "Gsl_Qrng primitive ctor: zero dimensions" );
89  ETX_2( 00 == m_qrng, "Gsl_Qrng primitive ctor: allocation failed" );
90  ETX_2( dimensions > maxdim, "Gsl_Qrng ctor: too many dimensions" );
91  gsl_qrng_init( m_qrng );
92  }
93 #else
94  {
95  KJB_THROW_2( Missing_dependency, "GNU GSL" );
96  }
97 #endif
98 
100  {
101 #ifdef KJB_HAVE_GSL
102  gsl_qrng_free( m_qrng );
103 #endif
104  }
105 
112  void read( double* destination )
113  {
114 #ifdef KJB_HAVE_GSL
115  ETX_2( 00 == destination, "Call to Gsl_Qrng::read( NULL )" );
116  GSL_ETX( gsl_qrng_get( m_qrng, destination ) );
117 #endif
118  }
119 
122  {
123  std::vector< double > sample( get_dimensions() );
124  read( & sample[ 0 ] ); // Herb Sutter says this is legitimate
125  return Vector( sample.begin(), sample.end() );
126  }
127 
128  const char* name() const
129  {
130 #ifdef KJB_HAVE_GSL
131  return gsl_qrng_name( m_qrng );
132 #endif
133  }
134 
135  unsigned get_dimensions() const
136  {
137  return m_dimensions;
138  }
139 
142 #ifdef KJB_HAVE_GSL
143  : m_dimensions( that.get_dimensions() ),
144  m_qrng( gsl_qrng_clone( that.m_qrng ) )
145  {
146  ETX_2( 0 == m_dimensions, "Gsl_Qrng copy ctor: zero dimensions" );
147  ETX_2( 00 == m_qrng, "Gsl_Qrng copy ctor: allocation failed" );
148  }
149 #else
150  {}
151 #endif
152 
155  {
156 #ifdef KJB_HAVE_GSL
157  using std::swap;
158 
159  swap( m_dimensions, that.m_dimensions );
160  swap( m_qrng, that.m_qrng );
161 #endif
162  }
163 
166  {
167 #ifdef KJB_HAVE_GSL
168  if ( this != &that )
169  {
170  GSL_ETX( gsl_qrng_memcpy( m_qrng, that.m_qrng ) );
171  }
172 #endif
173  return *this;
174  }
175 
176 };
177 
178 
188 class Gsl_Qrng_Niederreiter : public Gsl_Qrng_basic< GSL_QRNG_NIEDER > {
189 public:
193  {}
194 };
195 
205 class Gsl_Qrng_Sobol : public Gsl_Qrng_basic< GSL_QRNG_SOBOL > {
206 public:
208  Gsl_Qrng_Sobol( unsigned dimensions )
209  : Gsl_Qrng_basic< GSL_QRNG_SOBOL >( dimensions, gsl_qrng_sobol, 40 )
210  {}
211 };
212 
222 class Gsl_Qrng_Halton : public Gsl_Qrng_basic< GSL_QRNG_HALTON > {
223 public:
225  Gsl_Qrng_Halton( unsigned dimensions )
226  : Gsl_Qrng_basic< GSL_QRNG_HALTON >( dimensions, gsl_qrng_halton, 1229 )
227  {}
228 };
229 
241 class Gsl_Qrng_Rvs_Halton : public Gsl_Qrng_basic< GSL_QRNG_RVSHALTON > {
242 public:
244  Gsl_Qrng_Rvs_Halton( unsigned d )
246  {}
247 };
248 
249 
250 } // namespace kjb
251 
252 namespace std {
253 
255  template< unsigned KIND >
256  inline void swap(
259  )
260  {
261  m1.swap( m2 );
262  }
263 }
264 
265 
266 #endif /* GSL_QRNG_WRAP_H_KJBLIB_UARIZONAVISION */
Vector read()
Best way to read a single random sample of known dimensionality.
Definition: gsl_qrng.h:121
gsl_qrng_type * gsl_qrng_sobol
Definition: gsl_qrng.h:31
Quasi-random generator using the algorithm of Bratley et al.
Definition: gsl_qrng.h:188
Definition: gsl_qrng.h:40
Gsl_Qrng_Niederreiter(unsigned dims)
ctor for Niederreiter QRNG of given number of dimensions dims
Definition: gsl_qrng.h:191
Gsl_Qrng_Halton(unsigned dimensions)
ctor for Halton QRNG of given number of dimensions
Definition: gsl_qrng.h:225
void gsl_qrng
Definition: gsl_qrng.h:30
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
GSL utility stuff to help those using the C++ wrapper on GSL code.
Vector sample(const MV_gaussian_distribution &dist)
Sample from a multivariate normal distribution.
Definition: prob_sample.cpp:42
Quasi-random generator using the algorithm of Halton.
Definition: gsl_qrng.h:222
Gsl_Qrng_basic< KIND > & operator=(const Gsl_Qrng_basic< KIND > &that)
assignment operator
Definition: gsl_qrng.h:165
size_t dims(const Scene &scene, bool respect_changed=true, bool infer_head=true)
Computes the number of variables in this scene.
Definition: pt_scene.cpp:106
gsl_qrng_type * gsl_qrng_reversehalton
Definition: gsl_qrng.h:31
gsl_qrng_type * gsl_qrng_halton
Definition: gsl_qrng.h:31
const char * name() const
Definition: gsl_qrng.h:128
Definition: gsl_qrng.h:39
Gsl_Qrng_basic(const Gsl_Qrng_basic< KIND > &that)
copy ctor
Definition: gsl_qrng.h:141
Gsl_Qrng_Sobol(unsigned dimensions)
ctor for Sobol QRNG of given number of dimensions
Definition: gsl_qrng.h:208
Quasi-random generator using the algorithm of Antonov and Saleev.
Definition: gsl_qrng.h:205
#define GSL_ETX(gsl_expr)
Definition: gsl_util.h:37
Gsl_Qrng_basic(unsigned dimensions, const gsl_qrng_type *qtype, unsigned maxdim)
ctor builds the quasi-random generator
Definition: gsl_qrng.h:79
void swap(Gsl_Qrng_basic< KIND > &that)
swap two generators
Definition: gsl_qrng.h:154
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
void swap(kjb::Gsl_Multimin_fdf &m1, kjb::Gsl_Multimin_fdf &m2)
Swap two wrapped multimin objects.
Definition: gsl_multimin.h:693
Gsl_Qrng_Rvs_Halton(unsigned d)
ctor for Reverse Halton QRNG of dimensionality d
Definition: gsl_qrng.h:244
~Gsl_Qrng_basic()
Definition: gsl_qrng.h:99
#define ETX_2(a, msg)
Definition: l_exception.h:78
unsigned get_dimensions() const
Definition: gsl_qrng.h:135
Wrapper for one of GSL's quasi-random generators.
Definition: gsl_qrng.h:63
gsl_qrng_type * gsl_qrng_niederreiter_2
Definition: gsl_qrng.h:31
Support for error handling exception classes in libKJB.
Object thrown when a program lacks required resources or libraries.
Definition: l_exception.h:539
void gsl_qrng_type
Definition: gsl_qrng.h:29
Definition: gsl_qrng.h:41
Quasi-random generator using the algorithm of Vandewoestyne et al.
Definition: gsl_qrng.h:241
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...
void read(double *destination)
The old-fashioned way to read a quasi-random sample.
Definition: gsl_qrng.h:112
Definition: gsl_qrng.h:42