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