ccgsl 2.7.2
C++wrappersforGnuScientificLibrary
rng.hpp
Go to the documentation of this file.
1/*
2 * $Id: rng.hpp 293 2012-12-17 20:27:36Z jdl3 $
3 * Copyright (C) 2010, 2011 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_RNG_HPP
21#define CCGSL_RNG_HPP
22
23#include<new>
24#include<algorithm>
25#include<gsl/gsl_rng.h>
26
27namespace gsl {
31 class rng {
32 public:
36 typedef gsl_rng_type const* type;
41 static type const borosh13(){ return gsl_rng_borosh13; }
46 static type const coveyou(){ return gsl_rng_coveyou; }
51 static type const cmrg(){ return gsl_rng_cmrg; }
56 static type const fishman18(){ return gsl_rng_fishman18; }
61 static type const fishman20(){ return gsl_rng_fishman20; }
66 static type const fishman2x(){ return gsl_rng_fishman2x; }
71 static type const gfsr4(){ return gsl_rng_gfsr4; }
76 static type const knuthran(){ return gsl_rng_knuthran; }
81 static type const knuthran2(){ return gsl_rng_knuthran2; }
86 static type const knuthran2002(){ return gsl_rng_knuthran2002; }
91 static type const lecuyer21(){ return gsl_rng_lecuyer21; }
96 static type const minstd(){ return gsl_rng_minstd; }
101 static type const mrg(){ return gsl_rng_mrg; }
106 static type const mt19937(){ return gsl_rng_mt19937; }
111 static type const mt19937_1999(){ return gsl_rng_mt19937_1999; }
116 static type const mt19937_1998(){ return gsl_rng_mt19937_1998; }
121 static type const r250(){ return gsl_rng_r250; }
126 static type const ran0(){ return gsl_rng_ran0; }
131 static type const ran1(){ return gsl_rng_ran1; }
136 static type const ran2(){ return gsl_rng_ran2; }
141 static type const ran3(){ return gsl_rng_ran3; }
146 static type const rand(){ return gsl_rng_rand; }
151 static type const rand48(){ return gsl_rng_rand48; }
156 static type const random128_bsd(){ return gsl_rng_random128_bsd; }
161 static type const random128_glibc2(){ return gsl_rng_random128_glibc2; }
166 static type const random128_libc5(){ return gsl_rng_random128_libc5; }
171 static type const random256_bsd(){ return gsl_rng_random256_bsd; }
176 static type const random256_glibc2(){ return gsl_rng_random256_glibc2; }
181 static type const random256_libc5(){ return gsl_rng_random256_libc5; }
186 static type const random32_bsd(){ return gsl_rng_random32_bsd; }
191 static type const random32_glibc2(){ return gsl_rng_random32_glibc2; }
196 static type const random32_libc5(){ return gsl_rng_random32_libc5; }
201 static type const random64_bsd(){ return gsl_rng_random64_bsd; }
206 static type const random64_glibc2(){ return gsl_rng_random64_glibc2; }
211 static type const random64_libc5(){ return gsl_rng_random64_libc5; }
216 static type const random8_bsd(){ return gsl_rng_random8_bsd; }
221 static type const random8_glibc2(){ return gsl_rng_random8_glibc2; }
226 static type const random8_libc5(){ return gsl_rng_random8_libc5; }
231 static type const random_bsd(){ return gsl_rng_random_bsd; }
236 static type const random_glibc2(){ return gsl_rng_random_glibc2; }
241 static type const random_libc5(){ return gsl_rng_random_libc5; }
246 static type const randu(){ return gsl_rng_randu; }
251 static type const ranf(){ return gsl_rng_ranf; }
256 static type const ranlux(){ return gsl_rng_ranlux; }
261 static type const ranlux389(){ return gsl_rng_ranlux389; }
266 static type const ranlxd1(){ return gsl_rng_ranlxd1; }
271 static type const ranlxd2(){ return gsl_rng_ranlxd2; }
276 static type const ranlxs0(){ return gsl_rng_ranlxs0; }
281 static type const ranlxs1(){ return gsl_rng_ranlxs1; }
286 static type const ranlxs2(){ return gsl_rng_ranlxs2; }
291 static type const ranmar(){ return gsl_rng_ranmar; }
296 static type const slatec(){ return gsl_rng_slatec; }
301 static type const taus(){ return gsl_rng_taus; }
306 static type const taus2(){ return gsl_rng_taus2; }
311 static type const taus113(){ return gsl_rng_taus113; }
316 static type const transputer(){ return gsl_rng_transputer; }
321 static type const tt800(){ return gsl_rng_tt800; }
326 static type const uni(){ return gsl_rng_uni; }
331 static type const uni32(){ return gsl_rng_uni32; }
336 static type const vax(){ return gsl_rng_vax; }
341 static type const waterman14(){ return gsl_rng_waterman14; }
346 static type const zuf(){ return gsl_rng_zuf; }
351 static type const Default(){ return gsl_rng_default; }
352 public:
357 ccgsl_pointer = 0;
358 count = 0; // initially nullptr will do
359 }
360 // Refines assignable
365 explicit rng( gsl_rng_type const* T ){
366 ccgsl_pointer = gsl_rng_alloc( T );
367 // just plausibly we could allocate mathieu_rng but not count
368 try { count = new size_t; } catch( std::bad_alloc& e ){
369 // try to tidy up before rethrowing
370 gsl_rng_free( ccgsl_pointer );
371 throw e;
372 }
373 *count = 1; // initially there is just one reference to ccgsl_pointer
374 }
380 explicit rng( gsl_rng* v ){
381 ccgsl_pointer = v;
382 // just plausibly we could fail to allocate count: no further action needed.
383 count = new size_t;
384 *count = 1; // initially there is just one reference to ccgsl_pointer
385 }
386 // copy constructor
392 count = v.count; if( count != 0 ) ++*count; }
393 // assignment operator
398 rng& operator=( rng const& v ){
399 // first, possibly delete anything pointed to by this
400 if( count == 0 or --*count == 0 ){
401 if( ccgsl_pointer != 0 ) gsl_rng_free( ccgsl_pointer );
402 delete count;
403 } // Then copy
404 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count; return *this;
405 }
406 // destructor
411 if( count == 0 or --*count == 0 ){
412 // could have allocated null pointer
413 if( ccgsl_pointer != 0 ) gsl_rng_free( ccgsl_pointer );
414 delete count;
415 }
416 }
417 // Refines equality comparable
418#ifdef __GXX_EXPERIMENTAL_CXX0X__
423 rng( rng&& v ) : ccgsl_pointer( v.ccgsl_pointer ), count( nullptr ){
424 std::swap( count, v.count );
425 v.ccgsl_pointer = nullptr;
426 }
433 rng( std::move( v ) ).swap( *this );
434 return *this;
435 }
436#endif
437 // == operator
444 bool operator==( rng const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
445 // != operator
452 bool operator!=( rng const& v ) const { return not operator==( v ); }
453 // Refines forward container
454 // Refines less than comparable
455 // operator<
464 bool operator<( rng const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
465 // operator>
474 bool operator>( rng const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
475 // operator<=
484 bool operator<=( rng const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
485 // operator>=
494 bool operator>=( rng const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
499 bool empty() const { return ccgsl_pointer == 0; }
500 // swap() --- should work even if sizes don't match
506 void swap( rng& v ){
507 std::swap( ccgsl_pointer, v.ccgsl_pointer );
508 std::swap( count, v.count );
509 }
510 private:
518 size_t* count;
519 public:
520 // shared reference functions
525 gsl_rng* get() const { return ccgsl_pointer; }
531 bool unique() const { return count != 0 and *count == 1; }
536 size_t use_count() const { return count == 0 ? 0 : *count; }
542#ifdef __GXX_EXPERIMENTAL_CXX0X__
543 explicit
544#endif
545 operator bool() const { return ccgsl_pointer != 0; }
546 // Functions
551 static type* types_setup(){ return gsl_rng_types_setup(); }
557 int memcpy( rng const& src ){ return gsl_rng_memcpy( get(), src.get() ); }
562 rng clone() const { return rng( gsl_rng_clone( get() ) ); }
567 void set( unsigned long int seed ) const { gsl_rng_set( get(), seed ); }
573 unsigned long int max() const { return gsl_rng_max( get() ); }
578 unsigned long int min() const { return gsl_rng_min( get() ); }
583 char const* name() const { return gsl_rng_name( get() ); }
589 int fread( FILE* stream ){ return gsl_rng_fread( stream, get() ); }
595 int fwrite( FILE* stream ) const { return gsl_rng_fwrite( stream, get() ); }
600 size_t size() const { return gsl_rng_size( get() ); }
605 void* state() const { return gsl_rng_state( get() ); }
609 void print_state() const { gsl_rng_print_state( get() ); }
614 static type env_setup(){ return gsl_rng_env_setup(); }
619 unsigned long int get_value() const { return gsl_rng_get( get() ); }
624 double uniform() const { return gsl_rng_uniform( get() ); }
629 double uniform_pos() const { return gsl_rng_uniform_pos( get() ); }
635 unsigned long int uniform_int( unsigned long int n ) const {
636 return gsl_rng_uniform_int( get(), n ); }
637 };
638}
639
640#endif
Random number generator.
Definition: rng.hpp:31
static type const fishman18()
Random number static type.
Definition: rng.hpp:56
static type const knuthran()
Random number static type.
Definition: rng.hpp:76
static type const borosh13()
Random number static type.
Definition: rng.hpp:41
static type const vax()
Random number static type.
Definition: rng.hpp:336
gsl_rng * get() const
Get the gsl_rng.
Definition: rng.hpp:525
static type const random32_libc5()
Random number static type.
Definition: rng.hpp:196
static type const rand()
Random number static type.
Definition: rng.hpp:146
static type const random32_glibc2()
Random number static type.
Definition: rng.hpp:191
static type const random256_libc5()
Random number static type.
Definition: rng.hpp:181
static type const mt19937_1998()
Random number static type.
Definition: rng.hpp:116
bool operator<=(rng const &v) const
A container needs to define an ordering for sorting.
Definition: rng.hpp:484
double uniform_pos() const
C++ version of gsl_rng_uniform_pos().
Definition: rng.hpp:629
size_t size() const
C++ version of gsl_rng_size().
Definition: rng.hpp:600
static type const random128_libc5()
Random number static type.
Definition: rng.hpp:166
static type const slatec()
Random number static type.
Definition: rng.hpp:296
size_t use_count() const
Find how many rng objects share this pointer.
Definition: rng.hpp:536
static type const random_glibc2()
Random number static type.
Definition: rng.hpp:236
void * state() const
C++ version of gsl_rng_state().
Definition: rng.hpp:605
static type const randu()
Random number static type.
Definition: rng.hpp:246
rng(gsl_rng *v)
Could construct from a gsl_rng.
Definition: rng.hpp:380
static type const zuf()
Random number static type.
Definition: rng.hpp:346
bool operator!=(rng const &v) const
Two rng are different equal if their elements are not identical.
Definition: rng.hpp:452
static type const fishman2x()
Random number static type.
Definition: rng.hpp:66
static type const uni()
Random number static type.
Definition: rng.hpp:326
rng clone() const
C++ version of gsl_rng_clone().
Definition: rng.hpp:562
static type const ranmar()
Random number static type.
Definition: rng.hpp:291
static type const random_bsd()
Random number static type.
Definition: rng.hpp:231
rng(gsl_rng_type const *T)
C++ version of gsl_rng_alloc().
Definition: rng.hpp:365
static type const taus113()
Random number static type.
Definition: rng.hpp:311
static type const lecuyer21()
Random number static type.
Definition: rng.hpp:91
void swap(rng &v)
Swap two rng.
Definition: rng.hpp:506
int fread(FILE *stream)
C++ version of gsl_rng_fread().
Definition: rng.hpp:589
static type const random256_bsd()
Random number static type.
Definition: rng.hpp:171
static type const random128_bsd()
Random number static type.
Definition: rng.hpp:156
char const * name() const
C++ version of gsl_rng_name().
Definition: rng.hpp:583
gsl_rng * ccgsl_pointer
The shared pointer.
Definition: rng.hpp:514
unsigned long int min() const
C++ version of gsl_rng_min().
Definition: rng.hpp:578
static type const random32_bsd()
Random number static type.
Definition: rng.hpp:186
static type const ran0()
Random number static type.
Definition: rng.hpp:126
rng & operator=(rng &&v)
Move operator.
Definition: rng.hpp:432
unsigned long int get_value() const
C++ version of gsl_rng_get().
Definition: rng.hpp:619
static type const cmrg()
Random number static type.
Definition: rng.hpp:51
static type const ranlxs2()
Random number static type.
Definition: rng.hpp:286
unsigned long int uniform_int(unsigned long int n) const
C++ version of gsl_rng_uniform_int().
Definition: rng.hpp:635
bool operator<(rng const &v) const
A container needs to define an ordering for sorting.
Definition: rng.hpp:464
static type const mrg()
Random number static type.
Definition: rng.hpp:101
static type env_setup()
C++ version of gsl_rng_env_setup().
Definition: rng.hpp:614
bool operator==(rng const &v) const
Two rng are identically equal if their elements are identical.
Definition: rng.hpp:444
static type const ran1()
Random number static type.
Definition: rng.hpp:131
static type const uni32()
Random number static type.
Definition: rng.hpp:331
static type const random128_glibc2()
Random number static type.
Definition: rng.hpp:161
static type const ranlxs1()
Random number static type.
Definition: rng.hpp:281
static type const waterman14()
Random number static type.
Definition: rng.hpp:341
static type const mt19937()
Random number static type.
Definition: rng.hpp:106
static type const ran2()
Random number static type.
Definition: rng.hpp:136
bool unique() const
Find if this is the only object sharing the gsl_rng.
Definition: rng.hpp:531
static type const taus()
Random number static type.
Definition: rng.hpp:301
static type const knuthran2()
Random number static type.
Definition: rng.hpp:81
unsigned long int max() const
C++ version of gsl_rng_max().
Definition: rng.hpp:573
static type const Default()
Random number static type.
Definition: rng.hpp:351
static type const rand48()
Random number static type.
Definition: rng.hpp:151
static type const ranlxd2()
Random number static type.
Definition: rng.hpp:271
double uniform() const
C++ version of gsl_rng_uniform().
Definition: rng.hpp:624
static type const r250()
Random number static type.
Definition: rng.hpp:121
static type const random64_libc5()
Random number static type.
Definition: rng.hpp:211
bool empty() const
Find if the rng is empty.
Definition: rng.hpp:499
static type const transputer()
Random number static type.
Definition: rng.hpp:316
static type const ranlxs0()
Random number static type.
Definition: rng.hpp:276
rng()
The default constructor is only really useful for assigning to.
Definition: rng.hpp:356
static type const random8_bsd()
Random number static type.
Definition: rng.hpp:216
size_t * count
The shared reference count.
Definition: rng.hpp:518
static type const random8_glibc2()
Random number static type.
Definition: rng.hpp:221
bool operator>(rng const &v) const
A container needs to define an ordering for sorting.
Definition: rng.hpp:474
void print_state() const
C++ version of gsl_rng_print_state().
Definition: rng.hpp:609
static type const gfsr4()
Random number static type.
Definition: rng.hpp:71
static type const random8_libc5()
Random number static type.
Definition: rng.hpp:226
rng(rng &&v)
Move constructor.
Definition: rng.hpp:423
static type const ranf()
Random number static type.
Definition: rng.hpp:251
static type const random64_bsd()
Random number static type.
Definition: rng.hpp:201
static type const fishman20()
Random number static type.
Definition: rng.hpp:61
rng(rng const &v)
The copy constructor.
Definition: rng.hpp:391
static type const knuthran2002()
Random number static type.
Definition: rng.hpp:86
static type const ranlux389()
Random number static type.
Definition: rng.hpp:261
static type const random256_glibc2()
Random number static type.
Definition: rng.hpp:176
static type * types_setup()
C++ version of gsl_rng_types_setup().
Definition: rng.hpp:551
static type const tt800()
Random number static type.
Definition: rng.hpp:321
~rng()
The destructor only deletes the pointers if count reaches zero.
Definition: rng.hpp:410
static type const coveyou()
Random number static type.
Definition: rng.hpp:46
static type const ran3()
Random number static type.
Definition: rng.hpp:141
void set(unsigned long int seed) const
C++ version of gsl_rng_set().
Definition: rng.hpp:567
static type const random_libc5()
Random number static type.
Definition: rng.hpp:241
static type const ranlxd1()
Random number static type.
Definition: rng.hpp:266
static type const ranlux()
Random number static type.
Definition: rng.hpp:256
static type const taus2()
Random number static type.
Definition: rng.hpp:306
bool operator>=(rng const &v) const
A container needs to define an ordering for sorting.
Definition: rng.hpp:494
static type const minstd()
Random number static type.
Definition: rng.hpp:96
static type const mt19937_1999()
Random number static type.
Definition: rng.hpp:111
rng & operator=(rng const &v)
The assignment operator.
Definition: rng.hpp:398
int memcpy(rng const &src)
C++ version of gsl_rng_memcpy().
Definition: rng.hpp:557
static type const random64_glibc2()
Random number static type.
Definition: rng.hpp:206
gsl_rng_type const * type
Define gsl::rng::static type.
Definition: rng.hpp:36
int fwrite(FILE *stream) const
C++ version of gsl_rng_fwrite().
Definition: rng.hpp:595
size_t n(workspace const &w)
C++ version of gsl_rstat_n().
Definition: rstat.hpp:299
The gsl package creates an interface to the GNU Scientific Library for C++.
Definition: blas.hpp:34