523
|
1 // data.cc -*- C++ -*- |
|
2 /* |
|
3 |
1884
|
4 Copyright (C) 1996 John W. Eaton |
523
|
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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
523
|
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 |
1192
|
33 #include <config.h> |
523
|
34 #endif |
|
35 |
1728
|
36 #include <string> |
|
37 |
1755
|
38 #include "str-vec.h" |
|
39 |
1352
|
40 #include "defun.h" |
|
41 #include "error.h" |
|
42 #include "gripes.h" |
|
43 #include "help.h" |
|
44 #include "oct-map.h" |
1742
|
45 #include "pt-const.h" |
|
46 #include "oct-obj.h" |
523
|
47 #include "user-prefs.h" |
|
48 #include "utils.h" |
|
49 |
|
50 #ifndef MIN |
|
51 #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
|
52 #endif |
|
53 |
767
|
54 #ifndef ABS |
|
55 #define ABS(x) (((x) < 0) ? (-x) : (x)) |
|
56 #endif |
|
57 |
1488
|
58 DEFUN ("all", Fall, Sall, 10, |
523
|
59 "all (X): are all elements of X nonzero?") |
|
60 { |
|
61 Octave_object retval; |
|
62 |
|
63 int nargin = args.length (); |
|
64 |
712
|
65 if (nargin == 1 && args(0).is_defined ()) |
|
66 retval = args(0).all (); |
|
67 else |
523
|
68 print_usage ("all"); |
|
69 |
|
70 return retval; |
|
71 } |
|
72 |
1488
|
73 DEFUN ("any", Fany, Sany, 10, |
523
|
74 "any (X): are any elements of X nonzero?") |
|
75 { |
|
76 Octave_object retval; |
|
77 |
|
78 int nargin = args.length (); |
|
79 |
712
|
80 if (nargin == 1 && args(0).is_defined ()) |
|
81 retval = args(0).any (); |
|
82 else |
523
|
83 print_usage ("any"); |
|
84 |
|
85 return retval; |
|
86 } |
|
87 |
649
|
88 // These mapping functions may also be useful in other places, eh? |
|
89 |
|
90 typedef double (*d_dd_fcn) (double, double); |
|
91 |
|
92 static Matrix |
|
93 map (d_dd_fcn f, double x, const Matrix& y) |
|
94 { |
|
95 int nr = y.rows (); |
|
96 int nc = y.columns (); |
|
97 |
|
98 Matrix retval (nr, nc); |
|
99 |
|
100 for (int j = 0; j < nc; j++) |
|
101 for (int i = 0; i < nr; i++) |
|
102 retval.elem (i, j) = f (x, y.elem (i, j)); |
|
103 |
|
104 return retval; |
|
105 } |
|
106 |
|
107 static Matrix |
|
108 map (d_dd_fcn f, const Matrix& x, double y) |
|
109 { |
|
110 int nr = x.rows (); |
|
111 int nc = x.columns (); |
|
112 |
|
113 Matrix retval (nr, nc); |
|
114 |
|
115 for (int j = 0; j < nc; j++) |
|
116 for (int i = 0; i < nr; i++) |
|
117 retval.elem (i, j) = f (x.elem (i, j), y); |
|
118 |
|
119 return retval; |
|
120 } |
|
121 |
|
122 static Matrix |
|
123 map (d_dd_fcn f, const Matrix& x, const Matrix& y) |
|
124 { |
|
125 int x_nr = x.rows (); |
|
126 int x_nc = x.columns (); |
|
127 |
|
128 int y_nr = y.rows (); |
|
129 int y_nc = y.columns (); |
|
130 |
719
|
131 assert (x_nr == y_nr && x_nc == y_nc); |
649
|
132 |
|
133 Matrix retval (x_nr, x_nc); |
|
134 |
|
135 for (int j = 0; j < x_nc; j++) |
|
136 for (int i = 0; i < x_nr; i++) |
|
137 retval.elem (i, j) = f (x.elem (i, j), y.elem (i, j)); |
|
138 |
|
139 return retval; |
|
140 } |
|
141 |
1488
|
142 DEFUN ("atan2", Fatan2, Satan2, 10, |
649
|
143 "atan2 (Y, X): atan (Y / X) in range -pi to pi") |
|
144 { |
|
145 Octave_object retval; |
|
146 |
712
|
147 int nargin = args.length (); |
|
148 |
|
149 if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
649
|
150 { |
712
|
151 tree_constant arg_y = args(0); |
|
152 tree_constant arg_x = args(1); |
649
|
153 |
|
154 int y_nr = arg_y.rows (); |
|
155 int y_nc = arg_y.columns (); |
|
156 |
|
157 int x_nr = arg_x.rows (); |
|
158 int x_nc = arg_x.columns (); |
|
159 |
|
160 int arg_y_empty = empty_arg ("atan2", y_nr, y_nc); |
|
161 int arg_x_empty = empty_arg ("atan2", x_nr, x_nc); |
|
162 |
719
|
163 if (arg_y_empty > 0 && arg_x_empty > 0) |
|
164 return Matrix (); |
|
165 else if (arg_y_empty || arg_x_empty) |
649
|
166 return retval; |
|
167 |
|
168 int y_is_scalar = (y_nr == 1 && y_nc == 1); |
|
169 int x_is_scalar = (x_nr == 1 && x_nc == 1); |
|
170 |
|
171 if (y_is_scalar && x_is_scalar) |
|
172 { |
|
173 double y = arg_y.double_value (); |
|
174 |
|
175 if (! error_state) |
|
176 { |
|
177 double x = arg_x.double_value (); |
|
178 |
|
179 if (! error_state) |
|
180 retval = atan2 (y, x); |
|
181 } |
|
182 } |
|
183 else if (y_is_scalar) |
|
184 { |
|
185 double y = arg_y.double_value (); |
|
186 |
|
187 if (! error_state) |
|
188 { |
|
189 Matrix x = arg_x.matrix_value (); |
|
190 |
|
191 if (! error_state) |
|
192 retval = map (atan2, y, x); |
|
193 } |
|
194 } |
|
195 else if (x_is_scalar) |
|
196 { |
|
197 Matrix y = arg_y.matrix_value (); |
|
198 |
|
199 if (! error_state) |
|
200 { |
|
201 double x = arg_x.double_value (); |
|
202 |
|
203 if (! error_state) |
|
204 retval = map (atan2, y, x); |
|
205 } |
|
206 } |
|
207 else if (y_nr == x_nr && y_nc == x_nc) |
|
208 { |
|
209 Matrix y = arg_y.matrix_value (); |
|
210 |
|
211 if (! error_state) |
|
212 { |
|
213 Matrix x = arg_x.matrix_value (); |
|
214 |
|
215 if (! error_state) |
|
216 retval = map (atan2, y, x); |
|
217 } |
|
218 } |
|
219 else |
|
220 error ("atan2: nonconformant matrices"); |
|
221 } |
712
|
222 else |
|
223 print_usage ("atan2"); |
649
|
224 |
|
225 return retval; |
|
226 } |
|
227 |
1488
|
228 DEFUN ("cumprod", Fcumprod, Scumprod, 10, |
523
|
229 "cumprod (X): cumulative products") |
|
230 { |
|
231 Octave_object retval; |
|
232 |
|
233 int nargin = args.length (); |
|
234 |
760
|
235 if (nargin == 1) |
|
236 { |
|
237 tree_constant arg = args(0); |
|
238 |
|
239 if (arg.is_real_type ()) |
|
240 { |
|
241 Matrix tmp = arg.matrix_value (); |
|
242 |
|
243 if (! error_state) |
|
244 retval(0) = tmp.cumprod (); |
|
245 } |
|
246 else if (arg.is_complex_type ()) |
|
247 { |
|
248 ComplexMatrix tmp = arg.complex_matrix_value (); |
|
249 |
|
250 if (! error_state) |
|
251 retval(0) = tmp.cumprod (); |
|
252 } |
|
253 else |
|
254 { |
|
255 gripe_wrong_type_arg ("cumprod", arg); |
|
256 return retval; |
|
257 } |
|
258 } |
712
|
259 else |
523
|
260 print_usage ("cumprod"); |
|
261 |
|
262 return retval; |
|
263 } |
|
264 |
1488
|
265 DEFUN ("cumsum", Fcumsum, Scumsum, 10, |
523
|
266 "cumsum (X): cumulative sums") |
|
267 { |
|
268 Octave_object retval; |
|
269 |
|
270 int nargin = args.length (); |
|
271 |
760
|
272 if (nargin == 1) |
|
273 { |
|
274 tree_constant arg = args(0); |
|
275 |
|
276 if (arg.is_real_type ()) |
|
277 { |
|
278 Matrix tmp = arg.matrix_value (); |
|
279 |
|
280 if (! error_state) |
|
281 retval(0) = tmp.cumsum (); |
|
282 } |
|
283 else if (arg.is_complex_type ()) |
|
284 { |
|
285 ComplexMatrix tmp = arg.complex_matrix_value (); |
|
286 |
|
287 if (! error_state) |
|
288 retval(0) = tmp.cumsum (); |
|
289 } |
|
290 else |
|
291 { |
|
292 gripe_wrong_type_arg ("cumsum", arg); |
|
293 return retval; |
|
294 } |
|
295 } |
712
|
296 else |
523
|
297 print_usage ("cumsum"); |
|
298 |
|
299 return retval; |
|
300 } |
|
301 |
767
|
302 static tree_constant |
|
303 make_diag (const Matrix& v, int k) |
|
304 { |
|
305 int nr = v.rows (); |
|
306 int nc = v.columns (); |
|
307 assert (nc == 1 || nr == 1); |
|
308 |
|
309 tree_constant retval; |
|
310 |
|
311 int roff = 0; |
|
312 int coff = 0; |
|
313 if (k > 0) |
|
314 { |
|
315 roff = 0; |
|
316 coff = k; |
|
317 } |
|
318 else if (k < 0) |
|
319 { |
|
320 roff = -k; |
|
321 coff = 0; |
|
322 } |
|
323 |
|
324 if (nr == 1) |
|
325 { |
|
326 int n = nc + ABS (k); |
|
327 Matrix m (n, n, 0.0); |
|
328 for (int i = 0; i < nc; i++) |
|
329 m.elem (i+roff, i+coff) = v.elem (0, i); |
|
330 retval = tree_constant (m); |
|
331 } |
|
332 else |
|
333 { |
|
334 int n = nr + ABS (k); |
|
335 Matrix m (n, n, 0.0); |
|
336 for (int i = 0; i < nr; i++) |
|
337 m.elem (i+roff, i+coff) = v.elem (i, 0); |
|
338 retval = tree_constant (m); |
|
339 } |
|
340 |
|
341 return retval; |
|
342 } |
|
343 |
|
344 static tree_constant |
|
345 make_diag (const ComplexMatrix& v, int k) |
|
346 { |
|
347 int nr = v.rows (); |
|
348 int nc = v.columns (); |
|
349 assert (nc == 1 || nr == 1); |
|
350 |
|
351 tree_constant retval; |
|
352 |
|
353 int roff = 0; |
|
354 int coff = 0; |
|
355 if (k > 0) |
|
356 { |
|
357 roff = 0; |
|
358 coff = k; |
|
359 } |
|
360 else if (k < 0) |
|
361 { |
|
362 roff = -k; |
|
363 coff = 0; |
|
364 } |
|
365 |
|
366 if (nr == 1) |
|
367 { |
|
368 int n = nc + ABS (k); |
|
369 ComplexMatrix m (n, n, 0.0); |
|
370 for (int i = 0; i < nc; i++) |
|
371 m.elem (i+roff, i+coff) = v.elem (0, i); |
|
372 retval = tree_constant (m); |
|
373 } |
|
374 else |
|
375 { |
|
376 int n = nr + ABS (k); |
|
377 ComplexMatrix m (n, n, 0.0); |
|
378 for (int i = 0; i < nr; i++) |
|
379 m.elem (i+roff, i+coff) = v.elem (i, 0); |
|
380 retval = tree_constant (m); |
|
381 } |
|
382 |
|
383 return retval; |
|
384 } |
|
385 |
|
386 static tree_constant |
|
387 make_diag (const tree_constant& arg) |
|
388 { |
|
389 tree_constant retval; |
|
390 |
|
391 if (arg.is_real_type ()) |
|
392 { |
|
393 Matrix m = arg.matrix_value (); |
|
394 |
|
395 if (! error_state) |
|
396 { |
|
397 int nr = m.rows (); |
|
398 int nc = m.columns (); |
|
399 |
|
400 if (nr == 0 || nc == 0) |
|
401 retval = Matrix (); |
|
402 else if (nr == 1 || nc == 1) |
|
403 retval = make_diag (m, 0); |
|
404 else |
|
405 { |
|
406 ColumnVector v = m.diag (); |
|
407 if (v.capacity () > 0) |
|
408 retval = v; |
|
409 } |
|
410 } |
|
411 else |
|
412 gripe_wrong_type_arg ("diag", arg); |
|
413 } |
|
414 else if (arg.is_complex_type ()) |
|
415 { |
|
416 ComplexMatrix cm = arg.complex_matrix_value (); |
|
417 |
|
418 if (! error_state) |
|
419 { |
|
420 int nr = cm.rows (); |
|
421 int nc = cm.columns (); |
|
422 |
|
423 if (nr == 0 || nc == 0) |
|
424 retval = Matrix (); |
|
425 else if (nr == 1 || nc == 1) |
|
426 retval = make_diag (cm, 0); |
|
427 else |
|
428 { |
|
429 ComplexColumnVector v = cm.diag (); |
|
430 if (v.capacity () > 0) |
|
431 retval = v; |
|
432 } |
|
433 } |
|
434 else |
|
435 gripe_wrong_type_arg ("diag", arg); |
|
436 } |
|
437 else |
|
438 gripe_wrong_type_arg ("diag", arg); |
|
439 |
|
440 return retval; |
|
441 } |
|
442 |
|
443 static tree_constant |
|
444 make_diag (const tree_constant& a, const tree_constant& b) |
|
445 { |
|
446 tree_constant retval; |
|
447 |
|
448 double tmp = b.double_value (); |
|
449 |
|
450 if (error_state) |
|
451 { |
|
452 error ("diag: invalid second argument"); |
|
453 return retval; |
|
454 } |
|
455 |
|
456 int k = NINT (tmp); |
|
457 int n = ABS (k) + 1; |
|
458 |
|
459 if (a.is_real_type ()) |
|
460 { |
|
461 if (a.is_scalar_type ()) |
|
462 { |
|
463 double d = a.double_value (); |
|
464 |
|
465 if (k == 0) |
|
466 retval = d; |
|
467 else if (k > 0) |
|
468 { |
|
469 Matrix m (n, n, 0.0); |
|
470 m.elem (0, k) = d; |
|
471 retval = m; |
|
472 } |
|
473 else if (k < 0) |
|
474 { |
|
475 Matrix m (n, n, 0.0); |
|
476 m.elem (-k, 0) = d; |
|
477 retval = m; |
|
478 } |
|
479 } |
|
480 else if (a.is_matrix_type ()) |
|
481 { |
|
482 Matrix m = a.matrix_value (); |
|
483 |
|
484 int nr = m.rows (); |
|
485 int nc = m.columns (); |
|
486 |
|
487 if (nr == 0 || nc == 0) |
|
488 retval = Matrix (); |
|
489 else if (nr == 1 || nc == 1) |
|
490 retval = make_diag (m, k); |
|
491 else |
|
492 { |
|
493 ColumnVector d = m.diag (k); |
|
494 retval = d; |
|
495 } |
|
496 } |
|
497 else |
|
498 gripe_wrong_type_arg ("diag", a); |
|
499 } |
|
500 else if (a.is_complex_type ()) |
|
501 { |
|
502 if (a.is_scalar_type ()) |
|
503 { |
|
504 Complex c = a.complex_value (); |
|
505 |
|
506 if (k == 0) |
|
507 retval = c; |
|
508 else if (k > 0) |
|
509 { |
|
510 ComplexMatrix m (n, n, 0.0); |
|
511 m.elem (0, k) = c; |
|
512 retval = m; |
|
513 } |
|
514 else if (k < 0) |
|
515 { |
|
516 ComplexMatrix m (n, n, 0.0); |
|
517 m.elem (-k, 0) = c; |
|
518 retval = m; |
|
519 } |
|
520 } |
|
521 else if (a.is_matrix_type ()) |
|
522 { |
|
523 ComplexMatrix cm = a.complex_matrix_value (); |
|
524 |
|
525 int nr = cm.rows (); |
|
526 int nc = cm.columns (); |
|
527 |
|
528 if (nr == 0 || nc == 0) |
|
529 retval = Matrix (); |
|
530 else if (nr == 1 || nc == 1) |
|
531 retval = make_diag (cm, k); |
|
532 else |
|
533 { |
|
534 ComplexColumnVector d = cm.diag (k); |
|
535 retval = d; |
|
536 } |
|
537 } |
|
538 else |
|
539 gripe_wrong_type_arg ("diag", a); |
|
540 } |
|
541 else |
|
542 gripe_wrong_type_arg ("diag", a); |
|
543 |
|
544 return retval; |
|
545 } |
|
546 |
1488
|
547 DEFUN ("diag", Fdiag, Sdiag, 10, |
523
|
548 "diag (X [,k]): form/extract diagonals") |
|
549 { |
|
550 Octave_object retval; |
|
551 |
|
552 int nargin = args.length (); |
|
553 |
712
|
554 if (nargin == 1 && args(0).is_defined ()) |
767
|
555 retval = make_diag (args(0)); |
712
|
556 else if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
767
|
557 retval = make_diag (args(0), args(1)); |
523
|
558 else |
|
559 print_usage ("diag"); |
|
560 |
|
561 return retval; |
|
562 } |
|
563 |
1488
|
564 DEFUN ("prod", Fprod, Sprod, 10, |
523
|
565 "prod (X): products") |
|
566 { |
|
567 Octave_object retval; |
|
568 |
|
569 int nargin = args.length (); |
|
570 |
760
|
571 if (nargin == 1) |
|
572 { |
|
573 tree_constant arg = args(0); |
|
574 |
|
575 if (arg.is_real_type ()) |
|
576 { |
|
577 Matrix tmp = arg.matrix_value (); |
|
578 |
|
579 if (! error_state) |
|
580 retval(0) = tmp.prod (); |
|
581 } |
|
582 else if (arg.is_complex_type ()) |
|
583 { |
|
584 ComplexMatrix tmp = arg.complex_matrix_value (); |
|
585 |
|
586 if (! error_state) |
|
587 retval(0) = tmp.prod (); |
|
588 } |
|
589 else |
|
590 { |
|
591 gripe_wrong_type_arg ("prod", arg); |
|
592 return retval; |
|
593 } |
|
594 } |
712
|
595 else |
523
|
596 print_usage ("prod"); |
|
597 |
|
598 return retval; |
|
599 } |
|
600 |
1488
|
601 DEFUN ("size", Fsize, Ssize, 11, |
1032
|
602 "[m, n] = size (x): return rows and columns of X\n\ |
1031
|
603 \n\ |
|
604 d = size (x): return number of rows and columns of x as a row vector\n\ |
|
605 \n\ |
|
606 m = size (x, 1): return number of rows in x\n\ |
|
607 m = size (x, 2): return number of columns in x") |
523
|
608 { |
|
609 Octave_object retval; |
|
610 |
|
611 int nargin = args.length (); |
|
612 |
1031
|
613 if (nargin == 1 && nargout < 3) |
523
|
614 { |
712
|
615 int nr = args(0).rows (); |
|
616 int nc = args(0).columns (); |
1031
|
617 |
712
|
618 if (nargout == 0 || nargout == 1) |
523
|
619 { |
712
|
620 Matrix m (1, 2); |
|
621 m.elem (0, 0) = nr; |
|
622 m.elem (0, 1) = nc; |
|
623 retval = m; |
523
|
624 } |
712
|
625 else if (nargout == 2) |
|
626 { |
|
627 retval(1) = (double) nc; |
|
628 retval(0) = (double) nr; |
|
629 } |
1031
|
630 } |
|
631 else if (nargin == 2 && nargout < 2) |
|
632 { |
|
633 int nd = NINT (args(1).double_value ()); |
|
634 |
|
635 if (error_state) |
|
636 error ("size: expecting scalar as second argument"); |
712
|
637 else |
1031
|
638 { |
|
639 if (nd == 1) |
|
640 retval(0) = (double) (args(0).rows ()); |
|
641 else if (nd == 2) |
|
642 retval(0) = (double) (args(0).columns ()); |
|
643 else |
|
644 error ("size: invalid second argument -- expecting 1 or 2"); |
|
645 } |
523
|
646 } |
712
|
647 else |
|
648 print_usage ("size"); |
523
|
649 |
|
650 return retval; |
|
651 } |
|
652 |
1488
|
653 DEFUN ("sum", Fsum, Ssum, 10, |
523
|
654 "sum (X): sum of elements") |
|
655 { |
|
656 Octave_object retval; |
|
657 |
|
658 int nargin = args.length (); |
|
659 |
760
|
660 if (nargin == 1) |
|
661 { |
|
662 tree_constant arg = args(0); |
|
663 |
|
664 if (arg.is_real_type ()) |
|
665 { |
|
666 Matrix tmp = arg.matrix_value (); |
|
667 |
|
668 if (! error_state) |
|
669 retval(0) = tmp.sum (); |
|
670 } |
|
671 else if (arg.is_complex_type ()) |
|
672 { |
|
673 ComplexMatrix tmp = arg.complex_matrix_value (); |
|
674 |
|
675 if (! error_state) |
|
676 retval(0) = tmp.sum (); |
|
677 } |
|
678 else |
|
679 { |
|
680 gripe_wrong_type_arg ("sum", arg); |
|
681 return retval; |
|
682 } |
|
683 } |
523
|
684 else |
712
|
685 print_usage ("sum"); |
523
|
686 |
|
687 return retval; |
|
688 } |
|
689 |
1488
|
690 DEFUN ("sumsq", Fsumsq, Ssumsq, 10, |
523
|
691 "sumsq (X): sum of squares of elements") |
|
692 { |
|
693 Octave_object retval; |
|
694 |
|
695 int nargin = args.length (); |
|
696 |
760
|
697 if (nargin == 1) |
|
698 { |
|
699 tree_constant arg = args(0); |
|
700 |
|
701 if (arg.is_real_type ()) |
|
702 { |
|
703 Matrix tmp = arg.matrix_value (); |
|
704 |
|
705 if (! error_state) |
|
706 retval(0) = tmp.sumsq (); |
|
707 } |
|
708 else if (arg.is_complex_type ()) |
|
709 { |
|
710 ComplexMatrix tmp = arg.complex_matrix_value (); |
|
711 |
|
712 if (! error_state) |
|
713 retval(0) = tmp.sumsq (); |
|
714 } |
|
715 else |
|
716 { |
|
717 gripe_wrong_type_arg ("sumsq", arg); |
|
718 return retval; |
|
719 } |
|
720 } |
712
|
721 else |
523
|
722 print_usage ("sumsq"); |
|
723 |
|
724 return retval; |
|
725 } |
|
726 |
1488
|
727 DEFUN ("is_struct", Fis_struct, Sis_struct, 10, |
939
|
728 "is_struct (x): return nonzero if x is a structure") |
|
729 { |
|
730 Octave_object retval; |
|
731 |
|
732 int nargin = args.length (); |
|
733 |
|
734 if (nargin == 1) |
|
735 { |
|
736 tree_constant arg = args(0); |
|
737 |
|
738 if (arg.is_map ()) |
|
739 retval = 1.0; |
|
740 else |
|
741 retval = 0.0; |
|
742 } |
|
743 else |
|
744 print_usage ("is_struct"); |
|
745 |
|
746 return retval; |
|
747 } |
|
748 |
1488
|
749 DEFUN ("struct_elements", Fstruct_elements, Sstruct_elements, 10, |
1402
|
750 "struct_elements (S)\n\ |
|
751 \n\ |
|
752 Return a list of the names of the elements of the structure S.") |
|
753 { |
|
754 Octave_object retval; |
|
755 |
|
756 int nargin = args.length (); |
|
757 |
|
758 if (nargin == 1) |
|
759 { |
|
760 if (args (0).is_map ()) |
|
761 { |
|
762 Octave_map m = args(0).map_value (); |
1755
|
763 retval(0) = m.make_name_list (); |
1402
|
764 } |
|
765 else |
|
766 gripe_wrong_type_arg ("struct_elements", args (0)); |
|
767 } |
|
768 else |
|
769 print_usage ("struct_elements"); |
|
770 |
|
771 return retval; |
|
772 } |
|
773 |
1488
|
774 DEFUN ("struct_contains", Fstruct_contains, Sstruct_contains, 10, |
1216
|
775 "struct_contains (S, NAME)\n\ |
|
776 \n\ |
|
777 return nonzero if S is a structure with element NAME") |
|
778 { |
|
779 Octave_object retval; |
|
780 |
|
781 int nargin = args.length (); |
|
782 |
|
783 if (nargin == 2) |
|
784 { |
|
785 retval = 0.0; |
1277
|
786 if (args(0).is_map () && args(1).is_string ()) |
1216
|
787 { |
1755
|
788 string s = args(1).string_value (); |
1277
|
789 tree_constant tmp = args(0).lookup_map_element (s, 0, 1); |
|
790 retval = (double) tmp.is_defined (); |
1216
|
791 } |
|
792 } |
|
793 else |
|
794 print_usage ("struct_contains"); |
|
795 |
|
796 return retval; |
|
797 } |
|
798 |
523
|
799 static void |
|
800 check_dimensions (int& nr, int& nc, const char *warnfor) |
|
801 { |
|
802 if (nr < 0 || nc < 0) |
|
803 { |
|
804 if (user_pref.treat_neg_dim_as_zero) |
597
|
805 { |
|
806 nr = (nr < 0) ? 0 : nr; |
|
807 nc = (nc < 0) ? 0 : nc; |
1129
|
808 |
|
809 if (user_pref.treat_neg_dim_as_zero < 0) |
|
810 warning ("%s: converting negative dimension to zero", |
|
811 warnfor); |
597
|
812 } |
523
|
813 else |
|
814 error ("%s: can't create a matrix with negative dimensions", |
|
815 warnfor); |
|
816 } |
|
817 } |
|
818 |
|
819 static void |
|
820 get_dimensions (const tree_constant& a, const char *warn_for, |
|
821 int& nr, int& nc) |
|
822 { |
634
|
823 if (a.is_scalar_type ()) |
523
|
824 { |
634
|
825 double tmp = a.double_value (); |
523
|
826 nr = nc = NINT (tmp); |
|
827 } |
|
828 else |
|
829 { |
634
|
830 nr = a.rows (); |
|
831 nc = a.columns (); |
523
|
832 |
|
833 if ((nr == 1 && nc == 2) || (nr == 2 && nc == 1)) |
|
834 { |
634
|
835 ColumnVector v = a.vector_value (); |
523
|
836 |
633
|
837 if (error_state) |
|
838 return; |
|
839 |
523
|
840 nr = NINT (v.elem (0)); |
|
841 nc = NINT (v.elem (1)); |
|
842 } |
|
843 else |
|
844 warning ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
|
845 } |
|
846 |
|
847 check_dimensions (nr, nc, warn_for); // May set error_state. |
|
848 } |
|
849 |
|
850 static void |
|
851 get_dimensions (const tree_constant& a, const tree_constant& b, |
|
852 const char *warn_for, int& nr, int& nc) |
|
853 { |
634
|
854 nr = NINT (a.double_value ()); |
|
855 nc = NINT (b.double_value ()); |
523
|
856 |
634
|
857 if (error_state) |
|
858 error ("%s: expecting two scalar arguments", warn_for); |
523
|
859 else |
634
|
860 check_dimensions (nr, nc, warn_for); // May set error_state. |
523
|
861 } |
|
862 |
|
863 static tree_constant |
|
864 fill_matrix (const tree_constant& a, double val, const char *warn_for) |
|
865 { |
|
866 int nr, nc; |
|
867 get_dimensions (a, warn_for, nr, nc); |
|
868 |
|
869 if (error_state) |
|
870 return tree_constant (); |
|
871 |
|
872 Matrix m (nr, nc, val); |
|
873 |
|
874 return m; |
|
875 } |
|
876 |
|
877 static tree_constant |
|
878 fill_matrix (const tree_constant& a, const tree_constant& b, |
|
879 double val, const char *warn_for) |
|
880 { |
|
881 int nr, nc; |
|
882 get_dimensions (a, b, warn_for, nr, nc); // May set error_state. |
|
883 |
|
884 if (error_state) |
|
885 return tree_constant (); |
|
886 |
|
887 Matrix m (nr, nc, val); |
|
888 |
|
889 return m; |
|
890 } |
|
891 |
1488
|
892 DEFUN ("ones", Fones, Sones, 10, |
523
|
893 "ones (N), ones (N, M), ones (X): create a matrix of all ones") |
|
894 { |
|
895 Octave_object retval; |
|
896 |
|
897 int nargin = args.length (); |
|
898 |
|
899 switch (nargin) |
|
900 { |
712
|
901 case 0: |
|
902 retval = 1.0; |
|
903 break; |
777
|
904 |
610
|
905 case 1: |
712
|
906 retval = fill_matrix (args(0), 1.0, "ones"); |
610
|
907 break; |
777
|
908 |
523
|
909 case 2: |
712
|
910 retval = fill_matrix (args(0), args(1), 1.0, "ones"); |
523
|
911 break; |
777
|
912 |
523
|
913 default: |
|
914 print_usage ("ones"); |
|
915 break; |
|
916 } |
|
917 |
|
918 return retval; |
|
919 } |
|
920 |
1488
|
921 DEFUN ("zeros", Fzeros, Szeros, 10, |
523
|
922 "zeros (N), zeros (N, M), zeros (X): create a matrix of all zeros") |
|
923 { |
|
924 Octave_object retval; |
|
925 |
|
926 int nargin = args.length (); |
|
927 |
|
928 switch (nargin) |
|
929 { |
712
|
930 case 0: |
|
931 retval = 0.0; |
|
932 break; |
777
|
933 |
610
|
934 case 1: |
712
|
935 retval = fill_matrix (args(0), 0.0, "zeros"); |
610
|
936 break; |
777
|
937 |
523
|
938 case 2: |
712
|
939 retval = fill_matrix (args(0), args(1), 0.0, "zeros"); |
523
|
940 break; |
777
|
941 |
523
|
942 default: |
|
943 print_usage ("zeros"); |
|
944 break; |
|
945 } |
|
946 |
|
947 return retval; |
|
948 } |
|
949 |
|
950 static tree_constant |
|
951 identity_matrix (const tree_constant& a) |
|
952 { |
|
953 int nr, nc; |
|
954 get_dimensions (a, "eye", nr, nc); // May set error_state. |
|
955 |
|
956 if (error_state) |
|
957 return tree_constant (); |
|
958 |
|
959 Matrix m (nr, nc, 0.0); |
|
960 |
|
961 if (nr > 0 && nc > 0) |
|
962 { |
|
963 int n = MIN (nr, nc); |
|
964 for (int i = 0; i < n; i++) |
|
965 m.elem (i, i) = 1.0; |
|
966 } |
|
967 |
|
968 return m; |
|
969 } |
|
970 |
|
971 static tree_constant |
|
972 identity_matrix (const tree_constant& a, const tree_constant& b) |
|
973 { |
|
974 int nr, nc; |
|
975 get_dimensions (a, b, "eye", nr, nc); // May set error_state. |
|
976 |
|
977 if (error_state) |
|
978 return tree_constant (); |
|
979 |
|
980 Matrix m (nr, nc, 0.0); |
|
981 |
|
982 if (nr > 0 && nc > 0) |
|
983 { |
|
984 int n = MIN (nr, nc); |
|
985 for (int i = 0; i < n; i++) |
|
986 m.elem (i, i) = 1.0; |
|
987 } |
|
988 |
|
989 return m; |
|
990 } |
|
991 |
1488
|
992 DEFUN ("eye", Feye, Seye, 10, |
523
|
993 "eye (N), eye (N, M), eye (X): create an identity matrix") |
|
994 { |
|
995 Octave_object retval; |
|
996 |
|
997 int nargin = args.length (); |
|
998 |
|
999 switch (nargin) |
|
1000 { |
712
|
1001 case 0: |
|
1002 retval = 1.0; |
|
1003 break; |
777
|
1004 |
610
|
1005 case 1: |
712
|
1006 retval = identity_matrix (args(0)); |
610
|
1007 break; |
777
|
1008 |
523
|
1009 case 2: |
712
|
1010 retval = identity_matrix (args(0), args(1)); |
523
|
1011 break; |
777
|
1012 |
523
|
1013 default: |
|
1014 print_usage ("eye"); |
|
1015 break; |
|
1016 } |
|
1017 |
|
1018 return retval; |
|
1019 } |
|
1020 |
1488
|
1021 DEFUN ("linspace", Flinspace, Slinspace, 10, |
1100
|
1022 "usage: linspace (x1, x2, n)\n\ |
|
1023 \n\ |
|
1024 Return a vector of n equally spaced points between x1 and x2\n\ |
|
1025 inclusive.\n\ |
|
1026 \n\ |
|
1027 If the final argument is omitted, n = 100 is assumed.\n\ |
|
1028 \n\ |
|
1029 All three arguments must be scalars.\n\ |
|
1030 \n\ |
|
1031 See also: logspace") |
|
1032 { |
|
1033 Octave_object retval; |
|
1034 |
|
1035 int nargin = args.length (); |
|
1036 |
|
1037 int npoints = 100; |
|
1038 |
|
1039 if (nargin == 3) |
|
1040 { |
|
1041 double n = args(2).double_value (); |
|
1042 |
|
1043 if (! error_state) |
|
1044 npoints = NINT (n); |
|
1045 } |
|
1046 else |
|
1047 print_usage ("linspace"); |
|
1048 |
|
1049 if (! error_state) |
|
1050 { |
|
1051 if (npoints > 1) |
|
1052 { |
|
1053 tree_constant arg_1 = args(0); |
|
1054 tree_constant arg_2 = args(1); |
|
1055 |
|
1056 if (arg_1.is_complex_type () || arg_2.is_complex_type ()) |
|
1057 { |
|
1058 Complex x1 = arg_1.complex_value (); |
|
1059 Complex x2 = arg_2.complex_value (); |
|
1060 |
|
1061 if (! error_state) |
|
1062 { |
|
1063 ComplexRowVector rv = linspace (x1, x2, npoints); |
|
1064 |
|
1065 if (! error_state) |
|
1066 retval (0) = tree_constant (rv, 0); |
|
1067 } |
|
1068 } |
|
1069 else |
|
1070 { |
|
1071 double x1 = arg_1.double_value (); |
|
1072 double x2 = arg_2.double_value (); |
|
1073 |
|
1074 if (! error_state) |
|
1075 { |
|
1076 RowVector rv = linspace (x1, x2, npoints); |
|
1077 |
|
1078 if (! error_state) |
|
1079 retval (0) = tree_constant (rv, 0); |
|
1080 } |
|
1081 } |
|
1082 } |
|
1083 else |
|
1084 error ("linspace: npoints must be greater than 2"); |
|
1085 } |
|
1086 |
|
1087 return retval; |
|
1088 } |
|
1089 |
523
|
1090 /* |
|
1091 ;;; Local Variables: *** |
|
1092 ;;; mode: C++ *** |
|
1093 ;;; page-delimiter: "^/\\*" *** |
|
1094 ;;; End: *** |
|
1095 */ |