GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-rand.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2003-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://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 "Array.h"
32 #include "dNDArray.h"
33 #include "fNDArray.h"
34 #include "lo-ieee.h"
35 #include "uint32NDArray.h"
36 
37 //class dim_vector;
38 
39 class
40 OCTAVE_API
42 {
43 protected:
44 
45  octave_rand (void);
46 
47 public:
48 
49  ~octave_rand (void) = default;
50 
51  static bool instance_ok (void);
52 
53  // Return the current seed.
54  static double seed (void)
55  {
56  return instance_ok () ? instance->do_seed ()
58  }
59 
60  // Set the seed.
61  static void seed (double s)
62  {
63  if (instance_ok ())
64  instance->do_seed (s);
65  }
66 
67  // Reset the seed.
68  static void reset (void)
69  {
70  if (instance_ok ())
71  instance->do_reset ();
72  }
73 
74  // Return the current state.
75  static uint32NDArray state (const std::string& d = "")
76  {
77  return instance_ok () ? instance->do_state (d) : uint32NDArray ();
78  }
79 
80  // Set the current state/
81  static void state (const uint32NDArray& s,
82  const std::string& d = "")
83  {
84  if (instance_ok ())
85  instance->do_state (s, d);
86  }
87 
88  // Reset the current state/
89  static void reset (const std::string& d)
90  {
91  if (instance_ok ())
92  instance->do_reset (d);
93  }
94 
95  // Return the current distribution.
96  static std::string distribution (void)
97  {
98  return instance_ok () ? instance->do_distribution () : "";
99  }
100 
101  // Set the current distribution. May be either "uniform" (the
102  // default), "normal", "exponential", "poisson", or "gamma".
103  static void distribution (const std::string& d)
104  {
105  if (instance_ok ())
106  instance->do_distribution (d);
107  }
108 
109  static void uniform_distribution (void)
110  {
111  if (instance_ok ())
112  instance->do_uniform_distribution ();
113  }
114 
115  static void normal_distribution (void)
116  {
117  if (instance_ok ())
118  instance->do_normal_distribution ();
119  }
120 
121  static void exponential_distribution (void)
122  {
123  if (instance_ok ())
124  instance->do_exponential_distribution ();
125  }
126 
127  static void poisson_distribution (void)
128  {
129  if (instance_ok ())
130  instance->do_poisson_distribution ();
131  }
132 
133  static void gamma_distribution (void)
134  {
135  if (instance_ok ())
136  instance->do_gamma_distribution ();
137  }
138 
139  // Return the next number from the sequence.
140  static double scalar (double a = 1.0)
141  {
142  return instance_ok () ? instance->do_scalar (a)
144  }
145 
146  // Return the next number from the sequence.
147  static float float_scalar (float a = 1.0)
148  {
149  return instance_ok () ? instance->do_float_scalar (a)
151  }
152 
153  // Return an array of numbers from the sequence.
154  static Array<double> vector (octave_idx_type n, double a = 1.0)
155  {
156  return instance_ok () ? instance->do_vector (n, a) : Array<double> ();
157  }
158 
159  // Return an array of numbers from the sequence.
160  static Array<float> float_vector (octave_idx_type n, float a = 1.0)
161  {
162  return instance_ok () ? instance->do_float_vector (n, a) : Array<float> ();
163  }
164 
165  // Return an N-dimensional array of numbers from the sequence,
166  // filled in column major order.
167  static NDArray nd_array (const dim_vector& dims, double a = 1.0)
168  {
169  return instance_ok () ? instance->do_nd_array (dims, a) : NDArray ();
170  }
171 
172  // Return an N-dimensional array of numbers from the sequence,
173  // filled in column major order.
174  static FloatNDArray float_nd_array (const dim_vector& dims, float a = 1.0)
175  {
176  return instance_ok () ? instance->do_float_nd_array (dims, a)
177  : FloatNDArray ();
178  }
179 
180 private:
181 
183 
184  static void cleanup_instance (void) { delete instance; instance = nullptr; }
185 
186  enum
187  {
193  gamma_dist
194  };
195 
196  // Current distribution of random numbers.
198 
199  // If TRUE, use old RANLIB generators. Otherwise, use Mersenne
200  // Twister generator.
202 
203  // Saved MT states.
204  std::map<int, uint32NDArray> rand_states;
205 
206  // Return the current seed.
207  double do_seed (void);
208 
209  // Set the seed.
210  void do_seed (double s);
211 
212  // Reset the seed.
213  void do_reset ();
214 
215  // Return the current state.
216  uint32NDArray do_state (const std::string& d);
217 
218  // Set the current state/
219  void do_state (const uint32NDArray& s, const std::string& d);
220 
221  // Reset the current state/
222  void do_reset (const std::string& d);
223 
224  // Return the current distribution.
225  std::string do_distribution (void);
226 
227  // Set the current distribution. May be either "uniform" (the
228  // default), "normal", "exponential", "poisson", or "gamma".
229  void do_distribution (const std::string& d);
230 
231  void do_uniform_distribution (void);
232 
233  void do_normal_distribution (void);
234 
235  void do_exponential_distribution (void);
236 
237  void do_poisson_distribution (void);
238 
239  void do_gamma_distribution (void);
240 
241  // Return the next number from the sequence.
242  double do_scalar (double a = 1.);
243 
244  // Return the next number from the sequence.
245  float do_float_scalar (float a = 1.);
246 
247  // Return an array of numbers from the sequence.
248  Array<double> do_vector (octave_idx_type n, double a = 1.);
249 
250  // Return an array of numbers from the sequence.
251  Array<float> do_float_vector (octave_idx_type n, float a = 1.);
252 
253  // Return an N-dimensional array of numbers from the sequence,
254  // filled in column major order.
255  NDArray do_nd_array (const dim_vector& dims, double a = 1.);
256 
257  // Return an N-dimensional array of numbers from the sequence,
258  // filled in column major order.
259  FloatNDArray do_float_nd_array (const dim_vector& dims, float a = 1.);
260 
261  // Some helper functions.
262 
263  void initialize_ranlib_generators (void);
264 
265  void initialize_mersenne_twister (void);
266 
267  uint32NDArray get_internal_state (void);
268 
269  void save_state (void);
270 
271  int get_dist_id (const std::string& d);
272 
273  void set_internal_state (const uint32NDArray& s);
274 
275  void switch_to_generator (int dist);
276 
277  void fill (octave_idx_type len, double *v, double a);
278 
279  void fill (octave_idx_type len, float *v, float a);
280 };
281 
282 #endif
static void exponential_distribution(void)
Definition: oct-rand.h:121
intNDArray< octave_uint32 > uint32NDArray
Definition: uint32NDArray.h:33
static void cleanup_instance(void)
Definition: oct-rand.h:184
static void reset(void)
Definition: oct-rand.h:68
static Array< double > vector(octave_idx_type n, double a=1.0)
Definition: oct-rand.h:154
s
Definition: file-io.cc:2729
static float float_scalar(float a=1.0)
Definition: oct-rand.h:147
int current_distribution
Definition: oct-rand.h:197
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 const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &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:400
static void seed(double s)
Definition: oct-rand.h:61
static double seed(void)
Definition: oct-rand.h:54
static void gamma_distribution(void)
Definition: oct-rand.h:133
static void poisson_distribution(void)
Definition: oct-rand.h:127
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:167
static FloatNDArray float_nd_array(const dim_vector &dims, float a=1.0)
Definition: oct-rand.h:174
static octave_rand * instance
Definition: oct-rand.h:182
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 void normal_distribution(void)
Definition: oct-rand.h:115
std::map< int, uint32NDArray > rand_states
Definition: oct-rand.h:204
static void state(const uint32NDArray &s, const std::string &d="")
Definition: oct-rand.h:81
bool use_old_generators
Definition: oct-rand.h:201
static std::string distribution(void)
Definition: oct-rand.h:96
static void distribution(const std::string &d)
Definition: oct-rand.h:103
static double scalar(double a=1.0)
Definition: oct-rand.h:140
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:160
static void uniform_distribution(void)
Definition: oct-rand.h:109
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:888
static void reset(const std::string &d)
Definition: oct-rand.h:89
static uint32NDArray state(const std::string &d="")
Definition: oct-rand.h:75