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 |
712
|
47 DEFUN ("all", Fall, Sall, 1, 1, |
523
|
48 "all (X): are all elements of X nonzero?") |
|
49 { |
|
50 Octave_object retval; |
|
51 |
|
52 int nargin = args.length (); |
|
53 |
712
|
54 if (nargin == 1 && args(0).is_defined ()) |
|
55 retval = args(0).all (); |
|
56 else |
523
|
57 print_usage ("all"); |
|
58 |
|
59 return retval; |
|
60 } |
|
61 |
712
|
62 DEFUN ("any", Fany, Sany, 1, 1, |
523
|
63 "any (X): are any elements of X nonzero?") |
|
64 { |
|
65 Octave_object retval; |
|
66 |
|
67 int nargin = args.length (); |
|
68 |
712
|
69 if (nargin == 1 && args(0).is_defined ()) |
|
70 retval = args(0).any (); |
|
71 else |
523
|
72 print_usage ("any"); |
|
73 |
|
74 return retval; |
|
75 } |
|
76 |
649
|
77 // These mapping functions may also be useful in other places, eh? |
|
78 |
|
79 typedef double (*d_dd_fcn) (double, double); |
|
80 |
|
81 static Matrix |
|
82 map (d_dd_fcn f, double x, const Matrix& y) |
|
83 { |
|
84 int nr = y.rows (); |
|
85 int nc = y.columns (); |
|
86 |
|
87 Matrix retval (nr, nc); |
|
88 |
|
89 for (int j = 0; j < nc; j++) |
|
90 for (int i = 0; i < nr; i++) |
|
91 retval.elem (i, j) = f (x, y.elem (i, j)); |
|
92 |
|
93 return retval; |
|
94 } |
|
95 |
|
96 static Matrix |
|
97 map (d_dd_fcn f, const Matrix& x, double y) |
|
98 { |
|
99 int nr = x.rows (); |
|
100 int nc = x.columns (); |
|
101 |
|
102 Matrix retval (nr, nc); |
|
103 |
|
104 for (int j = 0; j < nc; j++) |
|
105 for (int i = 0; i < nr; i++) |
|
106 retval.elem (i, j) = f (x.elem (i, j), y); |
|
107 |
|
108 return retval; |
|
109 } |
|
110 |
|
111 static Matrix |
|
112 map (d_dd_fcn f, const Matrix& x, const Matrix& y) |
|
113 { |
|
114 int x_nr = x.rows (); |
|
115 int x_nc = x.columns (); |
|
116 |
|
117 int y_nr = y.rows (); |
|
118 int y_nc = y.columns (); |
|
119 |
|
120 assert (x_nr == x_nc && y_nr == y_nc); |
|
121 |
|
122 Matrix retval (x_nr, x_nc); |
|
123 |
|
124 for (int j = 0; j < x_nc; j++) |
|
125 for (int i = 0; i < x_nr; i++) |
|
126 retval.elem (i, j) = f (x.elem (i, j), y.elem (i, j)); |
|
127 |
|
128 return retval; |
|
129 } |
|
130 |
712
|
131 DEFUN ("atan2", Fatan2, Satan2, 2, 1, |
649
|
132 "atan2 (Y, X): atan (Y / X) in range -pi to pi") |
|
133 { |
|
134 Octave_object retval; |
|
135 |
712
|
136 int nargin = args.length (); |
|
137 |
|
138 if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
649
|
139 { |
712
|
140 tree_constant arg_y = args(0); |
|
141 tree_constant arg_x = args(1); |
649
|
142 |
|
143 int y_nr = arg_y.rows (); |
|
144 int y_nc = arg_y.columns (); |
|
145 |
|
146 int x_nr = arg_x.rows (); |
|
147 int x_nc = arg_x.columns (); |
|
148 |
|
149 int arg_y_empty = empty_arg ("atan2", y_nr, y_nc); |
|
150 int arg_x_empty = empty_arg ("atan2", x_nr, x_nc); |
|
151 |
|
152 if (arg_y_empty < 0 || arg_x_empty < 0) |
|
153 return retval; |
|
154 |
|
155 if (arg_y_empty || arg_x_empty) |
|
156 { |
|
157 retval = Matrix (); |
|
158 return retval; |
|
159 } |
|
160 |
|
161 int y_is_scalar = (y_nr == 1 && y_nc == 1); |
|
162 int x_is_scalar = (x_nr == 1 && x_nc == 1); |
|
163 |
|
164 if (y_is_scalar && x_is_scalar) |
|
165 { |
|
166 double y = arg_y.double_value (); |
|
167 |
|
168 if (! error_state) |
|
169 { |
|
170 double x = arg_x.double_value (); |
|
171 |
|
172 if (! error_state) |
|
173 retval = atan2 (y, x); |
|
174 } |
|
175 } |
|
176 else if (y_is_scalar) |
|
177 { |
|
178 double y = arg_y.double_value (); |
|
179 |
|
180 if (! error_state) |
|
181 { |
|
182 Matrix x = arg_x.matrix_value (); |
|
183 |
|
184 if (! error_state) |
|
185 retval = map (atan2, y, x); |
|
186 } |
|
187 } |
|
188 else if (x_is_scalar) |
|
189 { |
|
190 Matrix y = arg_y.matrix_value (); |
|
191 |
|
192 if (! error_state) |
|
193 { |
|
194 double x = arg_x.double_value (); |
|
195 |
|
196 if (! error_state) |
|
197 retval = map (atan2, y, x); |
|
198 } |
|
199 } |
|
200 else if (y_nr == x_nr && y_nc == x_nc) |
|
201 { |
|
202 Matrix y = arg_y.matrix_value (); |
|
203 |
|
204 if (! error_state) |
|
205 { |
|
206 Matrix x = arg_x.matrix_value (); |
|
207 |
|
208 if (! error_state) |
|
209 retval = map (atan2, y, x); |
|
210 } |
|
211 } |
|
212 else |
|
213 error ("atan2: nonconformant matrices"); |
|
214 } |
712
|
215 else |
|
216 print_usage ("atan2"); |
649
|
217 |
|
218 return retval; |
|
219 } |
|
220 |
712
|
221 DEFUN ("cumprod", Fcumprod, Scumprod, 1, 1, |
523
|
222 "cumprod (X): cumulative products") |
|
223 { |
|
224 Octave_object retval; |
|
225 |
|
226 int nargin = args.length (); |
|
227 |
712
|
228 if (nargin == 1 && args(0).is_defined ()) |
|
229 retval = args(0).cumprod (); |
|
230 else |
523
|
231 print_usage ("cumprod"); |
|
232 |
|
233 return retval; |
|
234 } |
|
235 |
712
|
236 DEFUN ("cumsum", Fcumsum, Scumsum, 1, 1, |
523
|
237 "cumsum (X): cumulative sums") |
|
238 { |
|
239 Octave_object retval; |
|
240 |
|
241 int nargin = args.length (); |
|
242 |
712
|
243 if (nargin == 1 && args(0).is_defined ()) |
|
244 retval = args(0).cumsum (); |
|
245 else |
523
|
246 print_usage ("cumsum"); |
|
247 |
|
248 return retval; |
|
249 } |
|
250 |
712
|
251 DEFUN ("diag", Fdiag, Sdiag, 2, 1, |
523
|
252 "diag (X [,k]): form/extract diagonals") |
|
253 { |
|
254 Octave_object retval; |
|
255 |
|
256 int nargin = args.length (); |
|
257 |
712
|
258 if (nargin == 1 && args(0).is_defined ()) |
|
259 retval = args(0).diag (); |
|
260 else if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
|
261 retval = args(0).diag (args(1)); |
523
|
262 else |
|
263 print_usage ("diag"); |
|
264 |
|
265 return retval; |
|
266 } |
|
267 |
712
|
268 DEFUN ("isstr", Fisstr, Sisstr, 1, 1, |
523
|
269 "isstr (X): return 1 if X is a string, 0 otherwise") |
|
270 { |
|
271 Octave_object retval; |
|
272 |
|
273 int nargin = args.length (); |
|
274 |
712
|
275 if (nargin == 1 && args(0).is_defined ()) |
|
276 retval = (double) args(0).is_string (); |
523
|
277 else |
712
|
278 print_usage ("isstr"); |
523
|
279 |
|
280 return retval; |
|
281 } |
|
282 |
712
|
283 DEFUN ("prod", Fprod, Sprod, 1, 1, |
523
|
284 "prod (X): products") |
|
285 { |
|
286 Octave_object retval; |
|
287 |
|
288 int nargin = args.length (); |
|
289 |
712
|
290 if (nargin == 1 && args(0).is_defined ()) |
|
291 retval = args(0).prod (); |
|
292 else |
523
|
293 print_usage ("prod"); |
|
294 |
|
295 return retval; |
|
296 } |
|
297 |
712
|
298 DEFUN ("setstr", Fsetstr, Ssetstr, 1, 1, |
523
|
299 "setstr (V): convert a vector to a string") |
|
300 { |
|
301 Octave_object retval; |
|
302 |
|
303 int nargin = args.length (); |
|
304 |
712
|
305 if (nargin == 1 && args(0).is_defined ()) |
|
306 retval = args(0).convert_to_str (); |
523
|
307 else |
|
308 print_usage ("setstr"); |
|
309 |
|
310 return retval; |
|
311 } |
|
312 |
712
|
313 DEFUN ("size", Fsize, Ssize, 1, 1, |
523
|
314 "[m, n] = size (x): return rows and columns of X") |
|
315 { |
|
316 Octave_object retval; |
|
317 |
|
318 int nargin = args.length (); |
|
319 |
712
|
320 if (nargin == 1 && args(0).is_defined ()) |
523
|
321 { |
712
|
322 int nr = args(0).rows (); |
|
323 int nc = args(0).columns (); |
|
324 if (nargout == 0 || nargout == 1) |
523
|
325 { |
712
|
326 Matrix m (1, 2); |
|
327 m.elem (0, 0) = nr; |
|
328 m.elem (0, 1) = nc; |
|
329 retval = m; |
523
|
330 } |
712
|
331 else if (nargout == 2) |
|
332 { |
|
333 retval(1) = (double) nc; |
|
334 retval(0) = (double) nr; |
|
335 } |
|
336 else |
|
337 print_usage ("size"); |
523
|
338 } |
712
|
339 else |
|
340 print_usage ("size"); |
523
|
341 |
|
342 return retval; |
|
343 } |
|
344 |
712
|
345 DEFUN ("sum", Fsum, Ssum, 1, 1, |
523
|
346 "sum (X): sum of elements") |
|
347 { |
|
348 Octave_object retval; |
|
349 |
|
350 int nargin = args.length (); |
|
351 |
712
|
352 if (nargin == 1 && args(0).is_defined ()) |
|
353 retval = args(0).sum (); |
523
|
354 else |
712
|
355 print_usage ("sum"); |
523
|
356 |
|
357 return retval; |
|
358 } |
|
359 |
712
|
360 DEFUN ("sumsq", Fsumsq, Ssumsq, 1, 1, |
523
|
361 "sumsq (X): sum of squares of elements") |
|
362 { |
|
363 Octave_object retval; |
|
364 |
|
365 int nargin = args.length (); |
|
366 |
712
|
367 if (nargin == 1 && args(0).is_defined ()) |
|
368 retval = args(0).sumsq (); |
|
369 else |
523
|
370 print_usage ("sumsq"); |
|
371 |
|
372 return retval; |
|
373 } |
|
374 |
|
375 static void |
|
376 check_dimensions (int& nr, int& nc, const char *warnfor) |
|
377 { |
|
378 if (nr < 0 || nc < 0) |
|
379 { |
|
380 if (user_pref.treat_neg_dim_as_zero) |
597
|
381 { |
|
382 nr = (nr < 0) ? 0 : nr; |
|
383 nc = (nc < 0) ? 0 : nc; |
|
384 } |
523
|
385 else |
|
386 error ("%s: can't create a matrix with negative dimensions", |
|
387 warnfor); |
|
388 } |
|
389 } |
|
390 |
|
391 static void |
|
392 get_dimensions (const tree_constant& a, const char *warn_for, |
|
393 int& nr, int& nc) |
|
394 { |
634
|
395 if (a.is_scalar_type ()) |
523
|
396 { |
634
|
397 double tmp = a.double_value (); |
523
|
398 nr = nc = NINT (tmp); |
|
399 } |
|
400 else |
|
401 { |
634
|
402 nr = a.rows (); |
|
403 nc = a.columns (); |
523
|
404 |
|
405 if ((nr == 1 && nc == 2) || (nr == 2 && nc == 1)) |
|
406 { |
634
|
407 ColumnVector v = a.vector_value (); |
523
|
408 |
633
|
409 if (error_state) |
|
410 return; |
|
411 |
523
|
412 nr = NINT (v.elem (0)); |
|
413 nc = NINT (v.elem (1)); |
|
414 } |
|
415 else |
|
416 warning ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
|
417 } |
|
418 |
|
419 check_dimensions (nr, nc, warn_for); // May set error_state. |
|
420 } |
|
421 |
|
422 static void |
|
423 get_dimensions (const tree_constant& a, const tree_constant& b, |
|
424 const char *warn_for, int& nr, int& nc) |
|
425 { |
634
|
426 nr = NINT (a.double_value ()); |
|
427 nc = NINT (b.double_value ()); |
523
|
428 |
634
|
429 if (error_state) |
|
430 error ("%s: expecting two scalar arguments", warn_for); |
523
|
431 else |
634
|
432 check_dimensions (nr, nc, warn_for); // May set error_state. |
523
|
433 } |
|
434 |
|
435 static tree_constant |
|
436 fill_matrix (const tree_constant& a, double val, const char *warn_for) |
|
437 { |
|
438 int nr, nc; |
|
439 get_dimensions (a, warn_for, nr, nc); |
|
440 |
|
441 if (error_state) |
|
442 return tree_constant (); |
|
443 |
|
444 Matrix m (nr, nc, val); |
|
445 |
|
446 return m; |
|
447 } |
|
448 |
|
449 static tree_constant |
|
450 fill_matrix (const tree_constant& a, const tree_constant& b, |
|
451 double val, const char *warn_for) |
|
452 { |
|
453 int nr, nc; |
|
454 get_dimensions (a, b, warn_for, nr, nc); // May set error_state. |
|
455 |
|
456 if (error_state) |
|
457 return tree_constant (); |
|
458 |
|
459 Matrix m (nr, nc, val); |
|
460 |
|
461 return m; |
|
462 } |
|
463 |
712
|
464 DEFUN ("ones", Fones, Sones, 2, 1, |
523
|
465 "ones (N), ones (N, M), ones (X): create a matrix of all ones") |
|
466 { |
|
467 Octave_object retval; |
|
468 |
|
469 int nargin = args.length (); |
|
470 |
|
471 switch (nargin) |
|
472 { |
712
|
473 case 0: |
|
474 retval = 1.0; |
|
475 break; |
610
|
476 case 1: |
712
|
477 retval = fill_matrix (args(0), 1.0, "ones"); |
610
|
478 break; |
523
|
479 case 2: |
712
|
480 retval = fill_matrix (args(0), args(1), 1.0, "ones"); |
523
|
481 break; |
|
482 default: |
|
483 print_usage ("ones"); |
|
484 break; |
|
485 } |
|
486 |
|
487 return retval; |
|
488 } |
|
489 |
712
|
490 DEFUN ("zeros", Fzeros, Szeros, 2, 1, |
523
|
491 "zeros (N), zeros (N, M), zeros (X): create a matrix of all zeros") |
|
492 { |
|
493 Octave_object retval; |
|
494 |
|
495 int nargin = args.length (); |
|
496 |
|
497 switch (nargin) |
|
498 { |
712
|
499 case 0: |
|
500 retval = 0.0; |
|
501 break; |
610
|
502 case 1: |
712
|
503 retval = fill_matrix (args(0), 0.0, "zeros"); |
610
|
504 break; |
523
|
505 case 2: |
712
|
506 retval = fill_matrix (args(0), args(1), 0.0, "zeros"); |
523
|
507 break; |
|
508 default: |
|
509 print_usage ("zeros"); |
|
510 break; |
|
511 } |
|
512 |
|
513 return retval; |
|
514 } |
|
515 |
|
516 static tree_constant |
|
517 identity_matrix (const tree_constant& a) |
|
518 { |
|
519 int nr, nc; |
|
520 get_dimensions (a, "eye", nr, nc); // May set error_state. |
|
521 |
|
522 if (error_state) |
|
523 return tree_constant (); |
|
524 |
|
525 Matrix m (nr, nc, 0.0); |
|
526 |
|
527 if (nr > 0 && nc > 0) |
|
528 { |
|
529 int n = MIN (nr, nc); |
|
530 for (int i = 0; i < n; i++) |
|
531 m.elem (i, i) = 1.0; |
|
532 } |
|
533 |
|
534 return m; |
|
535 } |
|
536 |
|
537 static tree_constant |
|
538 identity_matrix (const tree_constant& a, const tree_constant& b) |
|
539 { |
|
540 int nr, nc; |
|
541 get_dimensions (a, b, "eye", nr, nc); // May set error_state. |
|
542 |
|
543 if (error_state) |
|
544 return tree_constant (); |
|
545 |
|
546 Matrix m (nr, nc, 0.0); |
|
547 |
|
548 if (nr > 0 && nc > 0) |
|
549 { |
|
550 int n = MIN (nr, nc); |
|
551 for (int i = 0; i < n; i++) |
|
552 m.elem (i, i) = 1.0; |
|
553 } |
|
554 |
|
555 return m; |
|
556 } |
|
557 |
712
|
558 DEFUN ("eye", Feye, Seye, 2, 1, |
523
|
559 "eye (N), eye (N, M), eye (X): create an identity matrix") |
|
560 { |
|
561 Octave_object retval; |
|
562 |
|
563 int nargin = args.length (); |
|
564 |
|
565 switch (nargin) |
|
566 { |
712
|
567 case 0: |
|
568 retval = 1.0; |
|
569 break; |
610
|
570 case 1: |
712
|
571 retval = identity_matrix (args(0)); |
610
|
572 break; |
523
|
573 case 2: |
712
|
574 retval = identity_matrix (args(0), args(1)); |
523
|
575 break; |
|
576 default: |
|
577 print_usage ("eye"); |
|
578 break; |
|
579 } |
|
580 |
|
581 return retval; |
|
582 } |
|
583 |
|
584 /* |
|
585 ;;; Local Variables: *** |
|
586 ;;; mode: C++ *** |
|
587 ;;; page-delimiter: "^/\\*" *** |
|
588 ;;; End: *** |
|
589 */ |