4308
|
1 /* |
|
2 |
|
3 Copyright (C) 2003 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 "f77-fcn.h" |
|
28 #include "lo-error.h" |
|
29 #include "oct-rand.h" |
4415
|
30 #include "oct-time.h" |
4308
|
31 |
|
32 // Possible distributions of random numbers. This was handled with an |
|
33 // enum, but unwind_protecting that doesn't work so well. |
|
34 #define uniform_dist 1 |
|
35 #define normal_dist 2 |
|
36 |
|
37 // Current distribution of random numbers. |
|
38 static int current_distribution = uniform_dist; |
|
39 |
|
40 // Has the seed been set yet? |
|
41 static bool initialized = false; |
|
42 |
|
43 extern "C" |
|
44 { |
4552
|
45 F77_RET_T |
|
46 F77_FUNC (dgennor, DGENNOR) (const double&, const double&, double&); |
4308
|
47 |
4552
|
48 F77_RET_T |
|
49 F77_FUNC (dgenunf, DGENUNF) (const double&, const double&, double&); |
|
50 |
|
51 F77_RET_T |
|
52 F77_FUNC (setall, SETALL) (const int&, const int&); |
4308
|
53 |
4552
|
54 F77_RET_T |
|
55 F77_FUNC (getsd, GETSD) (int&, int&); |
4308
|
56 |
4552
|
57 F77_RET_T |
|
58 F77_FUNC (setsd, SETSD) (const int&, const int&); |
4308
|
59 |
4552
|
60 F77_RET_T |
|
61 F77_FUNC (setcgn, SETCGN) (const int&); |
4308
|
62 } |
|
63 |
|
64 static int |
|
65 force_to_fit_range (int i, int lo, int hi) |
|
66 { |
|
67 assert (hi > lo && lo >= 0 && hi > lo); |
|
68 |
|
69 i = i > 0 ? i : -i; |
|
70 |
|
71 if (i < lo) |
|
72 i = lo; |
|
73 else if (i > hi) |
|
74 i = i % hi; |
|
75 |
|
76 return i; |
|
77 } |
|
78 |
|
79 // Make the random number generator give us a different sequence every |
|
80 // time we start octave unless we specifically set the seed. The |
|
81 // technique used below will cycle monthly, but it it does seem to |
|
82 // work ok to give fairly different seeds each time Octave starts. |
|
83 |
|
84 static void |
|
85 do_initialization (void) |
|
86 { |
4415
|
87 octave_localtime tm; |
4308
|
88 |
4415
|
89 int hour = tm.hour() + 1; |
|
90 int minute = tm.min() + 1; |
|
91 int second = tm.sec() + 1; |
4308
|
92 |
4415
|
93 int s0 = tm.mday() * hour * minute * second; |
4308
|
94 int s1 = hour * minute * second; |
|
95 |
|
96 s0 = force_to_fit_range (s0, 1, 2147483563); |
|
97 s1 = force_to_fit_range (s1, 1, 2147483399); |
|
98 |
|
99 F77_FUNC (setall, SETALL) (s0, s1); |
|
100 |
|
101 initialized = true; |
|
102 } |
|
103 |
|
104 static inline void |
|
105 maybe_initialize (void) |
|
106 { |
|
107 if (! initialized) |
|
108 do_initialization (); |
|
109 } |
|
110 |
|
111 double |
|
112 octave_rand::seed (void) |
|
113 { |
|
114 maybe_initialize (); |
|
115 |
|
116 union d2i { double d; int i[2]; }; |
|
117 union d2i u; |
|
118 F77_FUNC (getsd, GETSD) (u.i[0], u.i[1]); |
|
119 return u.d; |
|
120 } |
|
121 |
|
122 void |
|
123 octave_rand::seed (double s) |
|
124 { |
|
125 maybe_initialize (); |
|
126 |
|
127 union d2i { double d; int i[2]; }; |
|
128 union d2i u; |
|
129 u.d = s; |
|
130 int i0 = force_to_fit_range (u.i[0], 1, 2147483563); |
|
131 int i1 = force_to_fit_range (u.i[1], 1, 2147483399); |
|
132 F77_FUNC (setsd, SETSD) (i0, i1); |
|
133 } |
|
134 |
|
135 std::string |
|
136 octave_rand::distribution (void) |
|
137 { |
|
138 maybe_initialize (); |
|
139 |
|
140 if (current_distribution == uniform_dist) |
|
141 return "uniform"; |
|
142 else if (current_distribution == normal_dist) |
|
143 return "normal"; |
|
144 else |
|
145 { |
|
146 abort (); |
|
147 return ""; |
|
148 } |
|
149 } |
|
150 |
|
151 void |
|
152 octave_rand::distribution (const std::string& d) |
|
153 { |
|
154 if (d == "uniform") |
|
155 octave_rand::uniform_distribution (); |
|
156 else if (d == "normal") |
|
157 octave_rand::normal_distribution (); |
|
158 else |
|
159 (*current_liboctave_error_handler) ("rand: invalid distribution"); |
|
160 } |
|
161 |
|
162 void |
|
163 octave_rand::uniform_distribution (void) |
|
164 { |
|
165 maybe_initialize (); |
|
166 |
|
167 current_distribution = uniform_dist; |
|
168 |
|
169 F77_FUNC (setcgn, SETCGN) (uniform_dist); |
|
170 } |
|
171 |
|
172 void |
|
173 octave_rand::normal_distribution (void) |
|
174 { |
|
175 maybe_initialize (); |
|
176 |
|
177 current_distribution = normal_dist; |
|
178 |
|
179 F77_FUNC (setcgn, SETCGN) (normal_dist); |
|
180 } |
|
181 |
|
182 double |
|
183 octave_rand::scalar (void) |
|
184 { |
|
185 maybe_initialize (); |
|
186 |
|
187 double retval = 0.0; |
|
188 |
|
189 switch (current_distribution) |
|
190 { |
|
191 case uniform_dist: |
|
192 F77_FUNC (dgenunf, DGENUNF) (0.0, 1.0, retval); |
|
193 break; |
|
194 |
|
195 case normal_dist: |
|
196 F77_FUNC (dgennor, DGENNOR) (0.0, 1.0, retval); |
|
197 break; |
|
198 |
|
199 default: |
|
200 abort (); |
|
201 break; |
|
202 } |
|
203 |
|
204 return retval; |
|
205 } |
|
206 |
|
207 #define MAKE_RAND_MAT(mat, nr, nc, f, F) \ |
|
208 do \ |
|
209 { \ |
|
210 double val; \ |
|
211 for (volatile int j = 0; j < nc; j++) \ |
|
212 for (volatile int i = 0; i < nr; i++) \ |
|
213 { \ |
|
214 OCTAVE_QUIT; \ |
|
215 F77_FUNC (f, F) (0.0, 1.0, val); \ |
|
216 mat(i,j) = val; \ |
|
217 } \ |
|
218 } \ |
|
219 while (0) |
|
220 |
|
221 Matrix |
|
222 octave_rand::matrix (int n, int m) |
|
223 { |
|
224 maybe_initialize (); |
|
225 |
|
226 Matrix retval; |
|
227 |
|
228 if (n >= 0 && m >= 0) |
|
229 { |
|
230 retval.resize (n, m); |
|
231 |
|
232 if (n > 0 && m > 0) |
|
233 { |
|
234 switch (current_distribution) |
|
235 { |
|
236 case uniform_dist: |
|
237 MAKE_RAND_MAT (retval, n, m, dgenunf, DGENUNF); |
|
238 break; |
|
239 |
|
240 case normal_dist: |
|
241 MAKE_RAND_MAT (retval, n, m, dgennor, DGENNOR); |
|
242 break; |
|
243 |
|
244 default: |
|
245 abort (); |
|
246 break; |
|
247 } |
|
248 } |
|
249 } |
|
250 else |
|
251 (*current_liboctave_error_handler) ("rand: invalid negative argument"); |
|
252 |
|
253 return retval; |
|
254 } |
|
255 |
4543
|
256 #define MAKE_RAND_ND_ARRAY(mat, len, f, F) \ |
|
257 do \ |
|
258 { \ |
|
259 double val; \ |
|
260 for (volatile int i = 0; i < len; i++) \ |
|
261 { \ |
|
262 OCTAVE_QUIT; \ |
|
263 F77_FUNC (f, F) (0.0, 1.0, val); \ |
|
264 mat(i) = val; \ |
|
265 } \ |
|
266 } \ |
|
267 while (0) |
|
268 |
|
269 NDArray |
|
270 octave_rand::nd_array (const dim_vector& dims) |
|
271 { |
|
272 maybe_initialize (); |
|
273 |
|
274 NDArray retval; |
|
275 |
|
276 if (! dims.all_zero ()) |
|
277 { |
|
278 retval.resize (dims); |
|
279 |
|
280 int len = retval.length (); |
|
281 |
|
282 switch (current_distribution) |
|
283 { |
|
284 case uniform_dist: |
|
285 MAKE_RAND_ND_ARRAY (retval, len, dgenunf, DGENUNF); |
|
286 break; |
|
287 |
|
288 case normal_dist: |
|
289 MAKE_RAND_ND_ARRAY (retval, len, dgennor, DGENNOR); |
|
290 break; |
|
291 |
|
292 default: |
|
293 abort (); |
|
294 break; |
|
295 } |
|
296 } |
|
297 |
|
298 return retval; |
|
299 } |
|
300 |
4308
|
301 #define MAKE_RAND_ARRAY(array, n, f, F) \ |
|
302 do \ |
|
303 { \ |
|
304 double val; \ |
|
305 for (volatile int i = 0; i < n; i++) \ |
|
306 { \ |
|
307 OCTAVE_QUIT; \ |
|
308 F77_FUNC (f, F) (0.0, 1.0, val); \ |
|
309 array(i) = val; \ |
|
310 } \ |
|
311 } \ |
|
312 while (0) |
|
313 |
|
314 Array<double> |
|
315 octave_rand::vector (int n) |
|
316 { |
|
317 maybe_initialize (); |
|
318 |
|
319 Array<double> retval; |
|
320 |
|
321 if (n > 0) |
|
322 { |
|
323 retval.resize (n); |
|
324 |
|
325 switch (current_distribution) |
|
326 { |
|
327 case uniform_dist: |
|
328 MAKE_RAND_ARRAY (retval, n, dgenunf, DGENUNF); |
|
329 break; |
|
330 |
|
331 case normal_dist: |
|
332 MAKE_RAND_ARRAY (retval, n, dgennor, DGENNOR); |
|
333 break; |
|
334 |
|
335 default: |
|
336 abort (); |
|
337 break; |
|
338 } |
|
339 } |
|
340 else if (n < 0) |
|
341 (*current_liboctave_error_handler) ("rand: invalid negative argument"); |
|
342 |
|
343 return retval; |
|
344 } |
|
345 |
|
346 /* |
|
347 ;;; Local Variables: *** |
|
348 ;;; mode: C++ *** |
|
349 ;;; End: *** |
|
350 */ |