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