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