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 |
719
|
120 assert (x_nr == y_nr && x_nc == y_nc); |
649
|
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 |
719
|
152 if (arg_y_empty > 0 && arg_x_empty > 0) |
|
153 return Matrix (); |
|
154 else if (arg_y_empty || arg_x_empty) |
649
|
155 return retval; |
|
156 |
|
157 int y_is_scalar = (y_nr == 1 && y_nc == 1); |
|
158 int x_is_scalar = (x_nr == 1 && x_nc == 1); |
|
159 |
|
160 if (y_is_scalar && x_is_scalar) |
|
161 { |
|
162 double y = arg_y.double_value (); |
|
163 |
|
164 if (! error_state) |
|
165 { |
|
166 double x = arg_x.double_value (); |
|
167 |
|
168 if (! error_state) |
|
169 retval = atan2 (y, x); |
|
170 } |
|
171 } |
|
172 else if (y_is_scalar) |
|
173 { |
|
174 double y = arg_y.double_value (); |
|
175 |
|
176 if (! error_state) |
|
177 { |
|
178 Matrix x = arg_x.matrix_value (); |
|
179 |
|
180 if (! error_state) |
|
181 retval = map (atan2, y, x); |
|
182 } |
|
183 } |
|
184 else if (x_is_scalar) |
|
185 { |
|
186 Matrix y = arg_y.matrix_value (); |
|
187 |
|
188 if (! error_state) |
|
189 { |
|
190 double x = arg_x.double_value (); |
|
191 |
|
192 if (! error_state) |
|
193 retval = map (atan2, y, x); |
|
194 } |
|
195 } |
|
196 else if (y_nr == x_nr && y_nc == x_nc) |
|
197 { |
|
198 Matrix y = arg_y.matrix_value (); |
|
199 |
|
200 if (! error_state) |
|
201 { |
|
202 Matrix x = arg_x.matrix_value (); |
|
203 |
|
204 if (! error_state) |
|
205 retval = map (atan2, y, x); |
|
206 } |
|
207 } |
|
208 else |
|
209 error ("atan2: nonconformant matrices"); |
|
210 } |
712
|
211 else |
|
212 print_usage ("atan2"); |
649
|
213 |
|
214 return retval; |
|
215 } |
|
216 |
712
|
217 DEFUN ("cumprod", Fcumprod, Scumprod, 1, 1, |
523
|
218 "cumprod (X): cumulative products") |
|
219 { |
|
220 Octave_object retval; |
|
221 |
|
222 int nargin = args.length (); |
|
223 |
760
|
224 if (nargin == 1) |
|
225 { |
|
226 tree_constant arg = args(0); |
|
227 |
|
228 if (arg.is_real_type ()) |
|
229 { |
|
230 Matrix tmp = arg.matrix_value (); |
|
231 |
|
232 if (! error_state) |
|
233 retval(0) = tmp.cumprod (); |
|
234 } |
|
235 else if (arg.is_complex_type ()) |
|
236 { |
|
237 ComplexMatrix tmp = arg.complex_matrix_value (); |
|
238 |
|
239 if (! error_state) |
|
240 retval(0) = tmp.cumprod (); |
|
241 } |
|
242 else |
|
243 { |
|
244 gripe_wrong_type_arg ("cumprod", arg); |
|
245 return retval; |
|
246 } |
|
247 } |
712
|
248 else |
523
|
249 print_usage ("cumprod"); |
|
250 |
|
251 return retval; |
|
252 } |
|
253 |
712
|
254 DEFUN ("cumsum", Fcumsum, Scumsum, 1, 1, |
523
|
255 "cumsum (X): cumulative sums") |
|
256 { |
|
257 Octave_object retval; |
|
258 |
|
259 int nargin = args.length (); |
|
260 |
760
|
261 if (nargin == 1) |
|
262 { |
|
263 tree_constant arg = args(0); |
|
264 |
|
265 if (arg.is_real_type ()) |
|
266 { |
|
267 Matrix tmp = arg.matrix_value (); |
|
268 |
|
269 if (! error_state) |
|
270 retval(0) = tmp.cumsum (); |
|
271 } |
|
272 else if (arg.is_complex_type ()) |
|
273 { |
|
274 ComplexMatrix tmp = arg.complex_matrix_value (); |
|
275 |
|
276 if (! error_state) |
|
277 retval(0) = tmp.cumsum (); |
|
278 } |
|
279 else |
|
280 { |
|
281 gripe_wrong_type_arg ("cumsum", arg); |
|
282 return retval; |
|
283 } |
|
284 } |
712
|
285 else |
523
|
286 print_usage ("cumsum"); |
|
287 |
|
288 return retval; |
|
289 } |
|
290 |
712
|
291 DEFUN ("diag", Fdiag, Sdiag, 2, 1, |
523
|
292 "diag (X [,k]): form/extract diagonals") |
|
293 { |
|
294 Octave_object retval; |
|
295 |
|
296 int nargin = args.length (); |
|
297 |
712
|
298 if (nargin == 1 && args(0).is_defined ()) |
|
299 retval = args(0).diag (); |
|
300 else if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
|
301 retval = args(0).diag (args(1)); |
523
|
302 else |
|
303 print_usage ("diag"); |
|
304 |
|
305 return retval; |
|
306 } |
|
307 |
712
|
308 DEFUN ("isstr", Fisstr, Sisstr, 1, 1, |
523
|
309 "isstr (X): return 1 if X is a string, 0 otherwise") |
|
310 { |
|
311 Octave_object retval; |
|
312 |
|
313 int nargin = args.length (); |
|
314 |
712
|
315 if (nargin == 1 && args(0).is_defined ()) |
|
316 retval = (double) args(0).is_string (); |
523
|
317 else |
712
|
318 print_usage ("isstr"); |
523
|
319 |
|
320 return retval; |
|
321 } |
|
322 |
712
|
323 DEFUN ("prod", Fprod, Sprod, 1, 1, |
523
|
324 "prod (X): products") |
|
325 { |
|
326 Octave_object retval; |
|
327 |
|
328 int nargin = args.length (); |
|
329 |
760
|
330 if (nargin == 1) |
|
331 { |
|
332 tree_constant arg = args(0); |
|
333 |
|
334 if (arg.is_real_type ()) |
|
335 { |
|
336 Matrix tmp = arg.matrix_value (); |
|
337 |
|
338 if (! error_state) |
|
339 retval(0) = tmp.prod (); |
|
340 } |
|
341 else if (arg.is_complex_type ()) |
|
342 { |
|
343 ComplexMatrix tmp = arg.complex_matrix_value (); |
|
344 |
|
345 if (! error_state) |
|
346 retval(0) = tmp.prod (); |
|
347 } |
|
348 else |
|
349 { |
|
350 gripe_wrong_type_arg ("prod", arg); |
|
351 return retval; |
|
352 } |
|
353 } |
712
|
354 else |
523
|
355 print_usage ("prod"); |
|
356 |
|
357 return retval; |
|
358 } |
|
359 |
712
|
360 DEFUN ("setstr", Fsetstr, Ssetstr, 1, 1, |
523
|
361 "setstr (V): convert a vector to a string") |
|
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).convert_to_str (); |
523
|
369 else |
|
370 print_usage ("setstr"); |
|
371 |
|
372 return retval; |
|
373 } |
|
374 |
712
|
375 DEFUN ("size", Fsize, Ssize, 1, 1, |
523
|
376 "[m, n] = size (x): return rows and columns of X") |
|
377 { |
|
378 Octave_object retval; |
|
379 |
|
380 int nargin = args.length (); |
|
381 |
712
|
382 if (nargin == 1 && args(0).is_defined ()) |
523
|
383 { |
712
|
384 int nr = args(0).rows (); |
|
385 int nc = args(0).columns (); |
|
386 if (nargout == 0 || nargout == 1) |
523
|
387 { |
712
|
388 Matrix m (1, 2); |
|
389 m.elem (0, 0) = nr; |
|
390 m.elem (0, 1) = nc; |
|
391 retval = m; |
523
|
392 } |
712
|
393 else if (nargout == 2) |
|
394 { |
|
395 retval(1) = (double) nc; |
|
396 retval(0) = (double) nr; |
|
397 } |
|
398 else |
|
399 print_usage ("size"); |
523
|
400 } |
712
|
401 else |
|
402 print_usage ("size"); |
523
|
403 |
|
404 return retval; |
|
405 } |
|
406 |
712
|
407 DEFUN ("sum", Fsum, Ssum, 1, 1, |
523
|
408 "sum (X): sum of elements") |
|
409 { |
|
410 Octave_object retval; |
|
411 |
|
412 int nargin = args.length (); |
|
413 |
760
|
414 if (nargin == 1) |
|
415 { |
|
416 tree_constant arg = args(0); |
|
417 |
|
418 if (arg.is_real_type ()) |
|
419 { |
|
420 Matrix tmp = arg.matrix_value (); |
|
421 |
|
422 if (! error_state) |
|
423 retval(0) = tmp.sum (); |
|
424 } |
|
425 else if (arg.is_complex_type ()) |
|
426 { |
|
427 ComplexMatrix tmp = arg.complex_matrix_value (); |
|
428 |
|
429 if (! error_state) |
|
430 retval(0) = tmp.sum (); |
|
431 } |
|
432 else |
|
433 { |
|
434 gripe_wrong_type_arg ("sum", arg); |
|
435 return retval; |
|
436 } |
|
437 } |
523
|
438 else |
712
|
439 print_usage ("sum"); |
523
|
440 |
|
441 return retval; |
|
442 } |
|
443 |
712
|
444 DEFUN ("sumsq", Fsumsq, Ssumsq, 1, 1, |
523
|
445 "sumsq (X): sum of squares of elements") |
|
446 { |
|
447 Octave_object retval; |
|
448 |
|
449 int nargin = args.length (); |
|
450 |
760
|
451 if (nargin == 1) |
|
452 { |
|
453 tree_constant arg = args(0); |
|
454 |
|
455 if (arg.is_real_type ()) |
|
456 { |
|
457 Matrix tmp = arg.matrix_value (); |
|
458 |
|
459 if (! error_state) |
|
460 retval(0) = tmp.sumsq (); |
|
461 } |
|
462 else if (arg.is_complex_type ()) |
|
463 { |
|
464 ComplexMatrix tmp = arg.complex_matrix_value (); |
|
465 |
|
466 if (! error_state) |
|
467 retval(0) = tmp.sumsq (); |
|
468 } |
|
469 else |
|
470 { |
|
471 gripe_wrong_type_arg ("sumsq", arg); |
|
472 return retval; |
|
473 } |
|
474 } |
712
|
475 else |
523
|
476 print_usage ("sumsq"); |
|
477 |
|
478 return retval; |
|
479 } |
|
480 |
|
481 static void |
|
482 check_dimensions (int& nr, int& nc, const char *warnfor) |
|
483 { |
|
484 if (nr < 0 || nc < 0) |
|
485 { |
|
486 if (user_pref.treat_neg_dim_as_zero) |
597
|
487 { |
|
488 nr = (nr < 0) ? 0 : nr; |
|
489 nc = (nc < 0) ? 0 : nc; |
|
490 } |
523
|
491 else |
|
492 error ("%s: can't create a matrix with negative dimensions", |
|
493 warnfor); |
|
494 } |
|
495 } |
|
496 |
|
497 static void |
|
498 get_dimensions (const tree_constant& a, const char *warn_for, |
|
499 int& nr, int& nc) |
|
500 { |
634
|
501 if (a.is_scalar_type ()) |
523
|
502 { |
634
|
503 double tmp = a.double_value (); |
523
|
504 nr = nc = NINT (tmp); |
|
505 } |
|
506 else |
|
507 { |
634
|
508 nr = a.rows (); |
|
509 nc = a.columns (); |
523
|
510 |
|
511 if ((nr == 1 && nc == 2) || (nr == 2 && nc == 1)) |
|
512 { |
634
|
513 ColumnVector v = a.vector_value (); |
523
|
514 |
633
|
515 if (error_state) |
|
516 return; |
|
517 |
523
|
518 nr = NINT (v.elem (0)); |
|
519 nc = NINT (v.elem (1)); |
|
520 } |
|
521 else |
|
522 warning ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
|
523 } |
|
524 |
|
525 check_dimensions (nr, nc, warn_for); // May set error_state. |
|
526 } |
|
527 |
|
528 static void |
|
529 get_dimensions (const tree_constant& a, const tree_constant& b, |
|
530 const char *warn_for, int& nr, int& nc) |
|
531 { |
634
|
532 nr = NINT (a.double_value ()); |
|
533 nc = NINT (b.double_value ()); |
523
|
534 |
634
|
535 if (error_state) |
|
536 error ("%s: expecting two scalar arguments", warn_for); |
523
|
537 else |
634
|
538 check_dimensions (nr, nc, warn_for); // May set error_state. |
523
|
539 } |
|
540 |
|
541 static tree_constant |
|
542 fill_matrix (const tree_constant& a, double val, const char *warn_for) |
|
543 { |
|
544 int nr, nc; |
|
545 get_dimensions (a, warn_for, nr, nc); |
|
546 |
|
547 if (error_state) |
|
548 return tree_constant (); |
|
549 |
|
550 Matrix m (nr, nc, val); |
|
551 |
|
552 return m; |
|
553 } |
|
554 |
|
555 static tree_constant |
|
556 fill_matrix (const tree_constant& a, const tree_constant& b, |
|
557 double val, const char *warn_for) |
|
558 { |
|
559 int nr, nc; |
|
560 get_dimensions (a, b, warn_for, nr, nc); // May set error_state. |
|
561 |
|
562 if (error_state) |
|
563 return tree_constant (); |
|
564 |
|
565 Matrix m (nr, nc, val); |
|
566 |
|
567 return m; |
|
568 } |
|
569 |
712
|
570 DEFUN ("ones", Fones, Sones, 2, 1, |
523
|
571 "ones (N), ones (N, M), ones (X): create a matrix of all ones") |
|
572 { |
|
573 Octave_object retval; |
|
574 |
|
575 int nargin = args.length (); |
|
576 |
|
577 switch (nargin) |
|
578 { |
712
|
579 case 0: |
|
580 retval = 1.0; |
|
581 break; |
610
|
582 case 1: |
712
|
583 retval = fill_matrix (args(0), 1.0, "ones"); |
610
|
584 break; |
523
|
585 case 2: |
712
|
586 retval = fill_matrix (args(0), args(1), 1.0, "ones"); |
523
|
587 break; |
|
588 default: |
|
589 print_usage ("ones"); |
|
590 break; |
|
591 } |
|
592 |
|
593 return retval; |
|
594 } |
|
595 |
712
|
596 DEFUN ("zeros", Fzeros, Szeros, 2, 1, |
523
|
597 "zeros (N), zeros (N, M), zeros (X): create a matrix of all zeros") |
|
598 { |
|
599 Octave_object retval; |
|
600 |
|
601 int nargin = args.length (); |
|
602 |
|
603 switch (nargin) |
|
604 { |
712
|
605 case 0: |
|
606 retval = 0.0; |
|
607 break; |
610
|
608 case 1: |
712
|
609 retval = fill_matrix (args(0), 0.0, "zeros"); |
610
|
610 break; |
523
|
611 case 2: |
712
|
612 retval = fill_matrix (args(0), args(1), 0.0, "zeros"); |
523
|
613 break; |
|
614 default: |
|
615 print_usage ("zeros"); |
|
616 break; |
|
617 } |
|
618 |
|
619 return retval; |
|
620 } |
|
621 |
|
622 static tree_constant |
|
623 identity_matrix (const tree_constant& a) |
|
624 { |
|
625 int nr, nc; |
|
626 get_dimensions (a, "eye", nr, nc); // May set error_state. |
|
627 |
|
628 if (error_state) |
|
629 return tree_constant (); |
|
630 |
|
631 Matrix m (nr, nc, 0.0); |
|
632 |
|
633 if (nr > 0 && nc > 0) |
|
634 { |
|
635 int n = MIN (nr, nc); |
|
636 for (int i = 0; i < n; i++) |
|
637 m.elem (i, i) = 1.0; |
|
638 } |
|
639 |
|
640 return m; |
|
641 } |
|
642 |
|
643 static tree_constant |
|
644 identity_matrix (const tree_constant& a, const tree_constant& b) |
|
645 { |
|
646 int nr, nc; |
|
647 get_dimensions (a, b, "eye", nr, nc); // May set error_state. |
|
648 |
|
649 if (error_state) |
|
650 return tree_constant (); |
|
651 |
|
652 Matrix m (nr, nc, 0.0); |
|
653 |
|
654 if (nr > 0 && nc > 0) |
|
655 { |
|
656 int n = MIN (nr, nc); |
|
657 for (int i = 0; i < n; i++) |
|
658 m.elem (i, i) = 1.0; |
|
659 } |
|
660 |
|
661 return m; |
|
662 } |
|
663 |
712
|
664 DEFUN ("eye", Feye, Seye, 2, 1, |
523
|
665 "eye (N), eye (N, M), eye (X): create an identity matrix") |
|
666 { |
|
667 Octave_object retval; |
|
668 |
|
669 int nargin = args.length (); |
|
670 |
|
671 switch (nargin) |
|
672 { |
712
|
673 case 0: |
|
674 retval = 1.0; |
|
675 break; |
610
|
676 case 1: |
712
|
677 retval = identity_matrix (args(0)); |
610
|
678 break; |
523
|
679 case 2: |
712
|
680 retval = identity_matrix (args(0), args(1)); |
523
|
681 break; |
|
682 default: |
|
683 print_usage ("eye"); |
|
684 break; |
|
685 } |
|
686 |
|
687 return retval; |
|
688 } |
|
689 |
|
690 /* |
|
691 ;;; Local Variables: *** |
|
692 ;;; mode: C++ *** |
|
693 ;;; page-delimiter: "^/\\*" *** |
|
694 ;;; End: *** |
|
695 */ |