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