523
|
1 // data.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 /* |
|
25 |
|
26 The function builtin_pwd adapted from a similar function from GNU |
|
27 Bash, the Bourne Again SHell, copyright (C) 1987, 1989, 1991 Free |
|
28 Software Foundation, Inc. |
|
29 |
|
30 */ |
|
31 |
|
32 #ifdef HAVE_CONFIG_H |
|
33 #include "config.h" |
|
34 #endif |
|
35 |
|
36 #include "tree-const.h" |
|
37 #include "user-prefs.h" |
565
|
38 #include "help.h" |
523
|
39 #include "utils.h" |
|
40 #include "error.h" |
|
41 #include "defun.h" |
|
42 |
|
43 #ifndef MIN |
|
44 #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
|
45 #endif |
|
46 |
767
|
47 #ifndef ABS |
|
48 #define ABS(x) (((x) < 0) ? (-x) : (x)) |
|
49 #endif |
|
50 |
712
|
51 DEFUN ("all", Fall, Sall, 1, 1, |
523
|
52 "all (X): are all elements of X nonzero?") |
|
53 { |
|
54 Octave_object retval; |
|
55 |
|
56 int nargin = args.length (); |
|
57 |
712
|
58 if (nargin == 1 && args(0).is_defined ()) |
|
59 retval = args(0).all (); |
|
60 else |
523
|
61 print_usage ("all"); |
|
62 |
|
63 return retval; |
|
64 } |
|
65 |
712
|
66 DEFUN ("any", Fany, Sany, 1, 1, |
523
|
67 "any (X): are any elements of X nonzero?") |
|
68 { |
|
69 Octave_object retval; |
|
70 |
|
71 int nargin = args.length (); |
|
72 |
712
|
73 if (nargin == 1 && args(0).is_defined ()) |
|
74 retval = args(0).any (); |
|
75 else |
523
|
76 print_usage ("any"); |
|
77 |
|
78 return retval; |
|
79 } |
|
80 |
649
|
81 // These mapping functions may also be useful in other places, eh? |
|
82 |
|
83 typedef double (*d_dd_fcn) (double, double); |
|
84 |
|
85 static Matrix |
|
86 map (d_dd_fcn f, double x, const Matrix& y) |
|
87 { |
|
88 int nr = y.rows (); |
|
89 int nc = y.columns (); |
|
90 |
|
91 Matrix retval (nr, nc); |
|
92 |
|
93 for (int j = 0; j < nc; j++) |
|
94 for (int i = 0; i < nr; i++) |
|
95 retval.elem (i, j) = f (x, y.elem (i, j)); |
|
96 |
|
97 return retval; |
|
98 } |
|
99 |
|
100 static Matrix |
|
101 map (d_dd_fcn f, const Matrix& x, double y) |
|
102 { |
|
103 int nr = x.rows (); |
|
104 int nc = x.columns (); |
|
105 |
|
106 Matrix retval (nr, nc); |
|
107 |
|
108 for (int j = 0; j < nc; j++) |
|
109 for (int i = 0; i < nr; i++) |
|
110 retval.elem (i, j) = f (x.elem (i, j), y); |
|
111 |
|
112 return retval; |
|
113 } |
|
114 |
|
115 static Matrix |
|
116 map (d_dd_fcn f, const Matrix& x, const Matrix& y) |
|
117 { |
|
118 int x_nr = x.rows (); |
|
119 int x_nc = x.columns (); |
|
120 |
|
121 int y_nr = y.rows (); |
|
122 int y_nc = y.columns (); |
|
123 |
719
|
124 assert (x_nr == y_nr && x_nc == y_nc); |
649
|
125 |
|
126 Matrix retval (x_nr, x_nc); |
|
127 |
|
128 for (int j = 0; j < x_nc; j++) |
|
129 for (int i = 0; i < x_nr; i++) |
|
130 retval.elem (i, j) = f (x.elem (i, j), y.elem (i, j)); |
|
131 |
|
132 return retval; |
|
133 } |
|
134 |
712
|
135 DEFUN ("atan2", Fatan2, Satan2, 2, 1, |
649
|
136 "atan2 (Y, X): atan (Y / X) in range -pi to pi") |
|
137 { |
|
138 Octave_object retval; |
|
139 |
712
|
140 int nargin = args.length (); |
|
141 |
|
142 if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
649
|
143 { |
712
|
144 tree_constant arg_y = args(0); |
|
145 tree_constant arg_x = args(1); |
649
|
146 |
|
147 int y_nr = arg_y.rows (); |
|
148 int y_nc = arg_y.columns (); |
|
149 |
|
150 int x_nr = arg_x.rows (); |
|
151 int x_nc = arg_x.columns (); |
|
152 |
|
153 int arg_y_empty = empty_arg ("atan2", y_nr, y_nc); |
|
154 int arg_x_empty = empty_arg ("atan2", x_nr, x_nc); |
|
155 |
719
|
156 if (arg_y_empty > 0 && arg_x_empty > 0) |
|
157 return Matrix (); |
|
158 else if (arg_y_empty || arg_x_empty) |
649
|
159 return retval; |
|
160 |
|
161 int y_is_scalar = (y_nr == 1 && y_nc == 1); |
|
162 int x_is_scalar = (x_nr == 1 && x_nc == 1); |
|
163 |
|
164 if (y_is_scalar && x_is_scalar) |
|
165 { |
|
166 double y = arg_y.double_value (); |
|
167 |
|
168 if (! error_state) |
|
169 { |
|
170 double x = arg_x.double_value (); |
|
171 |
|
172 if (! error_state) |
|
173 retval = atan2 (y, x); |
|
174 } |
|
175 } |
|
176 else if (y_is_scalar) |
|
177 { |
|
178 double y = arg_y.double_value (); |
|
179 |
|
180 if (! error_state) |
|
181 { |
|
182 Matrix x = arg_x.matrix_value (); |
|
183 |
|
184 if (! error_state) |
|
185 retval = map (atan2, y, x); |
|
186 } |
|
187 } |
|
188 else if (x_is_scalar) |
|
189 { |
|
190 Matrix y = arg_y.matrix_value (); |
|
191 |
|
192 if (! error_state) |
|
193 { |
|
194 double x = arg_x.double_value (); |
|
195 |
|
196 if (! error_state) |
|
197 retval = map (atan2, y, x); |
|
198 } |
|
199 } |
|
200 else if (y_nr == x_nr && y_nc == x_nc) |
|
201 { |
|
202 Matrix y = arg_y.matrix_value (); |
|
203 |
|
204 if (! error_state) |
|
205 { |
|
206 Matrix x = arg_x.matrix_value (); |
|
207 |
|
208 if (! error_state) |
|
209 retval = map (atan2, y, x); |
|
210 } |
|
211 } |
|
212 else |
|
213 error ("atan2: nonconformant matrices"); |
|
214 } |
712
|
215 else |
|
216 print_usage ("atan2"); |
649
|
217 |
|
218 return retval; |
|
219 } |
|
220 |
712
|
221 DEFUN ("cumprod", Fcumprod, Scumprod, 1, 1, |
523
|
222 "cumprod (X): cumulative products") |
|
223 { |
|
224 Octave_object retval; |
|
225 |
|
226 int nargin = args.length (); |
|
227 |
760
|
228 if (nargin == 1) |
|
229 { |
|
230 tree_constant arg = args(0); |
|
231 |
|
232 if (arg.is_real_type ()) |
|
233 { |
|
234 Matrix tmp = arg.matrix_value (); |
|
235 |
|
236 if (! error_state) |
|
237 retval(0) = tmp.cumprod (); |
|
238 } |
|
239 else if (arg.is_complex_type ()) |
|
240 { |
|
241 ComplexMatrix tmp = arg.complex_matrix_value (); |
|
242 |
|
243 if (! error_state) |
|
244 retval(0) = tmp.cumprod (); |
|
245 } |
|
246 else |
|
247 { |
|
248 gripe_wrong_type_arg ("cumprod", arg); |
|
249 return retval; |
|
250 } |
|
251 } |
712
|
252 else |
523
|
253 print_usage ("cumprod"); |
|
254 |
|
255 return retval; |
|
256 } |
|
257 |
712
|
258 DEFUN ("cumsum", Fcumsum, Scumsum, 1, 1, |
523
|
259 "cumsum (X): cumulative sums") |
|
260 { |
|
261 Octave_object retval; |
|
262 |
|
263 int nargin = args.length (); |
|
264 |
760
|
265 if (nargin == 1) |
|
266 { |
|
267 tree_constant arg = args(0); |
|
268 |
|
269 if (arg.is_real_type ()) |
|
270 { |
|
271 Matrix tmp = arg.matrix_value (); |
|
272 |
|
273 if (! error_state) |
|
274 retval(0) = tmp.cumsum (); |
|
275 } |
|
276 else if (arg.is_complex_type ()) |
|
277 { |
|
278 ComplexMatrix tmp = arg.complex_matrix_value (); |
|
279 |
|
280 if (! error_state) |
|
281 retval(0) = tmp.cumsum (); |
|
282 } |
|
283 else |
|
284 { |
|
285 gripe_wrong_type_arg ("cumsum", arg); |
|
286 return retval; |
|
287 } |
|
288 } |
712
|
289 else |
523
|
290 print_usage ("cumsum"); |
|
291 |
|
292 return retval; |
|
293 } |
|
294 |
767
|
295 static tree_constant |
|
296 make_diag (const Matrix& v, int k) |
|
297 { |
|
298 int nr = v.rows (); |
|
299 int nc = v.columns (); |
|
300 assert (nc == 1 || nr == 1); |
|
301 |
|
302 tree_constant retval; |
|
303 |
|
304 int roff = 0; |
|
305 int coff = 0; |
|
306 if (k > 0) |
|
307 { |
|
308 roff = 0; |
|
309 coff = k; |
|
310 } |
|
311 else if (k < 0) |
|
312 { |
|
313 roff = -k; |
|
314 coff = 0; |
|
315 } |
|
316 |
|
317 if (nr == 1) |
|
318 { |
|
319 int n = nc + ABS (k); |
|
320 Matrix m (n, n, 0.0); |
|
321 for (int i = 0; i < nc; i++) |
|
322 m.elem (i+roff, i+coff) = v.elem (0, i); |
|
323 retval = tree_constant (m); |
|
324 } |
|
325 else |
|
326 { |
|
327 int n = nr + ABS (k); |
|
328 Matrix m (n, n, 0.0); |
|
329 for (int i = 0; i < nr; i++) |
|
330 m.elem (i+roff, i+coff) = v.elem (i, 0); |
|
331 retval = tree_constant (m); |
|
332 } |
|
333 |
|
334 return retval; |
|
335 } |
|
336 |
|
337 static tree_constant |
|
338 make_diag (const ComplexMatrix& v, int k) |
|
339 { |
|
340 int nr = v.rows (); |
|
341 int nc = v.columns (); |
|
342 assert (nc == 1 || nr == 1); |
|
343 |
|
344 tree_constant retval; |
|
345 |
|
346 int roff = 0; |
|
347 int coff = 0; |
|
348 if (k > 0) |
|
349 { |
|
350 roff = 0; |
|
351 coff = k; |
|
352 } |
|
353 else if (k < 0) |
|
354 { |
|
355 roff = -k; |
|
356 coff = 0; |
|
357 } |
|
358 |
|
359 if (nr == 1) |
|
360 { |
|
361 int n = nc + ABS (k); |
|
362 ComplexMatrix m (n, n, 0.0); |
|
363 for (int i = 0; i < nc; i++) |
|
364 m.elem (i+roff, i+coff) = v.elem (0, i); |
|
365 retval = tree_constant (m); |
|
366 } |
|
367 else |
|
368 { |
|
369 int n = nr + ABS (k); |
|
370 ComplexMatrix m (n, n, 0.0); |
|
371 for (int i = 0; i < nr; i++) |
|
372 m.elem (i+roff, i+coff) = v.elem (i, 0); |
|
373 retval = tree_constant (m); |
|
374 } |
|
375 |
|
376 return retval; |
|
377 } |
|
378 |
|
379 static tree_constant |
|
380 make_diag (const tree_constant& arg) |
|
381 { |
|
382 tree_constant retval; |
|
383 |
|
384 if (arg.is_real_type ()) |
|
385 { |
|
386 Matrix m = arg.matrix_value (); |
|
387 |
|
388 if (! error_state) |
|
389 { |
|
390 int nr = m.rows (); |
|
391 int nc = m.columns (); |
|
392 |
|
393 if (nr == 0 || nc == 0) |
|
394 retval = Matrix (); |
|
395 else if (nr == 1 || nc == 1) |
|
396 retval = make_diag (m, 0); |
|
397 else |
|
398 { |
|
399 ColumnVector v = m.diag (); |
|
400 if (v.capacity () > 0) |
|
401 retval = v; |
|
402 } |
|
403 } |
|
404 else |
|
405 gripe_wrong_type_arg ("diag", arg); |
|
406 } |
|
407 else if (arg.is_complex_type ()) |
|
408 { |
|
409 ComplexMatrix cm = arg.complex_matrix_value (); |
|
410 |
|
411 if (! error_state) |
|
412 { |
|
413 int nr = cm.rows (); |
|
414 int nc = cm.columns (); |
|
415 |
|
416 if (nr == 0 || nc == 0) |
|
417 retval = Matrix (); |
|
418 else if (nr == 1 || nc == 1) |
|
419 retval = make_diag (cm, 0); |
|
420 else |
|
421 { |
|
422 ComplexColumnVector v = cm.diag (); |
|
423 if (v.capacity () > 0) |
|
424 retval = v; |
|
425 } |
|
426 } |
|
427 else |
|
428 gripe_wrong_type_arg ("diag", arg); |
|
429 } |
|
430 else |
|
431 gripe_wrong_type_arg ("diag", arg); |
|
432 |
|
433 return retval; |
|
434 } |
|
435 |
|
436 static tree_constant |
|
437 make_diag (const tree_constant& a, const tree_constant& b) |
|
438 { |
|
439 tree_constant retval; |
|
440 |
|
441 double tmp = b.double_value (); |
|
442 |
|
443 if (error_state) |
|
444 { |
|
445 error ("diag: invalid second argument"); |
|
446 return retval; |
|
447 } |
|
448 |
|
449 int k = NINT (tmp); |
|
450 int n = ABS (k) + 1; |
|
451 |
|
452 if (a.is_real_type ()) |
|
453 { |
|
454 if (a.is_scalar_type ()) |
|
455 { |
|
456 double d = a.double_value (); |
|
457 |
|
458 if (k == 0) |
|
459 retval = d; |
|
460 else if (k > 0) |
|
461 { |
|
462 Matrix m (n, n, 0.0); |
|
463 m.elem (0, k) = d; |
|
464 retval = m; |
|
465 } |
|
466 else if (k < 0) |
|
467 { |
|
468 Matrix m (n, n, 0.0); |
|
469 m.elem (-k, 0) = d; |
|
470 retval = m; |
|
471 } |
|
472 } |
|
473 else if (a.is_matrix_type ()) |
|
474 { |
|
475 Matrix m = a.matrix_value (); |
|
476 |
|
477 int nr = m.rows (); |
|
478 int nc = m.columns (); |
|
479 |
|
480 if (nr == 0 || nc == 0) |
|
481 retval = Matrix (); |
|
482 else if (nr == 1 || nc == 1) |
|
483 retval = make_diag (m, k); |
|
484 else |
|
485 { |
|
486 ColumnVector d = m.diag (k); |
|
487 retval = d; |
|
488 } |
|
489 } |
|
490 else |
|
491 gripe_wrong_type_arg ("diag", a); |
|
492 } |
|
493 else if (a.is_complex_type ()) |
|
494 { |
|
495 if (a.is_scalar_type ()) |
|
496 { |
|
497 Complex c = a.complex_value (); |
|
498 |
|
499 if (k == 0) |
|
500 retval = c; |
|
501 else if (k > 0) |
|
502 { |
|
503 ComplexMatrix m (n, n, 0.0); |
|
504 m.elem (0, k) = c; |
|
505 retval = m; |
|
506 } |
|
507 else if (k < 0) |
|
508 { |
|
509 ComplexMatrix m (n, n, 0.0); |
|
510 m.elem (-k, 0) = c; |
|
511 retval = m; |
|
512 } |
|
513 } |
|
514 else if (a.is_matrix_type ()) |
|
515 { |
|
516 ComplexMatrix cm = a.complex_matrix_value (); |
|
517 |
|
518 int nr = cm.rows (); |
|
519 int nc = cm.columns (); |
|
520 |
|
521 if (nr == 0 || nc == 0) |
|
522 retval = Matrix (); |
|
523 else if (nr == 1 || nc == 1) |
|
524 retval = make_diag (cm, k); |
|
525 else |
|
526 { |
|
527 ComplexColumnVector d = cm.diag (k); |
|
528 retval = d; |
|
529 } |
|
530 } |
|
531 else |
|
532 gripe_wrong_type_arg ("diag", a); |
|
533 } |
|
534 else |
|
535 gripe_wrong_type_arg ("diag", a); |
|
536 |
|
537 return retval; |
|
538 } |
|
539 |
712
|
540 DEFUN ("diag", Fdiag, Sdiag, 2, 1, |
523
|
541 "diag (X [,k]): form/extract diagonals") |
|
542 { |
|
543 Octave_object retval; |
|
544 |
|
545 int nargin = args.length (); |
|
546 |
712
|
547 if (nargin == 1 && args(0).is_defined ()) |
767
|
548 retval = make_diag (args(0)); |
712
|
549 else if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
767
|
550 retval = make_diag (args(0), args(1)); |
523
|
551 else |
|
552 print_usage ("diag"); |
|
553 |
|
554 return retval; |
|
555 } |
|
556 |
712
|
557 DEFUN ("isstr", Fisstr, Sisstr, 1, 1, |
523
|
558 "isstr (X): return 1 if X is a string, 0 otherwise") |
|
559 { |
|
560 Octave_object retval; |
|
561 |
|
562 int nargin = args.length (); |
|
563 |
712
|
564 if (nargin == 1 && args(0).is_defined ()) |
|
565 retval = (double) args(0).is_string (); |
523
|
566 else |
712
|
567 print_usage ("isstr"); |
523
|
568 |
|
569 return retval; |
|
570 } |
|
571 |
712
|
572 DEFUN ("prod", Fprod, Sprod, 1, 1, |
523
|
573 "prod (X): products") |
|
574 { |
|
575 Octave_object retval; |
|
576 |
|
577 int nargin = args.length (); |
|
578 |
760
|
579 if (nargin == 1) |
|
580 { |
|
581 tree_constant arg = args(0); |
|
582 |
|
583 if (arg.is_real_type ()) |
|
584 { |
|
585 Matrix tmp = arg.matrix_value (); |
|
586 |
|
587 if (! error_state) |
|
588 retval(0) = tmp.prod (); |
|
589 } |
|
590 else if (arg.is_complex_type ()) |
|
591 { |
|
592 ComplexMatrix tmp = arg.complex_matrix_value (); |
|
593 |
|
594 if (! error_state) |
|
595 retval(0) = tmp.prod (); |
|
596 } |
|
597 else |
|
598 { |
|
599 gripe_wrong_type_arg ("prod", arg); |
|
600 return retval; |
|
601 } |
|
602 } |
712
|
603 else |
523
|
604 print_usage ("prod"); |
|
605 |
|
606 return retval; |
|
607 } |
|
608 |
712
|
609 DEFUN ("setstr", Fsetstr, Ssetstr, 1, 1, |
523
|
610 "setstr (V): convert a vector to a string") |
|
611 { |
|
612 Octave_object retval; |
|
613 |
|
614 int nargin = args.length (); |
|
615 |
712
|
616 if (nargin == 1 && args(0).is_defined ()) |
|
617 retval = args(0).convert_to_str (); |
523
|
618 else |
|
619 print_usage ("setstr"); |
|
620 |
|
621 return retval; |
|
622 } |
|
623 |
712
|
624 DEFUN ("size", Fsize, Ssize, 1, 1, |
523
|
625 "[m, n] = size (x): return rows and columns of X") |
|
626 { |
|
627 Octave_object retval; |
|
628 |
|
629 int nargin = args.length (); |
|
630 |
712
|
631 if (nargin == 1 && args(0).is_defined ()) |
523
|
632 { |
712
|
633 int nr = args(0).rows (); |
|
634 int nc = args(0).columns (); |
|
635 if (nargout == 0 || nargout == 1) |
523
|
636 { |
712
|
637 Matrix m (1, 2); |
|
638 m.elem (0, 0) = nr; |
|
639 m.elem (0, 1) = nc; |
|
640 retval = m; |
523
|
641 } |
712
|
642 else if (nargout == 2) |
|
643 { |
|
644 retval(1) = (double) nc; |
|
645 retval(0) = (double) nr; |
|
646 } |
|
647 else |
|
648 print_usage ("size"); |
523
|
649 } |
712
|
650 else |
|
651 print_usage ("size"); |
523
|
652 |
|
653 return retval; |
|
654 } |
|
655 |
712
|
656 DEFUN ("sum", Fsum, Ssum, 1, 1, |
523
|
657 "sum (X): sum of elements") |
|
658 { |
|
659 Octave_object retval; |
|
660 |
|
661 int nargin = args.length (); |
|
662 |
760
|
663 if (nargin == 1) |
|
664 { |
|
665 tree_constant arg = args(0); |
|
666 |
|
667 if (arg.is_real_type ()) |
|
668 { |
|
669 Matrix tmp = arg.matrix_value (); |
|
670 |
|
671 if (! error_state) |
|
672 retval(0) = tmp.sum (); |
|
673 } |
|
674 else if (arg.is_complex_type ()) |
|
675 { |
|
676 ComplexMatrix tmp = arg.complex_matrix_value (); |
|
677 |
|
678 if (! error_state) |
|
679 retval(0) = tmp.sum (); |
|
680 } |
|
681 else |
|
682 { |
|
683 gripe_wrong_type_arg ("sum", arg); |
|
684 return retval; |
|
685 } |
|
686 } |
523
|
687 else |
712
|
688 print_usage ("sum"); |
523
|
689 |
|
690 return retval; |
|
691 } |
|
692 |
712
|
693 DEFUN ("sumsq", Fsumsq, Ssumsq, 1, 1, |
523
|
694 "sumsq (X): sum of squares of elements") |
|
695 { |
|
696 Octave_object retval; |
|
697 |
|
698 int nargin = args.length (); |
|
699 |
760
|
700 if (nargin == 1) |
|
701 { |
|
702 tree_constant arg = args(0); |
|
703 |
|
704 if (arg.is_real_type ()) |
|
705 { |
|
706 Matrix tmp = arg.matrix_value (); |
|
707 |
|
708 if (! error_state) |
|
709 retval(0) = tmp.sumsq (); |
|
710 } |
|
711 else if (arg.is_complex_type ()) |
|
712 { |
|
713 ComplexMatrix tmp = arg.complex_matrix_value (); |
|
714 |
|
715 if (! error_state) |
|
716 retval(0) = tmp.sumsq (); |
|
717 } |
|
718 else |
|
719 { |
|
720 gripe_wrong_type_arg ("sumsq", arg); |
|
721 return retval; |
|
722 } |
|
723 } |
712
|
724 else |
523
|
725 print_usage ("sumsq"); |
|
726 |
|
727 return retval; |
|
728 } |
|
729 |
|
730 static void |
|
731 check_dimensions (int& nr, int& nc, const char *warnfor) |
|
732 { |
|
733 if (nr < 0 || nc < 0) |
|
734 { |
|
735 if (user_pref.treat_neg_dim_as_zero) |
597
|
736 { |
|
737 nr = (nr < 0) ? 0 : nr; |
|
738 nc = (nc < 0) ? 0 : nc; |
|
739 } |
523
|
740 else |
|
741 error ("%s: can't create a matrix with negative dimensions", |
|
742 warnfor); |
|
743 } |
|
744 } |
|
745 |
|
746 static void |
|
747 get_dimensions (const tree_constant& a, const char *warn_for, |
|
748 int& nr, int& nc) |
|
749 { |
634
|
750 if (a.is_scalar_type ()) |
523
|
751 { |
634
|
752 double tmp = a.double_value (); |
523
|
753 nr = nc = NINT (tmp); |
|
754 } |
|
755 else |
|
756 { |
634
|
757 nr = a.rows (); |
|
758 nc = a.columns (); |
523
|
759 |
|
760 if ((nr == 1 && nc == 2) || (nr == 2 && nc == 1)) |
|
761 { |
634
|
762 ColumnVector v = a.vector_value (); |
523
|
763 |
633
|
764 if (error_state) |
|
765 return; |
|
766 |
523
|
767 nr = NINT (v.elem (0)); |
|
768 nc = NINT (v.elem (1)); |
|
769 } |
|
770 else |
|
771 warning ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
|
772 } |
|
773 |
|
774 check_dimensions (nr, nc, warn_for); // May set error_state. |
|
775 } |
|
776 |
|
777 static void |
|
778 get_dimensions (const tree_constant& a, const tree_constant& b, |
|
779 const char *warn_for, int& nr, int& nc) |
|
780 { |
634
|
781 nr = NINT (a.double_value ()); |
|
782 nc = NINT (b.double_value ()); |
523
|
783 |
634
|
784 if (error_state) |
|
785 error ("%s: expecting two scalar arguments", warn_for); |
523
|
786 else |
634
|
787 check_dimensions (nr, nc, warn_for); // May set error_state. |
523
|
788 } |
|
789 |
|
790 static tree_constant |
|
791 fill_matrix (const tree_constant& a, double val, const char *warn_for) |
|
792 { |
|
793 int nr, nc; |
|
794 get_dimensions (a, warn_for, nr, nc); |
|
795 |
|
796 if (error_state) |
|
797 return tree_constant (); |
|
798 |
|
799 Matrix m (nr, nc, val); |
|
800 |
|
801 return m; |
|
802 } |
|
803 |
|
804 static tree_constant |
|
805 fill_matrix (const tree_constant& a, const tree_constant& b, |
|
806 double val, const char *warn_for) |
|
807 { |
|
808 int nr, nc; |
|
809 get_dimensions (a, b, warn_for, nr, nc); // May set error_state. |
|
810 |
|
811 if (error_state) |
|
812 return tree_constant (); |
|
813 |
|
814 Matrix m (nr, nc, val); |
|
815 |
|
816 return m; |
|
817 } |
|
818 |
712
|
819 DEFUN ("ones", Fones, Sones, 2, 1, |
523
|
820 "ones (N), ones (N, M), ones (X): create a matrix of all ones") |
|
821 { |
|
822 Octave_object retval; |
|
823 |
|
824 int nargin = args.length (); |
|
825 |
|
826 switch (nargin) |
|
827 { |
712
|
828 case 0: |
|
829 retval = 1.0; |
|
830 break; |
610
|
831 case 1: |
712
|
832 retval = fill_matrix (args(0), 1.0, "ones"); |
610
|
833 break; |
523
|
834 case 2: |
712
|
835 retval = fill_matrix (args(0), args(1), 1.0, "ones"); |
523
|
836 break; |
|
837 default: |
|
838 print_usage ("ones"); |
|
839 break; |
|
840 } |
|
841 |
|
842 return retval; |
|
843 } |
|
844 |
712
|
845 DEFUN ("zeros", Fzeros, Szeros, 2, 1, |
523
|
846 "zeros (N), zeros (N, M), zeros (X): create a matrix of all zeros") |
|
847 { |
|
848 Octave_object retval; |
|
849 |
|
850 int nargin = args.length (); |
|
851 |
|
852 switch (nargin) |
|
853 { |
712
|
854 case 0: |
|
855 retval = 0.0; |
|
856 break; |
610
|
857 case 1: |
712
|
858 retval = fill_matrix (args(0), 0.0, "zeros"); |
610
|
859 break; |
523
|
860 case 2: |
712
|
861 retval = fill_matrix (args(0), args(1), 0.0, "zeros"); |
523
|
862 break; |
|
863 default: |
|
864 print_usage ("zeros"); |
|
865 break; |
|
866 } |
|
867 |
|
868 return retval; |
|
869 } |
|
870 |
|
871 static tree_constant |
|
872 identity_matrix (const tree_constant& a) |
|
873 { |
|
874 int nr, nc; |
|
875 get_dimensions (a, "eye", nr, nc); // May set error_state. |
|
876 |
|
877 if (error_state) |
|
878 return tree_constant (); |
|
879 |
|
880 Matrix m (nr, nc, 0.0); |
|
881 |
|
882 if (nr > 0 && nc > 0) |
|
883 { |
|
884 int n = MIN (nr, nc); |
|
885 for (int i = 0; i < n; i++) |
|
886 m.elem (i, i) = 1.0; |
|
887 } |
|
888 |
|
889 return m; |
|
890 } |
|
891 |
|
892 static tree_constant |
|
893 identity_matrix (const tree_constant& a, const tree_constant& b) |
|
894 { |
|
895 int nr, nc; |
|
896 get_dimensions (a, b, "eye", nr, nc); // May set error_state. |
|
897 |
|
898 if (error_state) |
|
899 return tree_constant (); |
|
900 |
|
901 Matrix m (nr, nc, 0.0); |
|
902 |
|
903 if (nr > 0 && nc > 0) |
|
904 { |
|
905 int n = MIN (nr, nc); |
|
906 for (int i = 0; i < n; i++) |
|
907 m.elem (i, i) = 1.0; |
|
908 } |
|
909 |
|
910 return m; |
|
911 } |
|
912 |
712
|
913 DEFUN ("eye", Feye, Seye, 2, 1, |
523
|
914 "eye (N), eye (N, M), eye (X): create an identity matrix") |
|
915 { |
|
916 Octave_object retval; |
|
917 |
|
918 int nargin = args.length (); |
|
919 |
|
920 switch (nargin) |
|
921 { |
712
|
922 case 0: |
|
923 retval = 1.0; |
|
924 break; |
610
|
925 case 1: |
712
|
926 retval = identity_matrix (args(0)); |
610
|
927 break; |
523
|
928 case 2: |
712
|
929 retval = identity_matrix (args(0), args(1)); |
523
|
930 break; |
|
931 default: |
|
932 print_usage ("eye"); |
|
933 break; |
|
934 } |
|
935 |
|
936 return retval; |
|
937 } |
|
938 |
|
939 /* |
|
940 ;;; Local Variables: *** |
|
941 ;;; mode: C++ *** |
|
942 ;;; page-delimiter: "^/\\*" *** |
|
943 ;;; End: *** |
|
944 */ |