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