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