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