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 |
|
448 double tmp = b.double_value (); |
|
449 |
|
450 if (error_state) |
|
451 { |
|
452 error ("diag: invalid second argument"); |
|
453 return retval; |
|
454 } |
|
455 |
|
456 int k = NINT (tmp); |
|
457 int n = ABS (k) + 1; |
|
458 |
|
459 if (a.is_real_type ()) |
|
460 { |
|
461 if (a.is_scalar_type ()) |
|
462 { |
|
463 double d = a.double_value (); |
|
464 |
|
465 if (k == 0) |
|
466 retval = d; |
|
467 else if (k > 0) |
|
468 { |
|
469 Matrix m (n, n, 0.0); |
2305
|
470 m (0, k) = d; |
767
|
471 retval = m; |
|
472 } |
|
473 else if (k < 0) |
|
474 { |
|
475 Matrix m (n, n, 0.0); |
2305
|
476 m (-k, 0) = d; |
767
|
477 retval = m; |
|
478 } |
|
479 } |
|
480 else if (a.is_matrix_type ()) |
|
481 { |
|
482 Matrix m = a.matrix_value (); |
|
483 |
|
484 int nr = m.rows (); |
|
485 int nc = m.columns (); |
|
486 |
|
487 if (nr == 0 || nc == 0) |
|
488 retval = Matrix (); |
|
489 else if (nr == 1 || nc == 1) |
|
490 retval = make_diag (m, k); |
|
491 else |
|
492 { |
|
493 ColumnVector d = m.diag (k); |
|
494 retval = d; |
|
495 } |
|
496 } |
|
497 else |
|
498 gripe_wrong_type_arg ("diag", a); |
|
499 } |
|
500 else if (a.is_complex_type ()) |
|
501 { |
|
502 if (a.is_scalar_type ()) |
|
503 { |
|
504 Complex c = a.complex_value (); |
|
505 |
|
506 if (k == 0) |
|
507 retval = c; |
|
508 else if (k > 0) |
|
509 { |
|
510 ComplexMatrix m (n, n, 0.0); |
2305
|
511 m (0, k) = c; |
767
|
512 retval = m; |
|
513 } |
|
514 else if (k < 0) |
|
515 { |
|
516 ComplexMatrix m (n, n, 0.0); |
2305
|
517 m (-k, 0) = c; |
767
|
518 retval = m; |
|
519 } |
|
520 } |
|
521 else if (a.is_matrix_type ()) |
|
522 { |
|
523 ComplexMatrix cm = a.complex_matrix_value (); |
|
524 |
|
525 int nr = cm.rows (); |
|
526 int nc = cm.columns (); |
|
527 |
|
528 if (nr == 0 || nc == 0) |
|
529 retval = Matrix (); |
|
530 else if (nr == 1 || nc == 1) |
|
531 retval = make_diag (cm, k); |
|
532 else |
|
533 { |
|
534 ComplexColumnVector d = cm.diag (k); |
|
535 retval = d; |
|
536 } |
|
537 } |
|
538 else |
|
539 gripe_wrong_type_arg ("diag", a); |
|
540 } |
|
541 else |
|
542 gripe_wrong_type_arg ("diag", a); |
|
543 |
|
544 return retval; |
|
545 } |
|
546 |
1957
|
547 DEFUN (diag, args, , |
523
|
548 "diag (X [,k]): form/extract diagonals") |
|
549 { |
2086
|
550 octave_value_list retval; |
523
|
551 |
|
552 int nargin = args.length (); |
|
553 |
712
|
554 if (nargin == 1 && args(0).is_defined ()) |
767
|
555 retval = make_diag (args(0)); |
712
|
556 else if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
767
|
557 retval = make_diag (args(0), args(1)); |
523
|
558 else |
|
559 print_usage ("diag"); |
|
560 |
|
561 return retval; |
|
562 } |
|
563 |
1957
|
564 DEFUN (prod, args, , |
523
|
565 "prod (X): products") |
|
566 { |
2086
|
567 octave_value_list retval; |
523
|
568 |
|
569 int nargin = args.length (); |
|
570 |
760
|
571 if (nargin == 1) |
|
572 { |
2086
|
573 octave_value arg = args(0); |
760
|
574 |
|
575 if (arg.is_real_type ()) |
|
576 { |
|
577 Matrix tmp = arg.matrix_value (); |
|
578 |
|
579 if (! error_state) |
|
580 retval(0) = tmp.prod (); |
|
581 } |
|
582 else if (arg.is_complex_type ()) |
|
583 { |
|
584 ComplexMatrix tmp = arg.complex_matrix_value (); |
|
585 |
|
586 if (! error_state) |
|
587 retval(0) = tmp.prod (); |
|
588 } |
|
589 else |
|
590 { |
|
591 gripe_wrong_type_arg ("prod", arg); |
|
592 return retval; |
|
593 } |
|
594 } |
712
|
595 else |
523
|
596 print_usage ("prod"); |
|
597 |
|
598 return retval; |
|
599 } |
|
600 |
1957
|
601 DEFUN (size, args, nargout, |
1032
|
602 "[m, n] = size (x): return rows and columns of X\n\ |
1031
|
603 \n\ |
|
604 d = size (x): return number of rows and columns of x as a row vector\n\ |
|
605 \n\ |
|
606 m = size (x, 1): return number of rows in x\n\ |
|
607 m = size (x, 2): return number of columns in x") |
523
|
608 { |
2086
|
609 octave_value_list retval; |
523
|
610 |
|
611 int nargin = args.length (); |
|
612 |
1031
|
613 if (nargin == 1 && nargout < 3) |
523
|
614 { |
712
|
615 int nr = args(0).rows (); |
|
616 int nc = args(0).columns (); |
1031
|
617 |
712
|
618 if (nargout == 0 || nargout == 1) |
523
|
619 { |
712
|
620 Matrix m (1, 2); |
2305
|
621 m (0, 0) = nr; |
|
622 m (0, 1) = nc; |
712
|
623 retval = m; |
523
|
624 } |
712
|
625 else if (nargout == 2) |
|
626 { |
2800
|
627 retval(1) = static_cast<double> (nc); |
|
628 retval(0) = static_cast<double> (nr); |
712
|
629 } |
1031
|
630 } |
|
631 else if (nargin == 2 && nargout < 2) |
|
632 { |
|
633 int nd = NINT (args(1).double_value ()); |
|
634 |
|
635 if (error_state) |
|
636 error ("size: expecting scalar as second argument"); |
712
|
637 else |
1031
|
638 { |
|
639 if (nd == 1) |
2800
|
640 retval(0) = static_cast<double> (args(0).rows ()); |
1031
|
641 else if (nd == 2) |
2800
|
642 retval(0) = static_cast<double> (args(0).columns ()); |
1031
|
643 else |
|
644 error ("size: invalid second argument -- expecting 1 or 2"); |
|
645 } |
523
|
646 } |
712
|
647 else |
|
648 print_usage ("size"); |
523
|
649 |
|
650 return retval; |
|
651 } |
|
652 |
1957
|
653 DEFUN (sum, args, , |
523
|
654 "sum (X): sum of elements") |
|
655 { |
2086
|
656 octave_value_list retval; |
523
|
657 |
|
658 int nargin = args.length (); |
|
659 |
760
|
660 if (nargin == 1) |
|
661 { |
2086
|
662 octave_value arg = args(0); |
760
|
663 |
|
664 if (arg.is_real_type ()) |
|
665 { |
|
666 Matrix tmp = arg.matrix_value (); |
|
667 |
|
668 if (! error_state) |
|
669 retval(0) = tmp.sum (); |
|
670 } |
|
671 else if (arg.is_complex_type ()) |
|
672 { |
|
673 ComplexMatrix tmp = arg.complex_matrix_value (); |
|
674 |
|
675 if (! error_state) |
|
676 retval(0) = tmp.sum (); |
|
677 } |
|
678 else |
|
679 { |
|
680 gripe_wrong_type_arg ("sum", arg); |
|
681 return retval; |
|
682 } |
|
683 } |
523
|
684 else |
712
|
685 print_usage ("sum"); |
523
|
686 |
|
687 return retval; |
|
688 } |
|
689 |
1957
|
690 DEFUN (sumsq, args, , |
3095
|
691 "sumsq (X): sum of squares of elements.\n\ |
|
692 \n\ |
|
693 This function is equivalent to computing\n\ |
|
694 \n\ |
|
695 sum (X .* conj (X))\n\ |
|
696 \n\ |
|
697 but it uses less memory and avoids calling conj if X is real.") |
523
|
698 { |
2086
|
699 octave_value_list retval; |
523
|
700 |
|
701 int nargin = args.length (); |
|
702 |
760
|
703 if (nargin == 1) |
|
704 { |
2086
|
705 octave_value arg = args(0); |
760
|
706 |
|
707 if (arg.is_real_type ()) |
|
708 { |
|
709 Matrix tmp = arg.matrix_value (); |
|
710 |
|
711 if (! error_state) |
|
712 retval(0) = tmp.sumsq (); |
|
713 } |
|
714 else if (arg.is_complex_type ()) |
|
715 { |
|
716 ComplexMatrix tmp = arg.complex_matrix_value (); |
|
717 |
|
718 if (! error_state) |
|
719 retval(0) = tmp.sumsq (); |
|
720 } |
|
721 else |
|
722 { |
|
723 gripe_wrong_type_arg ("sumsq", arg); |
|
724 return retval; |
|
725 } |
|
726 } |
712
|
727 else |
523
|
728 print_usage ("sumsq"); |
|
729 |
|
730 return retval; |
|
731 } |
|
732 |
3186
|
733 DEFUN (is_complex, args, , |
|
734 "is_complex (x): return nonzero if x is a complex numeric object") |
|
735 { |
|
736 octave_value retval; |
|
737 |
|
738 if (args.length () == 1) |
|
739 retval = args(0).is_complex_type () ? 1.0 : 0.0; |
|
740 else |
|
741 print_usage ("is_complex"); |
|
742 |
|
743 return retval; |
|
744 } |
|
745 |
1957
|
746 DEFUN (is_struct, args, , |
939
|
747 "is_struct (x): return nonzero if x is a structure") |
|
748 { |
3186
|
749 octave_value retval; |
939
|
750 |
3186
|
751 if (args.length () == 1) |
|
752 retval = args(0).is_map () ? 1.0 : 0.0; |
939
|
753 else |
|
754 print_usage ("is_struct"); |
|
755 |
|
756 return retval; |
|
757 } |
|
758 |
1957
|
759 DEFUN (struct_elements, args, , |
1402
|
760 "struct_elements (S)\n\ |
|
761 \n\ |
|
762 Return a list of the names of the elements of the structure S.") |
|
763 { |
2086
|
764 octave_value_list retval; |
1402
|
765 |
|
766 int nargin = args.length (); |
|
767 |
|
768 if (nargin == 1) |
|
769 { |
|
770 if (args (0).is_map ()) |
|
771 { |
|
772 Octave_map m = args(0).map_value (); |
1755
|
773 retval(0) = m.make_name_list (); |
1402
|
774 } |
|
775 else |
|
776 gripe_wrong_type_arg ("struct_elements", args (0)); |
|
777 } |
|
778 else |
|
779 print_usage ("struct_elements"); |
|
780 |
|
781 return retval; |
|
782 } |
|
783 |
1957
|
784 DEFUN (struct_contains, args, , |
1216
|
785 "struct_contains (S, NAME)\n\ |
|
786 \n\ |
2420
|
787 Return nonzero if S is a structure with element NAME.\n\ |
|
788 S must be a structure and NAME must be a string.") |
1216
|
789 { |
2086
|
790 octave_value_list retval; |
1216
|
791 |
|
792 int nargin = args.length (); |
|
793 |
|
794 if (nargin == 2) |
|
795 { |
|
796 retval = 0.0; |
2420
|
797 |
2963
|
798 // XXX FIXME XXX -- should this work for all types that can do |
|
799 // structure reference operations? |
|
800 |
1277
|
801 if (args(0).is_map () && args(1).is_string ()) |
1216
|
802 { |
1755
|
803 string s = args(1).string_value (); |
2963
|
804 octave_value tmp = args(0).do_struct_elt_index_op (s, true); |
2800
|
805 retval = static_cast<double> (tmp.is_defined ()); |
1216
|
806 } |
2420
|
807 else |
|
808 print_usage ("struct_contains"); |
1216
|
809 } |
|
810 else |
|
811 print_usage ("struct_contains"); |
|
812 |
|
813 return retval; |
|
814 } |
|
815 |
523
|
816 static void |
|
817 check_dimensions (int& nr, int& nc, const char *warnfor) |
|
818 { |
|
819 if (nr < 0 || nc < 0) |
|
820 { |
2188
|
821 if (Vtreat_neg_dim_as_zero) |
597
|
822 { |
|
823 nr = (nr < 0) ? 0 : nr; |
|
824 nc = (nc < 0) ? 0 : nc; |
1129
|
825 |
2188
|
826 if (Vtreat_neg_dim_as_zero < 0) |
1129
|
827 warning ("%s: converting negative dimension to zero", |
|
828 warnfor); |
597
|
829 } |
523
|
830 else |
|
831 error ("%s: can't create a matrix with negative dimensions", |
|
832 warnfor); |
|
833 } |
|
834 } |
|
835 |
|
836 static void |
2086
|
837 get_dimensions (const octave_value& a, const char *warn_for, |
523
|
838 int& nr, int& nc) |
|
839 { |
634
|
840 if (a.is_scalar_type ()) |
523
|
841 { |
634
|
842 double tmp = a.double_value (); |
523
|
843 nr = nc = NINT (tmp); |
|
844 } |
|
845 else |
|
846 { |
634
|
847 nr = a.rows (); |
|
848 nc = a.columns (); |
523
|
849 |
|
850 if ((nr == 1 && nc == 2) || (nr == 2 && nc == 1)) |
|
851 { |
634
|
852 ColumnVector v = a.vector_value (); |
523
|
853 |
633
|
854 if (error_state) |
|
855 return; |
|
856 |
2305
|
857 nr = NINT (v (0)); |
|
858 nc = NINT (v (1)); |
523
|
859 } |
|
860 else |
|
861 warning ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
|
862 } |
|
863 |
|
864 check_dimensions (nr, nc, warn_for); // May set error_state. |
|
865 } |
|
866 |
|
867 static void |
2086
|
868 get_dimensions (const octave_value& a, const octave_value& b, |
523
|
869 const char *warn_for, int& nr, int& nc) |
|
870 { |
3174
|
871 nr = (a.is_empty ()) ? 0 : NINT (a.double_value ()); |
|
872 nc = (b.is_empty ()) ? 0 : NINT (b.double_value ()); |
523
|
873 |
634
|
874 if (error_state) |
|
875 error ("%s: expecting two scalar arguments", warn_for); |
523
|
876 else |
634
|
877 check_dimensions (nr, nc, warn_for); // May set error_state. |
523
|
878 } |
|
879 |
2086
|
880 static octave_value |
|
881 fill_matrix (const octave_value& a, double val, const char *warn_for) |
523
|
882 { |
|
883 int nr, nc; |
|
884 get_dimensions (a, warn_for, nr, nc); |
|
885 |
|
886 if (error_state) |
2086
|
887 return octave_value (); |
523
|
888 |
|
889 Matrix m (nr, nc, val); |
|
890 |
|
891 return m; |
|
892 } |
|
893 |
2086
|
894 static octave_value |
|
895 fill_matrix (const octave_value& a, const octave_value& b, |
523
|
896 double val, const char *warn_for) |
|
897 { |
|
898 int nr, nc; |
|
899 get_dimensions (a, b, warn_for, nr, nc); // May set error_state. |
|
900 |
|
901 if (error_state) |
2086
|
902 return octave_value (); |
523
|
903 |
|
904 Matrix m (nr, nc, val); |
|
905 |
|
906 return m; |
|
907 } |
|
908 |
1957
|
909 DEFUN (ones, args, , |
523
|
910 "ones (N), ones (N, M), ones (X): create a matrix of all ones") |
|
911 { |
2086
|
912 octave_value_list retval; |
523
|
913 |
|
914 int nargin = args.length (); |
|
915 |
|
916 switch (nargin) |
|
917 { |
712
|
918 case 0: |
|
919 retval = 1.0; |
|
920 break; |
777
|
921 |
610
|
922 case 1: |
712
|
923 retval = fill_matrix (args(0), 1.0, "ones"); |
610
|
924 break; |
777
|
925 |
523
|
926 case 2: |
712
|
927 retval = fill_matrix (args(0), args(1), 1.0, "ones"); |
523
|
928 break; |
777
|
929 |
523
|
930 default: |
|
931 print_usage ("ones"); |
|
932 break; |
|
933 } |
|
934 |
|
935 return retval; |
|
936 } |
|
937 |
1957
|
938 DEFUN (zeros, args, , |
523
|
939 "zeros (N), zeros (N, M), zeros (X): create a matrix of all zeros") |
|
940 { |
2086
|
941 octave_value_list retval; |
523
|
942 |
|
943 int nargin = args.length (); |
|
944 |
|
945 switch (nargin) |
|
946 { |
712
|
947 case 0: |
|
948 retval = 0.0; |
|
949 break; |
777
|
950 |
610
|
951 case 1: |
712
|
952 retval = fill_matrix (args(0), 0.0, "zeros"); |
610
|
953 break; |
777
|
954 |
523
|
955 case 2: |
712
|
956 retval = fill_matrix (args(0), args(1), 0.0, "zeros"); |
523
|
957 break; |
777
|
958 |
523
|
959 default: |
|
960 print_usage ("zeros"); |
|
961 break; |
|
962 } |
|
963 |
|
964 return retval; |
|
965 } |
|
966 |
2086
|
967 static octave_value |
|
968 identity_matrix (const octave_value& a) |
523
|
969 { |
|
970 int nr, nc; |
|
971 get_dimensions (a, "eye", nr, nc); // May set error_state. |
|
972 |
|
973 if (error_state) |
2086
|
974 return octave_value (); |
523
|
975 |
|
976 Matrix m (nr, nc, 0.0); |
|
977 |
|
978 if (nr > 0 && nc > 0) |
|
979 { |
|
980 int n = MIN (nr, nc); |
|
981 for (int i = 0; i < n; i++) |
2305
|
982 m (i, i) = 1.0; |
523
|
983 } |
|
984 |
|
985 return m; |
|
986 } |
|
987 |
2086
|
988 static octave_value |
|
989 identity_matrix (const octave_value& a, const octave_value& b) |
523
|
990 { |
|
991 int nr, nc; |
|
992 get_dimensions (a, b, "eye", nr, nc); // May set error_state. |
|
993 |
|
994 if (error_state) |
2086
|
995 return octave_value (); |
523
|
996 |
|
997 Matrix m (nr, nc, 0.0); |
|
998 |
|
999 if (nr > 0 && nc > 0) |
|
1000 { |
|
1001 int n = MIN (nr, nc); |
|
1002 for (int i = 0; i < n; i++) |
2305
|
1003 m (i, i) = 1.0; |
523
|
1004 } |
|
1005 |
|
1006 return m; |
|
1007 } |
|
1008 |
1957
|
1009 DEFUN (eye, args, , |
523
|
1010 "eye (N), eye (N, M), eye (X): create an identity matrix") |
|
1011 { |
2086
|
1012 octave_value_list retval; |
523
|
1013 |
|
1014 int nargin = args.length (); |
|
1015 |
|
1016 switch (nargin) |
|
1017 { |
712
|
1018 case 0: |
|
1019 retval = 1.0; |
|
1020 break; |
777
|
1021 |
610
|
1022 case 1: |
712
|
1023 retval = identity_matrix (args(0)); |
610
|
1024 break; |
777
|
1025 |
523
|
1026 case 2: |
712
|
1027 retval = identity_matrix (args(0), args(1)); |
523
|
1028 break; |
777
|
1029 |
523
|
1030 default: |
|
1031 print_usage ("eye"); |
|
1032 break; |
|
1033 } |
|
1034 |
|
1035 return retval; |
|
1036 } |
|
1037 |
1957
|
1038 DEFUN (linspace, args, , |
1100
|
1039 "usage: linspace (x1, x2, n)\n\ |
|
1040 \n\ |
|
1041 Return a vector of n equally spaced points between x1 and x2\n\ |
|
1042 inclusive.\n\ |
|
1043 \n\ |
|
1044 If the final argument is omitted, n = 100 is assumed.\n\ |
|
1045 \n\ |
|
1046 All three arguments must be scalars.\n\ |
|
1047 \n\ |
|
1048 See also: logspace") |
|
1049 { |
2086
|
1050 octave_value_list retval; |
1100
|
1051 |
|
1052 int nargin = args.length (); |
|
1053 |
|
1054 int npoints = 100; |
|
1055 |
1940
|
1056 if (nargin != 2 && nargin != 3) |
|
1057 { |
|
1058 print_usage ("linspace"); |
|
1059 return retval; |
|
1060 } |
|
1061 |
1100
|
1062 if (nargin == 3) |
|
1063 { |
|
1064 double n = args(2).double_value (); |
|
1065 |
|
1066 if (! error_state) |
|
1067 npoints = NINT (n); |
|
1068 } |
|
1069 |
|
1070 if (! error_state) |
|
1071 { |
|
1072 if (npoints > 1) |
|
1073 { |
2086
|
1074 octave_value arg_1 = args(0); |
|
1075 octave_value arg_2 = args(1); |
1100
|
1076 |
|
1077 if (arg_1.is_complex_type () || arg_2.is_complex_type ()) |
|
1078 { |
|
1079 Complex x1 = arg_1.complex_value (); |
|
1080 Complex x2 = arg_2.complex_value (); |
|
1081 |
|
1082 if (! error_state) |
|
1083 { |
|
1084 ComplexRowVector rv = linspace (x1, x2, npoints); |
|
1085 |
|
1086 if (! error_state) |
2086
|
1087 retval (0) = octave_value (rv, 0); |
1100
|
1088 } |
|
1089 } |
|
1090 else |
|
1091 { |
|
1092 double x1 = arg_1.double_value (); |
|
1093 double x2 = arg_2.double_value (); |
|
1094 |
|
1095 if (! error_state) |
|
1096 { |
|
1097 RowVector rv = linspace (x1, x2, npoints); |
|
1098 |
|
1099 if (! error_state) |
2086
|
1100 retval (0) = octave_value (rv, 0); |
1100
|
1101 } |
|
1102 } |
|
1103 } |
|
1104 else |
|
1105 error ("linspace: npoints must be greater than 2"); |
|
1106 } |
|
1107 |
|
1108 return retval; |
|
1109 } |
|
1110 |
2188
|
1111 static int |
|
1112 treat_neg_dim_as_zero (void) |
|
1113 { |
|
1114 Vtreat_neg_dim_as_zero = check_preference ("treat_neg_dim_as_zero"); |
|
1115 |
|
1116 return 0; |
|
1117 } |
|
1118 |
2184
|
1119 void |
|
1120 symbols_of_data (void) |
|
1121 { |
3141
|
1122 DEFCONST (I, Complex (0.0, 1.0), |
2184
|
1123 "sqrt (-1)"); |
|
1124 |
3141
|
1125 DEFCONST (Inf, octave_Inf, |
2184
|
1126 "infinity"); |
|
1127 |
3141
|
1128 DEFCONST (J, Complex (0.0, 1.0), |
2184
|
1129 "sqrt (-1)"); |
|
1130 |
3141
|
1131 DEFCONST (NaN, octave_NaN, |
2184
|
1132 "not a number"); |
|
1133 |
|
1134 #if defined (M_E) |
|
1135 double e_val = M_E; |
|
1136 #else |
|
1137 double e_val = exp (1.0); |
|
1138 #endif |
|
1139 |
3141
|
1140 DEFCONST (e, e_val, |
2184
|
1141 "exp (1)"); |
|
1142 |
3141
|
1143 DEFCONST (eps, DBL_EPSILON, |
2184
|
1144 "machine precision"); |
|
1145 |
3141
|
1146 DEFCONST (i, Complex (0.0, 1.0), |
2184
|
1147 "sqrt (-1)"); |
|
1148 |
3141
|
1149 DEFCONST (inf, octave_Inf, |
2184
|
1150 "infinity"); |
|
1151 |
3141
|
1152 DEFCONST (j, Complex (0.0, 1.0), |
2184
|
1153 "sqrt (-1)"); |
|
1154 |
3141
|
1155 DEFCONST (nan, octave_NaN, |
2184
|
1156 "not a number"); |
|
1157 |
|
1158 #if defined (M_PI) |
|
1159 double pi_val = M_PI; |
|
1160 #else |
|
1161 double pi_val = 4.0 * atan (1.0); |
|
1162 #endif |
|
1163 |
3141
|
1164 DEFCONST (pi, pi_val, |
2184
|
1165 "ratio of the circumference of a circle to its diameter"); |
|
1166 |
3141
|
1167 DEFCONST (realmax, DBL_MAX, |
2184
|
1168 "realmax (): return largest representable floating point number"); |
|
1169 |
3141
|
1170 DEFCONST (realmin, DBL_MIN, |
2184
|
1171 "realmin (): return smallest representable floating point number"); |
2188
|
1172 |
|
1173 DEFVAR (treat_neg_dim_as_zero, 0.0, 0, treat_neg_dim_as_zero, |
|
1174 "convert negative dimensions to zero"); |
2184
|
1175 } |
|
1176 |
523
|
1177 /* |
|
1178 ;;; Local Variables: *** |
|
1179 ;;; mode: C++ *** |
|
1180 ;;; End: *** |
|
1181 */ |