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 |
|
88 error ("rand: unrecognized string argument"); |
|
89 } |
|
90 else if (tmp.is_scalar_type ()) |
|
91 { |
|
92 double dval = tmp.double_value (); |
2928
|
93 |
4543
|
94 if (xisnan (dval)) |
|
95 { |
|
96 error ("rand: NaN is invalid a matrix dimension"); |
|
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) |
|
124 error ("rand: all dimensions must be nonnegative"); |
|
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 |
|
137 error ("rand: expecting all elements of range to be integers"); |
|
138 } |
|
139 else if (tmp.is_matrix_type ()) |
|
140 { |
|
141 Array<int> iv = tmp.int_vector_value (true); |
|
142 |
|
143 if (! error_state) |
|
144 { |
|
145 int len = iv.length (); |
2928
|
146 |
4543
|
147 dims.resize (len); |
|
148 |
|
149 for (int i = 0; i < len; i++) |
|
150 { |
|
151 int elt = iv(i); |
|
152 |
|
153 if (elt < 0) |
|
154 { |
|
155 error ("rand: all dimensions must be nonnegative"); |
|
156 goto done; |
|
157 } |
|
158 |
|
159 dims(i) = iv(i); |
|
160 } |
2928
|
161 |
4543
|
162 goto gen_matrix; |
|
163 } |
|
164 else |
|
165 error ("rand: expecting integer vector"); |
|
166 } |
|
167 else |
|
168 { |
|
169 gripe_wrong_type_arg ("rand", tmp); |
|
170 return retval; |
|
171 } |
|
172 } |
|
173 break; |
|
174 |
|
175 default: |
|
176 { |
|
177 octave_value tmp = args(0); |
|
178 |
|
179 if (nargin == 2 && tmp.is_string ()) |
|
180 { |
|
181 if (tmp.string_value () == "seed") |
|
182 { |
|
183 double d = args(1).double_value (); |
2928
|
184 |
4543
|
185 if (! error_state) |
|
186 octave_rand::seed (d); |
|
187 } |
|
188 else |
|
189 error ("rand: unrecognized string argument"); |
|
190 } |
|
191 else |
|
192 { |
|
193 int nargin = args.length (); |
|
194 |
|
195 dims.resize (nargin); |
|
196 |
|
197 for (int i = 0; i < nargin; i++) |
|
198 { |
|
199 dims(i) = args(i).int_value (); |
|
200 |
|
201 if (error_state) |
|
202 { |
|
203 error ("rand: expecting integer arguments"); |
|
204 goto done; |
|
205 } |
|
206 } |
|
207 |
|
208 goto gen_matrix; |
|
209 } |
|
210 } |
|
211 break; |
2928
|
212 } |
|
213 |
4543
|
214 done: |
2928
|
215 |
|
216 return retval; |
|
217 |
|
218 gen_matrix: |
|
219 |
4543
|
220 return octave_rand::nd_array (dims); |
2928
|
221 } |
|
222 |
|
223 DEFUN_DLD (rand, args, nargout, |
3369
|
224 "-*- texinfo -*-\n\ |
|
225 @deftypefn {Loadable Function} {} rand (@var{x})\n\ |
|
226 @deftypefnx {Loadable Function} {} rand (@var{n}, @var{m})\n\ |
|
227 @deftypefnx {Loadable Function} {} rand (@code{\"seed\"}, @var{x})\n\ |
|
228 Return a matrix with random elements uniformly distributed on the\n\ |
|
229 interval (0, 1). The arguments are handled the same as the arguments\n\ |
|
230 for @code{eye}. In\n\ |
|
231 addition, you can set the seed for the random number generator using the\n\ |
|
232 form\n\ |
2928
|
233 \n\ |
3369
|
234 @example\n\ |
|
235 rand (\"seed\", @var{x})\n\ |
|
236 @end example\n\ |
|
237 \n\ |
|
238 @noindent\n\ |
|
239 where @var{x} is a scalar value. If called as\n\ |
2928
|
240 \n\ |
3369
|
241 @example\n\ |
|
242 rand (\"seed\")\n\ |
|
243 @end example\n\ |
|
244 \n\ |
|
245 @noindent\n\ |
|
246 @code{rand} returns the current value of the seed.\n\ |
|
247 @end deftypefn") |
2928
|
248 { |
4307
|
249 octave_value retval; |
2928
|
250 |
|
251 int nargin = args.length (); |
|
252 |
4543
|
253 retval = do_rand (args, nargin, "rand"); |
2928
|
254 |
|
255 return retval; |
|
256 } |
|
257 |
4307
|
258 static std::string current_distribution = octave_rand::distribution (); |
|
259 |
2928
|
260 static void |
|
261 reset_rand_generator (void *) |
|
262 { |
4307
|
263 octave_rand::distribution (current_distribution); |
2928
|
264 } |
|
265 |
|
266 DEFUN_DLD (randn, args, nargout, |
3369
|
267 "-*- texinfo -*-\n\ |
|
268 @deftypefn {Loadable Function} {} randn (@var{x})\n\ |
|
269 @deftypefnx {Loadable Function} {} randn (@var{n}, @var{m})\n\ |
|
270 @deftypefnx {Loadable Function} {} randn (@code{\"seed\"}, @var{x})\n\ |
|
271 Return a matrix with normally distributed random elements. The\n\ |
|
272 arguments are handled the same as the arguments for @code{eye}. In\n\ |
|
273 addition, you can set the seed for the random number generator using the\n\ |
|
274 form\n\ |
2928
|
275 \n\ |
3369
|
276 @example\n\ |
|
277 randn (\"seed\", @var{x})\n\ |
|
278 @end example\n\ |
|
279 \n\ |
|
280 @noindent\n\ |
|
281 where @var{x} is a scalar value. If called as\n\ |
2928
|
282 \n\ |
3369
|
283 @example\n\ |
|
284 randn (\"seed\")\n\ |
|
285 @end example\n\ |
|
286 \n\ |
|
287 @noindent\n\ |
|
288 @code{randn} returns the current value of the seed.\n\ |
|
289 @end deftypefn") |
2928
|
290 { |
4307
|
291 octave_value retval; |
2928
|
292 |
|
293 int nargin = args.length (); |
|
294 |
4543
|
295 unwind_protect::begin_frame ("randn"); |
2928
|
296 |
4543
|
297 // This relies on the fact that elements are popped from the unwind |
|
298 // stack in the reverse of the order they are pushed |
|
299 // (i.e. current_distribution will be reset before calling |
|
300 // reset_rand_generator()). |
2928
|
301 |
4543
|
302 unwind_protect::add (reset_rand_generator, 0); |
|
303 unwind_protect_str (current_distribution); |
2928
|
304 |
4543
|
305 current_distribution = "normal"; |
2928
|
306 |
4543
|
307 octave_rand::distribution (current_distribution); |
2928
|
308 |
4543
|
309 retval = do_rand (args, nargin, "randn"); |
2928
|
310 |
4543
|
311 unwind_protect::run_frame ("randn"); |
2928
|
312 |
|
313 return retval; |
|
314 } |
|
315 |
|
316 /* |
|
317 ;;; Local Variables: *** |
|
318 ;;; mode: C++ *** |
|
319 ;;; End: *** |
|
320 */ |