File: oct-rand.cc

package info (click to toggle)
octave3.0 1%3A3.0.1-6lenny3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 49,912 kB
  • ctags: 27,149
  • sloc: cpp: 166,567; fortran: 61,399; ansic: 8,766; sh: 5,856; lex: 4,419; makefile: 3,965; yacc: 3,013; lisp: 1,692; objc: 889; perl: 795; awk: 532
file content (689 lines) | stat: -rw-r--r-- 13,680 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/*

Copyright (C) 2003, 2005, 2006, 2007, 2008 John W. Eaton

This file is part of Octave.

Octave is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

Octave is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with Octave; see the file COPYING.  If not, see
<http://www.gnu.org/licenses/>.

*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <map>
#include <vector>

#include "f77-fcn.h"
#include "lo-ieee.h"
#include "lo-error.h"
#include "lo-mappers.h"
#include "oct-rand.h"
#include "oct-time.h"
#include "data-conv.h"
#include "randmtzig.h"
#include "randpoisson.h"
#include "randgamma.h"
#include "mach-info.h"

static const int unknown_dist = 0;
static const int uniform_dist = 1;
static const int normal_dist = 2;
static const int expon_dist = 3;
static const int poisson_dist = 4;
static const int gamma_dist = 5;

// Current distribution of random numbers.
static int current_distribution = uniform_dist;

// Has the seed/state been set yet?
static bool old_initialized = false;
static bool new_initialized = false;
static bool use_old_generators = false;

std::map<int, ColumnVector> rand_states;

extern "C"
{
  F77_RET_T
  F77_FUNC (dgennor, DGENNOR) (const double&, const double&, double&);

  F77_RET_T
  F77_FUNC (dgenunf, DGENUNF) (const double&, const double&, double&);

  F77_RET_T
  F77_FUNC (dgenexp, DGENEXP) (const double&, double&);

  F77_RET_T
  F77_FUNC (dignpoi, DIGNPOI) (const double&, double&);

  F77_RET_T
  F77_FUNC (dgengam, DGENGAM) (const double&, const double&, double&);

  F77_RET_T
  F77_FUNC (setall, SETALL) (const octave_idx_type&, const octave_idx_type&);

  F77_RET_T
  F77_FUNC (getsd, GETSD) (octave_idx_type&, octave_idx_type&);

  F77_RET_T
  F77_FUNC (setsd, SETSD) (const octave_idx_type&, const octave_idx_type&);

  F77_RET_T
  F77_FUNC (setcgn, SETCGN) (const octave_idx_type&);
}

static octave_idx_type
force_to_fit_range (octave_idx_type i, octave_idx_type lo, octave_idx_type hi)
{
  assert (hi > lo && lo >= 0 && hi > lo);

  i = i > 0 ? i : -i;

  if (i < lo)
    i = lo;
  else if (i > hi)
    i = i % hi;

  return i;
}

// Make the random number generator give us a different sequence every
// time we start octave unless we specifically set the seed.  The
// technique used below will cycle monthly, but it it does seem to
// work ok to give fairly different seeds each time Octave starts.

static void
do_old_initialization (void)
{
  octave_localtime tm;
  int stored_distribution = current_distribution;
  F77_FUNC (setcgn, SETCGN) (uniform_dist);

  int hour = tm.hour() + 1;
  int minute = tm.min() + 1;
  int second = tm.sec() + 1;

  octave_idx_type s0 = tm.mday() * hour * minute * second;
  octave_idx_type s1 = hour * minute * second;

  s0 = force_to_fit_range (s0, 1, 2147483563);
  s1 = force_to_fit_range (s1, 1, 2147483399);

  F77_FUNC (setall, SETALL) (s0, s1);
  F77_FUNC (setcgn, SETCGN) (stored_distribution);

  old_initialized = true;
}

static ColumnVector
get_internal_state (void)
{
  ColumnVector s (MT_N + 1);

  OCTAVE_LOCAL_BUFFER (uint32_t, tmp, MT_N + 1);

  oct_get_state (tmp);

  for (octave_idx_type i = 0; i <= MT_N; i++)
    s.elem (i) = static_cast<double> (tmp [i]);

  return s;
}

static inline void
save_state (void)
{
  rand_states[current_distribution] = get_internal_state ();;
}

static void
initialize_rand_states (void)
{
  if (! new_initialized)
    {
      oct_init_by_entropy ();

      ColumnVector s = get_internal_state ();

      rand_states[uniform_dist] = s;
      rand_states[normal_dist] = s;
      rand_states[expon_dist] = s;
      rand_states[poisson_dist] = s;
      rand_states[gamma_dist] = s;

      new_initialized = true;
    }
}

static inline void
maybe_initialize (void)
{
  if (use_old_generators)
    {
      if (! old_initialized)
	do_old_initialization ();
    }
  else
    {
      if (! new_initialized)
	initialize_rand_states ();
    }
}

static int
get_dist_id (const std::string& d)
{
  int retval = unknown_dist;

  if (d == "uniform" || d == "rand")
    retval = uniform_dist;
  else if (d == "normal" || d == "randn")
    retval = normal_dist;
  else if (d == "exponential" || d == "rande")
    retval = expon_dist;
  else if (d == "poisson" || d == "randp")
    retval = poisson_dist;
  else if (d == "gamma" || d == "randg")
    retval = gamma_dist;
  else
    (*current_liboctave_error_handler)
      ("rand: invalid distribution `%s'", d.c_str ());

  return retval;
}

static void
set_internal_state (const ColumnVector& s)
{
  octave_idx_type len = s.length ();
  octave_idx_type n = len < MT_N + 1 ? len : MT_N + 1;

  OCTAVE_LOCAL_BUFFER (uint32_t, tmp, MT_N + 1);

  for (octave_idx_type i = 0; i < n; i++)
    tmp[i] = static_cast<uint32_t> (s.elem(i));

  if (len == MT_N + 1 && tmp[MT_N] <= MT_N && tmp[MT_N] > 0)
    oct_set_state (tmp);
  else
    oct_init_by_array (tmp, len);
}

static inline void
switch_to_generator (int dist)
{
  if (dist != current_distribution)
    {
      current_distribution = dist;

      set_internal_state (rand_states[dist]);
    }
}

double
octave_rand::seed (void)
{
  if (! old_initialized)
    do_old_initialization ();

  union d2i { double d; octave_idx_type i[2]; };
  union d2i u;
    
  oct_mach_info::float_format ff = oct_mach_info::native_float_format ();

  switch (ff)
    {
    case oct_mach_info::flt_fmt_ieee_big_endian:
      F77_FUNC (getsd, GETSD) (u.i[1], u.i[0]);
      break;
    default:
      F77_FUNC (getsd, GETSD) (u.i[0], u.i[1]);
      break;
    }

  return u.d;
}

void
octave_rand::seed (double s)
{
  use_old_generators = true;

  maybe_initialize ();

  int i0, i1;
  union d2i { double d; octave_idx_type i[2]; };
  union d2i u;
  u.d = s;

  oct_mach_info::float_format ff = oct_mach_info::native_float_format ();

  switch (ff)
    {
    case oct_mach_info::flt_fmt_ieee_big_endian:
      i1 = force_to_fit_range (u.i[0], 1, 2147483563);
      i0 = force_to_fit_range (u.i[1], 1, 2147483399);
      break;
    default:
      i0 = force_to_fit_range (u.i[0], 1, 2147483563);
      i1 = force_to_fit_range (u.i[1], 1, 2147483399);
      break;
    }

  F77_FUNC (setsd, SETSD) (i0, i1);
}

ColumnVector
octave_rand::state (const std::string& d)
{
  if (! new_initialized)
    initialize_rand_states ();

  return rand_states[d.empty () ? current_distribution : get_dist_id (d)];
}

void
octave_rand::state (const ColumnVector& s, const std::string& d)
{
  use_old_generators = false;

  maybe_initialize ();

  int old_dist = current_distribution;

  int new_dist = d.empty () ? current_distribution : get_dist_id (d);

  ColumnVector saved_state;

  if (old_dist != new_dist)
    saved_state = get_internal_state ();

  set_internal_state (s);

  rand_states[new_dist] = get_internal_state ();

  if (old_dist != new_dist)
    rand_states[old_dist] = saved_state;
}

std::string
octave_rand::distribution (void)
{
  std::string retval;

  maybe_initialize ();

  switch (current_distribution)
    {
    case uniform_dist:
      retval = "uniform";
      break;

    case normal_dist:
      retval = "normal";
      break;

    case expon_dist:
      retval = "exponential";
      break;

    case poisson_dist:
      retval = "poisson";
      break;

    case gamma_dist:
      retval = "gamma";
      break;

    default:
      (*current_liboctave_error_handler)
	("rand: invalid distribution ID = %d", current_distribution);
      break;
    }

  return retval;
}

void
octave_rand::distribution (const std::string& d)
{
  int id = get_dist_id (d);

  switch (id)
    {
    case uniform_dist:
      octave_rand::uniform_distribution ();
      break;

    case normal_dist:
      octave_rand::normal_distribution ();
      break;

    case expon_dist:
      octave_rand::exponential_distribution ();
      break;

    case poisson_dist:
      octave_rand::poisson_distribution ();
      break;

    case gamma_dist:
      octave_rand::gamma_distribution ();
      break;

    default:
      (*current_liboctave_error_handler)
	("rand: invalid distribution ID = %d", id);
      break;
    }
}

void
octave_rand::uniform_distribution (void)
{
  maybe_initialize ();

  switch_to_generator (uniform_dist);

  F77_FUNC (setcgn, SETCGN) (uniform_dist);
}

void
octave_rand::normal_distribution (void)
{
  maybe_initialize ();

  switch_to_generator (normal_dist);

  F77_FUNC (setcgn, SETCGN) (normal_dist);
}

void
octave_rand::exponential_distribution (void)
{
  maybe_initialize ();

  switch_to_generator (expon_dist);

  F77_FUNC (setcgn, SETCGN) (expon_dist);
}

void
octave_rand::poisson_distribution (void)
{
  maybe_initialize ();

  switch_to_generator (poisson_dist);

  F77_FUNC (setcgn, SETCGN) (poisson_dist);
}

void
octave_rand::gamma_distribution (void)
{
  maybe_initialize ();

  switch_to_generator (gamma_dist);

  F77_FUNC (setcgn, SETCGN) (gamma_dist);
}


double
octave_rand::scalar (double a)
{
  maybe_initialize ();

  double retval = 0.0;

  if (use_old_generators)
    {
      switch (current_distribution)
	{
	case uniform_dist:
	  F77_FUNC (dgenunf, DGENUNF) (0.0, 1.0, retval);
	  break;

	case normal_dist:
	  F77_FUNC (dgennor, DGENNOR) (0.0, 1.0, retval);
	  break;

	case expon_dist:
	  F77_FUNC (dgenexp, DGENEXP) (1.0, retval);
	  break;

	case poisson_dist:
	  if (a < 0.0 || xisnan(a) || xisinf(a))
	    retval = octave_NaN;
	  else
	    {
	      // workaround bug in ignpoi, by calling with different Mu
	      F77_FUNC (dignpoi, DIGNPOI) (a + 1, retval);
	      F77_FUNC (dignpoi, DIGNPOI) (a, retval);
	    }
	  break;

	case gamma_dist:
	  if (a <= 0.0 || xisnan(a) || xisinf(a))
	    retval = octave_NaN;
	  else
	    F77_FUNC (dgengam, DGENGAM) (1.0, a, retval);
	  break;

	default:
	  (*current_liboctave_error_handler) 
	    ("rand: invalid distribution ID = %d", current_distribution);
	  break;
	}
    }
  else
    {
      switch (current_distribution)
	{
	case uniform_dist:
	  retval = oct_randu ();
	  break;

	case normal_dist:
	  retval = oct_randn ();
	  break;

	case expon_dist:
	  retval = oct_rande ();
	  break;

	case poisson_dist:
	  retval = oct_randp (a);
	  break;

	case gamma_dist:
	  retval = oct_randg (a);
	  break;

	default:
	  (*current_liboctave_error_handler)
	    ("rand: invalid distribution ID = %d", current_distribution);
	  break;
	}

      save_state ();
    }

  return retval;
}

#define MAKE_RAND(len) \
  do \
    { \
      double val; \
      for (volatile octave_idx_type i = 0; i < len; i++) \
	{ \
	  OCTAVE_QUIT; \
	  RAND_FUNC (val); \
	  v[i] = val; \
	} \
    } \
  while (0)

static void
fill_rand (octave_idx_type len, double *v, double a)
{
  maybe_initialize ();

  if (len < 1)
    return;

  switch (current_distribution)
    {
    case uniform_dist:
      if (use_old_generators)
	{
#define RAND_FUNC(x) F77_FUNC (dgenunf, DGENUNF) (0.0, 1.0, x)
	  MAKE_RAND (len);
#undef RAND_FUNC
	}
      else
	oct_fill_randu (len, v);
      break;

    case normal_dist:
      if (use_old_generators)
	{
#define RAND_FUNC(x) F77_FUNC (dgennor, DGENNOR) (0.0, 1.0, x)
	  MAKE_RAND (len);
#undef RAND_FUNC
	}
      else
	oct_fill_randn (len, v);
      break;

    case expon_dist:
      if (use_old_generators)
	{
#define RAND_FUNC(x) F77_FUNC (dgenexp, DGENEXP) (1.0, x)
	  MAKE_RAND (len);
#undef RAND_FUNC
	}
      else
	oct_fill_rande (len, v);
      break;

    case poisson_dist:
      if (use_old_generators)
	{
	  if (a < 0.0 || xisnan(a) || xisinf(a))
#define RAND_FUNC(x) x = octave_NaN;
	    MAKE_RAND (len);
#undef RAND_FUNC
	  else
	    {
	      // workaround bug in ignpoi, by calling with different Mu
	      double tmp;
	      F77_FUNC (dignpoi, DIGNPOI) (a + 1, tmp);
#define RAND_FUNC(x) F77_FUNC (dignpoi, DIGNPOI) (a, x)
		MAKE_RAND (len);
#undef RAND_FUNC
	    }
	}
      else
	oct_fill_randp (a, len, v);
      break;

    case gamma_dist:
      if (use_old_generators)
	{
	  if (a <= 0.0 || xisnan(a) || xisinf(a))
#define RAND_FUNC(x) x = octave_NaN;
	    MAKE_RAND (len);
#undef RAND_FUNC
	  else
#define RAND_FUNC(x) F77_FUNC (dgengam, DGENGAM) (1.0, a, x)
	    MAKE_RAND (len);
#undef RAND_FUNC
	}
      else
	oct_fill_randg (a, len, v);
      break;

    default:
      (*current_liboctave_error_handler)
	("rand: invalid distribution ID = %d", current_distribution);
      break;
    }

  save_state ();

  return;
}

Matrix
octave_rand::matrix (octave_idx_type n, octave_idx_type m, double a)
{
  Matrix retval;

  if (n >= 0 && m >= 0)
    {
      retval.resize (n, m);

      if (n > 0 && m > 0)
	fill_rand (retval.capacity(), retval.fortran_vec(), a);
    }
  else
    (*current_liboctave_error_handler) ("rand: invalid negative argument");

  return retval;
}

NDArray
octave_rand::nd_array (const dim_vector& dims, double a)
{
  NDArray retval;

  if (! dims.all_zero ())
    {
      retval.resize (dims);

      fill_rand (retval.capacity(), retval.fortran_vec(), a);
    }

  return retval;
}

Array<double>
octave_rand::vector (octave_idx_type n, double a)
{
  maybe_initialize ();

  Array<double> retval;

  if (n > 0)
    {
      retval.resize (n);

      fill_rand (retval.capacity(), retval.fortran_vec(), a);
    }
  else if (n < 0)
    (*current_liboctave_error_handler) ("rand: invalid negative argument");

  return retval;
}

/*
;;; Local Variables: ***
;;; mode: C++ ***
;;; End: ***
*/