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