GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
oct-rand.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2003-2013 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if !defined (octave_oct_rand_h)
24 #define octave_oct_rand_h 1
25 
26 #include <map>
27 #include <string>
28 
29 #include "dColVector.h"
30 #include "dNDArray.h"
31 #include "fNDArray.h"
32 #include "lo-ieee.h"
33 
34 class
35 OCTAVE_API
37 {
38 protected:
39 
40  octave_rand (void);
41 
42 public:
43 
44  ~octave_rand (void) { }
45 
46  static bool instance_ok (void);
47 
48  // Return the current seed.
49  static double seed (void)
50  {
51  return instance_ok () ? instance->do_seed () : octave_NaN;
52  }
53 
54  // Set the seed.
55  static void seed (double s)
56  {
57  if (instance_ok ())
58  instance->do_seed (s);
59  }
60 
61  // Reset the seed.
62  static void reset (void)
63  {
64  if (instance_ok ())
65  instance->do_reset ();
66  }
67 
68  // Return the current state.
69  static ColumnVector state (const std::string& d = std::string ())
70  {
71  return instance_ok () ? instance->do_state (d) : ColumnVector ();
72  }
73 
74  // Set the current state/
75  static void state (const ColumnVector &s,
76  const std::string& d = std::string ())
77  {
78  if (instance_ok ())
79  instance->do_state (s, d);
80  }
81 
82  // Reset the current state/
83  static void reset (const std::string& d)
84  {
85  if (instance_ok ())
86  instance->do_reset (d);
87  }
88 
89  // Return the current distribution.
90  static std::string distribution (void)
91  {
92  return instance_ok () ? instance->do_distribution () : std::string ();
93  }
94 
95  // Set the current distribution. May be either "uniform" (the
96  // default), "normal", "exponential", "poisson", or "gamma".
97  static void distribution (const std::string& d)
98  {
99  if (instance_ok ())
100  instance->do_distribution (d);
101  }
102 
103  static void uniform_distribution (void)
104  {
105  if (instance_ok ())
106  instance->do_uniform_distribution ();
107  }
108 
109  static void normal_distribution (void)
110  {
111  if (instance_ok ())
112  instance->do_normal_distribution ();
113  }
114 
115  static void exponential_distribution (void)
116  {
117  if (instance_ok ())
118  instance->do_exponential_distribution ();
119  }
120 
121  static void poisson_distribution (void)
122  {
123  if (instance_ok ())
124  instance->do_poisson_distribution ();
125  }
126 
127  static void gamma_distribution (void)
128  {
129  if (instance_ok ())
130  instance->do_gamma_distribution ();
131  }
132 
133  // Return the next number from the sequence.
134  static double scalar (double a = 1.0)
135  {
136  return instance_ok () ? instance->do_scalar (a) : octave_NaN;
137  }
138 
139  // Return the next number from the sequence.
140  static float float_scalar (float a = 1.0)
141  {
142  return instance_ok () ? instance->do_float_scalar (a) : octave_Float_NaN;
143  }
144 
145  // Return an array of numbers from the sequence.
146  static Array<double> vector (octave_idx_type n, double a = 1.0)
147  {
148  return instance_ok () ? instance->do_vector (n, a) : Array<double> ();
149  }
150 
151  // Return an array of numbers from the sequence.
152  static Array<float> float_vector (octave_idx_type n, float a = 1.0)
153  {
154  return instance_ok () ? instance->do_float_vector (n, a) : Array<float> ();
155  }
156 
157  // Return an N-dimensional array of numbers from the sequence,
158  // filled in column major order.
159  static NDArray nd_array (const dim_vector& dims, double a = 1.0)
160  {
161  return instance_ok () ? instance->do_nd_array (dims, a) : NDArray ();
162  }
163 
164 
165  // Return an N-dimensional array of numbers from the sequence,
166  // filled in column major order.
167  static FloatNDArray float_nd_array (const dim_vector& dims, float a = 1.0)
168  {
169  return instance_ok () ? instance->do_float_nd_array (dims, a)
170  : FloatNDArray ();
171  }
172 
173 private:
174 
176 
177  static void cleanup_instance (void) { delete instance; instance = 0; }
178 
179  enum
180  {
186  gamma_dist
187  };
188 
189  // Current distribution of random numbers.
191 
192  // If TRUE, use old RANLIB generators. Otherwise, use Mersenne
193  // Twister generator.
195 
196  // Saved MT states.
197  std::map<int, ColumnVector> rand_states;
198 
199  // Return the current seed.
200  double do_seed (void);
201 
202  // Set the seed.
203  void do_seed (double s);
204 
205  // Reset the seed.
206  void do_reset ();
207 
208  // Return the current state.
209  ColumnVector do_state (const std::string& d);
210 
211  // Set the current state/
212  void do_state (const ColumnVector &s, const std::string& d);
213 
214  // Reset the current state/
215  void do_reset (const std::string& d);
216 
217  // Return the current distribution.
218  std::string do_distribution (void);
219 
220  // Set the current distribution. May be either "uniform" (the
221  // default), "normal", "exponential", "poisson", or "gamma".
222  void do_distribution (const std::string& d);
223 
224  void do_uniform_distribution (void);
225 
226  void do_normal_distribution (void);
227 
228  void do_exponential_distribution (void);
229 
230  void do_poisson_distribution (void);
231 
232  void do_gamma_distribution (void);
233 
234  // Return the next number from the sequence.
235  double do_scalar (double a = 1.);
236 
237  // Return the next number from the sequence.
238  float do_float_scalar (float a = 1.);
239 
240  // Return an array of numbers from the sequence.
241  Array<double> do_vector (octave_idx_type n, double a = 1.);
242 
243  // Return an array of numbers from the sequence.
244  Array<float> do_float_vector (octave_idx_type n, float a = 1.);
245 
246  // Return an N-dimensional array of numbers from the sequence,
247  // filled in column major order.
248  NDArray do_nd_array (const dim_vector& dims, double a = 1.);
249 
250  // Return an N-dimensional array of numbers from the sequence,
251  // filled in column major order.
252  FloatNDArray do_float_nd_array (const dim_vector& dims, float a = 1.);
253 
254  // Some helper functions.
255 
256  void initialize_ranlib_generators (void);
257 
258  void initialize_mersenne_twister (void);
259 
260  ColumnVector get_internal_state (void);
261 
262  void save_state (void);
263 
264  int get_dist_id (const std::string& d);
265 
266  void set_internal_state (const ColumnVector& s);
267 
268  void switch_to_generator (int dist);
269 
270  void fill (octave_idx_type len, double *v, double a);
271 
272  void fill (octave_idx_type len, float *v, float a);
273 };
274 
275 #endif