4308
|
1 /* |
|
2 |
|
3 Copyright (C) 2003 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 2, or (at your option) any |
|
10 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, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
4308
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
5730
|
27 #include <vector> |
4308
|
28 |
|
29 #include "f77-fcn.h" |
5730
|
30 #include "lo-ieee.h" |
4308
|
31 #include "lo-error.h" |
5730
|
32 #include "lo-mappers.h" |
4308
|
33 #include "oct-rand.h" |
4415
|
34 #include "oct-time.h" |
5730
|
35 #include "data-conv.h" |
|
36 #include "randmtzig.h" |
|
37 #include "randpoisson.h" |
|
38 #include "randgamma.h" |
4308
|
39 |
|
40 // Possible distributions of random numbers. This was handled with an |
|
41 // enum, but unwind_protecting that doesn't work so well. |
|
42 #define uniform_dist 1 |
|
43 #define normal_dist 2 |
5730
|
44 #define expon_dist 3 |
|
45 #define poisson_dist 4 |
|
46 #define gamma_dist 5 |
4308
|
47 |
|
48 // Current distribution of random numbers. |
|
49 static int current_distribution = uniform_dist; |
|
50 |
5730
|
51 // Has the seed/state been set yet? |
|
52 static bool old_initialized = false; |
|
53 static bool new_initialized = false; |
|
54 static bool use_old_generators = false; |
4308
|
55 |
|
56 extern "C" |
|
57 { |
4552
|
58 F77_RET_T |
|
59 F77_FUNC (dgennor, DGENNOR) (const double&, const double&, double&); |
4308
|
60 |
4552
|
61 F77_RET_T |
|
62 F77_FUNC (dgenunf, DGENUNF) (const double&, const double&, double&); |
|
63 |
|
64 F77_RET_T |
5730
|
65 F77_FUNC (dgenexp, DGENEXP) (const double&, double&); |
|
66 |
|
67 F77_RET_T |
|
68 F77_FUNC (dignpoi, DIGNPOI) (const double&, double&); |
|
69 |
|
70 F77_RET_T |
|
71 F77_FUNC (dgengam, DGENGAM) (const double&, const double&, double&); |
|
72 |
|
73 F77_RET_T |
5275
|
74 F77_FUNC (setall, SETALL) (const octave_idx_type&, const octave_idx_type&); |
4308
|
75 |
4552
|
76 F77_RET_T |
5275
|
77 F77_FUNC (getsd, GETSD) (octave_idx_type&, octave_idx_type&); |
4308
|
78 |
4552
|
79 F77_RET_T |
5275
|
80 F77_FUNC (setsd, SETSD) (const octave_idx_type&, const octave_idx_type&); |
4308
|
81 |
4552
|
82 F77_RET_T |
5275
|
83 F77_FUNC (setcgn, SETCGN) (const octave_idx_type&); |
4308
|
84 } |
|
85 |
5275
|
86 static octave_idx_type |
|
87 force_to_fit_range (octave_idx_type i, octave_idx_type lo, octave_idx_type hi) |
4308
|
88 { |
|
89 assert (hi > lo && lo >= 0 && hi > lo); |
|
90 |
|
91 i = i > 0 ? i : -i; |
|
92 |
|
93 if (i < lo) |
|
94 i = lo; |
|
95 else if (i > hi) |
|
96 i = i % hi; |
|
97 |
|
98 return i; |
|
99 } |
|
100 |
|
101 // Make the random number generator give us a different sequence every |
|
102 // time we start octave unless we specifically set the seed. The |
|
103 // technique used below will cycle monthly, but it it does seem to |
|
104 // work ok to give fairly different seeds each time Octave starts. |
|
105 |
|
106 static void |
5730
|
107 do_old_initialization (void) |
4308
|
108 { |
4415
|
109 octave_localtime tm; |
4308
|
110 |
4415
|
111 int hour = tm.hour() + 1; |
|
112 int minute = tm.min() + 1; |
|
113 int second = tm.sec() + 1; |
4308
|
114 |
5275
|
115 octave_idx_type s0 = tm.mday() * hour * minute * second; |
|
116 octave_idx_type s1 = hour * minute * second; |
4308
|
117 |
|
118 s0 = force_to_fit_range (s0, 1, 2147483563); |
|
119 s1 = force_to_fit_range (s1, 1, 2147483399); |
|
120 |
|
121 F77_FUNC (setall, SETALL) (s0, s1); |
|
122 |
5730
|
123 old_initialized = true; |
4308
|
124 } |
|
125 |
|
126 static inline void |
|
127 maybe_initialize (void) |
|
128 { |
5730
|
129 if (use_old_generators) |
|
130 { |
|
131 if (! old_initialized) |
|
132 do_old_initialization (); |
|
133 } |
|
134 else |
|
135 { |
|
136 if (! new_initialized) |
|
137 { |
|
138 oct_init_by_entropy (); |
|
139 new_initialized = true; |
|
140 } |
|
141 } |
4308
|
142 } |
|
143 |
|
144 double |
|
145 octave_rand::seed (void) |
|
146 { |
5730
|
147 if (! old_initialized) |
|
148 do_old_initialization (); |
4308
|
149 |
5275
|
150 union d2i { double d; octave_idx_type i[2]; }; |
4308
|
151 union d2i u; |
|
152 F77_FUNC (getsd, GETSD) (u.i[0], u.i[1]); |
|
153 return u.d; |
|
154 } |
|
155 |
|
156 void |
|
157 octave_rand::seed (double s) |
|
158 { |
5730
|
159 use_old_generators = true; |
4308
|
160 maybe_initialize (); |
|
161 |
5275
|
162 union d2i { double d; octave_idx_type i[2]; }; |
4308
|
163 union d2i u; |
|
164 u.d = s; |
|
165 int i0 = force_to_fit_range (u.i[0], 1, 2147483563); |
|
166 int i1 = force_to_fit_range (u.i[1], 1, 2147483399); |
|
167 F77_FUNC (setsd, SETSD) (i0, i1); |
|
168 } |
|
169 |
5730
|
170 ColumnVector |
|
171 octave_rand::state (void) |
|
172 { |
|
173 ColumnVector s (MT_N + 1); |
|
174 if (! new_initialized) |
|
175 { |
|
176 oct_init_by_entropy (); |
|
177 new_initialized = true; |
|
178 } |
|
179 |
5828
|
180 OCTAVE_LOCAL_BUFFER (uint32_t, tmp, MT_N + 1); |
5730
|
181 oct_get_state (tmp); |
|
182 for (octave_idx_type i = 0; i <= MT_N; i++) |
|
183 s.elem (i) = static_cast<double>(tmp [i]); |
|
184 return s; |
|
185 } |
|
186 |
|
187 void |
|
188 octave_rand::state (const ColumnVector &s) |
|
189 { |
|
190 use_old_generators = false; |
|
191 maybe_initialize (); |
|
192 |
|
193 octave_idx_type len = s.length(); |
|
194 octave_idx_type n = len < MT_N + 1 ? len : MT_N + 1; |
5828
|
195 OCTAVE_LOCAL_BUFFER (uint32_t, tmp, MT_N + 1); |
5730
|
196 for (octave_idx_type i = 0; i < n; i++) |
5828
|
197 tmp[i] = static_cast<uint32_t> (s.elem(i)); |
5730
|
198 |
|
199 if (len == MT_N + 1 && tmp[MT_N] <= MT_N && tmp[MT_N] > 0) |
|
200 oct_set_state (tmp); |
|
201 else |
|
202 oct_init_by_array (tmp, len); |
|
203 } |
|
204 |
4308
|
205 std::string |
|
206 octave_rand::distribution (void) |
|
207 { |
|
208 maybe_initialize (); |
|
209 |
|
210 if (current_distribution == uniform_dist) |
|
211 return "uniform"; |
|
212 else if (current_distribution == normal_dist) |
|
213 return "normal"; |
5730
|
214 else if (current_distribution == expon_dist) |
|
215 return "exponential"; |
|
216 else if (current_distribution == poisson_dist) |
|
217 return "poisson"; |
|
218 else if (current_distribution == gamma_dist) |
|
219 return "gamma"; |
4308
|
220 else |
|
221 { |
|
222 abort (); |
|
223 return ""; |
|
224 } |
|
225 } |
|
226 |
|
227 void |
|
228 octave_rand::distribution (const std::string& d) |
|
229 { |
|
230 if (d == "uniform") |
|
231 octave_rand::uniform_distribution (); |
|
232 else if (d == "normal") |
|
233 octave_rand::normal_distribution (); |
5730
|
234 else if (d == "exponential") |
|
235 octave_rand::exponential_distribution (); |
|
236 else if (d == "poisson") |
|
237 octave_rand::poisson_distribution (); |
|
238 else if (d == "gamma") |
|
239 octave_rand::gamma_distribution (); |
4308
|
240 else |
|
241 (*current_liboctave_error_handler) ("rand: invalid distribution"); |
|
242 } |
|
243 |
|
244 void |
|
245 octave_rand::uniform_distribution (void) |
|
246 { |
|
247 maybe_initialize (); |
|
248 |
|
249 current_distribution = uniform_dist; |
|
250 |
|
251 F77_FUNC (setcgn, SETCGN) (uniform_dist); |
|
252 } |
|
253 |
|
254 void |
|
255 octave_rand::normal_distribution (void) |
|
256 { |
|
257 maybe_initialize (); |
|
258 |
|
259 current_distribution = normal_dist; |
|
260 |
|
261 F77_FUNC (setcgn, SETCGN) (normal_dist); |
|
262 } |
|
263 |
5730
|
264 void |
|
265 octave_rand::exponential_distribution (void) |
|
266 { |
|
267 maybe_initialize (); |
|
268 |
|
269 current_distribution = expon_dist; |
|
270 |
|
271 F77_FUNC (setcgn, SETCGN) (expon_dist); |
|
272 } |
|
273 |
|
274 void |
|
275 octave_rand::poisson_distribution (void) |
|
276 { |
|
277 maybe_initialize (); |
|
278 |
|
279 current_distribution = poisson_dist; |
|
280 |
|
281 F77_FUNC (setcgn, SETCGN) (poisson_dist); |
|
282 } |
|
283 |
|
284 void |
|
285 octave_rand::gamma_distribution (void) |
|
286 { |
|
287 maybe_initialize (); |
|
288 |
|
289 current_distribution = gamma_dist; |
|
290 |
|
291 F77_FUNC (setcgn, SETCGN) (gamma_dist); |
|
292 } |
|
293 |
|
294 |
4308
|
295 double |
5730
|
296 octave_rand::scalar (double a) |
4308
|
297 { |
|
298 maybe_initialize (); |
|
299 |
|
300 double retval = 0.0; |
|
301 |
5730
|
302 if (use_old_generators) |
4308
|
303 { |
5730
|
304 switch (current_distribution) |
|
305 { |
|
306 case uniform_dist: |
|
307 F77_FUNC (dgenunf, DGENUNF) (0.0, 1.0, retval); |
|
308 break; |
4308
|
309 |
5730
|
310 case normal_dist: |
|
311 F77_FUNC (dgennor, DGENNOR) (0.0, 1.0, retval); |
|
312 break; |
4308
|
313 |
5730
|
314 case expon_dist: |
|
315 F77_FUNC (dgenexp, DGENEXP) (1.0, retval); |
|
316 break; |
4308
|
317 |
5730
|
318 case poisson_dist: |
|
319 if (a < 0.0 || xisnan(a) || xisinf(a)) |
|
320 retval = octave_NaN; |
|
321 else |
|
322 { |
|
323 // workaround bug in ignpoi, by calling with different Mu |
|
324 F77_FUNC (dignpoi, DIGNPOI) (a + 1, retval); |
|
325 F77_FUNC (dignpoi, DIGNPOI) (a, retval); |
|
326 } |
|
327 break; |
4308
|
328 |
5730
|
329 case gamma_dist: |
|
330 if (a <= 0.0 || xisnan(a) || xisinf(a)) |
|
331 retval = octave_NaN; |
|
332 else |
|
333 F77_FUNC (dgengam, DGENGAM) (1.0, a, retval); |
|
334 break; |
4308
|
335 |
5730
|
336 default: |
|
337 abort (); |
|
338 break; |
4308
|
339 } |
|
340 } |
|
341 else |
4543
|
342 { |
|
343 switch (current_distribution) |
|
344 { |
|
345 case uniform_dist: |
5730
|
346 retval = oct_randu(); |
4543
|
347 break; |
|
348 |
|
349 case normal_dist: |
5730
|
350 retval = oct_randn(); |
|
351 break; |
|
352 |
|
353 case expon_dist: |
|
354 retval = oct_rande(); |
|
355 break; |
|
356 |
|
357 case poisson_dist: |
|
358 retval = oct_randp(a); |
|
359 break; |
|
360 |
|
361 case gamma_dist: |
|
362 retval = oct_randg(a); |
4543
|
363 break; |
|
364 |
|
365 default: |
|
366 abort (); |
|
367 break; |
|
368 } |
|
369 } |
|
370 |
|
371 return retval; |
|
372 } |
|
373 |
5730
|
374 #define MAKE_RAND(len) \ |
4308
|
375 do \ |
|
376 { \ |
|
377 double val; \ |
5730
|
378 for (volatile octave_idx_type i = 0; i < len; i++) \ |
4308
|
379 { \ |
|
380 OCTAVE_QUIT; \ |
5730
|
381 RAND_FUNC (val); \ |
|
382 v[i] = val; \ |
4308
|
383 } \ |
|
384 } \ |
|
385 while (0) |
|
386 |
5730
|
387 static void |
|
388 fill_rand (octave_idx_type len, double *v, double a) |
|
389 { |
|
390 maybe_initialize (); |
|
391 |
|
392 if (len < 1) |
|
393 return; |
|
394 |
|
395 switch (current_distribution) |
|
396 { |
|
397 case uniform_dist: |
|
398 if (use_old_generators) |
|
399 { |
|
400 #define RAND_FUNC(x) F77_FUNC (dgenunf, DGENUNF) (0.0, 1.0, x) |
|
401 MAKE_RAND (len); |
|
402 #undef RAND_FUNC |
|
403 } |
|
404 else |
|
405 oct_fill_randu (len, v); |
|
406 break; |
|
407 |
|
408 case normal_dist: |
|
409 if (use_old_generators) |
|
410 { |
|
411 #define RAND_FUNC(x) F77_FUNC (dgennor, DGENNOR) (0.0, 1.0, x) |
|
412 MAKE_RAND (len); |
|
413 #undef RAND_FUNC |
|
414 } |
|
415 else |
|
416 oct_fill_randn (len, v); |
|
417 break; |
|
418 |
|
419 case expon_dist: |
|
420 if (use_old_generators) |
|
421 { |
|
422 #define RAND_FUNC(x) F77_FUNC (dgenexp, DGENEXP) (1.0, x) |
|
423 MAKE_RAND (len); |
|
424 #undef RAND_FUNC |
|
425 } |
|
426 else |
|
427 oct_fill_rande (len, v); |
|
428 break; |
|
429 |
|
430 case poisson_dist: |
|
431 if (use_old_generators) |
|
432 { |
|
433 if (a < 0.0 || xisnan(a) || xisinf(a)) |
|
434 #define RAND_FUNC(x) x = octave_NaN; |
|
435 MAKE_RAND (len); |
|
436 #undef RAND_FUNC |
|
437 else |
|
438 { |
|
439 // workaround bug in ignpoi, by calling with different Mu |
|
440 double tmp; |
|
441 F77_FUNC (dignpoi, DIGNPOI) (a + 1, tmp); |
|
442 #define RAND_FUNC(x) F77_FUNC (dignpoi, DIGNPOI) (a, x) |
|
443 MAKE_RAND (len); |
|
444 #undef RAND_FUNC |
|
445 } |
|
446 } |
|
447 else |
|
448 oct_fill_randp (a, len, v); |
|
449 break; |
|
450 |
|
451 case gamma_dist: |
|
452 if (use_old_generators) |
|
453 { |
|
454 if (a <= 0.0 || xisnan(a) || xisinf(a)) |
|
455 #define RAND_FUNC(x) x = octave_NaN; |
|
456 MAKE_RAND (len); |
|
457 #undef RAND_FUNC |
|
458 else |
|
459 #define RAND_FUNC(x) F77_FUNC (dgengam, DGENGAM) (1.0, a, x) |
|
460 MAKE_RAND (len); |
|
461 #undef RAND_FUNC |
|
462 } |
|
463 else |
|
464 oct_fill_randg (a, len, v); |
|
465 break; |
|
466 |
|
467 default: |
|
468 abort (); |
|
469 break; |
|
470 } |
|
471 |
|
472 return; |
|
473 } |
|
474 |
|
475 Matrix |
|
476 octave_rand::matrix (octave_idx_type n, octave_idx_type m, double a) |
|
477 { |
|
478 Matrix retval; |
|
479 |
|
480 if (n >= 0 && m >= 0) |
|
481 { |
|
482 retval.resize (n, m); |
|
483 |
|
484 if (n > 0 && m > 0) |
|
485 fill_rand (retval.capacity(), retval.fortran_vec(), a); |
|
486 } |
|
487 else |
|
488 (*current_liboctave_error_handler) ("rand: invalid negative argument"); |
|
489 |
|
490 return retval; |
|
491 } |
|
492 |
|
493 NDArray |
|
494 octave_rand::nd_array (const dim_vector& dims, double a) |
|
495 { |
|
496 NDArray retval; |
|
497 |
|
498 if (! dims.all_zero ()) |
|
499 { |
|
500 retval.resize (dims); |
|
501 |
|
502 fill_rand (retval.capacity(), retval.fortran_vec(), a); |
|
503 } |
|
504 |
|
505 return retval; |
|
506 } |
|
507 |
4308
|
508 Array<double> |
5730
|
509 octave_rand::vector (octave_idx_type n, double a) |
4308
|
510 { |
|
511 maybe_initialize (); |
|
512 |
|
513 Array<double> retval; |
|
514 |
|
515 if (n > 0) |
|
516 { |
|
517 retval.resize (n); |
|
518 |
5730
|
519 fill_rand (retval.capacity(), retval.fortran_vec(), a); |
4308
|
520 } |
|
521 else if (n < 0) |
|
522 (*current_liboctave_error_handler) ("rand: invalid negative argument"); |
|
523 |
|
524 return retval; |
|
525 } |
|
526 |
|
527 /* |
|
528 ;;; Local Variables: *** |
|
529 ;;; mode: C++ *** |
|
530 ;;; End: *** |
|
531 */ |