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 |
2928
|
44 do_rand (const octave_value_list& args, int nargin) |
|
45 { |
4307
|
46 octave_value retval; |
2928
|
47 |
3157
|
48 volatile int n = 0; |
|
49 volatile int m = 0; |
2928
|
50 |
|
51 if (nargin == 0) |
|
52 { |
|
53 n = 1; |
|
54 m = 1; |
|
55 |
|
56 goto gen_matrix; |
|
57 } |
|
58 else if (nargin == 1) |
|
59 { |
|
60 octave_value tmp = args(0); |
|
61 |
|
62 if (tmp.is_string ()) |
|
63 { |
3523
|
64 std::string s_arg = tmp.string_value (); |
2928
|
65 |
|
66 if (s_arg == "dist") |
|
67 { |
4307
|
68 retval = octave_rand::distribution (); |
2928
|
69 } |
|
70 else if (s_arg == "seed") |
|
71 { |
4307
|
72 retval = octave_rand::seed (); |
2928
|
73 } |
|
74 else if (s_arg == "uniform") |
|
75 { |
4307
|
76 octave_rand::uniform_distribution (); |
2928
|
77 } |
|
78 else if (s_arg == "normal") |
|
79 { |
4307
|
80 octave_rand::normal_distribution (); |
2928
|
81 } |
|
82 else |
|
83 error ("rand: unrecognized string argument"); |
|
84 } |
|
85 else if (tmp.is_scalar_type ()) |
|
86 { |
|
87 double dval = tmp.double_value (); |
|
88 |
|
89 if (xisnan (dval)) |
|
90 { |
|
91 error ("rand: NaN is invalid a matrix dimension"); |
|
92 } |
|
93 else |
|
94 { |
|
95 m = n = NINT (tmp.double_value ()); |
|
96 |
|
97 if (! error_state) |
|
98 goto gen_matrix; |
|
99 } |
|
100 } |
|
101 else if (tmp.is_range ()) |
|
102 { |
|
103 Range r = tmp.range_value (); |
|
104 n = 1; |
|
105 m = r.nelem (); |
|
106 goto gen_matrix; |
|
107 } |
|
108 else if (tmp.is_matrix_type ()) |
|
109 { |
|
110 // XXX FIXME XXX -- this should probably use the function |
|
111 // from data.cc. |
|
112 |
|
113 Matrix a = args(0).matrix_value (); |
|
114 |
|
115 if (error_state) |
|
116 return retval; |
|
117 |
|
118 n = a.rows (); |
|
119 m = a.columns (); |
|
120 |
|
121 if (n == 1 && m == 2) |
|
122 { |
|
123 n = NINT (a (0, 0)); |
|
124 m = NINT (a (0, 1)); |
|
125 } |
|
126 else if (n == 2 && m == 1) |
|
127 { |
|
128 n = NINT (a (0, 0)); |
|
129 m = NINT (a (1, 0)); |
|
130 } |
|
131 else |
|
132 warning ("rand (A): use rand (size (A)) instead"); |
|
133 |
|
134 goto gen_matrix; |
|
135 } |
|
136 else |
|
137 { |
|
138 gripe_wrong_type_arg ("rand", tmp); |
|
139 return retval; |
|
140 } |
|
141 } |
|
142 else if (nargin == 2) |
|
143 { |
|
144 if (args(0).is_string ()) |
|
145 { |
|
146 if (args(0).string_value () == "seed") |
|
147 { |
|
148 double d = args(1).double_value (); |
|
149 |
|
150 if (! error_state) |
4307
|
151 octave_rand::seed (d); |
2928
|
152 } |
3086
|
153 else |
|
154 error ("rand: unrecognized string argument"); |
2928
|
155 } |
|
156 else |
|
157 { |
|
158 double dval = args(0).double_value (); |
|
159 |
|
160 if (xisnan (dval)) |
|
161 { |
|
162 error ("rand: NaN is invalid as a matrix dimension"); |
|
163 } |
|
164 else |
|
165 { |
|
166 n = NINT (dval); |
|
167 |
|
168 if (! error_state) |
|
169 { |
|
170 m = NINT (args(1).double_value ()); |
|
171 |
|
172 if (! error_state) |
|
173 goto gen_matrix; |
|
174 } |
|
175 } |
|
176 } |
|
177 } |
|
178 |
|
179 return retval; |
|
180 |
|
181 gen_matrix: |
|
182 |
4307
|
183 return octave_rand::matrix (n, m); |
2928
|
184 } |
|
185 |
|
186 DEFUN_DLD (rand, args, nargout, |
3369
|
187 "-*- texinfo -*-\n\ |
|
188 @deftypefn {Loadable Function} {} rand (@var{x})\n\ |
|
189 @deftypefnx {Loadable Function} {} rand (@var{n}, @var{m})\n\ |
|
190 @deftypefnx {Loadable Function} {} rand (@code{\"seed\"}, @var{x})\n\ |
|
191 Return a matrix with random elements uniformly distributed on the\n\ |
|
192 interval (0, 1). The arguments are handled the same as the arguments\n\ |
|
193 for @code{eye}. In\n\ |
|
194 addition, you can set the seed for the random number generator using the\n\ |
|
195 form\n\ |
2928
|
196 \n\ |
3369
|
197 @example\n\ |
|
198 rand (\"seed\", @var{x})\n\ |
|
199 @end example\n\ |
|
200 \n\ |
|
201 @noindent\n\ |
|
202 where @var{x} is a scalar value. If called as\n\ |
2928
|
203 \n\ |
3369
|
204 @example\n\ |
|
205 rand (\"seed\")\n\ |
|
206 @end example\n\ |
|
207 \n\ |
|
208 @noindent\n\ |
|
209 @code{rand} returns the current value of the seed.\n\ |
|
210 @end deftypefn") |
2928
|
211 { |
4307
|
212 octave_value retval; |
2928
|
213 |
|
214 int nargin = args.length (); |
|
215 |
|
216 if (nargin > 2 || nargout > 1) |
|
217 print_usage ("rand"); |
|
218 else |
4307
|
219 retval = do_rand (args, nargin); |
2928
|
220 |
|
221 return retval; |
|
222 } |
|
223 |
4307
|
224 static std::string current_distribution = octave_rand::distribution (); |
|
225 |
2928
|
226 static void |
|
227 reset_rand_generator (void *) |
|
228 { |
4307
|
229 octave_rand::distribution (current_distribution); |
2928
|
230 } |
|
231 |
|
232 DEFUN_DLD (randn, args, nargout, |
3369
|
233 "-*- texinfo -*-\n\ |
|
234 @deftypefn {Loadable Function} {} randn (@var{x})\n\ |
|
235 @deftypefnx {Loadable Function} {} randn (@var{n}, @var{m})\n\ |
|
236 @deftypefnx {Loadable Function} {} randn (@code{\"seed\"}, @var{x})\n\ |
|
237 Return a matrix with normally distributed random elements. The\n\ |
|
238 arguments are handled the same as the arguments for @code{eye}. In\n\ |
|
239 addition, you can set the seed for the random number generator using the\n\ |
|
240 form\n\ |
2928
|
241 \n\ |
3369
|
242 @example\n\ |
|
243 randn (\"seed\", @var{x})\n\ |
|
244 @end example\n\ |
|
245 \n\ |
|
246 @noindent\n\ |
|
247 where @var{x} is a scalar value. If called as\n\ |
2928
|
248 \n\ |
3369
|
249 @example\n\ |
|
250 randn (\"seed\")\n\ |
|
251 @end example\n\ |
|
252 \n\ |
|
253 @noindent\n\ |
|
254 @code{randn} returns the current value of the seed.\n\ |
|
255 @end deftypefn") |
2928
|
256 { |
4307
|
257 octave_value retval; |
2928
|
258 |
|
259 int nargin = args.length (); |
|
260 |
|
261 if (nargin > 2 || nargout > 1) |
|
262 print_usage ("randn"); |
|
263 else |
|
264 { |
2985
|
265 unwind_protect::begin_frame ("randn"); |
2928
|
266 |
|
267 // This relies on the fact that elements are popped from the |
|
268 // unwind stack in the reverse of the order they are pushed |
|
269 // (i.e. current_distribution will be reset before calling |
|
270 // reset_rand_generator()). |
|
271 |
2985
|
272 unwind_protect::add (reset_rand_generator, 0); |
4307
|
273 unwind_protect_str (current_distribution); |
2928
|
274 |
4307
|
275 current_distribution = "normal"; |
2928
|
276 |
4307
|
277 octave_rand::distribution (current_distribution); |
2928
|
278 |
|
279 retval = do_rand (args, nargin); |
|
280 |
2985
|
281 unwind_protect::run_frame ("randn"); |
2928
|
282 } |
|
283 |
|
284 return retval; |
|
285 } |
|
286 |
|
287 /* |
|
288 ;;; Local Variables: *** |
|
289 ;;; mode: C++ *** |
|
290 ;;; End: *** |
|
291 */ |