2928
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <ctime> |
|
28 |
|
29 #include <string> |
|
30 |
|
31 #include "f77-fcn.h" |
|
32 #include "lo-mappers.h" |
4153
|
33 #include "quit.h" |
2928
|
34 |
|
35 #include "defun-dld.h" |
|
36 #include "error.h" |
|
37 #include "gripes.h" |
|
38 #include "oct-obj.h" |
|
39 #include "unwind-prot.h" |
|
40 #include "utils.h" |
|
41 |
|
42 // Possible distributions of random numbers. This was handled with an |
|
43 // enum, but unwind_protecting that doesn't work so well. |
|
44 #define uniform_dist 1 |
|
45 #define normal_dist 2 |
|
46 |
|
47 // Current distribution of random numbers. |
|
48 static int current_distribution = uniform_dist; |
|
49 |
|
50 // Has the seed been set yet? |
|
51 static int initialized = 0; |
|
52 |
|
53 extern "C" |
|
54 { |
3887
|
55 int F77_FUNC (dgennor, DGENNOR) (const double&, const double&, |
3189
|
56 double&); |
2928
|
57 |
3887
|
58 int F77_FUNC (dgenunf, DGENUNF) (const double&, const double&, |
3189
|
59 double&); |
2928
|
60 |
3887
|
61 int F77_FUNC (setall, SETALL) (const int&, const int&); |
2928
|
62 |
3887
|
63 int F77_FUNC (getsd, GETSD) (int&, int&); |
2928
|
64 |
3887
|
65 int F77_FUNC (setsd, SETSD) (const int&, const int&); |
2928
|
66 |
3887
|
67 int F77_FUNC (setcgn, SETCGN) (const int&); |
2928
|
68 } |
|
69 |
|
70 static double |
|
71 curr_rand_seed (void) |
|
72 { |
|
73 union d2i { double d; int i[2]; }; |
|
74 union d2i u; |
4153
|
75 F77_FUNC (getsd, GETSD) (u.i[0], u.i[1]); |
2928
|
76 return u.d; |
|
77 } |
|
78 |
|
79 static int |
|
80 force_to_fit_range (int i, int lo, int hi) |
|
81 { |
|
82 assert (hi > lo && lo >= 0 && hi > lo); |
|
83 |
|
84 i = i > 0 ? i : -i; |
|
85 |
|
86 if (i < lo) |
|
87 i = lo; |
|
88 else if (i > hi) |
|
89 i = i % hi; |
|
90 |
|
91 return i; |
|
92 } |
|
93 |
|
94 static void |
|
95 set_rand_seed (double val) |
|
96 { |
|
97 union d2i { double d; int i[2]; }; |
|
98 union d2i u; |
|
99 u.d = val; |
|
100 int i0 = force_to_fit_range (u.i[0], 1, 2147483563); |
|
101 int i1 = force_to_fit_range (u.i[1], 1, 2147483399); |
4153
|
102 F77_FUNC (setsd, SETSD) (i0, i1); |
2928
|
103 } |
|
104 |
3571
|
105 static const char * |
2928
|
106 curr_rand_dist (void) |
|
107 { |
|
108 if (current_distribution == uniform_dist) |
|
109 return "uniform"; |
|
110 else if (current_distribution == normal_dist) |
|
111 return "normal"; |
|
112 else |
|
113 { |
|
114 panic_impossible (); |
|
115 return 0; |
|
116 } |
|
117 } |
|
118 |
|
119 // Make the random number generator give us a different sequence every |
|
120 // time we start octave unless we specifically set the seed. The |
|
121 // technique used below will cycle monthly, but it it does seem to |
|
122 // work ok to give fairly different seeds each time Octave starts. |
|
123 |
|
124 static void |
|
125 do_initialization (void) |
|
126 { |
|
127 time_t now; |
|
128 struct tm *tm; |
|
129 |
|
130 time (&now); |
|
131 tm = localtime (&now); |
|
132 |
|
133 int hour = tm->tm_hour + 1; |
|
134 int minute = tm->tm_min + 1; |
|
135 int second = tm->tm_sec + 1; |
|
136 |
|
137 int s0 = tm->tm_mday * hour * minute * second; |
|
138 int s1 = hour * minute * second; |
|
139 |
|
140 s0 = force_to_fit_range (s0, 1, 2147483563); |
|
141 s1 = force_to_fit_range (s1, 1, 2147483399); |
|
142 |
4153
|
143 F77_FUNC (setall, SETALL) (s0, s1); |
2928
|
144 |
|
145 initialized = 1; |
|
146 } |
|
147 |
4153
|
148 #define MAKE_RAND_MAT(mat, nr, nc, f, F) \ |
|
149 do \ |
|
150 { \ |
|
151 double val; \ |
|
152 for (volatile int j = 0; j < nc; j++) \ |
|
153 for (volatile int i = 0; i < nr; i++) \ |
|
154 { \ |
|
155 OCTAVE_QUIT; \ |
|
156 F77_FUNC (f, F) (0.0, 1.0, val); \ |
|
157 mat(i,j) = val; \ |
|
158 } \ |
|
159 } \ |
|
160 while (0) |
|
161 |
2928
|
162 static octave_value_list |
|
163 do_rand (const octave_value_list& args, int nargin) |
|
164 { |
|
165 octave_value_list retval; |
|
166 |
3157
|
167 volatile int n = 0; |
|
168 volatile int m = 0; |
2928
|
169 |
|
170 if (nargin == 0) |
|
171 { |
|
172 n = 1; |
|
173 m = 1; |
|
174 |
|
175 goto gen_matrix; |
|
176 } |
|
177 else if (nargin == 1) |
|
178 { |
|
179 octave_value tmp = args(0); |
|
180 |
|
181 if (tmp.is_string ()) |
|
182 { |
3523
|
183 std::string s_arg = tmp.string_value (); |
2928
|
184 |
|
185 if (s_arg == "dist") |
|
186 { |
|
187 retval(0) = curr_rand_dist (); |
|
188 } |
|
189 else if (s_arg == "seed") |
|
190 { |
|
191 retval(0) = curr_rand_seed (); |
|
192 } |
|
193 else if (s_arg == "uniform") |
|
194 { |
|
195 current_distribution = uniform_dist; |
|
196 |
4153
|
197 F77_FUNC (setcgn, SETCGN) (uniform_dist); |
2928
|
198 } |
|
199 else if (s_arg == "normal") |
|
200 { |
|
201 current_distribution = normal_dist; |
|
202 |
4153
|
203 F77_FUNC (setcgn, SETCGN) (normal_dist); |
2928
|
204 } |
|
205 else |
|
206 error ("rand: unrecognized string argument"); |
|
207 } |
|
208 else if (tmp.is_scalar_type ()) |
|
209 { |
|
210 double dval = tmp.double_value (); |
|
211 |
|
212 if (xisnan (dval)) |
|
213 { |
|
214 error ("rand: NaN is invalid a matrix dimension"); |
|
215 } |
|
216 else |
|
217 { |
|
218 m = n = NINT (tmp.double_value ()); |
|
219 |
|
220 if (! error_state) |
|
221 goto gen_matrix; |
|
222 } |
|
223 } |
|
224 else if (tmp.is_range ()) |
|
225 { |
|
226 Range r = tmp.range_value (); |
|
227 n = 1; |
|
228 m = r.nelem (); |
|
229 goto gen_matrix; |
|
230 } |
|
231 else if (tmp.is_matrix_type ()) |
|
232 { |
|
233 // XXX FIXME XXX -- this should probably use the function |
|
234 // from data.cc. |
|
235 |
|
236 Matrix a = args(0).matrix_value (); |
|
237 |
|
238 if (error_state) |
|
239 return retval; |
|
240 |
|
241 n = a.rows (); |
|
242 m = a.columns (); |
|
243 |
|
244 if (n == 1 && m == 2) |
|
245 { |
|
246 n = NINT (a (0, 0)); |
|
247 m = NINT (a (0, 1)); |
|
248 } |
|
249 else if (n == 2 && m == 1) |
|
250 { |
|
251 n = NINT (a (0, 0)); |
|
252 m = NINT (a (1, 0)); |
|
253 } |
|
254 else |
|
255 warning ("rand (A): use rand (size (A)) instead"); |
|
256 |
|
257 goto gen_matrix; |
|
258 } |
|
259 else |
|
260 { |
|
261 gripe_wrong_type_arg ("rand", tmp); |
|
262 return retval; |
|
263 } |
|
264 } |
|
265 else if (nargin == 2) |
|
266 { |
|
267 if (args(0).is_string ()) |
|
268 { |
|
269 if (args(0).string_value () == "seed") |
|
270 { |
|
271 double d = args(1).double_value (); |
|
272 |
|
273 if (! error_state) |
|
274 set_rand_seed (d); |
|
275 } |
3086
|
276 else |
|
277 error ("rand: unrecognized string argument"); |
2928
|
278 } |
|
279 else |
|
280 { |
|
281 double dval = args(0).double_value (); |
|
282 |
|
283 if (xisnan (dval)) |
|
284 { |
|
285 error ("rand: NaN is invalid as a matrix dimension"); |
|
286 } |
|
287 else |
|
288 { |
|
289 n = NINT (dval); |
|
290 |
|
291 if (! error_state) |
|
292 { |
|
293 m = NINT (args(1).double_value ()); |
|
294 |
|
295 if (! error_state) |
|
296 goto gen_matrix; |
|
297 } |
|
298 } |
|
299 } |
|
300 } |
|
301 |
|
302 return retval; |
|
303 |
|
304 gen_matrix: |
|
305 |
|
306 if (n == 0 || m == 0) |
|
307 { |
|
308 Matrix m; |
|
309 retval.resize (1, m); |
|
310 } |
|
311 else if (n > 0 && m > 0) |
|
312 { |
|
313 Matrix rand_mat (n, m); |
3157
|
314 |
4153
|
315 switch (current_distribution) |
|
316 { |
|
317 case uniform_dist: |
|
318 MAKE_RAND_MAT (rand_mat, n, m, dgenunf, DGENUNF); |
|
319 break; |
2928
|
320 |
4153
|
321 case normal_dist: |
|
322 MAKE_RAND_MAT (rand_mat, n, m, dgennor, DGENNOR); |
|
323 break; |
2928
|
324 |
4153
|
325 default: |
|
326 panic_impossible (); |
|
327 break; |
|
328 } |
2928
|
329 |
|
330 retval(0) = rand_mat; |
|
331 } |
|
332 else |
|
333 error ("rand: invalid negative argument"); |
|
334 |
|
335 return retval; |
|
336 } |
|
337 |
|
338 DEFUN_DLD (rand, args, nargout, |
3369
|
339 "-*- texinfo -*-\n\ |
|
340 @deftypefn {Loadable Function} {} rand (@var{x})\n\ |
|
341 @deftypefnx {Loadable Function} {} rand (@var{n}, @var{m})\n\ |
|
342 @deftypefnx {Loadable Function} {} rand (@code{\"seed\"}, @var{x})\n\ |
|
343 Return a matrix with random elements uniformly distributed on the\n\ |
|
344 interval (0, 1). The arguments are handled the same as the arguments\n\ |
|
345 for @code{eye}. In\n\ |
|
346 addition, you can set the seed for the random number generator using the\n\ |
|
347 form\n\ |
2928
|
348 \n\ |
3369
|
349 @example\n\ |
|
350 rand (\"seed\", @var{x})\n\ |
|
351 @end example\n\ |
|
352 \n\ |
|
353 @noindent\n\ |
|
354 where @var{x} is a scalar value. If called as\n\ |
2928
|
355 \n\ |
3369
|
356 @example\n\ |
|
357 rand (\"seed\")\n\ |
|
358 @end example\n\ |
|
359 \n\ |
|
360 @noindent\n\ |
|
361 @code{rand} returns the current value of the seed.\n\ |
|
362 @end deftypefn") |
2928
|
363 { |
|
364 octave_value_list retval; |
|
365 |
|
366 int nargin = args.length (); |
|
367 |
|
368 if (nargin > 2 || nargout > 1) |
|
369 print_usage ("rand"); |
|
370 else |
|
371 { |
|
372 if (! initialized) |
|
373 do_initialization (); |
|
374 |
|
375 retval = do_rand (args, nargin); |
|
376 } |
|
377 |
|
378 return retval; |
|
379 } |
|
380 |
|
381 static void |
|
382 reset_rand_generator (void *) |
|
383 { |
4153
|
384 F77_FUNC (setcgn, SETCGN) (current_distribution); |
2928
|
385 } |
|
386 |
|
387 DEFUN_DLD (randn, args, nargout, |
3369
|
388 "-*- texinfo -*-\n\ |
|
389 @deftypefn {Loadable Function} {} randn (@var{x})\n\ |
|
390 @deftypefnx {Loadable Function} {} randn (@var{n}, @var{m})\n\ |
|
391 @deftypefnx {Loadable Function} {} randn (@code{\"seed\"}, @var{x})\n\ |
|
392 Return a matrix with normally distributed random elements. The\n\ |
|
393 arguments are handled the same as the arguments for @code{eye}. In\n\ |
|
394 addition, you can set the seed for the random number generator using the\n\ |
|
395 form\n\ |
2928
|
396 \n\ |
3369
|
397 @example\n\ |
|
398 randn (\"seed\", @var{x})\n\ |
|
399 @end example\n\ |
|
400 \n\ |
|
401 @noindent\n\ |
|
402 where @var{x} is a scalar value. If called as\n\ |
2928
|
403 \n\ |
3369
|
404 @example\n\ |
|
405 randn (\"seed\")\n\ |
|
406 @end example\n\ |
|
407 \n\ |
|
408 @noindent\n\ |
|
409 @code{randn} returns the current value of the seed.\n\ |
|
410 @end deftypefn") |
2928
|
411 { |
|
412 octave_value_list retval; |
|
413 |
|
414 int nargin = args.length (); |
|
415 |
|
416 if (nargin > 2 || nargout > 1) |
|
417 print_usage ("randn"); |
|
418 else |
|
419 { |
|
420 if (! initialized) |
|
421 do_initialization (); |
|
422 |
2985
|
423 unwind_protect::begin_frame ("randn"); |
2928
|
424 |
|
425 // This relies on the fact that elements are popped from the |
|
426 // unwind stack in the reverse of the order they are pushed |
|
427 // (i.e. current_distribution will be reset before calling |
|
428 // reset_rand_generator()). |
|
429 |
2985
|
430 unwind_protect::add (reset_rand_generator, 0); |
2928
|
431 unwind_protect_int (current_distribution); |
|
432 |
|
433 current_distribution = normal_dist; |
|
434 |
4153
|
435 F77_FUNC (setcgn, SETCGN) (normal_dist); |
2928
|
436 |
|
437 retval = do_rand (args, nargin); |
|
438 |
2985
|
439 unwind_protect::run_frame ("randn"); |
2928
|
440 } |
|
441 |
|
442 return retval; |
|
443 } |
|
444 |
|
445 /* |
|
446 ;;; Local Variables: *** |
|
447 ;;; mode: C++ *** |
|
448 ;;; End: *** |
|
449 */ |