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