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 |
712
|
53 DEFUN ("all", Fall, Sall, 1, 1, |
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 |
712
|
68 DEFUN ("any", Fany, Sany, 1, 1, |
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 |
712
|
137 DEFUN ("atan2", Fatan2, Satan2, 2, 1, |
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 |
712
|
223 DEFUN ("cumprod", Fcumprod, Scumprod, 1, 1, |
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 |
712
|
260 DEFUN ("cumsum", Fcumsum, Scumsum, 1, 1, |
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 |
712
|
542 DEFUN ("diag", Fdiag, Sdiag, 2, 1, |
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 |
712
|
559 DEFUN ("prod", Fprod, Sprod, 1, 1, |
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 |
1031
|
596 DEFUN ("size", Fsize, Ssize, 2, 1, |
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 |
712
|
648 DEFUN ("sum", Fsum, Ssum, 1, 1, |
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 |
712
|
685 DEFUN ("sumsq", Fsumsq, Ssumsq, 1, 1, |
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 |
939
|
722 DEFUN ("is_struct", Fis_struct, Sis_struct, 1, 1, |
|
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 |
1216
|
744 DEFUN ("struct_contains", Fstruct_contains, Sstruct_contains, 1, 2, |
|
745 "struct_contains (S, NAME)\n\ |
|
746 \n\ |
|
747 return nonzero if S is a structure with element NAME") |
|
748 { |
|
749 Octave_object retval; |
|
750 |
|
751 int nargin = args.length (); |
|
752 |
|
753 if (nargin == 2) |
|
754 { |
|
755 retval = 0.0; |
1277
|
756 if (args(0).is_map () && args(1).is_string ()) |
1216
|
757 { |
1363
|
758 const char *s = args(1).string_value (); |
1277
|
759 tree_constant tmp = args(0).lookup_map_element (s, 0, 1); |
|
760 retval = (double) tmp.is_defined (); |
1216
|
761 } |
|
762 } |
|
763 else |
|
764 print_usage ("struct_contains"); |
|
765 |
|
766 return retval; |
|
767 } |
|
768 |
523
|
769 static void |
|
770 check_dimensions (int& nr, int& nc, const char *warnfor) |
|
771 { |
|
772 if (nr < 0 || nc < 0) |
|
773 { |
|
774 if (user_pref.treat_neg_dim_as_zero) |
597
|
775 { |
|
776 nr = (nr < 0) ? 0 : nr; |
|
777 nc = (nc < 0) ? 0 : nc; |
1129
|
778 |
|
779 if (user_pref.treat_neg_dim_as_zero < 0) |
|
780 warning ("%s: converting negative dimension to zero", |
|
781 warnfor); |
597
|
782 } |
523
|
783 else |
|
784 error ("%s: can't create a matrix with negative dimensions", |
|
785 warnfor); |
|
786 } |
|
787 } |
|
788 |
|
789 static void |
|
790 get_dimensions (const tree_constant& a, const char *warn_for, |
|
791 int& nr, int& nc) |
|
792 { |
634
|
793 if (a.is_scalar_type ()) |
523
|
794 { |
634
|
795 double tmp = a.double_value (); |
523
|
796 nr = nc = NINT (tmp); |
|
797 } |
|
798 else |
|
799 { |
634
|
800 nr = a.rows (); |
|
801 nc = a.columns (); |
523
|
802 |
|
803 if ((nr == 1 && nc == 2) || (nr == 2 && nc == 1)) |
|
804 { |
634
|
805 ColumnVector v = a.vector_value (); |
523
|
806 |
633
|
807 if (error_state) |
|
808 return; |
|
809 |
523
|
810 nr = NINT (v.elem (0)); |
|
811 nc = NINT (v.elem (1)); |
|
812 } |
|
813 else |
|
814 warning ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
|
815 } |
|
816 |
|
817 check_dimensions (nr, nc, warn_for); // May set error_state. |
|
818 } |
|
819 |
|
820 static void |
|
821 get_dimensions (const tree_constant& a, const tree_constant& b, |
|
822 const char *warn_for, int& nr, int& nc) |
|
823 { |
634
|
824 nr = NINT (a.double_value ()); |
|
825 nc = NINT (b.double_value ()); |
523
|
826 |
634
|
827 if (error_state) |
|
828 error ("%s: expecting two scalar arguments", warn_for); |
523
|
829 else |
634
|
830 check_dimensions (nr, nc, warn_for); // May set error_state. |
523
|
831 } |
|
832 |
|
833 static tree_constant |
|
834 fill_matrix (const tree_constant& a, double val, const char *warn_for) |
|
835 { |
|
836 int nr, nc; |
|
837 get_dimensions (a, warn_for, nr, nc); |
|
838 |
|
839 if (error_state) |
|
840 return tree_constant (); |
|
841 |
|
842 Matrix m (nr, nc, val); |
|
843 |
|
844 return m; |
|
845 } |
|
846 |
|
847 static tree_constant |
|
848 fill_matrix (const tree_constant& a, const tree_constant& b, |
|
849 double val, const char *warn_for) |
|
850 { |
|
851 int nr, nc; |
|
852 get_dimensions (a, b, warn_for, nr, nc); // May set error_state. |
|
853 |
|
854 if (error_state) |
|
855 return tree_constant (); |
|
856 |
|
857 Matrix m (nr, nc, val); |
|
858 |
|
859 return m; |
|
860 } |
|
861 |
712
|
862 DEFUN ("ones", Fones, Sones, 2, 1, |
523
|
863 "ones (N), ones (N, M), ones (X): create a matrix of all ones") |
|
864 { |
|
865 Octave_object retval; |
|
866 |
|
867 int nargin = args.length (); |
|
868 |
|
869 switch (nargin) |
|
870 { |
712
|
871 case 0: |
|
872 retval = 1.0; |
|
873 break; |
777
|
874 |
610
|
875 case 1: |
712
|
876 retval = fill_matrix (args(0), 1.0, "ones"); |
610
|
877 break; |
777
|
878 |
523
|
879 case 2: |
712
|
880 retval = fill_matrix (args(0), args(1), 1.0, "ones"); |
523
|
881 break; |
777
|
882 |
523
|
883 default: |
|
884 print_usage ("ones"); |
|
885 break; |
|
886 } |
|
887 |
|
888 return retval; |
|
889 } |
|
890 |
712
|
891 DEFUN ("zeros", Fzeros, Szeros, 2, 1, |
523
|
892 "zeros (N), zeros (N, M), zeros (X): create a matrix of all zeros") |
|
893 { |
|
894 Octave_object retval; |
|
895 |
|
896 int nargin = args.length (); |
|
897 |
|
898 switch (nargin) |
|
899 { |
712
|
900 case 0: |
|
901 retval = 0.0; |
|
902 break; |
777
|
903 |
610
|
904 case 1: |
712
|
905 retval = fill_matrix (args(0), 0.0, "zeros"); |
610
|
906 break; |
777
|
907 |
523
|
908 case 2: |
712
|
909 retval = fill_matrix (args(0), args(1), 0.0, "zeros"); |
523
|
910 break; |
777
|
911 |
523
|
912 default: |
|
913 print_usage ("zeros"); |
|
914 break; |
|
915 } |
|
916 |
|
917 return retval; |
|
918 } |
|
919 |
|
920 static tree_constant |
|
921 identity_matrix (const tree_constant& a) |
|
922 { |
|
923 int nr, nc; |
|
924 get_dimensions (a, "eye", nr, nc); // May set error_state. |
|
925 |
|
926 if (error_state) |
|
927 return tree_constant (); |
|
928 |
|
929 Matrix m (nr, nc, 0.0); |
|
930 |
|
931 if (nr > 0 && nc > 0) |
|
932 { |
|
933 int n = MIN (nr, nc); |
|
934 for (int i = 0; i < n; i++) |
|
935 m.elem (i, i) = 1.0; |
|
936 } |
|
937 |
|
938 return m; |
|
939 } |
|
940 |
|
941 static tree_constant |
|
942 identity_matrix (const tree_constant& a, const tree_constant& b) |
|
943 { |
|
944 int nr, nc; |
|
945 get_dimensions (a, b, "eye", nr, nc); // May set error_state. |
|
946 |
|
947 if (error_state) |
|
948 return tree_constant (); |
|
949 |
|
950 Matrix m (nr, nc, 0.0); |
|
951 |
|
952 if (nr > 0 && nc > 0) |
|
953 { |
|
954 int n = MIN (nr, nc); |
|
955 for (int i = 0; i < n; i++) |
|
956 m.elem (i, i) = 1.0; |
|
957 } |
|
958 |
|
959 return m; |
|
960 } |
|
961 |
712
|
962 DEFUN ("eye", Feye, Seye, 2, 1, |
523
|
963 "eye (N), eye (N, M), eye (X): create an identity matrix") |
|
964 { |
|
965 Octave_object retval; |
|
966 |
|
967 int nargin = args.length (); |
|
968 |
|
969 switch (nargin) |
|
970 { |
712
|
971 case 0: |
|
972 retval = 1.0; |
|
973 break; |
777
|
974 |
610
|
975 case 1: |
712
|
976 retval = identity_matrix (args(0)); |
610
|
977 break; |
777
|
978 |
523
|
979 case 2: |
712
|
980 retval = identity_matrix (args(0), args(1)); |
523
|
981 break; |
777
|
982 |
523
|
983 default: |
|
984 print_usage ("eye"); |
|
985 break; |
|
986 } |
|
987 |
|
988 return retval; |
|
989 } |
|
990 |
1100
|
991 DEFUN ("linspace", Flinspace, Slinspace, 2, 1, |
|
992 "usage: linspace (x1, x2, n)\n\ |
|
993 \n\ |
|
994 Return a vector of n equally spaced points between x1 and x2\n\ |
|
995 inclusive.\n\ |
|
996 \n\ |
|
997 If the final argument is omitted, n = 100 is assumed.\n\ |
|
998 \n\ |
|
999 All three arguments must be scalars.\n\ |
|
1000 \n\ |
|
1001 See also: logspace") |
|
1002 { |
|
1003 Octave_object retval; |
|
1004 |
|
1005 int nargin = args.length (); |
|
1006 |
|
1007 int npoints = 100; |
|
1008 |
|
1009 if (nargin == 3) |
|
1010 { |
|
1011 double n = args(2).double_value (); |
|
1012 |
|
1013 if (! error_state) |
|
1014 npoints = NINT (n); |
|
1015 } |
|
1016 else |
|
1017 print_usage ("linspace"); |
|
1018 |
|
1019 if (! error_state) |
|
1020 { |
|
1021 if (npoints > 1) |
|
1022 { |
|
1023 tree_constant arg_1 = args(0); |
|
1024 tree_constant arg_2 = args(1); |
|
1025 |
|
1026 if (arg_1.is_complex_type () || arg_2.is_complex_type ()) |
|
1027 { |
|
1028 Complex x1 = arg_1.complex_value (); |
|
1029 Complex x2 = arg_2.complex_value (); |
|
1030 |
|
1031 if (! error_state) |
|
1032 { |
|
1033 ComplexRowVector rv = linspace (x1, x2, npoints); |
|
1034 |
|
1035 if (! error_state) |
|
1036 retval (0) = tree_constant (rv, 0); |
|
1037 } |
|
1038 } |
|
1039 else |
|
1040 { |
|
1041 double x1 = arg_1.double_value (); |
|
1042 double x2 = arg_2.double_value (); |
|
1043 |
|
1044 if (! error_state) |
|
1045 { |
|
1046 RowVector rv = linspace (x1, x2, npoints); |
|
1047 |
|
1048 if (! error_state) |
|
1049 retval (0) = tree_constant (rv, 0); |
|
1050 } |
|
1051 } |
|
1052 } |
|
1053 else |
|
1054 error ("linspace: npoints must be greater than 2"); |
|
1055 } |
|
1056 |
|
1057 return retval; |
|
1058 } |
|
1059 |
523
|
1060 /* |
|
1061 ;;; Local Variables: *** |
|
1062 ;;; mode: C++ *** |
|
1063 ;;; page-delimiter: "^/\\*" *** |
|
1064 ;;; End: *** |
|
1065 */ |