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\ |
|
267 Compute the floating point remainder of @var{y} / @var{x} using the C\n\ |
|
268 library function @code{fmod}. The result has the same sign as @var{x}.\n\ |
|
269 If @var{y} is zero, the result implementation-defined.\n\ |
|
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 { \ |
|
373 Matrix tmp = arg.matrix_value (); \ |
|
374 \ |
|
375 if (! error_state) \ |
4233
|
376 retval = tmp.FCN (dim); \ |
3723
|
377 } \ |
|
378 else if (arg.is_complex_type ()) \ |
|
379 { \ |
|
380 ComplexMatrix tmp = arg.complex_matrix_value (); \ |
|
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 |
3202
|
570 int k = b.nint_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 |
3195
|
668 DEFUN (length, args, , |
3373
|
669 "-*- texinfo -*-\n\ |
|
670 @deftypefn {Built-in Function} {} length (@var{a})\n\ |
4176
|
671 Return the `length' of the object @var{a}. For matrix objects, the\n\ |
3373
|
672 length is the number of rows or columns, whichever is greater (this\n\ |
|
673 odd definition is used for compatibility with Matlab).\n\ |
|
674 @end deftypefn") |
3195
|
675 { |
|
676 octave_value retval; |
|
677 |
|
678 if (args.length () == 1) |
|
679 { |
|
680 int len = args(0).length (); |
|
681 |
|
682 if (! error_state) |
4233
|
683 retval = len; |
3195
|
684 } |
|
685 else |
|
686 print_usage ("length"); |
|
687 |
|
688 return retval; |
|
689 } |
|
690 |
4554
|
691 DEFUN (ndims, args, , |
|
692 "-*- texinfo -*-\n\ |
|
693 @deftypefn {Built-in Function} {} ndims (@var{a})\n\ |
|
694 Returns the number of dimensions of array @var{a}.\n\ |
|
695 For any array, the result will always be larger than or equal to 2.\n\ |
|
696 Trailing singleton dimensions are not counted.\n\ |
|
697 @end deftypefn") |
|
698 { |
|
699 octave_value retval; |
|
700 |
|
701 if (args.length () == 1) |
|
702 { |
|
703 int n_dims = args(0).ndims (); |
|
704 |
|
705 if (! error_state) |
|
706 retval = n_dims; |
|
707 } |
|
708 else |
|
709 print_usage ("ndims"); |
|
710 |
|
711 return retval; |
|
712 } |
|
713 |
4559
|
714 DEFUN (numel, args, , |
|
715 "-*- texinfo -*-\n\ |
|
716 @deftypefn {Built-in Function} {} numel (@var{a})\n\ |
|
717 Returns the number of elements in the object @var{a}.\n\ |
|
718 @end deftypefn") |
|
719 { |
|
720 octave_value retval; |
|
721 |
|
722 if (args.length () == 1) |
|
723 { |
|
724 int numel = args(0).numel (); |
|
725 |
|
726 if (! error_state) |
|
727 { |
|
728 if (numel < 0) |
|
729 numel = 0; |
|
730 |
|
731 retval = numel; |
|
732 } |
|
733 } |
|
734 else |
|
735 print_usage ("numel"); |
|
736 |
|
737 return retval; |
|
738 } |
|
739 |
1957
|
740 DEFUN (size, args, nargout, |
3373
|
741 "-*- texinfo -*-\n\ |
|
742 @deftypefn {Built-in Function} {} size (@var{a}, @var{n})\n\ |
|
743 Return the number rows and columns of @var{a}.\n\ |
|
744 \n\ |
|
745 With one input argument and one output argument, the result is returned\n\ |
|
746 in a 2 element row vector. If there are two output arguments, the\n\ |
|
747 number of rows is assigned to the first, and the number of columns to\n\ |
|
748 the second. For example,\n\ |
|
749 \n\ |
|
750 @example\n\ |
|
751 @group\n\ |
|
752 size ([1, 2; 3, 4; 5, 6])\n\ |
|
753 @result{} [ 3, 2 ]\n\ |
1031
|
754 \n\ |
3373
|
755 [nr, nc] = size ([1, 2; 3, 4; 5, 6])\n\ |
|
756 @result{} nr = 3\n\ |
|
757 @result{} nc = 2\n\ |
|
758 @end group\n\ |
|
759 @end example\n\ |
|
760 \n\ |
|
761 If given a second argument of either 1 or 2, @code{size} will return\n\ |
|
762 only the row or column dimension. For example\n\ |
1031
|
763 \n\ |
3373
|
764 @example\n\ |
|
765 size ([1, 2; 3, 4; 5, 6], 2)\n\ |
|
766 @result{} 2\n\ |
|
767 @end example\n\ |
|
768 \n\ |
|
769 @noindent\n\ |
|
770 returns the number of columns in the given matrix.\n\ |
|
771 @end deftypefn") |
523
|
772 { |
2086
|
773 octave_value_list retval; |
523
|
774 |
|
775 int nargin = args.length (); |
|
776 |
4513
|
777 if (nargin == 1) |
523
|
778 { |
4513
|
779 dim_vector dimensions = args(0).dims (); |
|
780 |
|
781 int ndims = dimensions.length (); |
1031
|
782 |
4513
|
783 Matrix m (1, ndims); |
|
784 |
|
785 if (nargout > 1) |
523
|
786 { |
4513
|
787 while (ndims--) |
|
788 retval(ndims) = dimensions(ndims); |
523
|
789 } |
4513
|
790 else |
712
|
791 { |
4513
|
792 for (int i = 0; i < ndims; i++) |
|
793 m(0, i) = dimensions(i); |
|
794 |
|
795 retval(0) = m; |
712
|
796 } |
1031
|
797 } |
|
798 else if (nargin == 2 && nargout < 2) |
|
799 { |
3202
|
800 int nd = args(1).nint_value (); |
1031
|
801 |
|
802 if (error_state) |
|
803 error ("size: expecting scalar as second argument"); |
712
|
804 else |
1031
|
805 { |
|
806 if (nd == 1) |
4233
|
807 retval(0) = args(0).rows (); |
1031
|
808 else if (nd == 2) |
4233
|
809 retval(0) = args(0).columns (); |
1031
|
810 else |
|
811 error ("size: invalid second argument -- expecting 1 or 2"); |
|
812 } |
523
|
813 } |
712
|
814 else |
|
815 print_usage ("size"); |
523
|
816 |
|
817 return retval; |
|
818 } |
|
819 |
1957
|
820 DEFUN (sum, args, , |
3428
|
821 "-*- texinfo -*-\n\ |
3723
|
822 @deftypefn {Built-in Function} {} sum (@var{x}, @var{dim})\n\ |
|
823 Sum of elements along dimension @var{dim}. If @var{dim} is\n\ |
|
824 omitted, it defaults to 1 (column-wise sum).\n\ |
3428
|
825 @end deftypefn") |
523
|
826 { |
3723
|
827 DATA_REDUCTION (sum); |
523
|
828 } |
|
829 |
1957
|
830 DEFUN (sumsq, args, , |
3428
|
831 "-*- texinfo -*-\n\ |
3723
|
832 @deftypefn {Built-in Function} {} sumsq (@var{x}, @var{dim})\n\ |
|
833 Sum of squares of elements along dimension @var{dim}. If @var{dim}\n\ |
|
834 is omitted, it defaults to 1 (column-wise sum of squares).\n\ |
3095
|
835 \n\ |
|
836 This function is equivalent to computing\n\ |
3723
|
837 @example\n\ |
|
838 sum (x .* conj (x), dim)\n\ |
|
839 @end example\n\ |
|
840 but it uses less memory and avoids calling conj if @var{x} is real.\n\ |
3428
|
841 @end deftypefn") |
523
|
842 { |
3723
|
843 DATA_REDUCTION (sumsq); |
523
|
844 } |
|
845 |
4028
|
846 DEFUN (isbool, args, , |
3428
|
847 "-*- texinfo -*-\n\ |
4028
|
848 @deftypefn {Built-in Functio} {} isbool (@var{x})\n\ |
3428
|
849 Return true if @var{x} is a boolean object.\n\ |
3439
|
850 @end deftypefn") |
3209
|
851 { |
|
852 octave_value retval; |
|
853 |
|
854 if (args.length () == 1) |
3258
|
855 retval = args(0).is_bool_type (); |
3209
|
856 else |
4028
|
857 print_usage ("isbool"); |
3209
|
858 |
|
859 return retval; |
|
860 } |
|
861 |
4028
|
862 DEFALIAS (islogical, isbool); |
3209
|
863 |
4028
|
864 DEFUN (iscomplex, args, , |
3428
|
865 "-*- texinfo -*-\n\ |
4028
|
866 @deftypefn {Built-in Function} {} iscomplex (@var{x})\n\ |
3428
|
867 Return true if @var{x} is a complex-valued numeric object.\n\ |
|
868 @end deftypefn") |
3186
|
869 { |
|
870 octave_value retval; |
|
871 |
|
872 if (args.length () == 1) |
3258
|
873 retval = args(0).is_complex_type (); |
3186
|
874 else |
4028
|
875 print_usage ("iscomplex"); |
3186
|
876 |
|
877 return retval; |
|
878 } |
|
879 |
3258
|
880 DEFUN (isreal, args, , |
3428
|
881 "-*- texinfo -*-\n\ |
|
882 @deftypefn {Built-in Function} {} isreal (@var{x})\n\ |
|
883 Return true if @var{x} is a real-valued numeric object.\n\ |
|
884 @end deftypefn") |
3258
|
885 { |
|
886 octave_value retval; |
|
887 |
|
888 if (args.length () == 1) |
|
889 retval = args(0).is_real_type (); |
|
890 else |
|
891 print_usage ("isreal"); |
|
892 |
|
893 return retval; |
|
894 } |
|
895 |
3202
|
896 DEFUN (isempty, args, , |
3373
|
897 "-*- texinfo -*-\n\ |
|
898 @deftypefn {Built-in Function} {} isempty (@var{a})\n\ |
|
899 Return 1 if @var{a} is an empty matrix (either the number of rows, or\n\ |
|
900 the number of columns, or both are zero). Otherwise, return 0.\n\ |
|
901 @end deftypefn") |
3202
|
902 { |
4233
|
903 octave_value retval = false; |
3202
|
904 |
|
905 if (args.length () == 1) |
4559
|
906 retval = args(0).is_empty (); |
3202
|
907 else |
|
908 print_usage ("isempty"); |
|
909 |
|
910 return retval; |
|
911 } |
|
912 |
3206
|
913 DEFUN (isnumeric, args, , |
3428
|
914 "-*- texinfo -*-\n\ |
|
915 @deftypefn {Built-in Function} {} isnumeric (@var{x})\n\ |
|
916 Return nonzero if @var{x} is a numeric object.\n\ |
|
917 @end deftypefn") |
3206
|
918 { |
|
919 octave_value retval; |
|
920 |
|
921 if (args.length () == 1) |
3258
|
922 retval = args(0).is_numeric_type (); |
3206
|
923 else |
3238
|
924 print_usage ("isnumeric"); |
3206
|
925 |
|
926 return retval; |
|
927 } |
|
928 |
4028
|
929 DEFUN (islist, args, , |
3526
|
930 "-*- texinfo -*-\n\ |
4028
|
931 @deftypefn {Built-in Function} {} islist (@var{x})\n\ |
3428
|
932 Return nonzero if @var{x} is a list.\n\ |
|
933 @end deftypefn") |
3204
|
934 { |
|
935 octave_value retval; |
|
936 |
|
937 if (args.length () == 1) |
3258
|
938 retval = args(0).is_list (); |
3204
|
939 else |
4028
|
940 print_usage ("islist"); |
3204
|
941 |
|
942 return retval; |
|
943 } |
|
944 |
4028
|
945 DEFUN (ismatrix, args, , |
3321
|
946 "-*- texinfo -*-\n\ |
4028
|
947 @deftypefn {Built-in Function} {} ismatrix (@var{a})\n\ |
3321
|
948 Return 1 if @var{a} is a matrix. Otherwise, return 0.\n\ |
3333
|
949 @end deftypefn") |
3202
|
950 { |
4233
|
951 octave_value retval = false; |
3202
|
952 |
|
953 if (args.length () == 1) |
|
954 { |
|
955 octave_value arg = args(0); |
|
956 |
3212
|
957 if (arg.is_scalar_type () || arg.is_range ()) |
4233
|
958 retval = true; |
3202
|
959 else if (arg.is_matrix_type ()) |
4233
|
960 retval = (arg.rows () >= 1 && arg.columns () >= 1); |
3202
|
961 } |
|
962 else |
4028
|
963 print_usage ("ismatrix"); |
3202
|
964 |
|
965 return retval; |
|
966 } |
|
967 |
3354
|
968 static octave_value |
|
969 fill_matrix (const octave_value_list& args, double val, const char *fcn) |
523
|
970 { |
3354
|
971 octave_value retval; |
523
|
972 |
|
973 int nargin = args.length (); |
|
974 |
4481
|
975 int ndim = 0; |
|
976 int type = 0; |
|
977 |
4513
|
978 dim_vector dims; |
4481
|
979 |
|
980 // Check for type information. |
|
981 |
|
982 if (nargin > 0 && args(nargin-1).is_string ()) |
|
983 { |
|
984 nargin--; |
|
985 |
|
986 // XXX FIXME XXX -- allow type of the resulting matrix to be |
|
987 // specified, e.g. |
|
988 // |
|
989 // zeros(n1, n2, ..., 'real') |
|
990 // zeros(n1, n2, ..., 'complex') |
|
991 // |
|
992 // type = get_type (args(nargin).string_value ()); |
|
993 } |
|
994 |
|
995 // determine matrix dimension |
|
996 |
523
|
997 switch (nargin) |
|
998 { |
712
|
999 case 0: |
4481
|
1000 ndim = 0; |
|
1001 type = 0; |
712
|
1002 break; |
777
|
1003 |
610
|
1004 case 1: |
4481
|
1005 get_dimensions (args(0), fcn, dims); |
610
|
1006 break; |
777
|
1007 |
4563
|
1008 default: |
|
1009 { |
|
1010 dims.resize (nargin); |
4481
|
1011 |
4563
|
1012 for (int i = 0; i < nargin; i++) |
|
1013 { |
|
1014 dims(i) = args(i).is_empty () ? 0 : args(i).nint_value (); |
4481
|
1015 |
4563
|
1016 if (error_state) |
|
1017 { |
|
1018 error ("%s: expecting scalar arguments", fcn); |
|
1019 break; |
|
1020 } |
|
1021 } |
|
1022 } |
|
1023 break; |
4481
|
1024 } |
|
1025 |
|
1026 if (! error_state) |
|
1027 { |
|
1028 ndim = dims.length (); |
|
1029 |
4565
|
1030 for (int i = ndim-1; i > 1; i--) |
|
1031 { |
|
1032 if (dims(i) == 1) |
|
1033 ndim--; |
|
1034 else |
|
1035 break; |
|
1036 } |
|
1037 |
|
1038 dims.resize (ndim); |
|
1039 |
4481
|
1040 check_dimensions (dims, fcn); |
3354
|
1041 |
4481
|
1042 if (! error_state) |
|
1043 { |
|
1044 // Construct either scalar, matrix or N-d array. |
|
1045 |
|
1046 switch (ndim) |
|
1047 { |
|
1048 case 0: |
|
1049 retval = val; |
|
1050 break; |
777
|
1051 |
4481
|
1052 case 1: |
|
1053 retval = Matrix (dims(0), dims(0), val); |
|
1054 break; |
|
1055 |
|
1056 case 2: |
|
1057 retval = Matrix (dims(0), dims(1), val); |
|
1058 break; |
|
1059 |
|
1060 default: |
4512
|
1061 retval = NDArray (dims, val); |
4481
|
1062 break; |
|
1063 } |
|
1064 } |
523
|
1065 } |
|
1066 |
|
1067 return retval; |
|
1068 } |
|
1069 |
3354
|
1070 DEFUN (ones, args, , |
3369
|
1071 "-*- texinfo -*-\n\ |
|
1072 @deftypefn {Built-in Function} {} ones (@var{x})\n\ |
|
1073 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m})\n\ |
4481
|
1074 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m}, @var{k},...)\n\ |
|
1075 Return a matrix or N-dimensional array whose elements are all 1.\n\ |
|
1076 The arguments are handled the same as the arguments for @code{eye}.\n\ |
3369
|
1077 \n\ |
|
1078 If you need to create a matrix whose values are all the same, you should\n\ |
|
1079 use an expression like\n\ |
|
1080 \n\ |
|
1081 @example\n\ |
|
1082 val_matrix = val * ones (n, m)\n\ |
|
1083 @end example\n\ |
|
1084 @end deftypefn") |
523
|
1085 { |
3354
|
1086 return fill_matrix (args, 1.0, "ones"); |
523
|
1087 } |
|
1088 |
3354
|
1089 DEFUN (zeros, args, , |
3369
|
1090 "-*- texinfo -*-\n\ |
|
1091 @deftypefn {Built-in Function} {} zeros (@var{x})\n\ |
|
1092 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m})\n\ |
4481
|
1093 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m}, @var{k},...)\n\ |
|
1094 Return a matrix or N-dimensional array whose elements are all 0.\n\ |
|
1095 The arguments are handled the same as the arguments for @code{eye}.\n\ |
3369
|
1096 @end deftypefn") |
523
|
1097 { |
3354
|
1098 return fill_matrix (args, 0.0, "zeros"); |
|
1099 } |
523
|
1100 |
1957
|
1101 DEFUN (eye, args, , |
3369
|
1102 "-*- texinfo -*-\n\ |
|
1103 @deftypefn {Built-in Function} {} eye (@var{x})\n\ |
|
1104 @deftypefnx {Built-in Function} {} eye (@var{n}, @var{m})\n\ |
|
1105 Return an identity matrix. If invoked with a single scalar argument,\n\ |
|
1106 @code{eye} returns a square matrix with the dimension specified. If you\n\ |
|
1107 supply two scalar arguments, @code{eye} takes them to be the number of\n\ |
|
1108 rows and columns. If given a vector with two elements, @code{eye} uses\n\ |
|
1109 the values of the elements as the number of rows and columns,\n\ |
|
1110 respectively. For example,\n\ |
|
1111 \n\ |
|
1112 @example\n\ |
|
1113 @group\n\ |
|
1114 eye (3)\n\ |
|
1115 @result{} 1 0 0\n\ |
|
1116 0 1 0\n\ |
|
1117 0 0 1\n\ |
|
1118 @end group\n\ |
|
1119 @end example\n\ |
|
1120 \n\ |
|
1121 The following expressions all produce the same result:\n\ |
|
1122 \n\ |
|
1123 @example\n\ |
|
1124 @group\n\ |
|
1125 eye (2)\n\ |
|
1126 @equiv{}\n\ |
|
1127 eye (2, 2)\n\ |
|
1128 @equiv{}\n\ |
|
1129 eye (size ([1, 2; 3, 4])\n\ |
|
1130 @end group\n\ |
|
1131 @end example\n\ |
|
1132 \n\ |
|
1133 For compatibility with @sc{Matlab}, calling @code{eye} with no arguments\n\ |
|
1134 is equivalent to calling it with an argument of 1.\n\ |
|
1135 @end deftypefn") |
523
|
1136 { |
3354
|
1137 octave_value retval; |
523
|
1138 |
|
1139 int nargin = args.length (); |
|
1140 |
|
1141 switch (nargin) |
|
1142 { |
712
|
1143 case 0: |
|
1144 retval = 1.0; |
|
1145 break; |
777
|
1146 |
610
|
1147 case 1: |
3354
|
1148 { |
|
1149 int nr, nc; |
|
1150 get_dimensions (args(0), "eye", nr, nc); |
|
1151 |
|
1152 if (! error_state) |
|
1153 retval = identity_matrix (nr, nc); |
|
1154 } |
610
|
1155 break; |
777
|
1156 |
523
|
1157 case 2: |
3354
|
1158 { |
|
1159 int nr, nc; |
|
1160 get_dimensions (args(0), args(1), "eye", nr, nc); |
|
1161 |
|
1162 if (! error_state) |
|
1163 retval = identity_matrix (nr, nc); |
|
1164 } |
523
|
1165 break; |
777
|
1166 |
523
|
1167 default: |
|
1168 print_usage ("eye"); |
|
1169 break; |
|
1170 } |
|
1171 |
|
1172 return retval; |
|
1173 } |
|
1174 |
1957
|
1175 DEFUN (linspace, args, , |
3369
|
1176 "-*- texinfo -*-\n\ |
|
1177 @deftypefn {Built-in Function} {} linspace (@var{base}, @var{limit}, @var{n})\n\ |
|
1178 Return a row vector with @var{n} linearly spaced elements between\n\ |
|
1179 @var{base} and @var{limit}. The number of elements, @var{n}, must be\n\ |
|
1180 greater than 1. The @var{base} and @var{limit} are always included in\n\ |
|
1181 the range. If @var{base} is greater than @var{limit}, the elements are\n\ |
|
1182 stored in decreasing order. If the number of points is not specified, a\n\ |
|
1183 value of 100 is used.\n\ |
1100
|
1184 \n\ |
4455
|
1185 The @code{linspace} function always returns a row vector.\n\ |
3369
|
1186 @end deftypefn") |
1100
|
1187 { |
3418
|
1188 octave_value retval; |
1100
|
1189 |
|
1190 int nargin = args.length (); |
|
1191 |
|
1192 int npoints = 100; |
|
1193 |
1940
|
1194 if (nargin != 2 && nargin != 3) |
|
1195 { |
|
1196 print_usage ("linspace"); |
|
1197 return retval; |
|
1198 } |
|
1199 |
1100
|
1200 if (nargin == 3) |
3202
|
1201 npoints = args(2).nint_value (); |
1100
|
1202 |
|
1203 if (! error_state) |
|
1204 { |
3322
|
1205 octave_value arg_1 = args(0); |
|
1206 octave_value arg_2 = args(1); |
1100
|
1207 |
3322
|
1208 if (arg_1.is_complex_type () || arg_2.is_complex_type ()) |
|
1209 { |
|
1210 Complex x1 = arg_1.complex_value (); |
|
1211 Complex x2 = arg_2.complex_value (); |
|
1212 |
|
1213 if (! error_state) |
1100
|
1214 { |
3322
|
1215 ComplexRowVector rv = linspace (x1, x2, npoints); |
1100
|
1216 |
|
1217 if (! error_state) |
3418
|
1218 retval = rv; |
1100
|
1219 } |
|
1220 } |
|
1221 else |
3322
|
1222 { |
|
1223 double x1 = arg_1.double_value (); |
|
1224 double x2 = arg_2.double_value (); |
|
1225 |
|
1226 if (! error_state) |
|
1227 { |
|
1228 RowVector rv = linspace (x1, x2, npoints); |
|
1229 |
|
1230 if (! error_state) |
3418
|
1231 retval = rv; |
3322
|
1232 } |
|
1233 } |
1100
|
1234 } |
|
1235 |
|
1236 return retval; |
|
1237 } |
|
1238 |
4567
|
1239 DEFUN (reshape, args, , |
|
1240 "-*- texinfo -*-\n\ |
|
1241 @deftypefn {Function File} {} reshape (@var{a}, @var{m}, @var{n}, @dots{})\n\ |
|
1242 @deftypefnx {Function File} {} reshape (@var{a}, @var{siz})\n\ |
|
1243 Return a matrix with the given dimensions whose elements are taken\n\ |
|
1244 from the matrix @var{a}. The elements of the matrix are access in\n\ |
|
1245 column-major order (like Fortran arrays are stored).\n\ |
|
1246 \n\ |
|
1247 For example,\n\ |
|
1248 \n\ |
|
1249 @example\n\ |
|
1250 @group\n\ |
|
1251 reshape ([1, 2, 3, 4], 2, 2)\n\ |
|
1252 @result{} 1 3\n\ |
|
1253 2 4\n\ |
|
1254 @end group\n\ |
|
1255 @end example\n\ |
|
1256 \n\ |
|
1257 @noindent\n\ |
|
1258 Note that the total number of elements in the original\n\ |
|
1259 matrix must match the total number of elements in the new matrix.\n\ |
|
1260 @end deftypefn") |
|
1261 { |
|
1262 octave_value retval; |
|
1263 |
|
1264 int nargin = args.length (); |
|
1265 |
|
1266 Array<int> new_size; |
|
1267 |
|
1268 if (nargin == 2) |
|
1269 new_size = args(1).int_vector_value (); |
|
1270 else if (nargin > 2) |
|
1271 { |
|
1272 new_size.resize (nargin-1); |
|
1273 |
|
1274 for (int i = 1; i < nargin; i++) |
|
1275 { |
|
1276 new_size(i-1) = args(i).int_value (); |
|
1277 |
|
1278 if (error_state) |
|
1279 break; |
|
1280 } |
|
1281 } |
|
1282 else |
|
1283 { |
|
1284 print_usage ("reshape"); |
|
1285 return retval; |
|
1286 } |
|
1287 |
|
1288 if (error_state) |
|
1289 { |
|
1290 error ("reshape: invalid arguments"); |
|
1291 return retval; |
|
1292 } |
|
1293 |
|
1294 int n = new_size.length (); |
|
1295 |
|
1296 if (n < 2) |
|
1297 { |
|
1298 error ("reshape: expecting size to be vector with at least 2 elements"); |
|
1299 return retval; |
|
1300 } |
|
1301 |
|
1302 dim_vector new_dims; |
|
1303 |
|
1304 new_dims.resize (n); |
|
1305 |
|
1306 for (int i = 0; i < n; i++) |
|
1307 new_dims(i) = new_size(i); |
|
1308 |
|
1309 octave_value arg = args(0); |
|
1310 |
|
1311 if (new_dims.numel () == arg.numel ()) |
|
1312 retval = (new_dims == arg.dims ()) ? arg : arg.reshape (new_dims); |
|
1313 else |
|
1314 error ("reshape: size mismatch"); |
|
1315 |
|
1316 return retval; |
|
1317 } |
|
1318 |
4532
|
1319 DEFUN (squeeze, args, , |
|
1320 "-*- texinfo -*-\n\ |
|
1321 @deftypefn {Built-in Function} {} squeeze (@var{x})\n\ |
|
1322 Remove singleton dimensions from @var{x} and return the result.\n\ |
|
1323 @end deftypefn") |
|
1324 { |
|
1325 octave_value retval; |
|
1326 |
|
1327 if (args.length () == 1) |
4545
|
1328 retval = args(0).squeeze (); |
4532
|
1329 else |
|
1330 print_usage ("squeeze"); |
|
1331 |
|
1332 return retval; |
|
1333 } |
|
1334 |
2184
|
1335 void |
|
1336 symbols_of_data (void) |
|
1337 { |
3321
|
1338 |
|
1339 #define IMAGINARY_DOC_STRING "-*- texinfo -*-\n\ |
|
1340 @defvr {Built-in Variable} I\n\ |
|
1341 @defvrx {Built-in Variable} J\n\ |
|
1342 @defvrx {Built-in Variable} i\n\ |
|
1343 @defvrx {Built-in Variable} j\n\ |
|
1344 A pure imaginary number, defined as\n\ |
|
1345 @iftex\n\ |
|
1346 @tex\n\ |
|
1347 $\\sqrt{-1}$.\n\ |
|
1348 @end tex\n\ |
|
1349 @end iftex\n\ |
|
1350 @ifinfo\n\ |
|
1351 @code{sqrt (-1)}.\n\ |
|
1352 @end ifinfo\n\ |
|
1353 The @code{I} and @code{J} forms are true constants, and cannot be\n\ |
|
1354 modified. The @code{i} and @code{j} forms are like ordinary variables,\n\ |
|
1355 and may be used for other purposes. However, unlike other variables,\n\ |
|
1356 they once again assume their special predefined values if they are\n\ |
|
1357 cleared @xref{Status of Variables}.\n\ |
|
1358 @end defvr" |
|
1359 |
|
1360 #define INFINITY_DOC_STRING "-*- texinfo -*-\n\ |
|
1361 @defvr {Built-in Variable} Inf\n\ |
|
1362 @defvrx {Built-in Variable} inf\n\ |
|
1363 Infinity. This is the result of an operation like 1/0, or an operation\n\ |
|
1364 that results in a floating point overflow.\n\ |
|
1365 @end defvr" |
|
1366 |
|
1367 #define NAN_DOC_STRING "-*- texinfo -*-\n\ |
|
1368 @defvr {Built-in Variable} NaN\n\ |
|
1369 @defvrx {Built-in Variable} nan\n\ |
|
1370 Not a number. This is the result of an operation like\n\ |
|
1371 @iftex\n\ |
|
1372 @tex\n\ |
|
1373 $0/0$, or $\\infty - \\infty$,\n\ |
|
1374 @end tex\n\ |
|
1375 @end iftex\n\ |
|
1376 @ifinfo\n\ |
|
1377 0/0, or @samp{Inf - Inf},\n\ |
|
1378 @end ifinfo\n\ |
|
1379 or any operation with a NaN.\n\ |
|
1380 \n\ |
|
1381 Note that NaN always compares not equal to NaN. This behavior is\n\ |
|
1382 specified by the IEEE standard for floating point arithmetic. To\n\ |
|
1383 find NaN values, you must use the @code{isnan} function.\n\ |
|
1384 @end defvr" |
|
1385 |
3141
|
1386 DEFCONST (I, Complex (0.0, 1.0), |
3321
|
1387 IMAGINARY_DOC_STRING); |
2184
|
1388 |
4102
|
1389 DEFCONST (Inf, lo_ieee_inf_value (), |
3321
|
1390 INFINITY_DOC_STRING); |
2184
|
1391 |
3141
|
1392 DEFCONST (J, Complex (0.0, 1.0), |
3321
|
1393 IMAGINARY_DOC_STRING); |
2184
|
1394 |
4102
|
1395 DEFCONST (NA, lo_ieee_na_value (), |
4025
|
1396 "-*- texinfo -*-\n\ |
|
1397 @defvr {Built-in Variable} NA\n\ |
|
1398 Missing value.\n\ |
|
1399 @end defvr"); |
|
1400 |
4102
|
1401 DEFCONST (NaN, lo_ieee_nan_value (), |
3321
|
1402 NAN_DOC_STRING); |
2184
|
1403 |
|
1404 #if defined (M_E) |
|
1405 double e_val = M_E; |
|
1406 #else |
|
1407 double e_val = exp (1.0); |
|
1408 #endif |
|
1409 |
3141
|
1410 DEFCONST (e, e_val, |
3321
|
1411 "-*- texinfo -*-\n\ |
|
1412 @defvr {Built-in Variable} e\n\ |
|
1413 The base of natural logarithms. The constant\n\ |
|
1414 @iftex\n\ |
|
1415 @tex\n\ |
|
1416 $e$\n\ |
|
1417 @end tex\n\ |
|
1418 @end iftex\n\ |
|
1419 @ifinfo\n\ |
|
1420 @var{e}\n\ |
|
1421 @end ifinfo\n\ |
|
1422 satisfies the equation\n\ |
|
1423 @iftex\n\ |
|
1424 @tex\n\ |
|
1425 $\\log (e) = 1$.\n\ |
|
1426 @end tex\n\ |
|
1427 @end iftex\n\ |
|
1428 @ifinfo\n\ |
|
1429 @code{log} (@var{e}) = 1.\n\ |
|
1430 @end ifinfo\n\ |
|
1431 @end defvr"); |
2184
|
1432 |
3141
|
1433 DEFCONST (eps, DBL_EPSILON, |
3321
|
1434 "-*- texinfo -*-\n\ |
|
1435 @defvr {Built-in Variable} eps\n\ |
|
1436 The machine precision. More precisely, @code{eps} is the largest\n\ |
|
1437 relative spacing between any two adjacent numbers in the machine's\n\ |
|
1438 floating point system. This number is obviously system-dependent. On\n\ |
|
1439 machines that support 64 bit IEEE floating point arithmetic, @code{eps}\n\ |
|
1440 is approximately\n\ |
|
1441 @ifinfo\n\ |
|
1442 2.2204e-16.\n\ |
|
1443 @end ifinfo\n\ |
|
1444 @iftex\n\ |
|
1445 @tex\n\ |
|
1446 $2.2204\\times10^{-16}$.\n\ |
|
1447 @end tex\n\ |
|
1448 @end iftex\n\ |
|
1449 @end defvr"); |
2184
|
1450 |
3258
|
1451 DEFCONST (false, false, |
3443
|
1452 "-*- texinfo -*-\n\ |
|
1453 @defvr {Built-in Variable} false\n\ |
|
1454 Logical false value.\n\ |
|
1455 @end defvr"); |
3258
|
1456 |
3141
|
1457 DEFCONST (i, Complex (0.0, 1.0), |
3321
|
1458 IMAGINARY_DOC_STRING); |
2184
|
1459 |
4102
|
1460 DEFCONST (inf, lo_ieee_inf_value (), |
3321
|
1461 INFINITY_DOC_STRING); |
2184
|
1462 |
3141
|
1463 DEFCONST (j, Complex (0.0, 1.0), |
3321
|
1464 IMAGINARY_DOC_STRING); |
2184
|
1465 |
4102
|
1466 DEFCONST (nan, lo_ieee_nan_value (), |
3321
|
1467 NAN_DOC_STRING); |
2184
|
1468 |
|
1469 #if defined (M_PI) |
|
1470 double pi_val = M_PI; |
|
1471 #else |
|
1472 double pi_val = 4.0 * atan (1.0); |
|
1473 #endif |
|
1474 |
3141
|
1475 DEFCONST (pi, pi_val, |
3321
|
1476 "-*- texinfo -*-\n\ |
|
1477 @defvr {Built-in Variable} pi\n\ |
|
1478 The ratio of the circumference of a circle to its diameter.\n\ |
|
1479 Internally, @code{pi} is computed as @samp{4.0 * atan (1.0)}.\n\ |
|
1480 @end defvr"); |
2184
|
1481 |
3141
|
1482 DEFCONST (realmax, DBL_MAX, |
3321
|
1483 "-*- texinfo -*-\n\ |
|
1484 @defvr {Built-in Variable} realmax\n\ |
|
1485 The largest floating point number that is representable. The actual\n\ |
4303
|
1486 value is system-dependent. On machines that support 64-bit IEEE\n\ |
3321
|
1487 floating point arithmetic, @code{realmax} is approximately\n\ |
|
1488 @ifinfo\n\ |
|
1489 1.7977e+308\n\ |
|
1490 @end ifinfo\n\ |
|
1491 @iftex\n\ |
|
1492 @tex\n\ |
|
1493 $1.7977\\times10^{308}$.\n\ |
|
1494 @end tex\n\ |
|
1495 @end iftex\n\ |
|
1496 @end defvr"); |
2184
|
1497 |
3141
|
1498 DEFCONST (realmin, DBL_MIN, |
3321
|
1499 "-*- texinfo -*-\n\ |
|
1500 @defvr {Built-in Variable} realmin\n\ |
4303
|
1501 The smallest normalized floating point number that is representable.\n\ |
|
1502 The actual value is system-dependent. On machines that support\n\ |
|
1503 64-bit IEEE floating point arithmetic, @code{realmin} is approximately\n\ |
3321
|
1504 @ifinfo\n\ |
|
1505 2.2251e-308\n\ |
|
1506 @end ifinfo\n\ |
|
1507 @iftex\n\ |
|
1508 @tex\n\ |
|
1509 $2.2251\\times10^{-308}$.\n\ |
|
1510 @end tex\n\ |
|
1511 @end iftex\n\ |
|
1512 @end defvr"); |
2188
|
1513 |
3258
|
1514 DEFCONST (true, true, |
3443
|
1515 "-*- texinfo -*-\n\ |
|
1516 @defvr {Built-in Variable} true\n\ |
|
1517 Logical true value.\n\ |
|
1518 @end defvr"); |
3354
|
1519 |
2184
|
1520 } |
|
1521 |
523
|
1522 /* |
|
1523 ;;; Local Variables: *** |
|
1524 ;;; mode: C++ *** |
|
1525 ;;; End: *** |
|
1526 */ |