GNU Octave  8.1.0
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-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_oct_rand_h)
27 #define octave_oct_rand_h 1
28 
29 #include "octave-config.h"
30 
31 #include <map>
32 #include <string>
33 
34 #include "Array.h"
35 #include "dNDArray.h"
36 #include "fNDArray.h"
37 #include "lo-ieee.h"
38 #include "uint32NDArray.h"
39 
40 //class dim_vector;
41 
43 
45 {
46 protected:
47 
48  OCTAVE_API rand (void);
49 
50 public:
51 
52  ~rand (void) = default;
53 
54  static bool instance_ok (void);
55 
56  // Return the current seed.
57  static double seed (void)
58  {
59  return (instance_ok ()
60  ? m_instance->do_seed () : numeric_limits<double>::NaN ());
61  }
62 
63  // Set the seed.
64  static void seed (double s)
65  {
66  if (instance_ok ())
67  m_instance->do_seed (s);
68  }
69 
70  // Reset the seed.
71  static void reset (void)
72  {
73  if (instance_ok ())
74  m_instance->do_reset ();
75  }
76 
77  // Return the current state.
78  static uint32NDArray state (const std::string& d = "")
79  {
80  return instance_ok () ? m_instance->do_state (d) : uint32NDArray ();
81  }
82 
83  // Set the current state/
84  static void state (const uint32NDArray& s,
85  const std::string& d = "")
86  {
87  if (instance_ok ())
88  m_instance->do_state (s, d);
89  }
90 
91  // Reset the current state/
92  static void reset (const std::string& d)
93  {
94  if (instance_ok ())
95  m_instance->do_reset (d);
96  }
97 
98  // Return the current distribution.
99  static std::string distribution (void)
100  {
101  return instance_ok () ? m_instance->do_distribution () : "";
102  }
103 
104  // Set the current distribution. May be either "uniform" (the
105  // default), "normal", "exponential", "poisson", or "gamma".
106  static void distribution (const std::string& d)
107  {
108  if (instance_ok ())
109  m_instance->do_distribution (d);
110  }
111 
112  static void uniform_distribution (void)
113  {
114  if (instance_ok ())
115  m_instance->do_uniform_distribution ();
116  }
117 
118  static void normal_distribution (void)
119  {
120  if (instance_ok ())
121  m_instance->do_normal_distribution ();
122  }
123 
124  static void exponential_distribution (void)
125  {
126  if (instance_ok ())
127  m_instance->do_exponential_distribution ();
128  }
129 
130  static void poisson_distribution (void)
131  {
132  if (instance_ok ())
133  m_instance->do_poisson_distribution ();
134  }
135 
136  static void gamma_distribution (void)
137  {
138  if (instance_ok ())
139  m_instance->do_gamma_distribution ();
140  }
141 
142  // Return the next number from the sequence.
143  static double scalar (double a = 1.0)
144  {
145  return (instance_ok ()
146  ? m_instance->do_scalar (a) : numeric_limits<double>::NaN ());
147  }
148 
149  // Return the next number from the sequence.
150  static float float_scalar (float a = 1.0)
151  {
152  return (instance_ok ()
153  ? m_instance->do_scalar (a) : numeric_limits<float>::NaN ());
154  }
155 
156  // Return an array of numbers from the sequence.
157  static Array<double> vector (octave_idx_type n, double a = 1.0)
158  {
159  return instance_ok () ? m_instance->do_vector (n, a) : Array<double> ();
160  }
161 
162  // Return an array of numbers from the sequence.
163  static Array<float> float_vector (octave_idx_type n, float a = 1.0)
164  {
165  return instance_ok () ? m_instance->do_vector (n, a) : Array<float> ();
166  }
167 
168  // Return an N-dimensional array of numbers from the sequence,
169  // filled in column major order.
170  static NDArray nd_array (const dim_vector& dims, double a = 1.0)
171  {
172  return instance_ok () ? m_instance->do_nd_array (dims, a) : NDArray ();
173  }
174 
175  // Return an N-dimensional array of numbers from the sequence,
176  // filled in column major order.
177  static FloatNDArray float_nd_array (const dim_vector& dims, float a = 1.0)
178  {
179  return (instance_ok ()
180  ? m_instance->do_float_nd_array (dims, a) : FloatNDArray ());
181  }
182 
183 private:
184 
185  static rand *m_instance;
186 
187  static void cleanup_instance (void)
188  { delete m_instance; m_instance = nullptr; }
189 
190  enum
191  {
197  gamma_dist
198  };
199 
200  // Current distribution of random numbers.
202 
203  // If TRUE, use old RANLIB generators. Otherwise, use Mersenne
204  // Twister generator.
206 
207  // Saved MT states.
208  std::map<int, uint32NDArray> m_rand_states;
209 
210  // Return the current seed.
211  OCTAVE_API double do_seed (void);
212 
213  // Set the seed.
214  OCTAVE_API void do_seed (double s);
215 
216  // Reset the seed.
217  OCTAVE_API void do_reset ();
218 
219  // Return the current state.
220  OCTAVE_API uint32NDArray do_state (const std::string& d);
221 
222  // Set the current state/
223  OCTAVE_API void do_state (const uint32NDArray& s, const std::string& d);
224 
225  // Reset the current state/
226  OCTAVE_API void do_reset (const std::string& d);
227 
228  // Return the current distribution.
229  OCTAVE_API std::string do_distribution (void);
230 
231  // Set the current distribution. May be either "uniform" (the
232  // default), "normal", "exponential", "poisson", or "gamma".
233  OCTAVE_API void do_distribution (const std::string& d);
234 
235  OCTAVE_API void do_uniform_distribution (void);
236 
237  OCTAVE_API void do_normal_distribution (void);
238 
239  OCTAVE_API void do_exponential_distribution (void);
240 
241  OCTAVE_API void do_poisson_distribution (void);
242 
243  OCTAVE_API void do_gamma_distribution (void);
244 
245  // The following templates only make sense for double and float
246  // types.
247 
248  template <typename T> OCTAVE_API T uniform (void);
249 
250  template <typename T> OCTAVE_API T normal (void);
251 
252  template <typename T> OCTAVE_API T exponential (void);
253 
254  template <typename T> OCTAVE_API T poisson (T a);
255 
256  template <typename T> OCTAVE_API T gamma (T a);
257 
258  // Return the next number from the sequence.
259  template <typename T> OCTAVE_API T do_scalar (T a = 1);
260 
261  // Return an array of numbers from the sequence.
262  template <typename T> OCTAVE_API Array<T>
264 
265  // Return an N-dimensional array of numbers from the sequence,
266  // filled in column major order.
267  OCTAVE_API NDArray do_nd_array (const dim_vector& dims, double a = 1.);
268 
269  // Return an N-dimensional array of numbers from the sequence,
270  // filled in column major order.
272  do_float_nd_array (const dim_vector& dims, float a = 1.);
273 
274  // Some helper functions.
275 
276  OCTAVE_API void initialize_ranlib_generators (void);
277 
278  OCTAVE_API void initialize_mersenne_twister (void);
279 
280  OCTAVE_API uint32NDArray get_internal_state (void);
281 
282  OCTAVE_API void save_state (void);
283 
284  OCTAVE_API int get_dist_id (const std::string& d);
285 
286  OCTAVE_API void set_internal_state (const uint32NDArray& s);
287 
288  OCTAVE_API void switch_to_generator (int dist);
289 
290  OCTAVE_API void fill (octave_idx_type len, double *v, double a);
291 
292  OCTAVE_API void fill (octave_idx_type len, float *v, float a);
293 };
294 
296 
297 #endif
template class OCTAVE_CLASS_TEMPLATE_INSTANTIATION_API Array< double >
Definition: Array-d.cc:169
template class OCTAVE_CLASS_TEMPLATE_INSTANTIATION_API Array< float >
Definition: Array-f.cc:169
OCTAVE_END_NAMESPACE(octave)
#define NaN
Definition: Faddeeva.cc:261
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
Definition: oct-rand.h:45
static void gamma_distribution(void)
Definition: oct-rand.h:136
bool m_use_old_generators
Definition: oct-rand.h:205
~rand(void)=default
static rand * m_instance
Definition: oct-rand.h:185
static FloatNDArray float_nd_array(const dim_vector &dims, float a=1.0)
Definition: oct-rand.h:177
static std::string distribution(void)
Definition: oct-rand.h:99
static uint32NDArray state(const std::string &d="")
Definition: oct-rand.h:78
static Array< float > float_vector(octave_idx_type n, float a=1.0)
Definition: oct-rand.h:163
OCTAVE_API T poisson(T a)
static void distribution(const std::string &d)
Definition: oct-rand.h:106
static NDArray nd_array(const dim_vector &dims, double a=1.0)
Definition: oct-rand.h:170
@ uniform_dist
Definition: oct-rand.h:193
@ expon_dist
Definition: oct-rand.h:195
@ poisson_dist
Definition: oct-rand.h:196
@ normal_dist
Definition: oct-rand.h:194
@ unknown_dist
Definition: oct-rand.h:192
static double seed(void)
Definition: oct-rand.h:57
static double scalar(double a=1.0)
Definition: oct-rand.h:143
static void seed(double s)
Definition: oct-rand.h:64
OCTAVE_API T normal(void)
OCTAVE_API T do_scalar(T a=1)
OCTAVE_API Array< T > do_vector(octave_idx_type n, T a=1)
static void uniform_distribution(void)
Definition: oct-rand.h:112
static float float_scalar(float a=1.0)
Definition: oct-rand.h:150
static void reset(const std::string &d)
Definition: oct-rand.h:92
OCTAVE_API T uniform(void)
static void exponential_distribution(void)
Definition: oct-rand.h:124
static void state(const uint32NDArray &s, const std::string &d="")
Definition: oct-rand.h:84
OCTAVE_API T gamma(T a)
static void reset(void)
Definition: oct-rand.h:71
static Array< double > vector(octave_idx_type n, double a=1.0)
Definition: oct-rand.h:157
std::map< int, uint32NDArray > m_rand_states
Definition: oct-rand.h:208
static void cleanup_instance(void)
Definition: oct-rand.h:187
static void poisson_distribution(void)
Definition: oct-rand.h:130
static void normal_distribution(void)
Definition: oct-rand.h:118
int m_current_distribution
Definition: oct-rand.h:201
OCTAVE_API T exponential(void)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
#define OCTAVE_API
Definition: main.in.cc:55
class OCTAVE_API NDArray
Definition: mx-fwd.h:38
octave_idx_type n
Definition: mx-inlines.cc:753
intNDArray< octave_uint32 > uint32NDArray
Definition: uint32NDArray.h:36
F77_RET_T len
Definition: xerbla.cc:61