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