USML
randgen.h
1 
5 #ifndef USML_UBLAS_RANDGEN_H
6 #define USML_UBLAS_RANDGEN_H
7 
8 #include <complex>
9 #include <boost/numeric/ublas/vector.hpp>
10 #include <boost/numeric/ublas/matrix.hpp>
11 #include <boost/numeric/ublas/io.hpp>
12 #include <boost/random.hpp>
13 #include <usml/usml_config.h>
14 
15 namespace usml {
16 namespace ublas {
17 
18  using std::cout ;
19  using std::operator<< ;
20  using std::endl ;
21  using std::complex ;
22  using namespace boost::numeric::ublas ;
23 
24  using boost::numeric::ublas::vector;
25 
28 
40  class randgen {
41 
42  public:
43 
49  typedef boost::kreutzer1986 random_type;
50 
51  private:
52 
57  static USML_DECLSPEC random_type master_gen;
58 
60  static USML_DECLSPEC boost::uniform_01 < random_type, double > uniform_gen;
61 
63  static USML_DECLSPEC boost::normal_distribution<double> gaussian_dist ;
64 
66  static USML_DECLSPEC boost::variate_generator< random_type,
67  boost::normal_distribution<double> > gaussian_gen;
68 
69  //******************************************************************
70  // Utility routines
71 
79  template < class T, class Random >
80  static inline void fill(vector< T > & result, Random & rand) {
81  const int N = result.size();
82  double * current = (double *) & result(0);
83  double * end = (double *) & result(N - 1);
84  while (current <= end) {
85  * current++ = rand();
86  }
87  }
88 
96  template < class T, class Random >
97  static inline void fill(matrix< T > & result, Random & rand) {
98  const int N = result.size1();
99  const int M = result.size2();
100  double * current = (double *) & result(0, 0);
101  double * end = (double *) & result(N - 1, M - 1);
102  while (current <= end) {
103  * current++ = rand();
104  }
105  }
106 
110  randgen() {
111  }
112 
113  public:
114 
122  static inline void seed(random_type::result_type value) {
123  master_gen.seed(value);
124  }
125 
126  //******************************************************************
127  // Scalar random numbers
128 
130  static inline double uniform() {
131  return uniform_gen();
132  }
133 
135  static inline double gaussian() {
136  return gaussian_gen();
137  }
138 
139  //******************************************************************
140  // Vector random numbers
141 
147  static inline vector<double> uniform(int N) {
148  vector<double> result(N);
149  fill(result, uniform_gen);
150  return result;
151  }
152 
158  static inline vector<double> gaussian(int N) {
159  vector<double> result(N);
160  fill(result, gaussian_gen);
161  return result;
162  }
163 
170  static inline vector< complex<double> > noise(int N) {
171  vector< complex<double> > result(N);
172  fill(result, gaussian_gen);
173  return result;
174  }
175 
176  //******************************************************************
177  // Matrix random numbers
178 
185  static inline matrix<double> uniform(int N, int M) {
186  matrix<double> result(N, M);
187  fill(result, uniform_gen);
188  return result;
189  }
190 
197  static inline matrix<double> gaussian(int N, int M) {
198  matrix<double> result(N, M);
199  fill(result, gaussian_gen);
200  return result;
201  }
202 
210  static inline matrix< complex<double> > noise(int N, int M) {
211  matrix< complex<double> > result(N, M);
212  fill(result, gaussian_gen);
213  return result;
214  }
215 
216  };
217 
219 } // end of ublas namespace
220 } // end of usml namespace
221 
222 #endif
Singleton class for integrating the Boost Random Number Library with uBLAS vectors and matrices...
Definition: randgen.h:40
static double uniform()
Generate a single random number from a Uniform distribution.
Definition: randgen.h:130
static void fill(matrix< T > &result, Random &rand)
Quickly fill a matrix with random numbers.
Definition: randgen.h:97
static double gaussian()
Generate a single random number from a Gaussian distribution.
Definition: randgen.h:135
static vector< double > gaussian(int N)
Generate a vector of random numbers from a Gaussian distribution.
Definition: randgen.h:158
static void seed(random_type::result_type value)
Initialize the random number generator with a specific seed.
Definition: randgen.h:122
randgen()
Don't allow users to construct this class.
Definition: randgen.h:110
static matrix< double > uniform(int N, int M)
Generate a matrix of random numbers from a Uniform distribution.
Definition: randgen.h:185
static void fill(vector< T > &result, Random &rand)
Quickly fill a vector with random numbers.
Definition: randgen.h:80
static vector< double > uniform(int N)
Generate a vector of random numbers from a Uniform distribution.
Definition: randgen.h:147
static vector< complex< double > > noise(int N)
Generate a vector of complex random numbers from a Gaussian distribution.
Definition: randgen.h:170
static matrix< double > gaussian(int N, int M)
Generate a matrix of random numbers from a Gaussian distribution.
Definition: randgen.h:197
static USML_DECLSPEC random_type master_gen
Master generator that underlies all distributions.
Definition: randgen.h:57
static USML_DECLSPEC boost::normal_distribution< double > gaussian_dist
variate_generator<> requires reference to abstract distribution.
Definition: randgen.h:63
static matrix< complex< double > > noise(int N, int M)
Generate a matrix of complex random numbers from a Gaussian distribution.
Definition: randgen.h:210
static USML_DECLSPEC boost::uniform_01< random_type, double > uniform_gen
Uniform random number generator for the range [0,1).
Definition: randgen.h:60
boost::kreutzer1986 random_type
Defines the class for the basic random number generator.
Definition: randgen.h:49
static USML_DECLSPEC boost::variate_generator< random_type, boost::normal_distribution< double > > gaussian_gen
Gaussian random number generator with sigma = 1.
Definition: randgen.h:67