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