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 |
4543
|
222 return octave_rand::nd_array (dims); |
2928
|
223 } |
|
224 |
4665
|
225 DEFUN_DLD (rand, args, , |
3369
|
226 "-*- texinfo -*-\n\ |
|
227 @deftypefn {Loadable Function} {} rand (@var{x})\n\ |
|
228 @deftypefnx {Loadable Function} {} rand (@var{n}, @var{m})\n\ |
|
229 @deftypefnx {Loadable Function} {} rand (@code{\"seed\"}, @var{x})\n\ |
|
230 Return a matrix with random elements uniformly distributed on the\n\ |
|
231 interval (0, 1). The arguments are handled the same as the arguments\n\ |
|
232 for @code{eye}. In\n\ |
|
233 addition, you can set the seed for the random number generator using the\n\ |
|
234 form\n\ |
2928
|
235 \n\ |
3369
|
236 @example\n\ |
|
237 rand (\"seed\", @var{x})\n\ |
|
238 @end example\n\ |
|
239 \n\ |
|
240 @noindent\n\ |
|
241 where @var{x} is a scalar value. If called as\n\ |
2928
|
242 \n\ |
3369
|
243 @example\n\ |
|
244 rand (\"seed\")\n\ |
|
245 @end example\n\ |
|
246 \n\ |
|
247 @noindent\n\ |
|
248 @code{rand} returns the current value of the seed.\n\ |
|
249 @end deftypefn") |
2928
|
250 { |
4307
|
251 octave_value retval; |
2928
|
252 |
|
253 int nargin = args.length (); |
|
254 |
4543
|
255 retval = do_rand (args, nargin, "rand"); |
2928
|
256 |
|
257 return retval; |
|
258 } |
|
259 |
4307
|
260 static std::string current_distribution = octave_rand::distribution (); |
|
261 |
2928
|
262 static void |
|
263 reset_rand_generator (void *) |
|
264 { |
4307
|
265 octave_rand::distribution (current_distribution); |
2928
|
266 } |
|
267 |
4665
|
268 DEFUN_DLD (randn, args, , |
3369
|
269 "-*- texinfo -*-\n\ |
|
270 @deftypefn {Loadable Function} {} randn (@var{x})\n\ |
|
271 @deftypefnx {Loadable Function} {} randn (@var{n}, @var{m})\n\ |
|
272 @deftypefnx {Loadable Function} {} randn (@code{\"seed\"}, @var{x})\n\ |
|
273 Return a matrix with normally distributed random elements. The\n\ |
|
274 arguments are handled the same as the arguments for @code{eye}. In\n\ |
|
275 addition, you can set the seed for the random number generator using the\n\ |
|
276 form\n\ |
2928
|
277 \n\ |
3369
|
278 @example\n\ |
|
279 randn (\"seed\", @var{x})\n\ |
|
280 @end example\n\ |
|
281 \n\ |
|
282 @noindent\n\ |
|
283 where @var{x} is a scalar value. If called as\n\ |
2928
|
284 \n\ |
3369
|
285 @example\n\ |
|
286 randn (\"seed\")\n\ |
|
287 @end example\n\ |
|
288 \n\ |
|
289 @noindent\n\ |
|
290 @code{randn} returns the current value of the seed.\n\ |
|
291 @end deftypefn") |
2928
|
292 { |
4307
|
293 octave_value retval; |
2928
|
294 |
|
295 int nargin = args.length (); |
|
296 |
4543
|
297 unwind_protect::begin_frame ("randn"); |
2928
|
298 |
4543
|
299 // This relies on the fact that elements are popped from the unwind |
|
300 // stack in the reverse of the order they are pushed |
|
301 // (i.e. current_distribution will be reset before calling |
|
302 // reset_rand_generator()). |
2928
|
303 |
4543
|
304 unwind_protect::add (reset_rand_generator, 0); |
|
305 unwind_protect_str (current_distribution); |
2928
|
306 |
4543
|
307 current_distribution = "normal"; |
2928
|
308 |
4543
|
309 octave_rand::distribution (current_distribution); |
2928
|
310 |
4543
|
311 retval = do_rand (args, nargin, "randn"); |
2928
|
312 |
4543
|
313 unwind_protect::run_frame ("randn"); |
2928
|
314 |
|
315 return retval; |
|
316 } |
|
317 |
|
318 /* |
|
319 ;;; Local Variables: *** |
|
320 ;;; mode: C++ *** |
|
321 ;;; End: *** |
|
322 */ |