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