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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2928
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <ctime> |
|
29 |
|
30 #include <string> |
|
31 |
|
32 #include "f77-fcn.h" |
|
33 #include "lo-mappers.h" |
4307
|
34 #include "oct-rand.h" |
4153
|
35 #include "quit.h" |
2928
|
36 |
|
37 #include "defun-dld.h" |
|
38 #include "error.h" |
|
39 #include "gripes.h" |
|
40 #include "oct-obj.h" |
|
41 #include "unwind-prot.h" |
|
42 #include "utils.h" |
|
43 |
4307
|
44 static octave_value |
4543
|
45 do_rand (const octave_value_list& args, int nargin, const char *fcn) |
2928
|
46 { |
4307
|
47 octave_value retval; |
2928
|
48 |
4543
|
49 dim_vector dims; |
2928
|
50 |
4543
|
51 switch (nargin) |
2928
|
52 { |
4543
|
53 case 0: |
|
54 { |
|
55 dims.resize (2); |
|
56 |
|
57 dims(0) = 1; |
|
58 dims(1) = 1; |
2928
|
59 |
4543
|
60 goto gen_matrix; |
|
61 } |
|
62 break; |
2928
|
63 |
4543
|
64 case 1: |
|
65 { |
|
66 octave_value tmp = args(0); |
|
67 |
|
68 if (tmp.is_string ()) |
|
69 { |
|
70 std::string s_arg = tmp.string_value (); |
2928
|
71 |
4543
|
72 if (s_arg == "dist") |
|
73 { |
|
74 retval = octave_rand::distribution (); |
|
75 } |
5164
|
76 else if (s_arg == "seed" || s_arg == "state") |
4543
|
77 { |
|
78 retval = octave_rand::seed (); |
|
79 } |
|
80 else if (s_arg == "uniform") |
|
81 { |
|
82 octave_rand::uniform_distribution (); |
|
83 } |
|
84 else if (s_arg == "normal") |
|
85 { |
|
86 octave_rand::normal_distribution (); |
|
87 } |
|
88 else |
4664
|
89 error ("%s: unrecognized string argument", fcn); |
4543
|
90 } |
|
91 else if (tmp.is_scalar_type ()) |
|
92 { |
|
93 double dval = tmp.double_value (); |
2928
|
94 |
4543
|
95 if (xisnan (dval)) |
|
96 { |
4664
|
97 error ("%s: NaN is invalid a matrix dimension", fcn); |
4543
|
98 } |
|
99 else |
|
100 { |
|
101 dims.resize (2); |
|
102 |
5275
|
103 dims(0) = NINTbig (tmp.double_value ()); |
|
104 dims(1) = NINTbig (tmp.double_value ()); |
2928
|
105 |
4543
|
106 if (! error_state) |
|
107 goto gen_matrix; |
|
108 } |
|
109 } |
|
110 else if (tmp.is_range ()) |
|
111 { |
|
112 Range r = tmp.range_value (); |
|
113 |
|
114 if (r.all_elements_are_ints ()) |
|
115 { |
5275
|
116 octave_idx_type n = r.nelem (); |
4543
|
117 |
|
118 dims.resize (n); |
|
119 |
5275
|
120 octave_idx_type base = NINTbig (r.base ()); |
|
121 octave_idx_type incr = NINTbig (r.inc ()); |
|
122 octave_idx_type lim = NINTbig (r.limit ()); |
2928
|
123 |
4543
|
124 if (base < 0 || lim < 0) |
4664
|
125 error ("%s: all dimensions must be nonnegative", fcn); |
4543
|
126 else |
|
127 { |
5275
|
128 for (octave_idx_type i = 0; i < n; i++) |
4543
|
129 { |
|
130 dims(i) = base; |
|
131 base += incr; |
|
132 } |
2928
|
133 |
4543
|
134 goto gen_matrix; |
|
135 } |
|
136 } |
|
137 else |
4664
|
138 error ("%s: expecting all elements of range to be integers", |
|
139 fcn); |
4543
|
140 } |
|
141 else if (tmp.is_matrix_type ()) |
|
142 { |
|
143 Array<int> iv = tmp.int_vector_value (true); |
|
144 |
|
145 if (! error_state) |
|
146 { |
5275
|
147 octave_idx_type len = iv.length (); |
2928
|
148 |
4543
|
149 dims.resize (len); |
|
150 |
5275
|
151 for (octave_idx_type i = 0; i < len; i++) |
4543
|
152 { |
5275
|
153 octave_idx_type elt = iv(i); |
4543
|
154 |
|
155 if (elt < 0) |
|
156 { |
4664
|
157 error ("%s: all dimensions must be nonnegative", fcn); |
4543
|
158 goto done; |
|
159 } |
|
160 |
|
161 dims(i) = iv(i); |
|
162 } |
2928
|
163 |
4543
|
164 goto gen_matrix; |
|
165 } |
|
166 else |
4664
|
167 error ("%s: expecting integer vector", fcn); |
4543
|
168 } |
|
169 else |
|
170 { |
|
171 gripe_wrong_type_arg ("rand", tmp); |
|
172 return retval; |
|
173 } |
|
174 } |
|
175 break; |
|
176 |
|
177 default: |
|
178 { |
|
179 octave_value tmp = args(0); |
|
180 |
|
181 if (nargin == 2 && tmp.is_string ()) |
|
182 { |
5164
|
183 std::string ts = tmp.string_value (); |
|
184 |
|
185 if (ts == "seed" || ts == "state") |
4543
|
186 { |
|
187 double d = args(1).double_value (); |
2928
|
188 |
4543
|
189 if (! error_state) |
|
190 octave_rand::seed (d); |
|
191 } |
|
192 else |
4664
|
193 error ("%s: unrecognized string argument", fcn); |
4543
|
194 } |
|
195 else |
|
196 { |
|
197 dims.resize (nargin); |
|
198 |
|
199 for (int i = 0; i < nargin; i++) |
|
200 { |
5275
|
201 dims(i) = (octave_idx_type)args(i).int_value (); |
4543
|
202 |
|
203 if (error_state) |
|
204 { |
4664
|
205 error ("%s: expecting integer arguments", fcn); |
4543
|
206 goto done; |
|
207 } |
|
208 } |
|
209 |
|
210 goto gen_matrix; |
|
211 } |
|
212 } |
|
213 break; |
2928
|
214 } |
|
215 |
4543
|
216 done: |
2928
|
217 |
|
218 return retval; |
|
219 |
|
220 gen_matrix: |
|
221 |
5355
|
222 dims.chop_trailing_singletons (); |
|
223 |
4543
|
224 return octave_rand::nd_array (dims); |
2928
|
225 } |
|
226 |
4665
|
227 DEFUN_DLD (rand, args, , |
3369
|
228 "-*- texinfo -*-\n\ |
|
229 @deftypefn {Loadable Function} {} rand (@var{x})\n\ |
|
230 @deftypefnx {Loadable Function} {} rand (@var{n}, @var{m})\n\ |
|
231 @deftypefnx {Loadable Function} {} rand (@code{\"seed\"}, @var{x})\n\ |
|
232 Return a matrix with random elements uniformly distributed on the\n\ |
|
233 interval (0, 1). The arguments are handled the same as the arguments\n\ |
|
234 for @code{eye}. In\n\ |
|
235 addition, you can set the seed for the random number generator using the\n\ |
|
236 form\n\ |
2928
|
237 \n\ |
3369
|
238 @example\n\ |
|
239 rand (\"seed\", @var{x})\n\ |
|
240 @end example\n\ |
|
241 \n\ |
|
242 @noindent\n\ |
|
243 where @var{x} is a scalar value. If called as\n\ |
2928
|
244 \n\ |
3369
|
245 @example\n\ |
|
246 rand (\"seed\")\n\ |
|
247 @end example\n\ |
|
248 \n\ |
|
249 @noindent\n\ |
|
250 @code{rand} returns the current value of the seed.\n\ |
|
251 @end deftypefn") |
2928
|
252 { |
4307
|
253 octave_value retval; |
2928
|
254 |
|
255 int nargin = args.length (); |
|
256 |
4543
|
257 retval = do_rand (args, nargin, "rand"); |
2928
|
258 |
|
259 return retval; |
|
260 } |
|
261 |
4307
|
262 static std::string current_distribution = octave_rand::distribution (); |
|
263 |
2928
|
264 static void |
|
265 reset_rand_generator (void *) |
|
266 { |
4307
|
267 octave_rand::distribution (current_distribution); |
2928
|
268 } |
|
269 |
4665
|
270 DEFUN_DLD (randn, args, , |
3369
|
271 "-*- texinfo -*-\n\ |
|
272 @deftypefn {Loadable Function} {} randn (@var{x})\n\ |
|
273 @deftypefnx {Loadable Function} {} randn (@var{n}, @var{m})\n\ |
|
274 @deftypefnx {Loadable Function} {} randn (@code{\"seed\"}, @var{x})\n\ |
|
275 Return a matrix with normally distributed random elements. The\n\ |
|
276 arguments are handled the same as the arguments for @code{eye}. In\n\ |
|
277 addition, you can set the seed for the random number generator using the\n\ |
|
278 form\n\ |
2928
|
279 \n\ |
3369
|
280 @example\n\ |
|
281 randn (\"seed\", @var{x})\n\ |
|
282 @end example\n\ |
|
283 \n\ |
|
284 @noindent\n\ |
|
285 where @var{x} is a scalar value. If called as\n\ |
2928
|
286 \n\ |
3369
|
287 @example\n\ |
|
288 randn (\"seed\")\n\ |
|
289 @end example\n\ |
|
290 \n\ |
|
291 @noindent\n\ |
|
292 @code{randn} returns the current value of the seed.\n\ |
|
293 @end deftypefn") |
2928
|
294 { |
4307
|
295 octave_value retval; |
2928
|
296 |
|
297 int nargin = args.length (); |
|
298 |
4543
|
299 unwind_protect::begin_frame ("randn"); |
2928
|
300 |
4543
|
301 // This relies on the fact that elements are popped from the unwind |
|
302 // stack in the reverse of the order they are pushed |
|
303 // (i.e. current_distribution will be reset before calling |
|
304 // reset_rand_generator()). |
2928
|
305 |
4543
|
306 unwind_protect::add (reset_rand_generator, 0); |
|
307 unwind_protect_str (current_distribution); |
2928
|
308 |
4543
|
309 current_distribution = "normal"; |
2928
|
310 |
4543
|
311 octave_rand::distribution (current_distribution); |
2928
|
312 |
4543
|
313 retval = do_rand (args, nargin, "randn"); |
2928
|
314 |
4543
|
315 unwind_protect::run_frame ("randn"); |
2928
|
316 |
|
317 return retval; |
|
318 } |
|
319 |
|
320 /* |
|
321 ;;; Local Variables: *** |
|
322 ;;; mode: C++ *** |
|
323 ;;; End: *** |
|
324 */ |