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
|
/*
Copyright (C) 2003 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 2, 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, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "f77-fcn.h"
#include "lo-error.h"
#include "oct-rand.h"
#include "oct-time.h"
// Possible distributions of random numbers. This was handled with an
// enum, but unwind_protecting that doesn't work so well.
#define uniform_dist 1
#define normal_dist 2
// Current distribution of random numbers.
static int current_distribution = uniform_dist;
// Has the seed been set yet?
static bool initialized = false;
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 (setall, SETALL) (const int&, const int&);
F77_RET_T
F77_FUNC (getsd, GETSD) (int&, int&);
F77_RET_T
F77_FUNC (setsd, SETSD) (const int&, const int&);
F77_RET_T
F77_FUNC (setcgn, SETCGN) (const int&);
}
static int
force_to_fit_range (int i, int lo, int 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_initialization (void)
{
octave_localtime tm;
int hour = tm.hour() + 1;
int minute = tm.min() + 1;
int second = tm.sec() + 1;
int s0 = tm.mday() * hour * minute * second;
int 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);
initialized = true;
}
static inline void
maybe_initialize (void)
{
if (! initialized)
do_initialization ();
}
double
octave_rand::seed (void)
{
maybe_initialize ();
union d2i { double d; int i[2]; };
union d2i u;
F77_FUNC (getsd, GETSD) (u.i[0], u.i[1]);
return u.d;
}
void
octave_rand::seed (double s)
{
maybe_initialize ();
union d2i { double d; int i[2]; };
union d2i u;
u.d = s;
int i0 = force_to_fit_range (u.i[0], 1, 2147483563);
int i1 = force_to_fit_range (u.i[1], 1, 2147483399);
F77_FUNC (setsd, SETSD) (i0, i1);
}
std::string
octave_rand::distribution (void)
{
maybe_initialize ();
if (current_distribution == uniform_dist)
return "uniform";
else if (current_distribution == normal_dist)
return "normal";
else
{
abort ();
return "";
}
}
void
octave_rand::distribution (const std::string& d)
{
if (d == "uniform")
octave_rand::uniform_distribution ();
else if (d == "normal")
octave_rand::normal_distribution ();
else
(*current_liboctave_error_handler) ("rand: invalid distribution");
}
void
octave_rand::uniform_distribution (void)
{
maybe_initialize ();
current_distribution = uniform_dist;
F77_FUNC (setcgn, SETCGN) (uniform_dist);
}
void
octave_rand::normal_distribution (void)
{
maybe_initialize ();
current_distribution = normal_dist;
F77_FUNC (setcgn, SETCGN) (normal_dist);
}
double
octave_rand::scalar (void)
{
maybe_initialize ();
double retval = 0.0;
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;
default:
abort ();
break;
}
return retval;
}
#define MAKE_RAND_MAT(mat, nr, nc, f, F) \
do \
{ \
double val; \
for (volatile int j = 0; j < nc; j++) \
for (volatile int i = 0; i < nr; i++) \
{ \
OCTAVE_QUIT; \
F77_FUNC (f, F) (0.0, 1.0, val); \
mat(i,j) = val; \
} \
} \
while (0)
Matrix
octave_rand::matrix (int n, int m)
{
maybe_initialize ();
Matrix retval;
if (n >= 0 && m >= 0)
{
retval.resize (n, m);
if (n > 0 && m > 0)
{
switch (current_distribution)
{
case uniform_dist:
MAKE_RAND_MAT (retval, n, m, dgenunf, DGENUNF);
break;
case normal_dist:
MAKE_RAND_MAT (retval, n, m, dgennor, DGENNOR);
break;
default:
abort ();
break;
}
}
}
else
(*current_liboctave_error_handler) ("rand: invalid negative argument");
return retval;
}
#define MAKE_RAND_ND_ARRAY(mat, len, f, F) \
do \
{ \
double val; \
for (volatile int i = 0; i < len; i++) \
{ \
OCTAVE_QUIT; \
F77_FUNC (f, F) (0.0, 1.0, val); \
mat(i) = val; \
} \
} \
while (0)
NDArray
octave_rand::nd_array (const dim_vector& dims)
{
maybe_initialize ();
NDArray retval;
if (! dims.all_zero ())
{
retval.resize (dims);
int len = retval.length ();
switch (current_distribution)
{
case uniform_dist:
MAKE_RAND_ND_ARRAY (retval, len, dgenunf, DGENUNF);
break;
case normal_dist:
MAKE_RAND_ND_ARRAY (retval, len, dgennor, DGENNOR);
break;
default:
abort ();
break;
}
}
return retval;
}
#define MAKE_RAND_ARRAY(array, n, f, F) \
do \
{ \
double val; \
for (volatile int i = 0; i < n; i++) \
{ \
OCTAVE_QUIT; \
F77_FUNC (f, F) (0.0, 1.0, val); \
array(i) = val; \
} \
} \
while (0)
Array<double>
octave_rand::vector (int n)
{
maybe_initialize ();
Array<double> retval;
if (n > 0)
{
retval.resize (n);
switch (current_distribution)
{
case uniform_dist:
MAKE_RAND_ARRAY (retval, n, dgenunf, DGENUNF);
break;
case normal_dist:
MAKE_RAND_ARRAY (retval, n, dgennor, DGENNOR);
break;
default:
abort ();
break;
}
}
else if (n < 0)
(*current_liboctave_error_handler) ("rand: invalid negative argument");
return retval;
}
/*
;;; Local Variables: ***
;;; mode: C++ ***
;;; End: ***
*/
|