GNU Octave  4.2.1
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-2017 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 "octave-config.h"
27 
28 #include <map>
29 #include <string>
30 
31 #include "dColVector.h"
32 #include "dNDArray.h"
33 #include "fNDArray.h"
34 #include "lo-ieee.h"
35 
36 class
37 OCTAVE_API
39 {
40 protected:
41 
42  octave_rand (void);
43 
44 public:
45 
46  ~octave_rand (void) { }
47 
48  static bool instance_ok (void);
49 
50  // Return the current seed.
51  static double seed (void)
52  {
53  return instance_ok () ? instance->do_seed ()
55  }
56 
57  // Set the seed.
58  static void seed (double s)
59  {
60  if (instance_ok ())
61  instance->do_seed (s);
62  }
63 
64  // Reset the seed.
65  static void reset (void)
66  {
67  if (instance_ok ())
68  instance->do_reset ();
69  }
70 
71  // Return the current state.
72  static ColumnVector state (const std::string& d = "")
73  {
74  return instance_ok () ? instance->do_state (d) : ColumnVector ();
75  }
76 
77  // Set the current state/
78  static void state (const ColumnVector &s,
79  const std::string& d = "")
80  {
81  if (instance_ok ())
82  instance->do_state (s, d);
83  }
84 
85  // Reset the current state/
86  static void reset (const std::string& d)
87  {
88  if (instance_ok ())
89  instance->do_reset (d);
90  }
91 
92  // Return the current distribution.
93  static std::string distribution (void)
94  {
95  return instance_ok () ? instance->do_distribution () : "";
96  }
97 
98  // Set the current distribution. May be either "uniform" (the
99  // default), "normal", "exponential", "poisson", or "gamma".
100  static void distribution (const std::string& d)
101  {
102  if (instance_ok ())
103  instance->do_distribution (d);
104  }
105 
106  static void uniform_distribution (void)
107  {
108  if (instance_ok ())
109  instance->do_uniform_distribution ();
110  }
111 
112  static void normal_distribution (void)
113  {
114  if (instance_ok ())
115  instance->do_normal_distribution ();
116  }
117 
118  static void exponential_distribution (void)
119  {
120  if (instance_ok ())
121  instance->do_exponential_distribution ();
122  }
123 
124  static void poisson_distribution (void)
125  {
126  if (instance_ok ())
127  instance->do_poisson_distribution ();
128  }
129 
130  static void gamma_distribution (void)
131  {
132  if (instance_ok ())
133  instance->do_gamma_distribution ();
134  }
135 
136  // Return the next number from the sequence.
137  static double scalar (double a = 1.0)
138  {
139  return instance_ok () ? instance->do_scalar (a)
141  }
142 
143  // Return the next number from the sequence.
144  static float float_scalar (float a = 1.0)
145  {
146  return instance_ok () ? instance->do_float_scalar (a)
148  }
149 
150  // Return an array of numbers from the sequence.
151  static Array<double> vector (octave_idx_type n, double a = 1.0)
152  {
153  return instance_ok () ? instance->do_vector (n, a) : Array<double> ();
154  }
155 
156  // Return an array of numbers from the sequence.
157  static Array<float> float_vector (octave_idx_type n, float a = 1.0)
158  {
159  return instance_ok () ? instance->do_float_vector (n, a) : Array<float> ();
160  }
161 
162  // Return an N-dimensional array of numbers from the sequence,
163  // filled in column major order.
164  static NDArray nd_array (const dim_vector& dims, double a = 1.0)
165  {
166  return instance_ok () ? instance->do_nd_array (dims, a) : NDArray ();
167  }
168 
169  // Return an N-dimensional array of numbers from the sequence,
170  // filled in column major order.
171  static FloatNDArray float_nd_array (const dim_vector& dims, float a = 1.0)
172  {
173  return instance_ok () ? instance->do_float_nd_array (dims, a)
174  : FloatNDArray ();
175  }
176 
177 private:
178 
180 
181  static void cleanup_instance (void) { delete instance; instance = 0; }
182 
183  enum
184  {
190  gamma_dist
191  };
192 
193  // Current distribution of random numbers.
195 
196  // If TRUE, use old RANLIB generators. Otherwise, use Mersenne
197  // Twister generator.
199 
200  // Saved MT states.
201  std::map<int, ColumnVector> rand_states;
202 
203  // Return the current seed.
204  double do_seed (void);
205 
206  // Set the seed.
207  void do_seed (double s);
208 
209  // Reset the seed.
210  void do_reset ();
211 
212  // Return the current state.
213  ColumnVector do_state (const std::string& d);
214 
215  // Set the current state/
216  void do_state (const ColumnVector &s, const std::string& d);
217 
218  // Reset the current state/
219  void do_reset (const std::string& d);
220 
221  // Return the current distribution.
222  std::string do_distribution (void);
223 
224  // Set the current distribution. May be either "uniform" (the
225  // default), "normal", "exponential", "poisson", or "gamma".
226  void do_distribution (const std::string& d);
227 
228  void do_uniform_distribution (void);
229 
230  void do_normal_distribution (void);
231 
232  void do_exponential_distribution (void);
233 
234  void do_poisson_distribution (void);
235 
236  void do_gamma_distribution (void);
237 
238  // Return the next number from the sequence.
239  double do_scalar (double a = 1.);
240 
241  // Return the next number from the sequence.
242  float do_float_scalar (float a = 1.);
243 
244  // Return an array of numbers from the sequence.
245  Array<double> do_vector (octave_idx_type n, double a = 1.);
246 
247  // Return an array of numbers from the sequence.
248  Array<float> do_float_vector (octave_idx_type n, float a = 1.);
249 
250  // Return an N-dimensional array of numbers from the sequence,
251  // filled in column major order.
252  NDArray do_nd_array (const dim_vector& dims, double a = 1.);
253 
254  // Return an N-dimensional array of numbers from the sequence,
255  // filled in column major order.
256  FloatNDArray do_float_nd_array (const dim_vector& dims, float a = 1.);
257 
258  // Some helper functions.
259 
260  void initialize_ranlib_generators (void);
261 
262  void initialize_mersenne_twister (void);
263 
264  ColumnVector get_internal_state (void);
265 
266  void save_state (void);
267 
268  int get_dist_id (const std::string& d);
269 
270  void set_internal_state (const ColumnVector& s);
271 
272  void switch_to_generator (int dist);
273 
274  void fill (octave_idx_type len, double *v, double a);
275 
276  void fill (octave_idx_type len, float *v, float a);
277 };
278 
279 #endif
static void exponential_distribution(void)
Definition: oct-rand.h:118
static ColumnVector state(const std::string &d="")
Definition: oct-rand.h:72
static void cleanup_instance(void)
Definition: oct-rand.h:181
static void reset(void)
Definition: oct-rand.h:65
static Array< double > vector(octave_idx_type n, double a=1.0)
Definition: oct-rand.h:151
s
Definition: file-io.cc:2682
static float float_scalar(float a=1.0)
Definition: oct-rand.h:144
int current_distribution
Definition: oct-rand.h:194
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
calling an anonymous function involves an overhead quite comparable to the overhead of an m file function Passing a handle to a built in function is because the interpreter is not involved in the internal loop For a
Definition: cellfun.cc:398
static void seed(double s)
Definition: oct-rand.h:58
static double seed(void)
Definition: oct-rand.h:51
static void gamma_distribution(void)
Definition: oct-rand.h:130
std::map< int, ColumnVector > rand_states
Definition: oct-rand.h:201
static void state(const ColumnVector &s, const std::string &d="")
Definition: oct-rand.h:78
static void poisson_distribution(void)
Definition: oct-rand.h:124
the exceeded dimensions are set to if fewer subscripts than dimensions are the exceeding dimensions are merged into the final requested dimension For consider the following dims
Definition: sub2ind.cc:255
static NDArray nd_array(const dim_vector &dims, double a=1.0)
Definition: oct-rand.h:164
static FloatNDArray float_nd_array(const dim_vector &dims, float a=1.0)
Definition: oct-rand.h:171
static octave_rand * instance
Definition: oct-rand.h:179
static void normal_distribution(void)
Definition: oct-rand.h:112
bool use_old_generators
Definition: oct-rand.h:198
OCTAVE_EXPORT octave_value_list or N dimensional array whose elements are all equal to the IEEE symbol NaN(Not a Number).NaN is the result of operations which do not produce a well defined 0 result.Common operations which produce a NaN are arithmetic with infinity ex($\infty-\infty $)
static std::string distribution(void)
Definition: oct-rand.h:93
static void distribution(const std::string &d)
Definition: oct-rand.h:100
static double scalar(double a=1.0)
Definition: oct-rand.h:137
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
static Array< float > float_vector(octave_idx_type n, float a=1.0)
Definition: oct-rand.h:157
static void uniform_distribution(void)
Definition: oct-rand.h:106
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:854
~octave_rand(void)
Definition: oct-rand.h:46
static void reset(const std::string &d)
Definition: oct-rand.h:86