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 "help.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 { |
|
55 int *F77_FCN (dgennor, DGENNOR) (const double&, const double&, |
|
56 double&); |
|
57 |
|
58 int *F77_FCN (dgenunf, DGENUNF) (const double&, const double&, |
|
59 double&); |
|
60 |
|
61 int *F77_FCN (setall, SETALL) (const int&, const int&); |
|
62 |
|
63 int *F77_FCN (getsd, GETSD) (int&, int&); |
|
64 |
|
65 int *F77_FCN (setsd, SETSD) (const int&, const int&); |
|
66 |
|
67 int *F77_FCN (setcgn, SETCGN) (const int&); |
|
68 } |
|
69 |
|
70 static double |
|
71 curr_rand_seed (void) |
|
72 { |
|
73 union d2i { double d; int i[2]; }; |
|
74 union d2i u; |
|
75 F77_FCN (getsd, GETSD) (u.i[0], u.i[1]); |
|
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); |
|
102 F77_FCN (setsd, SETSD) (i0, i1); |
|
103 } |
|
104 |
|
105 static char * |
|
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 |
|
143 F77_FCN (setall, SETALL) (s0, s1); |
|
144 |
|
145 initialized = 1; |
|
146 } |
|
147 |
|
148 static octave_value_list |
|
149 do_rand (const octave_value_list& args, int nargin) |
|
150 { |
|
151 octave_value_list retval; |
|
152 |
|
153 int n = 0; |
|
154 int m = 0; |
|
155 |
|
156 if (nargin == 0) |
|
157 { |
|
158 n = 1; |
|
159 m = 1; |
|
160 |
|
161 goto gen_matrix; |
|
162 } |
|
163 else if (nargin == 1) |
|
164 { |
|
165 octave_value tmp = args(0); |
|
166 |
|
167 if (tmp.is_string ()) |
|
168 { |
|
169 string s_arg = tmp.string_value (); |
|
170 |
|
171 if (s_arg == "dist") |
|
172 { |
|
173 retval(0) = curr_rand_dist (); |
|
174 } |
|
175 else if (s_arg == "seed") |
|
176 { |
|
177 retval(0) = curr_rand_seed (); |
|
178 } |
|
179 else if (s_arg == "uniform") |
|
180 { |
|
181 current_distribution = uniform_dist; |
|
182 |
|
183 F77_FCN (setcgn, SETCGN) (uniform_dist); |
|
184 } |
|
185 else if (s_arg == "normal") |
|
186 { |
|
187 current_distribution = normal_dist; |
|
188 |
|
189 F77_FCN (setcgn, SETCGN) (normal_dist); |
|
190 } |
|
191 else |
|
192 error ("rand: unrecognized string argument"); |
|
193 } |
|
194 else if (tmp.is_scalar_type ()) |
|
195 { |
|
196 double dval = tmp.double_value (); |
|
197 |
|
198 if (xisnan (dval)) |
|
199 { |
|
200 error ("rand: NaN is invalid a matrix dimension"); |
|
201 } |
|
202 else |
|
203 { |
|
204 m = n = NINT (tmp.double_value ()); |
|
205 |
|
206 if (! error_state) |
|
207 goto gen_matrix; |
|
208 } |
|
209 } |
|
210 else if (tmp.is_range ()) |
|
211 { |
|
212 Range r = tmp.range_value (); |
|
213 n = 1; |
|
214 m = r.nelem (); |
|
215 goto gen_matrix; |
|
216 } |
|
217 else if (tmp.is_matrix_type ()) |
|
218 { |
|
219 // XXX FIXME XXX -- this should probably use the function |
|
220 // from data.cc. |
|
221 |
|
222 Matrix a = args(0).matrix_value (); |
|
223 |
|
224 if (error_state) |
|
225 return retval; |
|
226 |
|
227 n = a.rows (); |
|
228 m = a.columns (); |
|
229 |
|
230 if (n == 1 && m == 2) |
|
231 { |
|
232 n = NINT (a (0, 0)); |
|
233 m = NINT (a (0, 1)); |
|
234 } |
|
235 else if (n == 2 && m == 1) |
|
236 { |
|
237 n = NINT (a (0, 0)); |
|
238 m = NINT (a (1, 0)); |
|
239 } |
|
240 else |
|
241 warning ("rand (A): use rand (size (A)) instead"); |
|
242 |
|
243 goto gen_matrix; |
|
244 } |
|
245 else |
|
246 { |
|
247 gripe_wrong_type_arg ("rand", tmp); |
|
248 return retval; |
|
249 } |
|
250 } |
|
251 else if (nargin == 2) |
|
252 { |
|
253 if (args(0).is_string ()) |
|
254 { |
|
255 if (args(0).string_value () == "seed") |
|
256 { |
|
257 double d = args(1).double_value (); |
|
258 |
|
259 if (! error_state) |
|
260 set_rand_seed (d); |
|
261 } |
|
262 } |
|
263 else |
|
264 { |
|
265 double dval = args(0).double_value (); |
|
266 |
|
267 if (xisnan (dval)) |
|
268 { |
|
269 error ("rand: NaN is invalid as a matrix dimension"); |
|
270 } |
|
271 else |
|
272 { |
|
273 n = NINT (dval); |
|
274 |
|
275 if (! error_state) |
|
276 { |
|
277 m = NINT (args(1).double_value ()); |
|
278 |
|
279 if (! error_state) |
|
280 goto gen_matrix; |
|
281 } |
|
282 } |
|
283 } |
|
284 } |
|
285 |
|
286 return retval; |
|
287 |
|
288 gen_matrix: |
|
289 |
|
290 if (n == 0 || m == 0) |
|
291 { |
|
292 Matrix m; |
|
293 retval.resize (1, m); |
|
294 } |
|
295 else if (n > 0 && m > 0) |
|
296 { |
|
297 Matrix rand_mat (n, m); |
|
298 for (int j = 0; j < m; j++) |
|
299 for (int i = 0; i < n; i++) |
|
300 { |
|
301 double val; |
|
302 switch (current_distribution) |
|
303 { |
|
304 case uniform_dist: |
|
305 F77_FCN (dgenunf, DGENUNF) (0.0, 1.0, val); |
|
306 rand_mat (i, j) = val; |
|
307 break; |
|
308 |
|
309 case normal_dist: |
|
310 F77_FCN (dgennor, DGENNOR) (0.0, 1.0, val); |
|
311 rand_mat (i, j) = val; |
|
312 break; |
|
313 |
|
314 default: |
|
315 panic_impossible (); |
|
316 break; |
|
317 } |
|
318 } |
|
319 |
|
320 retval(0) = rand_mat; |
|
321 } |
|
322 else |
|
323 error ("rand: invalid negative argument"); |
|
324 |
|
325 return retval; |
|
326 } |
|
327 |
|
328 DEFUN_DLD (rand, args, nargout, |
|
329 "rand -- generate a random value from a uniform distribution\n\ |
|
330 \n\ |
|
331 rand (N) -- generate N x N matrix\n\ |
|
332 rand (size (A)) -- generate matrix the size of A\n\ |
|
333 rand (N, M) -- generate N x M matrix\n\ |
|
334 rand (SEED) -- get current seed\n\ |
|
335 rand (SEED, N) -- set seed\n\ |
|
336 \n\ |
|
337 See also: randn") |
|
338 { |
|
339 octave_value_list retval; |
|
340 |
|
341 int nargin = args.length (); |
|
342 |
|
343 if (nargin > 2 || nargout > 1) |
|
344 print_usage ("rand"); |
|
345 else |
|
346 { |
|
347 if (! initialized) |
|
348 do_initialization (); |
|
349 |
|
350 retval = do_rand (args, nargin); |
|
351 } |
|
352 |
|
353 return retval; |
|
354 } |
|
355 |
|
356 static void |
|
357 reset_rand_generator (void *) |
|
358 { |
|
359 F77_FCN (setcgn, SETCGN) (current_distribution); |
|
360 } |
|
361 |
|
362 DEFUN_DLD (randn, args, nargout, |
|
363 "randn -- generate a random value from a normal distribution\n\ |
|
364 \n\ |
|
365 randn (N) -- generate N x N matrix\n\ |
|
366 randn (size (A)) -- generate matrix the size of A\n\ |
|
367 randn (N, M) -- generate N x M matrix\n\ |
|
368 randn (SEED) -- get current seed\n\ |
|
369 randn (SEED, N) -- set seed\n\ |
|
370 \n\ |
|
371 See also: rand") |
|
372 { |
|
373 octave_value_list retval; |
|
374 |
|
375 int nargin = args.length (); |
|
376 |
|
377 if (nargin > 2 || nargout > 1) |
|
378 print_usage ("randn"); |
|
379 else |
|
380 { |
|
381 if (! initialized) |
|
382 do_initialization (); |
|
383 |
|
384 begin_unwind_frame ("randn"); |
|
385 |
|
386 // This relies on the fact that elements are popped from the |
|
387 // unwind stack in the reverse of the order they are pushed |
|
388 // (i.e. current_distribution will be reset before calling |
|
389 // reset_rand_generator()). |
|
390 |
|
391 add_unwind_protect (reset_rand_generator, 0); |
|
392 unwind_protect_int (current_distribution); |
|
393 |
|
394 current_distribution = normal_dist; |
|
395 |
|
396 F77_FCN (setcgn, SETCGN) (normal_dist); |
|
397 |
|
398 retval = do_rand (args, nargin); |
|
399 |
|
400 run_unwind_frame ("randn"); |
|
401 } |
|
402 |
|
403 return retval; |
|
404 } |
|
405 |
|
406 /* |
|
407 ;;; Local Variables: *** |
|
408 ;;; mode: C++ *** |
|
409 ;;; End: *** |
|
410 */ |