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" |
4153
|
34 #include "quit.h" |
1755
|
35 |
1352
|
36 #include "defun.h" |
|
37 #include "error.h" |
|
38 #include "gripes.h" |
2366
|
39 #include "ov.h" |
|
40 #include "variables.h" |
1742
|
41 #include "oct-obj.h" |
523
|
42 #include "utils.h" |
|
43 |
4015
|
44 #define ANY_ALL(FCN) \ |
|
45 \ |
4233
|
46 octave_value retval; \ |
4015
|
47 \ |
|
48 int nargin = args.length (); \ |
|
49 \ |
4021
|
50 if (nargin == 1 || nargin == 2) \ |
4015
|
51 { \ |
4021
|
52 int dim = (nargin == 1 ? -1 : args(1).int_value (true) - 1); \ |
|
53 \ |
|
54 if (! error_state) \ |
|
55 { \ |
4556
|
56 if (dim >= -1) \ |
4015
|
57 retval = args(0).FCN (dim); \ |
4021
|
58 else \ |
|
59 error (#FCN ": invalid dimension argument = %d", dim + 1); \ |
|
60 } \ |
4015
|
61 else \ |
4021
|
62 error (#FCN ": expecting dimension argument to be an integer"); \ |
4015
|
63 } \ |
4021
|
64 else \ |
|
65 print_usage (#FCN); \ |
4015
|
66 \ |
|
67 return retval |
|
68 |
1957
|
69 DEFUN (all, args, , |
3369
|
70 "-*- texinfo -*-\n\ |
4015
|
71 @deftypefn {Built-in Function} {} all (@var{x}, @var{dim})\n\ |
3369
|
72 The function @code{all} behaves like the function @code{any}, except\n\ |
|
73 that it returns true only if all the elements of a vector, or all the\n\ |
4015
|
74 elements along dimension @var{dim} of a matrix, are nonzero.\n\ |
3369
|
75 @end deftypefn") |
523
|
76 { |
4015
|
77 ANY_ALL (all); |
523
|
78 } |
|
79 |
1957
|
80 DEFUN (any, args, , |
3369
|
81 "-*- texinfo -*-\n\ |
4015
|
82 @deftypefn {Built-in Function} {} any (@var{x}, @var{dim})\n\ |
3369
|
83 For a vector argument, return 1 if any element of the vector is\n\ |
|
84 nonzero.\n\ |
|
85 \n\ |
|
86 For a matrix argument, return a row vector of ones and\n\ |
|
87 zeros with each element indicating whether any of the elements of the\n\ |
|
88 corresponding column of the matrix are nonzero. For example,\n\ |
|
89 \n\ |
|
90 @example\n\ |
|
91 @group\n\ |
|
92 any (eye (2, 4))\n\ |
|
93 @result{} [ 1, 1, 0, 0 ]\n\ |
|
94 @end group\n\ |
|
95 @end example\n\ |
|
96 \n\ |
4015
|
97 If the optional argument @var{dim} is supplied, work along dimension\n\ |
|
98 @var{dim}. For example,\n\ |
3369
|
99 \n\ |
|
100 @example\n\ |
4015
|
101 @group\n\ |
|
102 any (eye (2, 4), 2)\n\ |
|
103 @result{} [ 1; 1 ]\n\ |
|
104 @end group\n\ |
3369
|
105 @end example\n\ |
|
106 @end deftypefn") |
523
|
107 { |
4015
|
108 ANY_ALL (any); |
523
|
109 } |
|
110 |
649
|
111 // These mapping functions may also be useful in other places, eh? |
|
112 |
|
113 typedef double (*d_dd_fcn) (double, double); |
|
114 |
|
115 static Matrix |
2672
|
116 map_d_m (d_dd_fcn f, double x, const Matrix& y) |
649
|
117 { |
|
118 int nr = y.rows (); |
|
119 int nc = y.columns (); |
|
120 |
|
121 Matrix retval (nr, nc); |
|
122 |
|
123 for (int j = 0; j < nc; j++) |
|
124 for (int i = 0; i < nr; i++) |
4153
|
125 { |
|
126 OCTAVE_QUIT; |
|
127 retval (i, j) = f (x, y (i, j)); |
|
128 } |
649
|
129 |
|
130 return retval; |
|
131 } |
|
132 |
|
133 static Matrix |
2672
|
134 map_m_d (d_dd_fcn f, const Matrix& x, double y) |
649
|
135 { |
|
136 int nr = x.rows (); |
|
137 int nc = x.columns (); |
|
138 |
|
139 Matrix retval (nr, nc); |
|
140 |
|
141 for (int j = 0; j < nc; j++) |
|
142 for (int i = 0; i < nr; i++) |
4153
|
143 { |
|
144 OCTAVE_QUIT; |
|
145 retval (i, j) = f (x (i, j), y); |
|
146 } |
649
|
147 |
|
148 return retval; |
|
149 } |
|
150 |
|
151 static Matrix |
2672
|
152 map_m_m (d_dd_fcn f, const Matrix& x, const Matrix& y) |
649
|
153 { |
|
154 int x_nr = x.rows (); |
|
155 int x_nc = x.columns (); |
|
156 |
|
157 int y_nr = y.rows (); |
|
158 int y_nc = y.columns (); |
|
159 |
719
|
160 assert (x_nr == y_nr && x_nc == y_nc); |
649
|
161 |
|
162 Matrix retval (x_nr, x_nc); |
|
163 |
|
164 for (int j = 0; j < x_nc; j++) |
|
165 for (int i = 0; i < x_nr; i++) |
4153
|
166 { |
|
167 OCTAVE_QUIT; |
|
168 retval (i, j) = f (x (i, j), y (i, j)); |
|
169 } |
649
|
170 |
|
171 return retval; |
|
172 } |
|
173 |
1957
|
174 DEFUN (atan2, args, , |
3428
|
175 "-*- texinfo -*-\n\ |
|
176 @deftypefn {Mapping Function} {} atan2 (@var{y}, @var{x})\n\ |
|
177 Compute atan (@var{y} / @var{x}) for corresponding elements of @var{y}\n\ |
|
178 and @var{x}. The result is in range -pi to pi.\n\ |
3439
|
179 @end deftypefn") |
649
|
180 { |
4233
|
181 octave_value retval; |
649
|
182 |
712
|
183 int nargin = args.length (); |
|
184 |
|
185 if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
649
|
186 { |
2086
|
187 octave_value arg_y = args(0); |
|
188 octave_value arg_x = args(1); |
649
|
189 |
|
190 int y_nr = arg_y.rows (); |
|
191 int y_nc = arg_y.columns (); |
|
192 |
|
193 int x_nr = arg_x.rows (); |
|
194 int x_nc = arg_x.columns (); |
|
195 |
|
196 int arg_y_empty = empty_arg ("atan2", y_nr, y_nc); |
|
197 int arg_x_empty = empty_arg ("atan2", x_nr, x_nc); |
|
198 |
719
|
199 if (arg_y_empty > 0 && arg_x_empty > 0) |
4233
|
200 return octave_value (Matrix ()); |
719
|
201 else if (arg_y_empty || arg_x_empty) |
649
|
202 return retval; |
|
203 |
|
204 int y_is_scalar = (y_nr == 1 && y_nc == 1); |
|
205 int x_is_scalar = (x_nr == 1 && x_nc == 1); |
|
206 |
|
207 if (y_is_scalar && x_is_scalar) |
|
208 { |
|
209 double y = arg_y.double_value (); |
|
210 |
|
211 if (! error_state) |
|
212 { |
|
213 double x = arg_x.double_value (); |
|
214 |
|
215 if (! error_state) |
|
216 retval = atan2 (y, x); |
|
217 } |
|
218 } |
|
219 else if (y_is_scalar) |
|
220 { |
|
221 double y = arg_y.double_value (); |
|
222 |
|
223 if (! error_state) |
|
224 { |
|
225 Matrix x = arg_x.matrix_value (); |
|
226 |
|
227 if (! error_state) |
2672
|
228 retval = map_d_m (atan2, y, x); |
649
|
229 } |
|
230 } |
|
231 else if (x_is_scalar) |
|
232 { |
|
233 Matrix y = arg_y.matrix_value (); |
|
234 |
|
235 if (! error_state) |
|
236 { |
|
237 double x = arg_x.double_value (); |
|
238 |
|
239 if (! error_state) |
2672
|
240 retval = map_m_d (atan2, y, x); |
649
|
241 } |
|
242 } |
|
243 else if (y_nr == x_nr && y_nc == x_nc) |
|
244 { |
|
245 Matrix y = arg_y.matrix_value (); |
|
246 |
|
247 if (! error_state) |
|
248 { |
|
249 Matrix x = arg_x.matrix_value (); |
|
250 |
|
251 if (! error_state) |
2672
|
252 retval = map_m_m (atan2, y, x); |
649
|
253 } |
|
254 } |
|
255 else |
|
256 error ("atan2: nonconformant matrices"); |
|
257 } |
712
|
258 else |
|
259 print_usage ("atan2"); |
649
|
260 |
|
261 return retval; |
|
262 } |
|
263 |
4311
|
264 DEFUN (fmod, args, , |
|
265 "-*- texinfo -*-\n\ |
|
266 @deftypefn {Mapping Function} {} fmod (@var{x}, @var{y})\n\ |
4685
|
267 Compute the floating point remainder of dividing @var{x} by @var{y}\n\ |
|
268 using the C library function @code{fmod}. The result has the same\n\ |
|
269 sign as @var{x}. If @var{y} is zero, the result implementation-defined.\n\ |
4311
|
270 @end deftypefn") |
|
271 { |
|
272 octave_value retval; |
|
273 |
|
274 int nargin = args.length (); |
|
275 |
|
276 if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
|
277 { |
|
278 octave_value arg_x = args(0); |
|
279 octave_value arg_y = args(1); |
|
280 |
|
281 int y_nr = arg_y.rows (); |
|
282 int y_nc = arg_y.columns (); |
|
283 |
|
284 int x_nr = arg_x.rows (); |
|
285 int x_nc = arg_x.columns (); |
|
286 |
|
287 int arg_y_empty = empty_arg ("fmod", y_nr, y_nc); |
|
288 int arg_x_empty = empty_arg ("fmod", x_nr, x_nc); |
|
289 |
|
290 if (arg_y_empty > 0 && arg_x_empty > 0) |
|
291 return octave_value (Matrix ()); |
|
292 else if (arg_y_empty || arg_x_empty) |
|
293 return retval; |
|
294 |
|
295 int y_is_scalar = (y_nr == 1 && y_nc == 1); |
|
296 int x_is_scalar = (x_nr == 1 && x_nc == 1); |
|
297 |
|
298 if (y_is_scalar && x_is_scalar) |
|
299 { |
|
300 double y = arg_y.double_value (); |
|
301 |
|
302 if (! error_state) |
|
303 { |
|
304 double x = arg_x.double_value (); |
|
305 |
|
306 if (! error_state) |
|
307 retval = fmod (x, y); |
|
308 } |
|
309 } |
|
310 else if (y_is_scalar) |
|
311 { |
|
312 double y = arg_y.double_value (); |
|
313 |
|
314 if (! error_state) |
|
315 { |
|
316 Matrix x = arg_x.matrix_value (); |
|
317 |
|
318 if (! error_state) |
|
319 retval = map_m_d (fmod, x, y); |
|
320 } |
|
321 } |
|
322 else if (x_is_scalar) |
|
323 { |
|
324 Matrix y = arg_y.matrix_value (); |
|
325 |
|
326 if (! error_state) |
|
327 { |
|
328 double x = arg_x.double_value (); |
|
329 |
|
330 if (! error_state) |
|
331 retval = map_d_m (fmod, x, y); |
|
332 } |
|
333 } |
|
334 else if (y_nr == x_nr && y_nc == x_nc) |
|
335 { |
|
336 Matrix y = arg_y.matrix_value (); |
|
337 |
|
338 if (! error_state) |
|
339 { |
|
340 Matrix x = arg_x.matrix_value (); |
|
341 |
|
342 if (! error_state) |
|
343 retval = map_m_m (fmod, x, y); |
|
344 } |
|
345 } |
|
346 else |
|
347 error ("fmod: nonconformant matrices"); |
|
348 } |
|
349 else |
|
350 print_usage ("fmod"); |
|
351 |
|
352 return retval; |
|
353 } |
|
354 |
3723
|
355 #define DATA_REDUCTION(FCN) \ |
|
356 \ |
4233
|
357 octave_value retval; \ |
3723
|
358 \ |
|
359 int nargin = args.length (); \ |
|
360 \ |
|
361 if (nargin == 1 || nargin == 2) \ |
|
362 { \ |
|
363 octave_value arg = args(0); \ |
|
364 \ |
3864
|
365 int dim = (nargin == 1 ? -1 : args(1).int_value (true) - 1); \ |
3723
|
366 \ |
|
367 if (! error_state) \ |
|
368 { \ |
4556
|
369 if (dim >= -1) \ |
3723
|
370 { \ |
|
371 if (arg.is_real_type ()) \ |
|
372 { \ |
4569
|
373 NDArray tmp = arg.array_value (); \ |
3723
|
374 \ |
|
375 if (! error_state) \ |
4233
|
376 retval = tmp.FCN (dim); \ |
3723
|
377 } \ |
|
378 else if (arg.is_complex_type ()) \ |
|
379 { \ |
4569
|
380 ComplexNDArray tmp = arg.complex_array_value (); \ |
3723
|
381 \ |
|
382 if (! error_state) \ |
4233
|
383 retval = tmp.FCN (dim); \ |
3723
|
384 } \ |
|
385 else \ |
|
386 { \ |
|
387 gripe_wrong_type_arg (#FCN, arg); \ |
|
388 return retval; \ |
|
389 } \ |
|
390 } \ |
|
391 else \ |
|
392 error (#FCN ": invalid dimension argument = %d", dim + 1); \ |
|
393 } \ |
|
394 } \ |
|
395 else \ |
|
396 print_usage (#FCN); \ |
|
397 \ |
|
398 return retval |
|
399 |
1957
|
400 DEFUN (cumprod, args, , |
3428
|
401 "-*- texinfo -*-\n\ |
3723
|
402 @deftypefn {Built-in Function} {} cumprod (@var{x}, @var{dim})\n\ |
|
403 Cumulative product of elements along dimension @var{dim}. If\n\ |
|
404 @var{dim} is omitted, it defaults to 1 (column-wise cumulative\n\ |
|
405 products).\n\ |
3428
|
406 @end deftypefn") |
523
|
407 { |
3723
|
408 DATA_REDUCTION (cumprod); |
523
|
409 } |
|
410 |
1957
|
411 DEFUN (cumsum, args, , |
3428
|
412 "-*- texinfo -*-\n\ |
3723
|
413 @deftypefn {Built-in Function} {} cumsum (@var{x}, @var{dim})\n\ |
|
414 Cumulative sum of elements along dimension @var{dim}. If @var{dim}\n\ |
|
415 is omitted, it defaults to 1 (column-wise cumulative sums).\n\ |
3428
|
416 @end deftypefn") |
523
|
417 { |
3723
|
418 DATA_REDUCTION (cumsum); |
523
|
419 } |
|
420 |
3972
|
421 // XXX FIXME XXX -- we could eliminate some duplicate code here with |
|
422 // some template functions or macros. |
|
423 |
2086
|
424 static octave_value |
767
|
425 make_diag (const Matrix& v, int k) |
|
426 { |
|
427 int nr = v.rows (); |
|
428 int nc = v.columns (); |
|
429 assert (nc == 1 || nr == 1); |
|
430 |
2086
|
431 octave_value retval; |
767
|
432 |
|
433 int roff = 0; |
|
434 int coff = 0; |
|
435 if (k > 0) |
|
436 { |
|
437 roff = 0; |
|
438 coff = k; |
|
439 } |
|
440 else if (k < 0) |
|
441 { |
|
442 roff = -k; |
|
443 coff = 0; |
|
444 } |
|
445 |
|
446 if (nr == 1) |
|
447 { |
4479
|
448 int n = nc + std::abs (k); |
767
|
449 Matrix m (n, n, 0.0); |
|
450 for (int i = 0; i < nc; i++) |
2305
|
451 m (i+roff, i+coff) = v (0, i); |
4233
|
452 retval = m; |
767
|
453 } |
|
454 else |
|
455 { |
4479
|
456 int n = nr + std::abs (k); |
767
|
457 Matrix m (n, n, 0.0); |
|
458 for (int i = 0; i < nr; i++) |
2305
|
459 m (i+roff, i+coff) = v (i, 0); |
4233
|
460 retval = m; |
767
|
461 } |
|
462 |
|
463 return retval; |
|
464 } |
|
465 |
2086
|
466 static octave_value |
767
|
467 make_diag (const ComplexMatrix& v, int k) |
|
468 { |
|
469 int nr = v.rows (); |
|
470 int nc = v.columns (); |
|
471 assert (nc == 1 || nr == 1); |
|
472 |
2086
|
473 octave_value retval; |
767
|
474 |
|
475 int roff = 0; |
|
476 int coff = 0; |
|
477 if (k > 0) |
|
478 { |
|
479 roff = 0; |
|
480 coff = k; |
|
481 } |
|
482 else if (k < 0) |
|
483 { |
|
484 roff = -k; |
|
485 coff = 0; |
|
486 } |
|
487 |
|
488 if (nr == 1) |
|
489 { |
4479
|
490 int n = nc + std::abs (k); |
767
|
491 ComplexMatrix m (n, n, 0.0); |
|
492 for (int i = 0; i < nc; i++) |
2305
|
493 m (i+roff, i+coff) = v (0, i); |
4233
|
494 retval = m; |
767
|
495 } |
|
496 else |
|
497 { |
4479
|
498 int n = nr + std::abs (k); |
767
|
499 ComplexMatrix m (n, n, 0.0); |
|
500 for (int i = 0; i < nr; i++) |
2305
|
501 m (i+roff, i+coff) = v (i, 0); |
4233
|
502 retval = m; |
767
|
503 } |
|
504 |
|
505 return retval; |
|
506 } |
|
507 |
2086
|
508 static octave_value |
|
509 make_diag (const octave_value& arg) |
767
|
510 { |
2086
|
511 octave_value retval; |
767
|
512 |
|
513 if (arg.is_real_type ()) |
|
514 { |
|
515 Matrix m = arg.matrix_value (); |
|
516 |
|
517 if (! error_state) |
|
518 { |
|
519 int nr = m.rows (); |
|
520 int nc = m.columns (); |
|
521 |
|
522 if (nr == 0 || nc == 0) |
|
523 retval = Matrix (); |
|
524 else if (nr == 1 || nc == 1) |
|
525 retval = make_diag (m, 0); |
|
526 else |
|
527 { |
|
528 ColumnVector v = m.diag (); |
|
529 if (v.capacity () > 0) |
|
530 retval = v; |
|
531 } |
|
532 } |
|
533 else |
|
534 gripe_wrong_type_arg ("diag", arg); |
|
535 } |
|
536 else if (arg.is_complex_type ()) |
|
537 { |
|
538 ComplexMatrix cm = arg.complex_matrix_value (); |
|
539 |
|
540 if (! error_state) |
|
541 { |
|
542 int nr = cm.rows (); |
|
543 int nc = cm.columns (); |
|
544 |
|
545 if (nr == 0 || nc == 0) |
|
546 retval = Matrix (); |
|
547 else if (nr == 1 || nc == 1) |
|
548 retval = make_diag (cm, 0); |
|
549 else |
|
550 { |
|
551 ComplexColumnVector v = cm.diag (); |
|
552 if (v.capacity () > 0) |
|
553 retval = v; |
|
554 } |
|
555 } |
|
556 else |
|
557 gripe_wrong_type_arg ("diag", arg); |
|
558 } |
|
559 else |
|
560 gripe_wrong_type_arg ("diag", arg); |
|
561 |
|
562 return retval; |
|
563 } |
|
564 |
2086
|
565 static octave_value |
|
566 make_diag (const octave_value& a, const octave_value& b) |
767
|
567 { |
2086
|
568 octave_value retval; |
767
|
569 |
4732
|
570 int k = b.int_value (); |
767
|
571 |
|
572 if (error_state) |
|
573 { |
|
574 error ("diag: invalid second argument"); |
|
575 return retval; |
|
576 } |
|
577 |
|
578 if (a.is_real_type ()) |
|
579 { |
3307
|
580 Matrix m = a.matrix_value (); |
767
|
581 |
3307
|
582 if (! error_state) |
767
|
583 { |
|
584 int nr = m.rows (); |
|
585 int nc = m.columns (); |
|
586 |
3972
|
587 if (nr == 1 || nc == 1) |
|
588 retval = make_diag (m, k); |
|
589 else if (nr == 0 || nc == 0) |
767
|
590 retval = Matrix (); |
|
591 else |
|
592 { |
|
593 ColumnVector d = m.diag (k); |
|
594 retval = d; |
|
595 } |
|
596 } |
|
597 } |
|
598 else if (a.is_complex_type ()) |
|
599 { |
3307
|
600 ComplexMatrix cm = a.complex_matrix_value (); |
767
|
601 |
3307
|
602 if (! error_state) |
767
|
603 { |
|
604 int nr = cm.rows (); |
|
605 int nc = cm.columns (); |
|
606 |
3972
|
607 if (nr == 1 || nc == 1) |
|
608 retval = make_diag (cm, k); |
|
609 else if (nr == 0 || nc == 0) |
767
|
610 retval = Matrix (); |
|
611 else |
|
612 { |
|
613 ComplexColumnVector d = cm.diag (k); |
|
614 retval = d; |
|
615 } |
|
616 } |
|
617 } |
|
618 else |
|
619 gripe_wrong_type_arg ("diag", a); |
|
620 |
|
621 return retval; |
|
622 } |
|
623 |
1957
|
624 DEFUN (diag, args, , |
3369
|
625 "-*- texinfo -*-\n\ |
|
626 @deftypefn {Built-in Function} {} diag (@var{v}, @var{k})\n\ |
|
627 Return a diagonal matrix with vector @var{v} on diagonal @var{k}. The\n\ |
|
628 second argument is optional. If it is positive, the vector is placed on\n\ |
|
629 the @var{k}-th super-diagonal. If it is negative, it is placed on the\n\ |
|
630 @var{-k}-th sub-diagonal. The default value of @var{k} is 0, and the\n\ |
|
631 vector is placed on the main diagonal. For example,\n\ |
|
632 \n\ |
|
633 @example\n\ |
|
634 @group\n\ |
|
635 diag ([1, 2, 3], 1)\n\ |
|
636 @result{} 0 1 0 0\n\ |
|
637 0 0 2 0\n\ |
|
638 0 0 0 3\n\ |
|
639 0 0 0 0\n\ |
|
640 @end group\n\ |
|
641 @end example\n\ |
|
642 @end deftypefn") |
523
|
643 { |
4233
|
644 octave_value retval; |
523
|
645 |
|
646 int nargin = args.length (); |
|
647 |
712
|
648 if (nargin == 1 && args(0).is_defined ()) |
767
|
649 retval = make_diag (args(0)); |
712
|
650 else if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
767
|
651 retval = make_diag (args(0), args(1)); |
523
|
652 else |
|
653 print_usage ("diag"); |
|
654 |
|
655 return retval; |
|
656 } |
|
657 |
1957
|
658 DEFUN (prod, args, , |
3428
|
659 "-*- texinfo -*-\n\ |
3723
|
660 @deftypefn {Built-in Function} {} prod (@var{x}, @var{dim})\n\ |
|
661 Product of elements along dimension @var{dim}. If @var{dim} is\n\ |
|
662 omitted, it defaults to 1 (column-wise products).\n\ |
3428
|
663 @end deftypefn") |
523
|
664 { |
3723
|
665 DATA_REDUCTION (prod); |
523
|
666 } |
|
667 |
4593
|
668 static octave_value |
|
669 do_permute (const octave_value_list& args, bool inv, const std::string& fname) |
|
670 { |
|
671 octave_value retval; |
|
672 |
|
673 if (args.length () == 2 && args(1).length () == args(0).dims ().length ()) |
|
674 { |
|
675 Array<int> vec = args(1).int_vector_value (); |
|
676 |
|
677 octave_value ret = args(0).permute (vec, inv); |
|
678 |
|
679 if (! error_state) |
|
680 retval = ret; |
|
681 } |
|
682 else |
|
683 print_usage (fname); |
|
684 |
|
685 return retval; |
|
686 } |
|
687 |
|
688 DEFUN (permute, args, , |
|
689 "-*- texinfo -*-\n\ |
|
690 @deftypefn {Built-in Function} {} permute (@var{a}, @var{perm})\n\ |
|
691 Return the generalized transpose for an N-d array object @var{a}.\n\ |
|
692 The permutation vector @var{perm} must contain the elements\n\ |
|
693 @code{1:ndims(a)} (in any order, but each element must appear just once).\n\ |
|
694 \n\ |
|
695 @end deftypefn\n\ |
|
696 @seealso{ipermute}") |
|
697 { |
|
698 return do_permute (args, false, "permute"); |
|
699 } |
|
700 |
|
701 DEFUN (ipermute, args, , |
|
702 "-*- texinfo -*-\n\ |
|
703 @deftypefn {Built-in Function} {} ipermute (@var{a}, @var{iperm})\n\ |
|
704 The inverse of the @code{permute} function. The expression\n\ |
|
705 \n\ |
|
706 @example\n\ |
|
707 ipermute (permute (a, perm), perm)\n\ |
|
708 @end example\n\ |
|
709 returns the original array @var{a}.\n\ |
|
710 \n\ |
|
711 @end deftypefn\n\ |
|
712 @seealso{permute}") |
|
713 { |
|
714 return do_permute (args, true, "ipermute"); |
|
715 } |
|
716 |
3195
|
717 DEFUN (length, args, , |
3373
|
718 "-*- texinfo -*-\n\ |
|
719 @deftypefn {Built-in Function} {} length (@var{a})\n\ |
4176
|
720 Return the `length' of the object @var{a}. For matrix objects, the\n\ |
3373
|
721 length is the number of rows or columns, whichever is greater (this\n\ |
|
722 odd definition is used for compatibility with Matlab).\n\ |
|
723 @end deftypefn") |
3195
|
724 { |
|
725 octave_value retval; |
|
726 |
|
727 if (args.length () == 1) |
|
728 { |
|
729 int len = args(0).length (); |
|
730 |
|
731 if (! error_state) |
4233
|
732 retval = len; |
3195
|
733 } |
|
734 else |
|
735 print_usage ("length"); |
|
736 |
|
737 return retval; |
|
738 } |
|
739 |
4554
|
740 DEFUN (ndims, args, , |
|
741 "-*- texinfo -*-\n\ |
|
742 @deftypefn {Built-in Function} {} ndims (@var{a})\n\ |
|
743 Returns the number of dimensions of array @var{a}.\n\ |
|
744 For any array, the result will always be larger than or equal to 2.\n\ |
|
745 Trailing singleton dimensions are not counted.\n\ |
|
746 @end deftypefn") |
|
747 { |
|
748 octave_value retval; |
|
749 |
|
750 if (args.length () == 1) |
|
751 { |
|
752 int n_dims = args(0).ndims (); |
|
753 |
|
754 if (! error_state) |
|
755 retval = n_dims; |
|
756 } |
|
757 else |
|
758 print_usage ("ndims"); |
|
759 |
|
760 return retval; |
|
761 } |
|
762 |
4559
|
763 DEFUN (numel, args, , |
|
764 "-*- texinfo -*-\n\ |
|
765 @deftypefn {Built-in Function} {} numel (@var{a})\n\ |
|
766 Returns the number of elements in the object @var{a}.\n\ |
|
767 @end deftypefn") |
|
768 { |
|
769 octave_value retval; |
|
770 |
|
771 if (args.length () == 1) |
|
772 { |
|
773 int numel = args(0).numel (); |
|
774 |
|
775 if (! error_state) |
|
776 { |
|
777 if (numel < 0) |
|
778 numel = 0; |
|
779 |
|
780 retval = numel; |
|
781 } |
|
782 } |
|
783 else |
|
784 print_usage ("numel"); |
|
785 |
|
786 return retval; |
|
787 } |
|
788 |
1957
|
789 DEFUN (size, args, nargout, |
3373
|
790 "-*- texinfo -*-\n\ |
|
791 @deftypefn {Built-in Function} {} size (@var{a}, @var{n})\n\ |
|
792 Return the number rows and columns of @var{a}.\n\ |
|
793 \n\ |
|
794 With one input argument and one output argument, the result is returned\n\ |
4741
|
795 in a row vector. If there are multiple output arguments, the number of\n\ |
|
796 rows is assigned to the first, and the number of columns to the second,\n\ |
|
797 etc. For example,\n\ |
3373
|
798 \n\ |
|
799 @example\n\ |
|
800 @group\n\ |
|
801 size ([1, 2; 3, 4; 5, 6])\n\ |
|
802 @result{} [ 3, 2 ]\n\ |
1031
|
803 \n\ |
3373
|
804 [nr, nc] = size ([1, 2; 3, 4; 5, 6])\n\ |
|
805 @result{} nr = 3\n\ |
|
806 @result{} nc = 2\n\ |
|
807 @end group\n\ |
|
808 @end example\n\ |
|
809 \n\ |
4741
|
810 If given a second argument, @code{size} will return the size of the\n\ |
|
811 corresponding dimension. For example\n\ |
1031
|
812 \n\ |
3373
|
813 @example\n\ |
|
814 size ([1, 2; 3, 4; 5, 6], 2)\n\ |
|
815 @result{} 2\n\ |
|
816 @end example\n\ |
|
817 \n\ |
|
818 @noindent\n\ |
|
819 returns the number of columns in the given matrix.\n\ |
|
820 @end deftypefn") |
523
|
821 { |
2086
|
822 octave_value_list retval; |
523
|
823 |
|
824 int nargin = args.length (); |
|
825 |
4513
|
826 if (nargin == 1) |
523
|
827 { |
4513
|
828 dim_vector dimensions = args(0).dims (); |
|
829 |
|
830 int ndims = dimensions.length (); |
1031
|
831 |
4513
|
832 Matrix m (1, ndims); |
|
833 |
|
834 if (nargout > 1) |
523
|
835 { |
4513
|
836 while (ndims--) |
|
837 retval(ndims) = dimensions(ndims); |
523
|
838 } |
4513
|
839 else |
712
|
840 { |
4513
|
841 for (int i = 0; i < ndims; i++) |
|
842 m(0, i) = dimensions(i); |
|
843 |
|
844 retval(0) = m; |
712
|
845 } |
1031
|
846 } |
|
847 else if (nargin == 2 && nargout < 2) |
|
848 { |
4732
|
849 int nd = args(1).int_value (true); |
1031
|
850 |
|
851 if (error_state) |
|
852 error ("size: expecting scalar as second argument"); |
712
|
853 else |
1031
|
854 { |
4741
|
855 dim_vector dv = args(0).dims (); |
|
856 |
|
857 if (nd > 0 && nd <= dv.length ()) |
|
858 retval(0) = dv(nd-1); |
1031
|
859 else |
4741
|
860 error ("size: requested dimension (= %d) out of range", nd); |
1031
|
861 } |
523
|
862 } |
712
|
863 else |
|
864 print_usage ("size"); |
523
|
865 |
|
866 return retval; |
|
867 } |
|
868 |
1957
|
869 DEFUN (sum, args, , |
3428
|
870 "-*- texinfo -*-\n\ |
3723
|
871 @deftypefn {Built-in Function} {} sum (@var{x}, @var{dim})\n\ |
|
872 Sum of elements along dimension @var{dim}. If @var{dim} is\n\ |
|
873 omitted, it defaults to 1 (column-wise sum).\n\ |
3428
|
874 @end deftypefn") |
523
|
875 { |
3723
|
876 DATA_REDUCTION (sum); |
523
|
877 } |
|
878 |
1957
|
879 DEFUN (sumsq, args, , |
3428
|
880 "-*- texinfo -*-\n\ |
3723
|
881 @deftypefn {Built-in Function} {} sumsq (@var{x}, @var{dim})\n\ |
|
882 Sum of squares of elements along dimension @var{dim}. If @var{dim}\n\ |
|
883 is omitted, it defaults to 1 (column-wise sum of squares).\n\ |
3095
|
884 \n\ |
|
885 This function is equivalent to computing\n\ |
3723
|
886 @example\n\ |
|
887 sum (x .* conj (x), dim)\n\ |
|
888 @end example\n\ |
|
889 but it uses less memory and avoids calling conj if @var{x} is real.\n\ |
3428
|
890 @end deftypefn") |
523
|
891 { |
3723
|
892 DATA_REDUCTION (sumsq); |
523
|
893 } |
|
894 |
4028
|
895 DEFUN (isbool, args, , |
3428
|
896 "-*- texinfo -*-\n\ |
4028
|
897 @deftypefn {Built-in Functio} {} isbool (@var{x})\n\ |
3428
|
898 Return true if @var{x} is a boolean object.\n\ |
3439
|
899 @end deftypefn") |
3209
|
900 { |
|
901 octave_value retval; |
|
902 |
|
903 if (args.length () == 1) |
3258
|
904 retval = args(0).is_bool_type (); |
3209
|
905 else |
4028
|
906 print_usage ("isbool"); |
3209
|
907 |
|
908 return retval; |
|
909 } |
|
910 |
4028
|
911 DEFALIAS (islogical, isbool); |
3209
|
912 |
4028
|
913 DEFUN (iscomplex, args, , |
3428
|
914 "-*- texinfo -*-\n\ |
4028
|
915 @deftypefn {Built-in Function} {} iscomplex (@var{x})\n\ |
3428
|
916 Return true if @var{x} is a complex-valued numeric object.\n\ |
|
917 @end deftypefn") |
3186
|
918 { |
|
919 octave_value retval; |
|
920 |
|
921 if (args.length () == 1) |
3258
|
922 retval = args(0).is_complex_type (); |
3186
|
923 else |
4028
|
924 print_usage ("iscomplex"); |
3186
|
925 |
|
926 return retval; |
|
927 } |
|
928 |
3258
|
929 DEFUN (isreal, args, , |
3428
|
930 "-*- texinfo -*-\n\ |
|
931 @deftypefn {Built-in Function} {} isreal (@var{x})\n\ |
|
932 Return true if @var{x} is a real-valued numeric object.\n\ |
|
933 @end deftypefn") |
3258
|
934 { |
|
935 octave_value retval; |
|
936 |
|
937 if (args.length () == 1) |
|
938 retval = args(0).is_real_type (); |
|
939 else |
|
940 print_usage ("isreal"); |
|
941 |
|
942 return retval; |
|
943 } |
|
944 |
3202
|
945 DEFUN (isempty, args, , |
3373
|
946 "-*- texinfo -*-\n\ |
|
947 @deftypefn {Built-in Function} {} isempty (@var{a})\n\ |
|
948 Return 1 if @var{a} is an empty matrix (either the number of rows, or\n\ |
|
949 the number of columns, or both are zero). Otherwise, return 0.\n\ |
|
950 @end deftypefn") |
3202
|
951 { |
4233
|
952 octave_value retval = false; |
3202
|
953 |
|
954 if (args.length () == 1) |
4559
|
955 retval = args(0).is_empty (); |
3202
|
956 else |
|
957 print_usage ("isempty"); |
|
958 |
|
959 return retval; |
|
960 } |
|
961 |
3206
|
962 DEFUN (isnumeric, args, , |
3428
|
963 "-*- texinfo -*-\n\ |
|
964 @deftypefn {Built-in Function} {} isnumeric (@var{x})\n\ |
|
965 Return nonzero if @var{x} is a numeric object.\n\ |
|
966 @end deftypefn") |
3206
|
967 { |
|
968 octave_value retval; |
|
969 |
|
970 if (args.length () == 1) |
3258
|
971 retval = args(0).is_numeric_type (); |
3206
|
972 else |
3238
|
973 print_usage ("isnumeric"); |
3206
|
974 |
|
975 return retval; |
|
976 } |
|
977 |
4028
|
978 DEFUN (islist, args, , |
3526
|
979 "-*- texinfo -*-\n\ |
4028
|
980 @deftypefn {Built-in Function} {} islist (@var{x})\n\ |
3428
|
981 Return nonzero if @var{x} is a list.\n\ |
|
982 @end deftypefn") |
3204
|
983 { |
|
984 octave_value retval; |
|
985 |
|
986 if (args.length () == 1) |
3258
|
987 retval = args(0).is_list (); |
3204
|
988 else |
4028
|
989 print_usage ("islist"); |
3204
|
990 |
|
991 return retval; |
|
992 } |
|
993 |
4028
|
994 DEFUN (ismatrix, args, , |
3321
|
995 "-*- texinfo -*-\n\ |
4028
|
996 @deftypefn {Built-in Function} {} ismatrix (@var{a})\n\ |
3321
|
997 Return 1 if @var{a} is a matrix. Otherwise, return 0.\n\ |
3333
|
998 @end deftypefn") |
3202
|
999 { |
4233
|
1000 octave_value retval = false; |
3202
|
1001 |
|
1002 if (args.length () == 1) |
|
1003 { |
|
1004 octave_value arg = args(0); |
|
1005 |
3212
|
1006 if (arg.is_scalar_type () || arg.is_range ()) |
4233
|
1007 retval = true; |
3202
|
1008 else if (arg.is_matrix_type ()) |
4233
|
1009 retval = (arg.rows () >= 1 && arg.columns () >= 1); |
3202
|
1010 } |
|
1011 else |
4028
|
1012 print_usage ("ismatrix"); |
3202
|
1013 |
|
1014 return retval; |
|
1015 } |
|
1016 |
3354
|
1017 static octave_value |
|
1018 fill_matrix (const octave_value_list& args, double val, const char *fcn) |
523
|
1019 { |
3354
|
1020 octave_value retval; |
523
|
1021 |
|
1022 int nargin = args.length (); |
|
1023 |
4481
|
1024 int ndim = 0; |
|
1025 int type = 0; |
|
1026 |
4513
|
1027 dim_vector dims; |
4481
|
1028 |
|
1029 // Check for type information. |
|
1030 |
|
1031 if (nargin > 0 && args(nargin-1).is_string ()) |
|
1032 { |
|
1033 nargin--; |
|
1034 |
|
1035 // XXX FIXME XXX -- allow type of the resulting matrix to be |
|
1036 // specified, e.g. |
|
1037 // |
|
1038 // zeros(n1, n2, ..., 'real') |
|
1039 // zeros(n1, n2, ..., 'complex') |
|
1040 // |
|
1041 // type = get_type (args(nargin).string_value ()); |
|
1042 } |
|
1043 |
|
1044 // determine matrix dimension |
|
1045 |
523
|
1046 switch (nargin) |
|
1047 { |
712
|
1048 case 0: |
4481
|
1049 ndim = 0; |
|
1050 type = 0; |
712
|
1051 break; |
777
|
1052 |
610
|
1053 case 1: |
4481
|
1054 get_dimensions (args(0), fcn, dims); |
610
|
1055 break; |
777
|
1056 |
4563
|
1057 default: |
|
1058 { |
|
1059 dims.resize (nargin); |
4481
|
1060 |
4563
|
1061 for (int i = 0; i < nargin; i++) |
|
1062 { |
4732
|
1063 dims(i) = args(i).is_empty () ? 0 : args(i).int_value (); |
4481
|
1064 |
4563
|
1065 if (error_state) |
|
1066 { |
4732
|
1067 error ("%s: expecting scalar integer arguments", fcn); |
4563
|
1068 break; |
|
1069 } |
|
1070 } |
|
1071 } |
|
1072 break; |
4481
|
1073 } |
|
1074 |
|
1075 if (! error_state) |
|
1076 { |
|
1077 ndim = dims.length (); |
|
1078 |
4565
|
1079 for (int i = ndim-1; i > 1; i--) |
|
1080 { |
|
1081 if (dims(i) == 1) |
|
1082 ndim--; |
|
1083 else |
|
1084 break; |
|
1085 } |
|
1086 |
|
1087 dims.resize (ndim); |
|
1088 |
4481
|
1089 check_dimensions (dims, fcn); |
3354
|
1090 |
4481
|
1091 if (! error_state) |
|
1092 { |
|
1093 // Construct either scalar, matrix or N-d array. |
|
1094 |
|
1095 switch (ndim) |
|
1096 { |
|
1097 case 0: |
|
1098 retval = val; |
|
1099 break; |
777
|
1100 |
4481
|
1101 case 1: |
|
1102 retval = Matrix (dims(0), dims(0), val); |
|
1103 break; |
|
1104 |
|
1105 case 2: |
|
1106 retval = Matrix (dims(0), dims(1), val); |
|
1107 break; |
|
1108 |
|
1109 default: |
4512
|
1110 retval = NDArray (dims, val); |
4481
|
1111 break; |
|
1112 } |
|
1113 } |
523
|
1114 } |
|
1115 |
|
1116 return retval; |
|
1117 } |
|
1118 |
3354
|
1119 DEFUN (ones, args, , |
3369
|
1120 "-*- texinfo -*-\n\ |
|
1121 @deftypefn {Built-in Function} {} ones (@var{x})\n\ |
|
1122 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m})\n\ |
4481
|
1123 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m}, @var{k},...)\n\ |
|
1124 Return a matrix or N-dimensional array whose elements are all 1.\n\ |
|
1125 The arguments are handled the same as the arguments for @code{eye}.\n\ |
3369
|
1126 \n\ |
|
1127 If you need to create a matrix whose values are all the same, you should\n\ |
|
1128 use an expression like\n\ |
|
1129 \n\ |
|
1130 @example\n\ |
|
1131 val_matrix = val * ones (n, m)\n\ |
|
1132 @end example\n\ |
|
1133 @end deftypefn") |
523
|
1134 { |
3354
|
1135 return fill_matrix (args, 1.0, "ones"); |
523
|
1136 } |
|
1137 |
3354
|
1138 DEFUN (zeros, args, , |
3369
|
1139 "-*- texinfo -*-\n\ |
|
1140 @deftypefn {Built-in Function} {} zeros (@var{x})\n\ |
|
1141 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m})\n\ |
4481
|
1142 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m}, @var{k},...)\n\ |
|
1143 Return a matrix or N-dimensional array whose elements are all 0.\n\ |
|
1144 The arguments are handled the same as the arguments for @code{eye}.\n\ |
3369
|
1145 @end deftypefn") |
523
|
1146 { |
3354
|
1147 return fill_matrix (args, 0.0, "zeros"); |
|
1148 } |
523
|
1149 |
1957
|
1150 DEFUN (eye, args, , |
3369
|
1151 "-*- texinfo -*-\n\ |
|
1152 @deftypefn {Built-in Function} {} eye (@var{x})\n\ |
|
1153 @deftypefnx {Built-in Function} {} eye (@var{n}, @var{m})\n\ |
|
1154 Return an identity matrix. If invoked with a single scalar argument,\n\ |
|
1155 @code{eye} returns a square matrix with the dimension specified. If you\n\ |
|
1156 supply two scalar arguments, @code{eye} takes them to be the number of\n\ |
|
1157 rows and columns. If given a vector with two elements, @code{eye} uses\n\ |
|
1158 the values of the elements as the number of rows and columns,\n\ |
|
1159 respectively. For example,\n\ |
|
1160 \n\ |
|
1161 @example\n\ |
|
1162 @group\n\ |
|
1163 eye (3)\n\ |
|
1164 @result{} 1 0 0\n\ |
|
1165 0 1 0\n\ |
|
1166 0 0 1\n\ |
|
1167 @end group\n\ |
|
1168 @end example\n\ |
|
1169 \n\ |
|
1170 The following expressions all produce the same result:\n\ |
|
1171 \n\ |
|
1172 @example\n\ |
|
1173 @group\n\ |
|
1174 eye (2)\n\ |
|
1175 @equiv{}\n\ |
|
1176 eye (2, 2)\n\ |
|
1177 @equiv{}\n\ |
|
1178 eye (size ([1, 2; 3, 4])\n\ |
|
1179 @end group\n\ |
|
1180 @end example\n\ |
|
1181 \n\ |
|
1182 For compatibility with @sc{Matlab}, calling @code{eye} with no arguments\n\ |
|
1183 is equivalent to calling it with an argument of 1.\n\ |
|
1184 @end deftypefn") |
523
|
1185 { |
3354
|
1186 octave_value retval; |
523
|
1187 |
|
1188 int nargin = args.length (); |
|
1189 |
|
1190 switch (nargin) |
|
1191 { |
712
|
1192 case 0: |
|
1193 retval = 1.0; |
|
1194 break; |
777
|
1195 |
610
|
1196 case 1: |
3354
|
1197 { |
|
1198 int nr, nc; |
|
1199 get_dimensions (args(0), "eye", nr, nc); |
|
1200 |
|
1201 if (! error_state) |
|
1202 retval = identity_matrix (nr, nc); |
|
1203 } |
610
|
1204 break; |
777
|
1205 |
523
|
1206 case 2: |
3354
|
1207 { |
|
1208 int nr, nc; |
|
1209 get_dimensions (args(0), args(1), "eye", nr, nc); |
|
1210 |
|
1211 if (! error_state) |
|
1212 retval = identity_matrix (nr, nc); |
|
1213 } |
523
|
1214 break; |
777
|
1215 |
523
|
1216 default: |
|
1217 print_usage ("eye"); |
|
1218 break; |
|
1219 } |
|
1220 |
|
1221 return retval; |
|
1222 } |
|
1223 |
1957
|
1224 DEFUN (linspace, args, , |
3369
|
1225 "-*- texinfo -*-\n\ |
|
1226 @deftypefn {Built-in Function} {} linspace (@var{base}, @var{limit}, @var{n})\n\ |
|
1227 Return a row vector with @var{n} linearly spaced elements between\n\ |
|
1228 @var{base} and @var{limit}. The number of elements, @var{n}, must be\n\ |
|
1229 greater than 1. The @var{base} and @var{limit} are always included in\n\ |
|
1230 the range. If @var{base} is greater than @var{limit}, the elements are\n\ |
|
1231 stored in decreasing order. If the number of points is not specified, a\n\ |
|
1232 value of 100 is used.\n\ |
1100
|
1233 \n\ |
4455
|
1234 The @code{linspace} function always returns a row vector.\n\ |
3369
|
1235 @end deftypefn") |
1100
|
1236 { |
3418
|
1237 octave_value retval; |
1100
|
1238 |
|
1239 int nargin = args.length (); |
|
1240 |
|
1241 int npoints = 100; |
|
1242 |
1940
|
1243 if (nargin != 2 && nargin != 3) |
|
1244 { |
|
1245 print_usage ("linspace"); |
|
1246 return retval; |
|
1247 } |
|
1248 |
1100
|
1249 if (nargin == 3) |
4732
|
1250 npoints = args(2).int_value (); |
1100
|
1251 |
|
1252 if (! error_state) |
|
1253 { |
3322
|
1254 octave_value arg_1 = args(0); |
|
1255 octave_value arg_2 = args(1); |
1100
|
1256 |
3322
|
1257 if (arg_1.is_complex_type () || arg_2.is_complex_type ()) |
|
1258 { |
|
1259 Complex x1 = arg_1.complex_value (); |
|
1260 Complex x2 = arg_2.complex_value (); |
|
1261 |
|
1262 if (! error_state) |
1100
|
1263 { |
3322
|
1264 ComplexRowVector rv = linspace (x1, x2, npoints); |
1100
|
1265 |
|
1266 if (! error_state) |
3418
|
1267 retval = rv; |
1100
|
1268 } |
|
1269 } |
|
1270 else |
3322
|
1271 { |
|
1272 double x1 = arg_1.double_value (); |
|
1273 double x2 = arg_2.double_value (); |
|
1274 |
|
1275 if (! error_state) |
|
1276 { |
|
1277 RowVector rv = linspace (x1, x2, npoints); |
|
1278 |
|
1279 if (! error_state) |
3418
|
1280 retval = rv; |
3322
|
1281 } |
|
1282 } |
1100
|
1283 } |
4732
|
1284 else |
|
1285 error ("linspace: expecting third argument to be an integer"); |
1100
|
1286 |
|
1287 return retval; |
|
1288 } |
|
1289 |
4567
|
1290 DEFUN (reshape, args, , |
|
1291 "-*- texinfo -*-\n\ |
|
1292 @deftypefn {Function File} {} reshape (@var{a}, @var{m}, @var{n}, @dots{})\n\ |
|
1293 @deftypefnx {Function File} {} reshape (@var{a}, @var{siz})\n\ |
|
1294 Return a matrix with the given dimensions whose elements are taken\n\ |
|
1295 from the matrix @var{a}. The elements of the matrix are access in\n\ |
|
1296 column-major order (like Fortran arrays are stored).\n\ |
|
1297 \n\ |
|
1298 For example,\n\ |
|
1299 \n\ |
|
1300 @example\n\ |
|
1301 @group\n\ |
|
1302 reshape ([1, 2, 3, 4], 2, 2)\n\ |
|
1303 @result{} 1 3\n\ |
|
1304 2 4\n\ |
|
1305 @end group\n\ |
|
1306 @end example\n\ |
|
1307 \n\ |
|
1308 @noindent\n\ |
|
1309 Note that the total number of elements in the original\n\ |
|
1310 matrix must match the total number of elements in the new matrix.\n\ |
|
1311 @end deftypefn") |
|
1312 { |
|
1313 octave_value retval; |
|
1314 |
|
1315 int nargin = args.length (); |
|
1316 |
|
1317 Array<int> new_size; |
|
1318 |
|
1319 if (nargin == 2) |
|
1320 new_size = args(1).int_vector_value (); |
|
1321 else if (nargin > 2) |
|
1322 { |
|
1323 new_size.resize (nargin-1); |
|
1324 |
|
1325 for (int i = 1; i < nargin; i++) |
|
1326 { |
|
1327 new_size(i-1) = args(i).int_value (); |
|
1328 |
|
1329 if (error_state) |
|
1330 break; |
|
1331 } |
|
1332 } |
|
1333 else |
|
1334 { |
|
1335 print_usage ("reshape"); |
|
1336 return retval; |
|
1337 } |
|
1338 |
|
1339 if (error_state) |
|
1340 { |
|
1341 error ("reshape: invalid arguments"); |
|
1342 return retval; |
|
1343 } |
|
1344 |
4739
|
1345 // Remove trailing singletons in new_size, but leave at least 2 |
|
1346 // elements. |
|
1347 |
4567
|
1348 int n = new_size.length (); |
|
1349 |
4739
|
1350 while (n > 2) |
|
1351 { |
|
1352 if (new_size(n-1) == 1) |
|
1353 n--; |
|
1354 else |
|
1355 break; |
|
1356 } |
|
1357 |
|
1358 new_size.resize (n); |
|
1359 |
4567
|
1360 if (n < 2) |
|
1361 { |
|
1362 error ("reshape: expecting size to be vector with at least 2 elements"); |
|
1363 return retval; |
|
1364 } |
|
1365 |
|
1366 dim_vector new_dims; |
|
1367 |
|
1368 new_dims.resize (n); |
|
1369 |
|
1370 for (int i = 0; i < n; i++) |
|
1371 new_dims(i) = new_size(i); |
|
1372 |
|
1373 octave_value arg = args(0); |
|
1374 |
|
1375 if (new_dims.numel () == arg.numel ()) |
|
1376 retval = (new_dims == arg.dims ()) ? arg : arg.reshape (new_dims); |
|
1377 else |
|
1378 error ("reshape: size mismatch"); |
|
1379 |
|
1380 return retval; |
|
1381 } |
|
1382 |
4532
|
1383 DEFUN (squeeze, args, , |
|
1384 "-*- texinfo -*-\n\ |
|
1385 @deftypefn {Built-in Function} {} squeeze (@var{x})\n\ |
|
1386 Remove singleton dimensions from @var{x} and return the result.\n\ |
|
1387 @end deftypefn") |
|
1388 { |
|
1389 octave_value retval; |
|
1390 |
|
1391 if (args.length () == 1) |
4545
|
1392 retval = args(0).squeeze (); |
4532
|
1393 else |
|
1394 print_usage ("squeeze"); |
|
1395 |
|
1396 return retval; |
|
1397 } |
|
1398 |
2184
|
1399 void |
|
1400 symbols_of_data (void) |
|
1401 { |
3321
|
1402 |
|
1403 #define IMAGINARY_DOC_STRING "-*- texinfo -*-\n\ |
|
1404 @defvr {Built-in Variable} I\n\ |
|
1405 @defvrx {Built-in Variable} J\n\ |
|
1406 @defvrx {Built-in Variable} i\n\ |
|
1407 @defvrx {Built-in Variable} j\n\ |
|
1408 A pure imaginary number, defined as\n\ |
|
1409 @iftex\n\ |
|
1410 @tex\n\ |
|
1411 $\\sqrt{-1}$.\n\ |
|
1412 @end tex\n\ |
|
1413 @end iftex\n\ |
|
1414 @ifinfo\n\ |
|
1415 @code{sqrt (-1)}.\n\ |
|
1416 @end ifinfo\n\ |
|
1417 The @code{I} and @code{J} forms are true constants, and cannot be\n\ |
|
1418 modified. The @code{i} and @code{j} forms are like ordinary variables,\n\ |
|
1419 and may be used for other purposes. However, unlike other variables,\n\ |
|
1420 they once again assume their special predefined values if they are\n\ |
|
1421 cleared @xref{Status of Variables}.\n\ |
|
1422 @end defvr" |
|
1423 |
|
1424 #define INFINITY_DOC_STRING "-*- texinfo -*-\n\ |
|
1425 @defvr {Built-in Variable} Inf\n\ |
|
1426 @defvrx {Built-in Variable} inf\n\ |
|
1427 Infinity. This is the result of an operation like 1/0, or an operation\n\ |
|
1428 that results in a floating point overflow.\n\ |
|
1429 @end defvr" |
|
1430 |
|
1431 #define NAN_DOC_STRING "-*- texinfo -*-\n\ |
|
1432 @defvr {Built-in Variable} NaN\n\ |
|
1433 @defvrx {Built-in Variable} nan\n\ |
|
1434 Not a number. This is the result of an operation like\n\ |
|
1435 @iftex\n\ |
|
1436 @tex\n\ |
|
1437 $0/0$, or $\\infty - \\infty$,\n\ |
|
1438 @end tex\n\ |
|
1439 @end iftex\n\ |
|
1440 @ifinfo\n\ |
|
1441 0/0, or @samp{Inf - Inf},\n\ |
|
1442 @end ifinfo\n\ |
|
1443 or any operation with a NaN.\n\ |
|
1444 \n\ |
|
1445 Note that NaN always compares not equal to NaN. This behavior is\n\ |
|
1446 specified by the IEEE standard for floating point arithmetic. To\n\ |
|
1447 find NaN values, you must use the @code{isnan} function.\n\ |
|
1448 @end defvr" |
|
1449 |
3141
|
1450 DEFCONST (I, Complex (0.0, 1.0), |
3321
|
1451 IMAGINARY_DOC_STRING); |
2184
|
1452 |
4102
|
1453 DEFCONST (Inf, lo_ieee_inf_value (), |
3321
|
1454 INFINITY_DOC_STRING); |
2184
|
1455 |
3141
|
1456 DEFCONST (J, Complex (0.0, 1.0), |
3321
|
1457 IMAGINARY_DOC_STRING); |
2184
|
1458 |
4102
|
1459 DEFCONST (NA, lo_ieee_na_value (), |
4025
|
1460 "-*- texinfo -*-\n\ |
|
1461 @defvr {Built-in Variable} NA\n\ |
|
1462 Missing value.\n\ |
|
1463 @end defvr"); |
|
1464 |
4102
|
1465 DEFCONST (NaN, lo_ieee_nan_value (), |
3321
|
1466 NAN_DOC_STRING); |
2184
|
1467 |
|
1468 #if defined (M_E) |
|
1469 double e_val = M_E; |
|
1470 #else |
|
1471 double e_val = exp (1.0); |
|
1472 #endif |
|
1473 |
3141
|
1474 DEFCONST (e, e_val, |
3321
|
1475 "-*- texinfo -*-\n\ |
|
1476 @defvr {Built-in Variable} e\n\ |
|
1477 The base of natural logarithms. The constant\n\ |
|
1478 @iftex\n\ |
|
1479 @tex\n\ |
|
1480 $e$\n\ |
|
1481 @end tex\n\ |
|
1482 @end iftex\n\ |
|
1483 @ifinfo\n\ |
|
1484 @var{e}\n\ |
|
1485 @end ifinfo\n\ |
|
1486 satisfies the equation\n\ |
|
1487 @iftex\n\ |
|
1488 @tex\n\ |
|
1489 $\\log (e) = 1$.\n\ |
|
1490 @end tex\n\ |
|
1491 @end iftex\n\ |
|
1492 @ifinfo\n\ |
|
1493 @code{log} (@var{e}) = 1.\n\ |
|
1494 @end ifinfo\n\ |
|
1495 @end defvr"); |
2184
|
1496 |
3141
|
1497 DEFCONST (eps, DBL_EPSILON, |
3321
|
1498 "-*- texinfo -*-\n\ |
|
1499 @defvr {Built-in Variable} eps\n\ |
|
1500 The machine precision. More precisely, @code{eps} is the largest\n\ |
|
1501 relative spacing between any two adjacent numbers in the machine's\n\ |
|
1502 floating point system. This number is obviously system-dependent. On\n\ |
|
1503 machines that support 64 bit IEEE floating point arithmetic, @code{eps}\n\ |
|
1504 is approximately\n\ |
|
1505 @ifinfo\n\ |
|
1506 2.2204e-16.\n\ |
|
1507 @end ifinfo\n\ |
|
1508 @iftex\n\ |
|
1509 @tex\n\ |
|
1510 $2.2204\\times10^{-16}$.\n\ |
|
1511 @end tex\n\ |
|
1512 @end iftex\n\ |
|
1513 @end defvr"); |
2184
|
1514 |
3258
|
1515 DEFCONST (false, false, |
3443
|
1516 "-*- texinfo -*-\n\ |
|
1517 @defvr {Built-in Variable} false\n\ |
|
1518 Logical false value.\n\ |
|
1519 @end defvr"); |
3258
|
1520 |
3141
|
1521 DEFCONST (i, Complex (0.0, 1.0), |
3321
|
1522 IMAGINARY_DOC_STRING); |
2184
|
1523 |
4102
|
1524 DEFCONST (inf, lo_ieee_inf_value (), |
3321
|
1525 INFINITY_DOC_STRING); |
2184
|
1526 |
3141
|
1527 DEFCONST (j, Complex (0.0, 1.0), |
3321
|
1528 IMAGINARY_DOC_STRING); |
2184
|
1529 |
4102
|
1530 DEFCONST (nan, lo_ieee_nan_value (), |
3321
|
1531 NAN_DOC_STRING); |
2184
|
1532 |
|
1533 #if defined (M_PI) |
|
1534 double pi_val = M_PI; |
|
1535 #else |
|
1536 double pi_val = 4.0 * atan (1.0); |
|
1537 #endif |
|
1538 |
3141
|
1539 DEFCONST (pi, pi_val, |
3321
|
1540 "-*- texinfo -*-\n\ |
|
1541 @defvr {Built-in Variable} pi\n\ |
|
1542 The ratio of the circumference of a circle to its diameter.\n\ |
|
1543 Internally, @code{pi} is computed as @samp{4.0 * atan (1.0)}.\n\ |
|
1544 @end defvr"); |
2184
|
1545 |
3141
|
1546 DEFCONST (realmax, DBL_MAX, |
3321
|
1547 "-*- texinfo -*-\n\ |
|
1548 @defvr {Built-in Variable} realmax\n\ |
|
1549 The largest floating point number that is representable. The actual\n\ |
4303
|
1550 value is system-dependent. On machines that support 64-bit IEEE\n\ |
3321
|
1551 floating point arithmetic, @code{realmax} is approximately\n\ |
|
1552 @ifinfo\n\ |
|
1553 1.7977e+308\n\ |
|
1554 @end ifinfo\n\ |
|
1555 @iftex\n\ |
|
1556 @tex\n\ |
|
1557 $1.7977\\times10^{308}$.\n\ |
|
1558 @end tex\n\ |
|
1559 @end iftex\n\ |
|
1560 @end defvr"); |
2184
|
1561 |
3141
|
1562 DEFCONST (realmin, DBL_MIN, |
3321
|
1563 "-*- texinfo -*-\n\ |
|
1564 @defvr {Built-in Variable} realmin\n\ |
4303
|
1565 The smallest normalized floating point number that is representable.\n\ |
|
1566 The actual value is system-dependent. On machines that support\n\ |
|
1567 64-bit IEEE floating point arithmetic, @code{realmin} is approximately\n\ |
3321
|
1568 @ifinfo\n\ |
|
1569 2.2251e-308\n\ |
|
1570 @end ifinfo\n\ |
|
1571 @iftex\n\ |
|
1572 @tex\n\ |
|
1573 $2.2251\\times10^{-308}$.\n\ |
|
1574 @end tex\n\ |
|
1575 @end iftex\n\ |
|
1576 @end defvr"); |
2188
|
1577 |
3258
|
1578 DEFCONST (true, true, |
3443
|
1579 "-*- texinfo -*-\n\ |
|
1580 @defvr {Built-in Variable} true\n\ |
|
1581 Logical true value.\n\ |
|
1582 @end defvr"); |
3354
|
1583 |
2184
|
1584 } |
|
1585 |
523
|
1586 /* |
|
1587 ;;; Local Variables: *** |
|
1588 ;;; mode: C++ *** |
|
1589 ;;; End: *** |
|
1590 */ |