4308
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 2003, 2005, 2006, 2007 John W. Eaton |
4308
|
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 |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
4308
|
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 |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
4308
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
5730
|
26 #include <vector> |
4308
|
27 |
|
28 #include "f77-fcn.h" |
5730
|
29 #include "lo-ieee.h" |
4308
|
30 #include "lo-error.h" |
5730
|
31 #include "lo-mappers.h" |
4308
|
32 #include "oct-rand.h" |
4415
|
33 #include "oct-time.h" |
5730
|
34 #include "data-conv.h" |
|
35 #include "randmtzig.h" |
|
36 #include "randpoisson.h" |
|
37 #include "randgamma.h" |
6435
|
38 #include "mach-info.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; |
6326
|
110 int stored_distribution = current_distribution; |
|
111 F77_FUNC (setcgn, SETCGN) (uniform_dist); |
|
112 |
4415
|
113 int hour = tm.hour() + 1; |
|
114 int minute = tm.min() + 1; |
|
115 int second = tm.sec() + 1; |
4308
|
116 |
5275
|
117 octave_idx_type s0 = tm.mday() * hour * minute * second; |
|
118 octave_idx_type s1 = hour * minute * second; |
4308
|
119 |
|
120 s0 = force_to_fit_range (s0, 1, 2147483563); |
|
121 s1 = force_to_fit_range (s1, 1, 2147483399); |
|
122 |
|
123 F77_FUNC (setall, SETALL) (s0, s1); |
6326
|
124 F77_FUNC (setcgn, SETCGN) (stored_distribution); |
4308
|
125 |
5730
|
126 old_initialized = true; |
4308
|
127 } |
|
128 |
|
129 static inline void |
|
130 maybe_initialize (void) |
|
131 { |
5730
|
132 if (use_old_generators) |
|
133 { |
|
134 if (! old_initialized) |
|
135 do_old_initialization (); |
|
136 } |
|
137 else |
|
138 { |
|
139 if (! new_initialized) |
|
140 { |
|
141 oct_init_by_entropy (); |
|
142 new_initialized = true; |
|
143 } |
|
144 } |
4308
|
145 } |
|
146 |
|
147 double |
|
148 octave_rand::seed (void) |
|
149 { |
5730
|
150 if (! old_initialized) |
|
151 do_old_initialization (); |
4308
|
152 |
5275
|
153 union d2i { double d; octave_idx_type i[2]; }; |
4308
|
154 union d2i u; |
6435
|
155 |
|
156 oct_mach_info::float_format ff = oct_mach_info::native_float_format (); |
|
157 |
|
158 switch (ff) |
|
159 { |
|
160 case oct_mach_info::flt_fmt_ieee_big_endian: |
|
161 F77_FUNC (getsd, GETSD) (u.i[1], u.i[0]); |
|
162 break; |
|
163 default: |
|
164 F77_FUNC (getsd, GETSD) (u.i[0], u.i[1]); |
|
165 break; |
|
166 } |
|
167 |
4308
|
168 return u.d; |
|
169 } |
|
170 |
|
171 void |
|
172 octave_rand::seed (double s) |
|
173 { |
5730
|
174 use_old_generators = true; |
4308
|
175 maybe_initialize (); |
|
176 |
6435
|
177 int i0, i1; |
5275
|
178 union d2i { double d; octave_idx_type i[2]; }; |
4308
|
179 union d2i u; |
|
180 u.d = s; |
6435
|
181 |
|
182 oct_mach_info::float_format ff = oct_mach_info::native_float_format (); |
|
183 |
|
184 switch (ff) |
|
185 { |
|
186 case oct_mach_info::flt_fmt_ieee_big_endian: |
|
187 i1 = force_to_fit_range (u.i[0], 1, 2147483563); |
|
188 i0 = force_to_fit_range (u.i[1], 1, 2147483399); |
|
189 break; |
|
190 default: |
|
191 i0 = force_to_fit_range (u.i[0], 1, 2147483563); |
|
192 i1 = force_to_fit_range (u.i[1], 1, 2147483399); |
|
193 break; |
|
194 } |
|
195 |
4308
|
196 F77_FUNC (setsd, SETSD) (i0, i1); |
|
197 } |
|
198 |
5730
|
199 ColumnVector |
|
200 octave_rand::state (void) |
|
201 { |
|
202 ColumnVector s (MT_N + 1); |
|
203 if (! new_initialized) |
|
204 { |
|
205 oct_init_by_entropy (); |
|
206 new_initialized = true; |
|
207 } |
|
208 |
5828
|
209 OCTAVE_LOCAL_BUFFER (uint32_t, tmp, MT_N + 1); |
5730
|
210 oct_get_state (tmp); |
|
211 for (octave_idx_type i = 0; i <= MT_N; i++) |
|
212 s.elem (i) = static_cast<double>(tmp [i]); |
|
213 return s; |
|
214 } |
|
215 |
|
216 void |
|
217 octave_rand::state (const ColumnVector &s) |
|
218 { |
|
219 use_old_generators = false; |
|
220 maybe_initialize (); |
|
221 |
|
222 octave_idx_type len = s.length(); |
|
223 octave_idx_type n = len < MT_N + 1 ? len : MT_N + 1; |
5828
|
224 OCTAVE_LOCAL_BUFFER (uint32_t, tmp, MT_N + 1); |
5730
|
225 for (octave_idx_type i = 0; i < n; i++) |
5828
|
226 tmp[i] = static_cast<uint32_t> (s.elem(i)); |
5730
|
227 |
|
228 if (len == MT_N + 1 && tmp[MT_N] <= MT_N && tmp[MT_N] > 0) |
|
229 oct_set_state (tmp); |
|
230 else |
|
231 oct_init_by_array (tmp, len); |
|
232 } |
|
233 |
4308
|
234 std::string |
|
235 octave_rand::distribution (void) |
|
236 { |
|
237 maybe_initialize (); |
|
238 |
|
239 if (current_distribution == uniform_dist) |
|
240 return "uniform"; |
|
241 else if (current_distribution == normal_dist) |
|
242 return "normal"; |
5730
|
243 else if (current_distribution == expon_dist) |
|
244 return "exponential"; |
|
245 else if (current_distribution == poisson_dist) |
|
246 return "poisson"; |
|
247 else if (current_distribution == gamma_dist) |
|
248 return "gamma"; |
4308
|
249 else |
|
250 { |
|
251 abort (); |
|
252 return ""; |
|
253 } |
|
254 } |
|
255 |
|
256 void |
|
257 octave_rand::distribution (const std::string& d) |
|
258 { |
|
259 if (d == "uniform") |
|
260 octave_rand::uniform_distribution (); |
|
261 else if (d == "normal") |
|
262 octave_rand::normal_distribution (); |
5730
|
263 else if (d == "exponential") |
|
264 octave_rand::exponential_distribution (); |
|
265 else if (d == "poisson") |
|
266 octave_rand::poisson_distribution (); |
|
267 else if (d == "gamma") |
|
268 octave_rand::gamma_distribution (); |
4308
|
269 else |
|
270 (*current_liboctave_error_handler) ("rand: invalid distribution"); |
|
271 } |
|
272 |
|
273 void |
|
274 octave_rand::uniform_distribution (void) |
|
275 { |
|
276 maybe_initialize (); |
|
277 |
|
278 current_distribution = uniform_dist; |
|
279 |
|
280 F77_FUNC (setcgn, SETCGN) (uniform_dist); |
|
281 } |
|
282 |
|
283 void |
|
284 octave_rand::normal_distribution (void) |
|
285 { |
|
286 maybe_initialize (); |
|
287 |
|
288 current_distribution = normal_dist; |
|
289 |
|
290 F77_FUNC (setcgn, SETCGN) (normal_dist); |
|
291 } |
|
292 |
5730
|
293 void |
|
294 octave_rand::exponential_distribution (void) |
|
295 { |
|
296 maybe_initialize (); |
|
297 |
|
298 current_distribution = expon_dist; |
|
299 |
|
300 F77_FUNC (setcgn, SETCGN) (expon_dist); |
|
301 } |
|
302 |
|
303 void |
|
304 octave_rand::poisson_distribution (void) |
|
305 { |
|
306 maybe_initialize (); |
|
307 |
|
308 current_distribution = poisson_dist; |
|
309 |
|
310 F77_FUNC (setcgn, SETCGN) (poisson_dist); |
|
311 } |
|
312 |
|
313 void |
|
314 octave_rand::gamma_distribution (void) |
|
315 { |
|
316 maybe_initialize (); |
|
317 |
|
318 current_distribution = gamma_dist; |
|
319 |
|
320 F77_FUNC (setcgn, SETCGN) (gamma_dist); |
|
321 } |
|
322 |
|
323 |
4308
|
324 double |
5730
|
325 octave_rand::scalar (double a) |
4308
|
326 { |
|
327 maybe_initialize (); |
|
328 |
|
329 double retval = 0.0; |
|
330 |
5730
|
331 if (use_old_generators) |
4308
|
332 { |
5730
|
333 switch (current_distribution) |
|
334 { |
|
335 case uniform_dist: |
|
336 F77_FUNC (dgenunf, DGENUNF) (0.0, 1.0, retval); |
|
337 break; |
4308
|
338 |
5730
|
339 case normal_dist: |
|
340 F77_FUNC (dgennor, DGENNOR) (0.0, 1.0, retval); |
|
341 break; |
4308
|
342 |
5730
|
343 case expon_dist: |
|
344 F77_FUNC (dgenexp, DGENEXP) (1.0, retval); |
|
345 break; |
4308
|
346 |
5730
|
347 case poisson_dist: |
|
348 if (a < 0.0 || xisnan(a) || xisinf(a)) |
|
349 retval = octave_NaN; |
|
350 else |
|
351 { |
|
352 // workaround bug in ignpoi, by calling with different Mu |
|
353 F77_FUNC (dignpoi, DIGNPOI) (a + 1, retval); |
|
354 F77_FUNC (dignpoi, DIGNPOI) (a, retval); |
|
355 } |
|
356 break; |
4308
|
357 |
5730
|
358 case gamma_dist: |
|
359 if (a <= 0.0 || xisnan(a) || xisinf(a)) |
|
360 retval = octave_NaN; |
|
361 else |
|
362 F77_FUNC (dgengam, DGENGAM) (1.0, a, retval); |
|
363 break; |
4308
|
364 |
5730
|
365 default: |
|
366 abort (); |
|
367 break; |
4308
|
368 } |
|
369 } |
|
370 else |
4543
|
371 { |
|
372 switch (current_distribution) |
|
373 { |
|
374 case uniform_dist: |
5730
|
375 retval = oct_randu(); |
4543
|
376 break; |
|
377 |
|
378 case normal_dist: |
5730
|
379 retval = oct_randn(); |
|
380 break; |
|
381 |
|
382 case expon_dist: |
|
383 retval = oct_rande(); |
|
384 break; |
|
385 |
|
386 case poisson_dist: |
|
387 retval = oct_randp(a); |
|
388 break; |
|
389 |
|
390 case gamma_dist: |
|
391 retval = oct_randg(a); |
4543
|
392 break; |
|
393 |
|
394 default: |
|
395 abort (); |
|
396 break; |
|
397 } |
|
398 } |
|
399 |
|
400 return retval; |
|
401 } |
|
402 |
5730
|
403 #define MAKE_RAND(len) \ |
4308
|
404 do \ |
|
405 { \ |
|
406 double val; \ |
5730
|
407 for (volatile octave_idx_type i = 0; i < len; i++) \ |
4308
|
408 { \ |
|
409 OCTAVE_QUIT; \ |
5730
|
410 RAND_FUNC (val); \ |
|
411 v[i] = val; \ |
4308
|
412 } \ |
|
413 } \ |
|
414 while (0) |
|
415 |
5730
|
416 static void |
|
417 fill_rand (octave_idx_type len, double *v, double a) |
|
418 { |
|
419 maybe_initialize (); |
|
420 |
|
421 if (len < 1) |
|
422 return; |
|
423 |
|
424 switch (current_distribution) |
|
425 { |
|
426 case uniform_dist: |
|
427 if (use_old_generators) |
|
428 { |
|
429 #define RAND_FUNC(x) F77_FUNC (dgenunf, DGENUNF) (0.0, 1.0, x) |
|
430 MAKE_RAND (len); |
|
431 #undef RAND_FUNC |
|
432 } |
|
433 else |
|
434 oct_fill_randu (len, v); |
|
435 break; |
|
436 |
|
437 case normal_dist: |
|
438 if (use_old_generators) |
|
439 { |
|
440 #define RAND_FUNC(x) F77_FUNC (dgennor, DGENNOR) (0.0, 1.0, x) |
|
441 MAKE_RAND (len); |
|
442 #undef RAND_FUNC |
|
443 } |
|
444 else |
|
445 oct_fill_randn (len, v); |
|
446 break; |
|
447 |
|
448 case expon_dist: |
|
449 if (use_old_generators) |
|
450 { |
|
451 #define RAND_FUNC(x) F77_FUNC (dgenexp, DGENEXP) (1.0, x) |
|
452 MAKE_RAND (len); |
|
453 #undef RAND_FUNC |
|
454 } |
|
455 else |
|
456 oct_fill_rande (len, v); |
|
457 break; |
|
458 |
|
459 case poisson_dist: |
|
460 if (use_old_generators) |
|
461 { |
|
462 if (a < 0.0 || xisnan(a) || xisinf(a)) |
|
463 #define RAND_FUNC(x) x = octave_NaN; |
|
464 MAKE_RAND (len); |
|
465 #undef RAND_FUNC |
|
466 else |
|
467 { |
|
468 // workaround bug in ignpoi, by calling with different Mu |
|
469 double tmp; |
|
470 F77_FUNC (dignpoi, DIGNPOI) (a + 1, tmp); |
|
471 #define RAND_FUNC(x) F77_FUNC (dignpoi, DIGNPOI) (a, x) |
|
472 MAKE_RAND (len); |
|
473 #undef RAND_FUNC |
|
474 } |
|
475 } |
|
476 else |
|
477 oct_fill_randp (a, len, v); |
|
478 break; |
|
479 |
|
480 case gamma_dist: |
|
481 if (use_old_generators) |
|
482 { |
|
483 if (a <= 0.0 || xisnan(a) || xisinf(a)) |
|
484 #define RAND_FUNC(x) x = octave_NaN; |
|
485 MAKE_RAND (len); |
|
486 #undef RAND_FUNC |
|
487 else |
|
488 #define RAND_FUNC(x) F77_FUNC (dgengam, DGENGAM) (1.0, a, x) |
|
489 MAKE_RAND (len); |
|
490 #undef RAND_FUNC |
|
491 } |
|
492 else |
|
493 oct_fill_randg (a, len, v); |
|
494 break; |
|
495 |
|
496 default: |
|
497 abort (); |
|
498 break; |
|
499 } |
|
500 |
|
501 return; |
|
502 } |
|
503 |
|
504 Matrix |
|
505 octave_rand::matrix (octave_idx_type n, octave_idx_type m, double a) |
|
506 { |
|
507 Matrix retval; |
|
508 |
|
509 if (n >= 0 && m >= 0) |
|
510 { |
|
511 retval.resize (n, m); |
|
512 |
|
513 if (n > 0 && m > 0) |
|
514 fill_rand (retval.capacity(), retval.fortran_vec(), a); |
|
515 } |
|
516 else |
|
517 (*current_liboctave_error_handler) ("rand: invalid negative argument"); |
|
518 |
|
519 return retval; |
|
520 } |
|
521 |
|
522 NDArray |
|
523 octave_rand::nd_array (const dim_vector& dims, double a) |
|
524 { |
|
525 NDArray retval; |
|
526 |
|
527 if (! dims.all_zero ()) |
|
528 { |
|
529 retval.resize (dims); |
|
530 |
|
531 fill_rand (retval.capacity(), retval.fortran_vec(), a); |
|
532 } |
|
533 |
|
534 return retval; |
|
535 } |
|
536 |
4308
|
537 Array<double> |
5730
|
538 octave_rand::vector (octave_idx_type n, double a) |
4308
|
539 { |
|
540 maybe_initialize (); |
|
541 |
|
542 Array<double> retval; |
|
543 |
|
544 if (n > 0) |
|
545 { |
|
546 retval.resize (n); |
|
547 |
5730
|
548 fill_rand (retval.capacity(), retval.fortran_vec(), a); |
4308
|
549 } |
|
550 else if (n < 0) |
|
551 (*current_liboctave_error_handler) ("rand: invalid negative argument"); |
|
552 |
|
553 return retval; |
|
554 } |
|
555 |
|
556 /* |
|
557 ;;; Local Variables: *** |
|
558 ;;; mode: C++ *** |
|
559 ;;; End: *** |
|
560 */ |