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