523
|
1 // data.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 /* |
|
25 |
|
26 The function builtin_pwd adapted from a similar function from GNU |
|
27 Bash, the Bourne Again SHell, copyright (C) 1987, 1989, 1991 Free |
|
28 Software Foundation, Inc. |
|
29 |
|
30 */ |
|
31 |
|
32 #ifdef HAVE_CONFIG_H |
|
33 #include "config.h" |
|
34 #endif |
|
35 |
|
36 #include "tree-const.h" |
|
37 #include "user-prefs.h" |
565
|
38 #include "help.h" |
523
|
39 #include "utils.h" |
|
40 #include "error.h" |
|
41 #include "defun.h" |
|
42 |
|
43 #ifndef MIN |
|
44 #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
|
45 #endif |
|
46 |
|
47 DEFUN ("all", Fall, Sall, 2, 1, |
|
48 "all (X): are all elements of X nonzero?") |
|
49 { |
|
50 Octave_object retval; |
|
51 |
|
52 int nargin = args.length (); |
|
53 |
|
54 if (nargin != 2) |
|
55 print_usage ("all"); |
|
56 else if (nargin > 0 && args(1).is_defined ()) |
|
57 retval = args(1).all (); |
|
58 |
|
59 return retval; |
|
60 } |
|
61 |
|
62 DEFUN ("any", Fany, Sany, 2, 1, |
|
63 "any (X): are any elements of X nonzero?") |
|
64 { |
|
65 Octave_object retval; |
|
66 |
|
67 int nargin = args.length (); |
|
68 |
|
69 if (nargin != 2) |
|
70 print_usage ("any"); |
|
71 else if (nargin > 0 && args(1).is_defined ()) |
|
72 retval = args(1).any (); |
|
73 |
|
74 return retval; |
|
75 } |
|
76 |
633
|
77 DEFUN ("atan2", Fatan2, Satan2, 3, 1, |
|
78 "atan2 (Y, X): atan (Y / X) in range -pi to pi") |
|
79 { |
|
80 Octave_object retval; |
|
81 |
|
82 if (args.length () != 3) |
|
83 print_usage ("atan2"); |
|
84 else |
|
85 { |
|
86 double y = args(1).double_value (); |
|
87 |
|
88 if (! error_state) |
|
89 { |
|
90 double x = args(2).double_value (); |
|
91 |
|
92 if (! error_state) |
|
93 retval = atan2 (y, x); |
|
94 } |
|
95 } |
|
96 |
|
97 return retval; |
|
98 } |
|
99 |
523
|
100 DEFUN ("cumprod", Fcumprod, Scumprod, 2, 1, |
|
101 "cumprod (X): cumulative products") |
|
102 { |
|
103 Octave_object retval; |
|
104 |
|
105 int nargin = args.length (); |
|
106 |
|
107 if (nargin != 2) |
|
108 print_usage ("cumprod"); |
|
109 else if (nargin > 0 && args(1).is_defined ()) |
|
110 retval = args(1).cumprod (); |
|
111 |
|
112 return retval; |
|
113 } |
|
114 |
|
115 DEFUN ("cumsum", Fcumsum, Scumsum, 2, 1, |
|
116 "cumsum (X): cumulative sums") |
|
117 { |
|
118 Octave_object retval; |
|
119 |
|
120 int nargin = args.length (); |
|
121 |
|
122 if (nargin != 2) |
|
123 print_usage ("cumsum"); |
|
124 else if (nargin > 0 && args(1).is_defined ()) |
|
125 retval = args(1).cumsum (); |
|
126 |
|
127 return retval; |
|
128 } |
|
129 |
|
130 DEFUN ("diag", Fdiag, Sdiag, 3, 1, |
|
131 "diag (X [,k]): form/extract diagonals") |
|
132 { |
|
133 Octave_object retval; |
|
134 |
|
135 int nargin = args.length (); |
|
136 |
|
137 if (nargin == 2) |
|
138 retval = args(1).diag (); |
|
139 else if (nargin == 3) |
|
140 retval = args(1).diag (args(2)); |
|
141 else |
|
142 print_usage ("diag"); |
|
143 |
|
144 return retval; |
|
145 } |
|
146 |
|
147 DEFUN ("isstr", Fisstr, Sisstr, 2, 1, |
|
148 "isstr (X): return 1 if X is a string, 0 otherwise") |
|
149 { |
|
150 Octave_object retval; |
|
151 |
|
152 int nargin = args.length (); |
|
153 |
|
154 if (nargin != 2) |
|
155 print_usage ("isstr"); |
|
156 else |
|
157 { |
|
158 if (nargin > 0 && args(1).is_defined ()) |
620
|
159 retval = (double) args(1).is_string (); |
523
|
160 } |
|
161 |
|
162 return retval; |
|
163 } |
|
164 |
|
165 DEFUN ("prod", Fprod, Sprod, 2, 1, |
|
166 "prod (X): products") |
|
167 { |
|
168 Octave_object retval; |
|
169 |
|
170 int nargin = args.length (); |
|
171 |
|
172 if (nargin != 2) |
|
173 print_usage ("prod"); |
|
174 else if (nargin > 0 && args(1).is_defined ()) |
|
175 retval = args(1).prod (); |
|
176 |
|
177 return retval; |
|
178 } |
|
179 |
|
180 DEFUN ("setstr", Fsetstr, Ssetstr, 2, 1, |
|
181 "setstr (V): convert a vector to a string") |
|
182 { |
|
183 Octave_object retval; |
|
184 |
|
185 int nargin = args.length (); |
|
186 |
|
187 if (nargin == 2) |
|
188 retval = args(1).convert_to_str (); |
|
189 else |
|
190 print_usage ("setstr"); |
|
191 |
|
192 return retval; |
|
193 } |
|
194 |
|
195 DEFUN ("size", Fsize, Ssize, 2, 1, |
|
196 "[m, n] = size (x): return rows and columns of X") |
|
197 { |
|
198 Octave_object retval; |
|
199 |
|
200 int nargin = args.length (); |
|
201 |
|
202 if (nargin != 2) |
|
203 print_usage ("size"); |
|
204 else |
|
205 { |
|
206 if (nargin > 0 && args(1).is_defined ()) |
|
207 { |
|
208 int nr = args(1).rows (); |
|
209 int nc = args(1).columns (); |
|
210 if (nargout == 0 || nargout == 1) |
|
211 { |
|
212 Matrix m (1, 2); |
|
213 m.elem (0, 0) = nr; |
|
214 m.elem (0, 1) = nc; |
|
215 retval = m; |
|
216 } |
|
217 else if (nargout == 2) |
|
218 { |
|
219 retval(1) = (double) nc; |
|
220 retval(0) = (double) nr; |
|
221 } |
|
222 else |
|
223 print_usage ("size"); |
|
224 } |
|
225 } |
|
226 |
|
227 return retval; |
|
228 } |
|
229 |
|
230 DEFUN ("sum", Fsum, Ssum, 2, 1, |
|
231 "sum (X): sum of elements") |
|
232 { |
|
233 Octave_object retval; |
|
234 |
|
235 int nargin = args.length (); |
|
236 |
|
237 if (nargin != 2) |
|
238 print_usage ("sum"); |
|
239 else |
|
240 { |
|
241 if (nargin > 0 && args(1).is_defined ()) |
|
242 retval = args(1).sum (); |
|
243 } |
|
244 |
|
245 return retval; |
|
246 } |
|
247 |
|
248 DEFUN ("sumsq", Fsumsq, Ssumsq, 2, 1, |
|
249 "sumsq (X): sum of squares of elements") |
|
250 { |
|
251 Octave_object retval; |
|
252 |
|
253 int nargin = args.length (); |
|
254 |
|
255 if (nargin != 2) |
|
256 print_usage ("sumsq"); |
|
257 else if (nargin > 0 && args(1).is_defined ()) |
|
258 retval = args(1).sumsq (); |
|
259 |
|
260 return retval; |
|
261 } |
|
262 |
|
263 static void |
|
264 check_dimensions (int& nr, int& nc, const char *warnfor) |
|
265 { |
|
266 if (nr < 0 || nc < 0) |
|
267 { |
|
268 if (user_pref.treat_neg_dim_as_zero) |
597
|
269 { |
|
270 nr = (nr < 0) ? 0 : nr; |
|
271 nc = (nc < 0) ? 0 : nc; |
|
272 } |
523
|
273 else |
|
274 error ("%s: can't create a matrix with negative dimensions", |
|
275 warnfor); |
|
276 } |
|
277 } |
|
278 |
|
279 static void |
|
280 get_dimensions (const tree_constant& a, const char *warn_for, |
|
281 int& nr, int& nc) |
|
282 { |
634
|
283 if (a.is_scalar_type ()) |
523
|
284 { |
634
|
285 double tmp = a.double_value (); |
523
|
286 nr = nc = NINT (tmp); |
|
287 } |
|
288 else |
|
289 { |
634
|
290 nr = a.rows (); |
|
291 nc = a.columns (); |
523
|
292 |
|
293 if ((nr == 1 && nc == 2) || (nr == 2 && nc == 1)) |
|
294 { |
634
|
295 ColumnVector v = a.vector_value (); |
523
|
296 |
633
|
297 if (error_state) |
|
298 return; |
|
299 |
523
|
300 nr = NINT (v.elem (0)); |
|
301 nc = NINT (v.elem (1)); |
|
302 } |
|
303 else |
|
304 warning ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
|
305 } |
|
306 |
|
307 check_dimensions (nr, nc, warn_for); // May set error_state. |
|
308 } |
|
309 |
|
310 static void |
|
311 get_dimensions (const tree_constant& a, const tree_constant& b, |
|
312 const char *warn_for, int& nr, int& nc) |
|
313 { |
634
|
314 nr = NINT (a.double_value ()); |
|
315 nc = NINT (b.double_value ()); |
523
|
316 |
634
|
317 if (error_state) |
|
318 error ("%s: expecting two scalar arguments", warn_for); |
523
|
319 else |
634
|
320 check_dimensions (nr, nc, warn_for); // May set error_state. |
523
|
321 } |
|
322 |
|
323 static tree_constant |
|
324 fill_matrix (const tree_constant& a, double val, const char *warn_for) |
|
325 { |
|
326 int nr, nc; |
|
327 get_dimensions (a, warn_for, nr, nc); |
|
328 |
|
329 if (error_state) |
|
330 return tree_constant (); |
|
331 |
|
332 Matrix m (nr, nc, val); |
|
333 |
|
334 return m; |
|
335 } |
|
336 |
|
337 static tree_constant |
|
338 fill_matrix (const tree_constant& a, const tree_constant& b, |
|
339 double val, const char *warn_for) |
|
340 { |
|
341 int nr, nc; |
|
342 get_dimensions (a, b, warn_for, nr, nc); // May set error_state. |
|
343 |
|
344 if (error_state) |
|
345 return tree_constant (); |
|
346 |
|
347 Matrix m (nr, nc, val); |
|
348 |
|
349 return m; |
|
350 } |
|
351 |
|
352 DEFUN ("ones", Fones, Sones, 3, 1, |
|
353 "ones (N), ones (N, M), ones (X): create a matrix of all ones") |
|
354 { |
|
355 Octave_object retval; |
|
356 |
|
357 int nargin = args.length (); |
|
358 |
|
359 switch (nargin) |
|
360 { |
610
|
361 case 1: |
|
362 retval = 1.0; |
|
363 break; |
523
|
364 case 2: |
|
365 retval = fill_matrix (args(1), 1.0, "ones"); |
|
366 break; |
|
367 case 3: |
|
368 retval = fill_matrix (args(1), args(2), 1.0, "ones"); |
|
369 break; |
|
370 default: |
|
371 print_usage ("ones"); |
|
372 break; |
|
373 } |
|
374 |
|
375 return retval; |
|
376 } |
|
377 |
|
378 DEFUN ("zeros", Fzeros, Szeros, 3, 1, |
|
379 "zeros (N), zeros (N, M), zeros (X): create a matrix of all zeros") |
|
380 { |
|
381 Octave_object retval; |
|
382 |
|
383 int nargin = args.length (); |
|
384 |
|
385 switch (nargin) |
|
386 { |
610
|
387 case 1: |
|
388 retval = 0.0; |
|
389 break; |
523
|
390 case 2: |
|
391 retval = fill_matrix (args(1), 0.0, "zeros"); |
|
392 break; |
|
393 case 3: |
|
394 retval = fill_matrix (args(1), args(2), 0.0, "zeros"); |
|
395 break; |
|
396 default: |
|
397 print_usage ("zeros"); |
|
398 break; |
|
399 } |
|
400 |
|
401 return retval; |
|
402 } |
|
403 |
|
404 static tree_constant |
|
405 identity_matrix (const tree_constant& a) |
|
406 { |
|
407 int nr, nc; |
|
408 get_dimensions (a, "eye", nr, nc); // May set error_state. |
|
409 |
|
410 if (error_state) |
|
411 return tree_constant (); |
|
412 |
|
413 Matrix m (nr, nc, 0.0); |
|
414 |
|
415 if (nr > 0 && nc > 0) |
|
416 { |
|
417 int n = MIN (nr, nc); |
|
418 for (int i = 0; i < n; i++) |
|
419 m.elem (i, i) = 1.0; |
|
420 } |
|
421 |
|
422 return m; |
|
423 } |
|
424 |
|
425 static tree_constant |
|
426 identity_matrix (const tree_constant& a, const tree_constant& b) |
|
427 { |
|
428 int nr, nc; |
|
429 get_dimensions (a, b, "eye", nr, nc); // May set error_state. |
|
430 |
|
431 if (error_state) |
|
432 return tree_constant (); |
|
433 |
|
434 Matrix m (nr, nc, 0.0); |
|
435 |
|
436 if (nr > 0 && nc > 0) |
|
437 { |
|
438 int n = MIN (nr, nc); |
|
439 for (int i = 0; i < n; i++) |
|
440 m.elem (i, i) = 1.0; |
|
441 } |
|
442 |
|
443 return m; |
|
444 } |
|
445 |
|
446 DEFUN ("eye", Feye, Seye, 3, 1, |
|
447 "eye (N), eye (N, M), eye (X): create an identity matrix") |
|
448 { |
|
449 Octave_object retval; |
|
450 |
|
451 int nargin = args.length (); |
|
452 |
|
453 switch (nargin) |
|
454 { |
610
|
455 case 1: |
|
456 retval = 1.0; |
|
457 break; |
523
|
458 case 2: |
|
459 retval = identity_matrix (args(1)); |
|
460 break; |
|
461 case 3: |
|
462 retval = identity_matrix (args(1), args(2)); |
|
463 break; |
|
464 default: |
|
465 print_usage ("eye"); |
|
466 break; |
|
467 } |
|
468 |
|
469 return retval; |
|
470 } |
|
471 |
|
472 /* |
|
473 ;;; Local Variables: *** |
|
474 ;;; mode: C++ *** |
|
475 ;;; page-delimiter: "^/\\*" *** |
|
476 ;;; End: *** |
|
477 */ |