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