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