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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
523
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
523
|
26 #endif |
|
27 |
2184
|
28 #include <cfloat> |
|
29 #include <cmath> |
|
30 |
1728
|
31 #include <string> |
|
32 |
2184
|
33 #include "lo-ieee.h" |
1755
|
34 #include "str-vec.h" |
4153
|
35 #include "quit.h" |
1755
|
36 |
1352
|
37 #include "defun.h" |
|
38 #include "error.h" |
|
39 #include "gripes.h" |
2366
|
40 #include "ov.h" |
5476
|
41 #include "ov-complex.h" |
|
42 #include "ov-cx-mat.h" |
2366
|
43 #include "variables.h" |
1742
|
44 #include "oct-obj.h" |
523
|
45 #include "utils.h" |
4806
|
46 #include "Cell.h" |
|
47 #include "oct-map.h" |
4915
|
48 #include "pt-mat.h" |
523
|
49 |
4015
|
50 #define ANY_ALL(FCN) \ |
|
51 \ |
4233
|
52 octave_value retval; \ |
4015
|
53 \ |
|
54 int nargin = args.length (); \ |
|
55 \ |
4021
|
56 if (nargin == 1 || nargin == 2) \ |
4015
|
57 { \ |
4021
|
58 int dim = (nargin == 1 ? -1 : args(1).int_value (true) - 1); \ |
|
59 \ |
|
60 if (! error_state) \ |
|
61 { \ |
4556
|
62 if (dim >= -1) \ |
4015
|
63 retval = args(0).FCN (dim); \ |
4021
|
64 else \ |
|
65 error (#FCN ": invalid dimension argument = %d", dim + 1); \ |
|
66 } \ |
4015
|
67 else \ |
4021
|
68 error (#FCN ": expecting dimension argument to be an integer"); \ |
4015
|
69 } \ |
4021
|
70 else \ |
|
71 print_usage (#FCN); \ |
4015
|
72 \ |
|
73 return retval |
|
74 |
1957
|
75 DEFUN (all, args, , |
3369
|
76 "-*- texinfo -*-\n\ |
4015
|
77 @deftypefn {Built-in Function} {} all (@var{x}, @var{dim})\n\ |
3369
|
78 The function @code{all} behaves like the function @code{any}, except\n\ |
|
79 that it returns true only if all the elements of a vector, or all the\n\ |
4015
|
80 elements along dimension @var{dim} of a matrix, are nonzero.\n\ |
3369
|
81 @end deftypefn") |
523
|
82 { |
4015
|
83 ANY_ALL (all); |
523
|
84 } |
|
85 |
1957
|
86 DEFUN (any, args, , |
3369
|
87 "-*- texinfo -*-\n\ |
4015
|
88 @deftypefn {Built-in Function} {} any (@var{x}, @var{dim})\n\ |
3369
|
89 For a vector argument, return 1 if any element of the vector is\n\ |
|
90 nonzero.\n\ |
|
91 \n\ |
|
92 For a matrix argument, return a row vector of ones and\n\ |
|
93 zeros with each element indicating whether any of the elements of the\n\ |
|
94 corresponding column of the matrix are nonzero. For example,\n\ |
|
95 \n\ |
|
96 @example\n\ |
|
97 @group\n\ |
|
98 any (eye (2, 4))\n\ |
|
99 @result{} [ 1, 1, 0, 0 ]\n\ |
|
100 @end group\n\ |
|
101 @end example\n\ |
|
102 \n\ |
4015
|
103 If the optional argument @var{dim} is supplied, work along dimension\n\ |
|
104 @var{dim}. For example,\n\ |
3369
|
105 \n\ |
|
106 @example\n\ |
4015
|
107 @group\n\ |
|
108 any (eye (2, 4), 2)\n\ |
|
109 @result{} [ 1; 1 ]\n\ |
|
110 @end group\n\ |
3369
|
111 @end example\n\ |
|
112 @end deftypefn") |
523
|
113 { |
4015
|
114 ANY_ALL (any); |
523
|
115 } |
|
116 |
649
|
117 // These mapping functions may also be useful in other places, eh? |
|
118 |
|
119 typedef double (*d_dd_fcn) (double, double); |
|
120 |
|
121 static Matrix |
2672
|
122 map_d_m (d_dd_fcn f, double x, const Matrix& y) |
649
|
123 { |
5275
|
124 octave_idx_type nr = y.rows (); |
|
125 octave_idx_type nc = y.columns (); |
649
|
126 |
|
127 Matrix retval (nr, nc); |
|
128 |
5275
|
129 for (octave_idx_type j = 0; j < nc; j++) |
|
130 for (octave_idx_type i = 0; i < nr; i++) |
4153
|
131 { |
|
132 OCTAVE_QUIT; |
|
133 retval (i, j) = f (x, y (i, j)); |
|
134 } |
649
|
135 |
|
136 return retval; |
|
137 } |
|
138 |
|
139 static Matrix |
2672
|
140 map_m_d (d_dd_fcn f, const Matrix& x, double y) |
649
|
141 { |
5275
|
142 octave_idx_type nr = x.rows (); |
|
143 octave_idx_type nc = x.columns (); |
649
|
144 |
|
145 Matrix retval (nr, nc); |
|
146 |
5275
|
147 for (octave_idx_type j = 0; j < nc; j++) |
|
148 for (octave_idx_type i = 0; i < nr; i++) |
4153
|
149 { |
|
150 OCTAVE_QUIT; |
|
151 retval (i, j) = f (x (i, j), y); |
|
152 } |
649
|
153 |
|
154 return retval; |
|
155 } |
|
156 |
|
157 static Matrix |
2672
|
158 map_m_m (d_dd_fcn f, const Matrix& x, const Matrix& y) |
649
|
159 { |
5275
|
160 octave_idx_type x_nr = x.rows (); |
|
161 octave_idx_type x_nc = x.columns (); |
649
|
162 |
5275
|
163 octave_idx_type y_nr = y.rows (); |
|
164 octave_idx_type y_nc = y.columns (); |
649
|
165 |
719
|
166 assert (x_nr == y_nr && x_nc == y_nc); |
649
|
167 |
|
168 Matrix retval (x_nr, x_nc); |
|
169 |
5275
|
170 for (octave_idx_type j = 0; j < x_nc; j++) |
|
171 for (octave_idx_type i = 0; i < x_nr; i++) |
4153
|
172 { |
|
173 OCTAVE_QUIT; |
|
174 retval (i, j) = f (x (i, j), y (i, j)); |
|
175 } |
649
|
176 |
|
177 return retval; |
|
178 } |
|
179 |
1957
|
180 DEFUN (atan2, args, , |
3428
|
181 "-*- texinfo -*-\n\ |
|
182 @deftypefn {Mapping Function} {} atan2 (@var{y}, @var{x})\n\ |
|
183 Compute atan (@var{y} / @var{x}) for corresponding elements of @var{y}\n\ |
|
184 and @var{x}. The result is in range -pi to pi.\n\ |
3439
|
185 @end deftypefn") |
649
|
186 { |
4233
|
187 octave_value retval; |
649
|
188 |
712
|
189 int nargin = args.length (); |
|
190 |
|
191 if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
649
|
192 { |
2086
|
193 octave_value arg_y = args(0); |
|
194 octave_value arg_x = args(1); |
649
|
195 |
5275
|
196 octave_idx_type y_nr = arg_y.rows (); |
|
197 octave_idx_type y_nc = arg_y.columns (); |
649
|
198 |
5275
|
199 octave_idx_type x_nr = arg_x.rows (); |
|
200 octave_idx_type x_nc = arg_x.columns (); |
649
|
201 |
|
202 int arg_y_empty = empty_arg ("atan2", y_nr, y_nc); |
|
203 int arg_x_empty = empty_arg ("atan2", x_nr, x_nc); |
|
204 |
719
|
205 if (arg_y_empty > 0 && arg_x_empty > 0) |
4233
|
206 return octave_value (Matrix ()); |
719
|
207 else if (arg_y_empty || arg_x_empty) |
649
|
208 return retval; |
|
209 |
5275
|
210 octave_idx_type y_is_scalar = (y_nr == 1 && y_nc == 1); |
|
211 octave_idx_type x_is_scalar = (x_nr == 1 && x_nc == 1); |
649
|
212 |
|
213 if (y_is_scalar && x_is_scalar) |
|
214 { |
|
215 double y = arg_y.double_value (); |
|
216 |
|
217 if (! error_state) |
|
218 { |
|
219 double x = arg_x.double_value (); |
|
220 |
|
221 if (! error_state) |
|
222 retval = atan2 (y, x); |
|
223 } |
|
224 } |
|
225 else if (y_is_scalar) |
|
226 { |
|
227 double y = arg_y.double_value (); |
|
228 |
|
229 if (! error_state) |
|
230 { |
|
231 Matrix x = arg_x.matrix_value (); |
|
232 |
|
233 if (! error_state) |
2672
|
234 retval = map_d_m (atan2, y, x); |
649
|
235 } |
|
236 } |
|
237 else if (x_is_scalar) |
|
238 { |
|
239 Matrix y = arg_y.matrix_value (); |
|
240 |
|
241 if (! error_state) |
|
242 { |
|
243 double x = arg_x.double_value (); |
|
244 |
|
245 if (! error_state) |
2672
|
246 retval = map_m_d (atan2, y, x); |
649
|
247 } |
|
248 } |
|
249 else if (y_nr == x_nr && y_nc == x_nc) |
|
250 { |
|
251 Matrix y = arg_y.matrix_value (); |
|
252 |
|
253 if (! error_state) |
|
254 { |
|
255 Matrix x = arg_x.matrix_value (); |
|
256 |
|
257 if (! error_state) |
2672
|
258 retval = map_m_m (atan2, y, x); |
649
|
259 } |
|
260 } |
|
261 else |
|
262 error ("atan2: nonconformant matrices"); |
|
263 } |
712
|
264 else |
|
265 print_usage ("atan2"); |
649
|
266 |
|
267 return retval; |
|
268 } |
|
269 |
4311
|
270 DEFUN (fmod, args, , |
|
271 "-*- texinfo -*-\n\ |
|
272 @deftypefn {Mapping Function} {} fmod (@var{x}, @var{y})\n\ |
4685
|
273 Compute the floating point remainder of dividing @var{x} by @var{y}\n\ |
|
274 using the C library function @code{fmod}. The result has the same\n\ |
|
275 sign as @var{x}. If @var{y} is zero, the result implementation-defined.\n\ |
4311
|
276 @end deftypefn") |
|
277 { |
|
278 octave_value retval; |
|
279 |
|
280 int nargin = args.length (); |
|
281 |
|
282 if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
|
283 { |
|
284 octave_value arg_x = args(0); |
|
285 octave_value arg_y = args(1); |
|
286 |
5275
|
287 octave_idx_type y_nr = arg_y.rows (); |
|
288 octave_idx_type y_nc = arg_y.columns (); |
4311
|
289 |
5275
|
290 octave_idx_type x_nr = arg_x.rows (); |
|
291 octave_idx_type x_nc = arg_x.columns (); |
4311
|
292 |
|
293 int arg_y_empty = empty_arg ("fmod", y_nr, y_nc); |
|
294 int arg_x_empty = empty_arg ("fmod", x_nr, x_nc); |
|
295 |
|
296 if (arg_y_empty > 0 && arg_x_empty > 0) |
|
297 return octave_value (Matrix ()); |
|
298 else if (arg_y_empty || arg_x_empty) |
|
299 return retval; |
|
300 |
5275
|
301 octave_idx_type y_is_scalar = (y_nr == 1 && y_nc == 1); |
|
302 octave_idx_type x_is_scalar = (x_nr == 1 && x_nc == 1); |
4311
|
303 |
|
304 if (y_is_scalar && x_is_scalar) |
|
305 { |
|
306 double y = arg_y.double_value (); |
|
307 |
|
308 if (! error_state) |
|
309 { |
|
310 double x = arg_x.double_value (); |
|
311 |
|
312 if (! error_state) |
|
313 retval = fmod (x, y); |
|
314 } |
|
315 } |
|
316 else if (y_is_scalar) |
|
317 { |
|
318 double y = arg_y.double_value (); |
|
319 |
|
320 if (! error_state) |
|
321 { |
|
322 Matrix x = arg_x.matrix_value (); |
|
323 |
|
324 if (! error_state) |
|
325 retval = map_m_d (fmod, x, y); |
|
326 } |
|
327 } |
|
328 else if (x_is_scalar) |
|
329 { |
|
330 Matrix y = arg_y.matrix_value (); |
|
331 |
|
332 if (! error_state) |
|
333 { |
|
334 double x = arg_x.double_value (); |
|
335 |
|
336 if (! error_state) |
|
337 retval = map_d_m (fmod, x, y); |
|
338 } |
|
339 } |
|
340 else if (y_nr == x_nr && y_nc == x_nc) |
|
341 { |
|
342 Matrix y = arg_y.matrix_value (); |
|
343 |
|
344 if (! error_state) |
|
345 { |
|
346 Matrix x = arg_x.matrix_value (); |
|
347 |
|
348 if (! error_state) |
|
349 retval = map_m_m (fmod, x, y); |
|
350 } |
|
351 } |
|
352 else |
|
353 error ("fmod: nonconformant matrices"); |
|
354 } |
|
355 else |
|
356 print_usage ("fmod"); |
|
357 |
|
358 return retval; |
|
359 } |
|
360 |
3723
|
361 #define DATA_REDUCTION(FCN) \ |
|
362 \ |
4233
|
363 octave_value retval; \ |
3723
|
364 \ |
|
365 int nargin = args.length (); \ |
|
366 \ |
|
367 if (nargin == 1 || nargin == 2) \ |
|
368 { \ |
|
369 octave_value arg = args(0); \ |
|
370 \ |
3864
|
371 int dim = (nargin == 1 ? -1 : args(1).int_value (true) - 1); \ |
3723
|
372 \ |
|
373 if (! error_state) \ |
|
374 { \ |
4556
|
375 if (dim >= -1) \ |
3723
|
376 { \ |
|
377 if (arg.is_real_type ()) \ |
|
378 { \ |
4569
|
379 NDArray tmp = arg.array_value (); \ |
3723
|
380 \ |
|
381 if (! error_state) \ |
4233
|
382 retval = tmp.FCN (dim); \ |
3723
|
383 } \ |
|
384 else if (arg.is_complex_type ()) \ |
|
385 { \ |
4569
|
386 ComplexNDArray tmp = arg.complex_array_value (); \ |
3723
|
387 \ |
|
388 if (! error_state) \ |
4233
|
389 retval = tmp.FCN (dim); \ |
3723
|
390 } \ |
|
391 else \ |
|
392 { \ |
|
393 gripe_wrong_type_arg (#FCN, arg); \ |
|
394 return retval; \ |
|
395 } \ |
|
396 } \ |
|
397 else \ |
|
398 error (#FCN ": invalid dimension argument = %d", dim + 1); \ |
|
399 } \ |
|
400 } \ |
|
401 else \ |
|
402 print_usage (#FCN); \ |
|
403 \ |
|
404 return retval |
|
405 |
1957
|
406 DEFUN (cumprod, args, , |
3428
|
407 "-*- texinfo -*-\n\ |
3723
|
408 @deftypefn {Built-in Function} {} cumprod (@var{x}, @var{dim})\n\ |
|
409 Cumulative product of elements along dimension @var{dim}. If\n\ |
|
410 @var{dim} is omitted, it defaults to 1 (column-wise cumulative\n\ |
|
411 products).\n\ |
5061
|
412 \n\ |
|
413 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ |
|
414 return the cumulative product of the elements as a vector with the\n\ |
|
415 same orientation as @var{x}.\n\ |
3428
|
416 @end deftypefn") |
523
|
417 { |
3723
|
418 DATA_REDUCTION (cumprod); |
523
|
419 } |
|
420 |
1957
|
421 DEFUN (cumsum, args, , |
3428
|
422 "-*- texinfo -*-\n\ |
3723
|
423 @deftypefn {Built-in Function} {} cumsum (@var{x}, @var{dim})\n\ |
|
424 Cumulative sum of elements along dimension @var{dim}. If @var{dim}\n\ |
|
425 is omitted, it defaults to 1 (column-wise cumulative sums).\n\ |
5061
|
426 \n\ |
|
427 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ |
|
428 return the cumulative sum of the elements as a vector with the\n\ |
|
429 same orientation as @var{x}.\n\ |
3428
|
430 @end deftypefn") |
523
|
431 { |
3723
|
432 DATA_REDUCTION (cumsum); |
523
|
433 } |
|
434 |
3972
|
435 // XXX FIXME XXX -- we could eliminate some duplicate code here with |
|
436 // some template functions or macros. |
|
437 |
2086
|
438 static octave_value |
5275
|
439 make_diag (const Matrix& v, octave_idx_type k) |
767
|
440 { |
5275
|
441 octave_idx_type nr = v.rows (); |
|
442 octave_idx_type nc = v.columns (); |
767
|
443 assert (nc == 1 || nr == 1); |
|
444 |
2086
|
445 octave_value retval; |
767
|
446 |
5275
|
447 octave_idx_type roff = 0; |
|
448 octave_idx_type coff = 0; |
767
|
449 if (k > 0) |
|
450 { |
|
451 roff = 0; |
|
452 coff = k; |
|
453 } |
|
454 else if (k < 0) |
|
455 { |
|
456 roff = -k; |
|
457 coff = 0; |
|
458 } |
|
459 |
|
460 if (nr == 1) |
|
461 { |
5275
|
462 octave_idx_type n = nc + std::abs (k); |
767
|
463 Matrix m (n, n, 0.0); |
5275
|
464 for (octave_idx_type i = 0; i < nc; i++) |
2305
|
465 m (i+roff, i+coff) = v (0, i); |
4233
|
466 retval = m; |
767
|
467 } |
|
468 else |
|
469 { |
5275
|
470 octave_idx_type n = nr + std::abs (k); |
767
|
471 Matrix m (n, n, 0.0); |
5275
|
472 for (octave_idx_type i = 0; i < nr; i++) |
2305
|
473 m (i+roff, i+coff) = v (i, 0); |
4233
|
474 retval = m; |
767
|
475 } |
|
476 |
|
477 return retval; |
|
478 } |
|
479 |
2086
|
480 static octave_value |
5275
|
481 make_diag (const ComplexMatrix& v, octave_idx_type k) |
767
|
482 { |
5275
|
483 octave_idx_type nr = v.rows (); |
|
484 octave_idx_type nc = v.columns (); |
767
|
485 assert (nc == 1 || nr == 1); |
|
486 |
2086
|
487 octave_value retval; |
767
|
488 |
5275
|
489 octave_idx_type roff = 0; |
|
490 octave_idx_type coff = 0; |
767
|
491 if (k > 0) |
|
492 { |
|
493 roff = 0; |
|
494 coff = k; |
|
495 } |
|
496 else if (k < 0) |
|
497 { |
|
498 roff = -k; |
|
499 coff = 0; |
|
500 } |
|
501 |
|
502 if (nr == 1) |
|
503 { |
5275
|
504 octave_idx_type n = nc + std::abs (k); |
767
|
505 ComplexMatrix m (n, n, 0.0); |
5275
|
506 for (octave_idx_type i = 0; i < nc; i++) |
2305
|
507 m (i+roff, i+coff) = v (0, i); |
4233
|
508 retval = m; |
767
|
509 } |
|
510 else |
|
511 { |
5275
|
512 octave_idx_type n = nr + std::abs (k); |
767
|
513 ComplexMatrix m (n, n, 0.0); |
5275
|
514 for (octave_idx_type i = 0; i < nr; i++) |
2305
|
515 m (i+roff, i+coff) = v (i, 0); |
4233
|
516 retval = m; |
767
|
517 } |
|
518 |
|
519 return retval; |
|
520 } |
|
521 |
2086
|
522 static octave_value |
|
523 make_diag (const octave_value& arg) |
767
|
524 { |
2086
|
525 octave_value retval; |
767
|
526 |
|
527 if (arg.is_real_type ()) |
|
528 { |
|
529 Matrix m = arg.matrix_value (); |
|
530 |
|
531 if (! error_state) |
|
532 { |
5275
|
533 octave_idx_type nr = m.rows (); |
|
534 octave_idx_type nc = m.columns (); |
767
|
535 |
|
536 if (nr == 0 || nc == 0) |
|
537 retval = Matrix (); |
|
538 else if (nr == 1 || nc == 1) |
|
539 retval = make_diag (m, 0); |
|
540 else |
|
541 { |
|
542 ColumnVector v = m.diag (); |
5164
|
543 if (v.numel () > 0) |
767
|
544 retval = v; |
|
545 } |
|
546 } |
|
547 else |
|
548 gripe_wrong_type_arg ("diag", arg); |
|
549 } |
|
550 else if (arg.is_complex_type ()) |
|
551 { |
|
552 ComplexMatrix cm = arg.complex_matrix_value (); |
|
553 |
|
554 if (! error_state) |
|
555 { |
5275
|
556 octave_idx_type nr = cm.rows (); |
|
557 octave_idx_type nc = cm.columns (); |
767
|
558 |
|
559 if (nr == 0 || nc == 0) |
|
560 retval = Matrix (); |
|
561 else if (nr == 1 || nc == 1) |
|
562 retval = make_diag (cm, 0); |
|
563 else |
|
564 { |
|
565 ComplexColumnVector v = cm.diag (); |
5164
|
566 if (v.numel () > 0) |
767
|
567 retval = v; |
|
568 } |
|
569 } |
|
570 else |
|
571 gripe_wrong_type_arg ("diag", arg); |
|
572 } |
|
573 else |
|
574 gripe_wrong_type_arg ("diag", arg); |
|
575 |
|
576 return retval; |
|
577 } |
|
578 |
2086
|
579 static octave_value |
|
580 make_diag (const octave_value& a, const octave_value& b) |
767
|
581 { |
2086
|
582 octave_value retval; |
767
|
583 |
5275
|
584 octave_idx_type k = b.int_value (); |
767
|
585 |
|
586 if (error_state) |
|
587 { |
|
588 error ("diag: invalid second argument"); |
|
589 return retval; |
|
590 } |
|
591 |
|
592 if (a.is_real_type ()) |
|
593 { |
3307
|
594 Matrix m = a.matrix_value (); |
767
|
595 |
3307
|
596 if (! error_state) |
767
|
597 { |
5275
|
598 octave_idx_type nr = m.rows (); |
|
599 octave_idx_type nc = m.columns (); |
767
|
600 |
3972
|
601 if (nr == 1 || nc == 1) |
|
602 retval = make_diag (m, k); |
|
603 else if (nr == 0 || nc == 0) |
767
|
604 retval = Matrix (); |
|
605 else |
|
606 { |
|
607 ColumnVector d = m.diag (k); |
|
608 retval = d; |
|
609 } |
|
610 } |
|
611 } |
|
612 else if (a.is_complex_type ()) |
|
613 { |
3307
|
614 ComplexMatrix cm = a.complex_matrix_value (); |
767
|
615 |
3307
|
616 if (! error_state) |
767
|
617 { |
5275
|
618 octave_idx_type nr = cm.rows (); |
|
619 octave_idx_type nc = cm.columns (); |
767
|
620 |
3972
|
621 if (nr == 1 || nc == 1) |
|
622 retval = make_diag (cm, k); |
|
623 else if (nr == 0 || nc == 0) |
767
|
624 retval = Matrix (); |
|
625 else |
|
626 { |
|
627 ComplexColumnVector d = cm.diag (k); |
|
628 retval = d; |
|
629 } |
|
630 } |
|
631 } |
|
632 else |
|
633 gripe_wrong_type_arg ("diag", a); |
|
634 |
|
635 return retval; |
|
636 } |
|
637 |
1957
|
638 DEFUN (diag, args, , |
3369
|
639 "-*- texinfo -*-\n\ |
|
640 @deftypefn {Built-in Function} {} diag (@var{v}, @var{k})\n\ |
|
641 Return a diagonal matrix with vector @var{v} on diagonal @var{k}. The\n\ |
|
642 second argument is optional. If it is positive, the vector is placed on\n\ |
|
643 the @var{k}-th super-diagonal. If it is negative, it is placed on the\n\ |
|
644 @var{-k}-th sub-diagonal. The default value of @var{k} is 0, and the\n\ |
|
645 vector is placed on the main diagonal. For example,\n\ |
|
646 \n\ |
|
647 @example\n\ |
|
648 @group\n\ |
|
649 diag ([1, 2, 3], 1)\n\ |
|
650 @result{} 0 1 0 0\n\ |
|
651 0 0 2 0\n\ |
|
652 0 0 0 3\n\ |
|
653 0 0 0 0\n\ |
|
654 @end group\n\ |
|
655 @end example\n\ |
|
656 @end deftypefn") |
523
|
657 { |
4233
|
658 octave_value retval; |
523
|
659 |
|
660 int nargin = args.length (); |
|
661 |
712
|
662 if (nargin == 1 && args(0).is_defined ()) |
767
|
663 retval = make_diag (args(0)); |
712
|
664 else if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
767
|
665 retval = make_diag (args(0), args(1)); |
523
|
666 else |
|
667 print_usage ("diag"); |
|
668 |
|
669 return retval; |
|
670 } |
|
671 |
1957
|
672 DEFUN (prod, args, , |
3428
|
673 "-*- texinfo -*-\n\ |
3723
|
674 @deftypefn {Built-in Function} {} prod (@var{x}, @var{dim})\n\ |
|
675 Product of elements along dimension @var{dim}. If @var{dim} is\n\ |
|
676 omitted, it defaults to 1 (column-wise products).\n\ |
5061
|
677 \n\ |
|
678 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ |
|
679 return the product of the elements.\n\ |
3428
|
680 @end deftypefn") |
523
|
681 { |
3723
|
682 DATA_REDUCTION (prod); |
523
|
683 } |
|
684 |
4824
|
685 static octave_value |
|
686 do_cat (const octave_value_list& args, std::string fname) |
4806
|
687 { |
|
688 octave_value retval; |
|
689 |
4824
|
690 int n_args = args.length (); |
4806
|
691 |
5507
|
692 if (n_args == 2) |
|
693 retval = args(1); |
|
694 else if (n_args > 2) |
4824
|
695 { |
5275
|
696 octave_idx_type dim = args(0).int_value () - 1; |
4806
|
697 |
4824
|
698 if (error_state) |
4806
|
699 { |
4824
|
700 error ("cat: expecting first argument to be a integer"); |
4806
|
701 return retval; |
|
702 } |
|
703 |
4824
|
704 if (dim >= 0) |
|
705 { |
4915
|
706 |
|
707 dim_vector dv = args(1).dims (); |
4824
|
708 |
4915
|
709 for (int i = 2; i < args.length (); i++) |
|
710 { |
|
711 // add_dims constructs a dimension vector which holds the |
4824
|
712 // dimensions of the final array after concatenation. |
4806
|
713 |
4915
|
714 if (! dv.concat (args(i).dims (), dim)) |
4806
|
715 { |
4824
|
716 // Dimensions do not match. |
4915
|
717 error ("cat: dimension mismatch"); |
4806
|
718 return retval; |
|
719 } |
4824
|
720 } |
|
721 |
4915
|
722 // The lines below might seem crazy, since we take a copy |
|
723 // of the first argument, resize it to be empty and then resize |
|
724 // it to be full. This is done since it means that there is no |
|
725 // recopying of data, as would happen if we used a single resize. |
|
726 // It should be noted that resize operation is also significantly |
|
727 // slower than the do_cat_op function, so it makes sense to have an |
|
728 // empty matrix and copy all data. |
4824
|
729 // |
4915
|
730 // We might also start with a empty octave_value using |
|
731 // tmp = octave_value_typeinfo::lookup_type (args(1).type_name()); |
|
732 // and then directly resize. However, for some types there might be |
|
733 // some additional setup needed, and so this should be avoided. |
5533
|
734 |
4915
|
735 octave_value tmp; |
5533
|
736 |
4915
|
737 for (int i = 1; i < n_args; i++) |
5533
|
738 { |
|
739 if (! args (i).all_zero_dims ()) |
|
740 { |
|
741 tmp = args (i); |
|
742 break; |
|
743 } |
|
744 } |
5164
|
745 |
5533
|
746 tmp = tmp.resize (dim_vector (0,0)).resize (dv); |
4824
|
747 |
4915
|
748 if (error_state) |
|
749 return retval; |
4824
|
750 |
4915
|
751 Array<int> ra_idx (dv.length (), 0); |
5533
|
752 |
4915
|
753 for (int i = 1; i < n_args; i++) |
|
754 { |
|
755 tmp = do_cat_op (tmp, args (i), ra_idx); |
4824
|
756 |
4915
|
757 if (error_state) |
|
758 return retval; |
4806
|
759 |
4915
|
760 dim_vector dv_tmp = args (i).dims (); |
5533
|
761 |
4915
|
762 ra_idx (dim) += (dim < dv_tmp.length () ? dv_tmp (dim) : 1); |
|
763 } |
4806
|
764 |
5533
|
765 retval = tmp; |
4806
|
766 } |
5533
|
767 else |
|
768 error ("%s: invalid dimension argument", fname.c_str ()); |
4806
|
769 } |
|
770 else |
4824
|
771 print_usage (fname); |
4806
|
772 |
|
773 return retval; |
|
774 } |
|
775 |
|
776 DEFUN (horzcat, args, , |
4824
|
777 "-*- texinfo -*-\n\ |
4806
|
778 @deftypefn {Built-in Function} {} horzcat (@var{array1}, @var{array2}, @dots{}, @var{arrayN})\n\ |
|
779 Return the horizontal concatenation of N-d array objects, @var{array1},\n\ |
|
780 @var{array2}, @dots{}, @var{arrayN} along dimension 2.\n\ |
|
781 @end deftypefn\n\ |
|
782 @seealso{cat and vertcat}") |
|
783 { |
|
784 octave_value_list args_tmp = args; |
|
785 |
|
786 int dim = 2; |
|
787 |
|
788 octave_value d (dim); |
|
789 |
|
790 args_tmp.prepend (d); |
|
791 |
4824
|
792 return do_cat (args_tmp, "horzcat"); |
4806
|
793 } |
|
794 |
|
795 DEFUN (vertcat, args, , |
|
796 "-*- texinfo -*-\n\ |
|
797 @deftypefn {Built-in Function} {} vertcat (@var{array1}, @var{array2}, @dots{}, @var{arrayN})\n\ |
|
798 Return the vertical concatenation of N-d array objects, @var{array1},\n\ |
|
799 @var{array2}, @dots{}, @var{arrayN} along dimension 1.\n\ |
|
800 @end deftypefn\n\ |
|
801 @seealso{cat and horzcat}") |
|
802 { |
|
803 octave_value_list args_tmp = args; |
|
804 |
|
805 int dim = 1; |
|
806 |
|
807 octave_value d (dim); |
|
808 |
|
809 args_tmp.prepend (d); |
|
810 |
4824
|
811 return do_cat (args_tmp, "vertcat"); |
4806
|
812 } |
|
813 |
4758
|
814 DEFUN (cat, args, , |
|
815 "-*- texinfo -*-\n\ |
|
816 @deftypefn {Built-in Function} {} cat (@var{dim}, @var{array1}, @var{array2}, @dots{}, @var{arrayN})\n\ |
4806
|
817 Return the concatenation of N-d array objects, @var{array1},\n\ |
|
818 @var{array2}, @dots{}, @var{arrayN} along dimension @var{dim}.\n\ |
4758
|
819 \n\ |
|
820 @example\n\ |
|
821 @group\n\ |
|
822 A = ones (2, 2);\n\ |
|
823 B = zeros (2, 2);\n\ |
|
824 cat (2, A, B)\n\ |
|
825 @result{} ans =\n\ |
|
826 \n\ |
|
827 1 1 0 0\n\ |
|
828 1 1 0 0\n\ |
|
829 @end group\n\ |
|
830 @end example\n\ |
|
831 \n\ |
|
832 Alternatively, we can concatenate @var{A} and @var{B} along the\n\ |
|
833 second dimension the following way:\n\ |
|
834 \n\ |
|
835 @example\n\ |
|
836 @group\n\ |
|
837 [A, B].\n\ |
|
838 @end group\n\ |
|
839 @end example\n\ |
|
840 \n\ |
|
841 @var{dim} can be larger than the dimensions of the N-d array objects\n\ |
|
842 and the result will thus have @var{dim} dimensions as the\n\ |
|
843 following example shows:\n\ |
|
844 @example\n\ |
|
845 @group\n\ |
|
846 cat (4, ones(2, 2), zeros (2, 2))\n\ |
|
847 @result{} ans =\n\ |
|
848 \n\ |
|
849 ans(:,:,1,1) =\n\ |
|
850 \n\ |
|
851 1 1\n\ |
|
852 1 1\n\ |
|
853 \n\ |
|
854 ans(:,:,1,2) =\n\ |
|
855 0 0\n\ |
|
856 0 0\n\ |
|
857 @end group\n\ |
|
858 @end example\n\ |
|
859 \n\ |
4806
|
860 @end deftypefn\n\ |
|
861 @seealso{horzcat and vertcat}") |
4758
|
862 { |
4824
|
863 return do_cat (args, "cat"); |
4758
|
864 } |
|
865 |
4593
|
866 static octave_value |
|
867 do_permute (const octave_value_list& args, bool inv, const std::string& fname) |
|
868 { |
|
869 octave_value retval; |
|
870 |
5148
|
871 if (args.length () == 2 && args(1).length () >= args(1).ndims ()) |
4593
|
872 { |
|
873 Array<int> vec = args(1).int_vector_value (); |
|
874 |
5148
|
875 // XXX FIXME XXX -- maybe we shoudl create an idx_vector object |
|
876 // here and pass that to permute? |
|
877 |
|
878 int n = vec.length (); |
|
879 |
|
880 for (int i = 0; i < n; i++) |
|
881 vec(i)--; |
|
882 |
4593
|
883 octave_value ret = args(0).permute (vec, inv); |
|
884 |
|
885 if (! error_state) |
|
886 retval = ret; |
|
887 } |
|
888 else |
|
889 print_usage (fname); |
|
890 |
|
891 return retval; |
|
892 } |
|
893 |
|
894 DEFUN (permute, args, , |
|
895 "-*- texinfo -*-\n\ |
|
896 @deftypefn {Built-in Function} {} permute (@var{a}, @var{perm})\n\ |
|
897 Return the generalized transpose for an N-d array object @var{a}.\n\ |
|
898 The permutation vector @var{perm} must contain the elements\n\ |
|
899 @code{1:ndims(a)} (in any order, but each element must appear just once).\n\ |
|
900 \n\ |
|
901 @end deftypefn\n\ |
|
902 @seealso{ipermute}") |
|
903 { |
|
904 return do_permute (args, false, "permute"); |
|
905 } |
|
906 |
|
907 DEFUN (ipermute, args, , |
|
908 "-*- texinfo -*-\n\ |
|
909 @deftypefn {Built-in Function} {} ipermute (@var{a}, @var{iperm})\n\ |
|
910 The inverse of the @code{permute} function. The expression\n\ |
|
911 \n\ |
|
912 @example\n\ |
|
913 ipermute (permute (a, perm), perm)\n\ |
|
914 @end example\n\ |
|
915 returns the original array @var{a}.\n\ |
|
916 \n\ |
|
917 @end deftypefn\n\ |
|
918 @seealso{permute}") |
|
919 { |
|
920 return do_permute (args, true, "ipermute"); |
|
921 } |
|
922 |
3195
|
923 DEFUN (length, args, , |
3373
|
924 "-*- texinfo -*-\n\ |
|
925 @deftypefn {Built-in Function} {} length (@var{a})\n\ |
4176
|
926 Return the `length' of the object @var{a}. For matrix objects, the\n\ |
3373
|
927 length is the number of rows or columns, whichever is greater (this\n\ |
|
928 odd definition is used for compatibility with Matlab).\n\ |
|
929 @end deftypefn") |
3195
|
930 { |
|
931 octave_value retval; |
|
932 |
|
933 if (args.length () == 1) |
|
934 { |
|
935 int len = args(0).length (); |
|
936 |
|
937 if (! error_state) |
4233
|
938 retval = len; |
3195
|
939 } |
|
940 else |
|
941 print_usage ("length"); |
|
942 |
|
943 return retval; |
|
944 } |
|
945 |
4554
|
946 DEFUN (ndims, args, , |
|
947 "-*- texinfo -*-\n\ |
|
948 @deftypefn {Built-in Function} {} ndims (@var{a})\n\ |
|
949 Returns the number of dimensions of array @var{a}.\n\ |
|
950 For any array, the result will always be larger than or equal to 2.\n\ |
|
951 Trailing singleton dimensions are not counted.\n\ |
|
952 @end deftypefn") |
|
953 { |
|
954 octave_value retval; |
|
955 |
|
956 if (args.length () == 1) |
|
957 { |
|
958 int n_dims = args(0).ndims (); |
|
959 |
|
960 if (! error_state) |
|
961 retval = n_dims; |
|
962 } |
|
963 else |
|
964 print_usage ("ndims"); |
|
965 |
|
966 return retval; |
|
967 } |
|
968 |
4559
|
969 DEFUN (numel, args, , |
|
970 "-*- texinfo -*-\n\ |
|
971 @deftypefn {Built-in Function} {} numel (@var{a})\n\ |
|
972 Returns the number of elements in the object @var{a}.\n\ |
|
973 @end deftypefn") |
|
974 { |
|
975 octave_value retval; |
|
976 |
|
977 if (args.length () == 1) |
|
978 { |
|
979 int numel = args(0).numel (); |
|
980 |
|
981 if (! error_state) |
|
982 { |
|
983 if (numel < 0) |
|
984 numel = 0; |
|
985 |
|
986 retval = numel; |
|
987 } |
|
988 } |
|
989 else |
|
990 print_usage ("numel"); |
|
991 |
|
992 return retval; |
|
993 } |
|
994 |
1957
|
995 DEFUN (size, args, nargout, |
3373
|
996 "-*- texinfo -*-\n\ |
|
997 @deftypefn {Built-in Function} {} size (@var{a}, @var{n})\n\ |
|
998 Return the number rows and columns of @var{a}.\n\ |
|
999 \n\ |
|
1000 With one input argument and one output argument, the result is returned\n\ |
4741
|
1001 in a row vector. If there are multiple output arguments, the number of\n\ |
|
1002 rows is assigned to the first, and the number of columns to the second,\n\ |
|
1003 etc. For example,\n\ |
3373
|
1004 \n\ |
|
1005 @example\n\ |
|
1006 @group\n\ |
|
1007 size ([1, 2; 3, 4; 5, 6])\n\ |
|
1008 @result{} [ 3, 2 ]\n\ |
1031
|
1009 \n\ |
3373
|
1010 [nr, nc] = size ([1, 2; 3, 4; 5, 6])\n\ |
|
1011 @result{} nr = 3\n\ |
|
1012 @result{} nc = 2\n\ |
|
1013 @end group\n\ |
|
1014 @end example\n\ |
|
1015 \n\ |
4741
|
1016 If given a second argument, @code{size} will return the size of the\n\ |
|
1017 corresponding dimension. For example\n\ |
1031
|
1018 \n\ |
3373
|
1019 @example\n\ |
|
1020 size ([1, 2; 3, 4; 5, 6], 2)\n\ |
|
1021 @result{} 2\n\ |
|
1022 @end example\n\ |
|
1023 \n\ |
|
1024 @noindent\n\ |
|
1025 returns the number of columns in the given matrix.\n\ |
|
1026 @end deftypefn") |
523
|
1027 { |
2086
|
1028 octave_value_list retval; |
523
|
1029 |
|
1030 int nargin = args.length (); |
|
1031 |
4513
|
1032 if (nargin == 1) |
523
|
1033 { |
4513
|
1034 dim_vector dimensions = args(0).dims (); |
|
1035 |
|
1036 int ndims = dimensions.length (); |
1031
|
1037 |
4513
|
1038 Matrix m (1, ndims); |
|
1039 |
|
1040 if (nargout > 1) |
523
|
1041 { |
4513
|
1042 while (ndims--) |
|
1043 retval(ndims) = dimensions(ndims); |
523
|
1044 } |
4513
|
1045 else |
712
|
1046 { |
4513
|
1047 for (int i = 0; i < ndims; i++) |
|
1048 m(0, i) = dimensions(i); |
|
1049 |
|
1050 retval(0) = m; |
712
|
1051 } |
1031
|
1052 } |
|
1053 else if (nargin == 2 && nargout < 2) |
|
1054 { |
5275
|
1055 octave_idx_type nd = args(1).int_value (true); |
1031
|
1056 |
|
1057 if (error_state) |
|
1058 error ("size: expecting scalar as second argument"); |
712
|
1059 else |
1031
|
1060 { |
4741
|
1061 dim_vector dv = args(0).dims (); |
|
1062 |
4911
|
1063 if (nd > 0) |
|
1064 { |
|
1065 if (nd <= dv.length ()) |
|
1066 retval(0) = dv(nd-1); |
|
1067 else |
|
1068 retval(0) = 1; |
|
1069 } |
1031
|
1070 else |
4741
|
1071 error ("size: requested dimension (= %d) out of range", nd); |
1031
|
1072 } |
523
|
1073 } |
712
|
1074 else |
|
1075 print_usage ("size"); |
523
|
1076 |
|
1077 return retval; |
|
1078 } |
|
1079 |
1957
|
1080 DEFUN (sum, args, , |
3428
|
1081 "-*- texinfo -*-\n\ |
3723
|
1082 @deftypefn {Built-in Function} {} sum (@var{x}, @var{dim})\n\ |
|
1083 Sum of elements along dimension @var{dim}. If @var{dim} is\n\ |
|
1084 omitted, it defaults to 1 (column-wise sum).\n\ |
5061
|
1085 \n\ |
|
1086 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ |
|
1087 return the sum of the elements.\n\ |
3428
|
1088 @end deftypefn") |
523
|
1089 { |
3723
|
1090 DATA_REDUCTION (sum); |
523
|
1091 } |
|
1092 |
1957
|
1093 DEFUN (sumsq, args, , |
3428
|
1094 "-*- texinfo -*-\n\ |
3723
|
1095 @deftypefn {Built-in Function} {} sumsq (@var{x}, @var{dim})\n\ |
|
1096 Sum of squares of elements along dimension @var{dim}. If @var{dim}\n\ |
|
1097 is omitted, it defaults to 1 (column-wise sum of squares).\n\ |
3095
|
1098 \n\ |
5061
|
1099 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ |
|
1100 return the sum of squares of the elements.\n\ |
|
1101 \n\ |
|
1102 This function is conceptually equivalent to computing\n\ |
3723
|
1103 @example\n\ |
|
1104 sum (x .* conj (x), dim)\n\ |
|
1105 @end example\n\ |
|
1106 but it uses less memory and avoids calling conj if @var{x} is real.\n\ |
3428
|
1107 @end deftypefn") |
523
|
1108 { |
3723
|
1109 DATA_REDUCTION (sumsq); |
523
|
1110 } |
|
1111 |
4028
|
1112 DEFUN (isbool, args, , |
3428
|
1113 "-*- texinfo -*-\n\ |
4028
|
1114 @deftypefn {Built-in Functio} {} isbool (@var{x})\n\ |
3428
|
1115 Return true if @var{x} is a boolean object.\n\ |
3439
|
1116 @end deftypefn") |
3209
|
1117 { |
|
1118 octave_value retval; |
|
1119 |
|
1120 if (args.length () == 1) |
3258
|
1121 retval = args(0).is_bool_type (); |
3209
|
1122 else |
4028
|
1123 print_usage ("isbool"); |
3209
|
1124 |
|
1125 return retval; |
|
1126 } |
|
1127 |
4028
|
1128 DEFALIAS (islogical, isbool); |
3209
|
1129 |
4028
|
1130 DEFUN (iscomplex, args, , |
3428
|
1131 "-*- texinfo -*-\n\ |
4028
|
1132 @deftypefn {Built-in Function} {} iscomplex (@var{x})\n\ |
3428
|
1133 Return true if @var{x} is a complex-valued numeric object.\n\ |
|
1134 @end deftypefn") |
3186
|
1135 { |
|
1136 octave_value retval; |
|
1137 |
|
1138 if (args.length () == 1) |
3258
|
1139 retval = args(0).is_complex_type (); |
3186
|
1140 else |
4028
|
1141 print_usage ("iscomplex"); |
3186
|
1142 |
|
1143 return retval; |
|
1144 } |
|
1145 |
5476
|
1146 // XXX FIXME XXX -- perhaps this should be implemented with an |
|
1147 // octave_value member function? |
|
1148 |
|
1149 DEFUN (complex, args, , |
|
1150 "-*- texinfo -*-\n\ |
|
1151 @deftypefn {Built-in Function} {} complex (@var{val})\n\ |
|
1152 @deftypefnx {Built-in Function} {} complex (@var{re}, @var{im})\n\ |
|
1153 Convert @var{x} to a complex value.\n\ |
|
1154 @end deftypefn") |
|
1155 { |
|
1156 octave_value retval; |
|
1157 |
|
1158 int nargin = args.length (); |
|
1159 |
|
1160 if (nargin == 1) |
|
1161 { |
|
1162 octave_value arg = args(0); |
|
1163 |
|
1164 if (arg.is_complex_type ()) |
|
1165 retval = arg; |
|
1166 else |
|
1167 { |
|
1168 if (arg.numel () == 1) |
|
1169 { |
|
1170 Complex val = arg.complex_value (); |
|
1171 |
|
1172 if (! error_state) |
|
1173 retval = octave_value (new octave_complex (val)); |
|
1174 } |
|
1175 else |
|
1176 { |
|
1177 ComplexNDArray val = arg.complex_array_value (); |
|
1178 |
|
1179 if (! error_state) |
|
1180 retval = octave_value (new octave_complex_matrix (val)); |
|
1181 } |
|
1182 |
|
1183 if (error_state) |
|
1184 error ("complex: invalid conversion"); |
|
1185 } |
|
1186 } |
|
1187 else if (nargin == 2) |
|
1188 { |
|
1189 octave_value re = args(0); |
|
1190 octave_value im = args(1); |
|
1191 |
|
1192 if (re.numel () == 1) |
|
1193 { |
|
1194 double re_val = re.double_value (); |
|
1195 |
|
1196 if (im.numel () == 1) |
|
1197 { |
|
1198 double im_val = im.double_value (); |
|
1199 |
|
1200 if (! error_state) |
|
1201 retval = octave_value (new octave_complex (Complex (re_val, im_val))); |
|
1202 } |
|
1203 else |
|
1204 { |
|
1205 const NDArray im_val = im.array_value (); |
|
1206 |
|
1207 if (! error_state) |
|
1208 { |
|
1209 ComplexNDArray result (im_val.dims (), Complex ()); |
|
1210 |
|
1211 for (octave_idx_type i = 0; i < im_val.numel (); i++) |
|
1212 result.xelem (i) = Complex (re_val, im_val(i)); |
|
1213 |
|
1214 retval = octave_value (new octave_complex_matrix (result)); |
|
1215 } |
|
1216 } |
|
1217 } |
|
1218 else |
|
1219 { |
|
1220 const NDArray re_val = re.array_value (); |
|
1221 |
|
1222 if (im.numel () == 1) |
|
1223 { |
|
1224 double im_val = im.double_value (); |
|
1225 |
|
1226 if (! error_state) |
|
1227 { |
|
1228 ComplexNDArray result (re_val.dims (), Complex ()); |
|
1229 |
|
1230 for (octave_idx_type i = 0; i < re_val.numel (); i++) |
|
1231 result.xelem (i) = Complex (re_val(i), im_val); |
|
1232 |
|
1233 retval = octave_value (new octave_complex_matrix (result)); |
|
1234 } |
|
1235 } |
|
1236 else |
|
1237 { |
|
1238 const NDArray im_val = im.array_value (); |
|
1239 |
|
1240 if (! error_state) |
|
1241 { |
|
1242 if (re_val.dims () == im_val.dims ()) |
|
1243 { |
|
1244 ComplexNDArray result (re_val.dims (), Complex ()); |
|
1245 |
|
1246 for (octave_idx_type i = 0; i < re_val.numel (); i++) |
|
1247 result.xelem (i) = Complex (re_val(i), im_val(i)); |
|
1248 |
|
1249 retval = octave_value (new octave_complex_matrix (result)); |
|
1250 } |
|
1251 else |
|
1252 error ("complex: dimension mismatch"); |
|
1253 } |
|
1254 } |
|
1255 } |
|
1256 |
|
1257 if (error_state) |
|
1258 error ("complex: invalid conversion"); |
|
1259 } |
|
1260 else |
|
1261 print_usage ("complex"); |
|
1262 |
|
1263 return retval; |
|
1264 } |
|
1265 |
|
1266 |
3258
|
1267 DEFUN (isreal, args, , |
3428
|
1268 "-*- texinfo -*-\n\ |
|
1269 @deftypefn {Built-in Function} {} isreal (@var{x})\n\ |
|
1270 Return true if @var{x} is a real-valued numeric object.\n\ |
|
1271 @end deftypefn") |
3258
|
1272 { |
|
1273 octave_value retval; |
|
1274 |
|
1275 if (args.length () == 1) |
|
1276 retval = args(0).is_real_type (); |
|
1277 else |
|
1278 print_usage ("isreal"); |
|
1279 |
|
1280 return retval; |
|
1281 } |
|
1282 |
3202
|
1283 DEFUN (isempty, args, , |
3373
|
1284 "-*- texinfo -*-\n\ |
|
1285 @deftypefn {Built-in Function} {} isempty (@var{a})\n\ |
|
1286 Return 1 if @var{a} is an empty matrix (either the number of rows, or\n\ |
|
1287 the number of columns, or both are zero). Otherwise, return 0.\n\ |
|
1288 @end deftypefn") |
3202
|
1289 { |
4233
|
1290 octave_value retval = false; |
3202
|
1291 |
|
1292 if (args.length () == 1) |
4559
|
1293 retval = args(0).is_empty (); |
3202
|
1294 else |
|
1295 print_usage ("isempty"); |
|
1296 |
|
1297 return retval; |
|
1298 } |
|
1299 |
3206
|
1300 DEFUN (isnumeric, args, , |
3428
|
1301 "-*- texinfo -*-\n\ |
|
1302 @deftypefn {Built-in Function} {} isnumeric (@var{x})\n\ |
|
1303 Return nonzero if @var{x} is a numeric object.\n\ |
|
1304 @end deftypefn") |
3206
|
1305 { |
|
1306 octave_value retval; |
|
1307 |
|
1308 if (args.length () == 1) |
3258
|
1309 retval = args(0).is_numeric_type (); |
3206
|
1310 else |
3238
|
1311 print_usage ("isnumeric"); |
3206
|
1312 |
|
1313 return retval; |
|
1314 } |
|
1315 |
4028
|
1316 DEFUN (islist, args, , |
3526
|
1317 "-*- texinfo -*-\n\ |
4028
|
1318 @deftypefn {Built-in Function} {} islist (@var{x})\n\ |
3428
|
1319 Return nonzero if @var{x} is a list.\n\ |
|
1320 @end deftypefn") |
3204
|
1321 { |
|
1322 octave_value retval; |
|
1323 |
|
1324 if (args.length () == 1) |
3258
|
1325 retval = args(0).is_list (); |
3204
|
1326 else |
4028
|
1327 print_usage ("islist"); |
3204
|
1328 |
|
1329 return retval; |
|
1330 } |
|
1331 |
4028
|
1332 DEFUN (ismatrix, args, , |
3321
|
1333 "-*- texinfo -*-\n\ |
4028
|
1334 @deftypefn {Built-in Function} {} ismatrix (@var{a})\n\ |
3321
|
1335 Return 1 if @var{a} is a matrix. Otherwise, return 0.\n\ |
3333
|
1336 @end deftypefn") |
3202
|
1337 { |
4233
|
1338 octave_value retval = false; |
3202
|
1339 |
|
1340 if (args.length () == 1) |
|
1341 { |
|
1342 octave_value arg = args(0); |
|
1343 |
3212
|
1344 if (arg.is_scalar_type () || arg.is_range ()) |
4233
|
1345 retval = true; |
3202
|
1346 else if (arg.is_matrix_type ()) |
4233
|
1347 retval = (arg.rows () >= 1 && arg.columns () >= 1); |
3202
|
1348 } |
|
1349 else |
4028
|
1350 print_usage ("ismatrix"); |
3202
|
1351 |
|
1352 return retval; |
|
1353 } |
|
1354 |
3354
|
1355 static octave_value |
|
1356 fill_matrix (const octave_value_list& args, double val, const char *fcn) |
523
|
1357 { |
3354
|
1358 octave_value retval; |
523
|
1359 |
|
1360 int nargin = args.length (); |
|
1361 |
4946
|
1362 oct_data_conv::data_type dt = oct_data_conv::dt_double; |
4481
|
1363 |
4946
|
1364 dim_vector dims (1, 1); |
4481
|
1365 |
|
1366 if (nargin > 0 && args(nargin-1).is_string ()) |
|
1367 { |
4946
|
1368 std::string nm = args(nargin-1).string_value (); |
4481
|
1369 nargin--; |
|
1370 |
4946
|
1371 dt = oct_data_conv::string_to_data_type (nm); |
|
1372 |
|
1373 if (error_state) |
|
1374 return retval; |
4481
|
1375 } |
|
1376 |
523
|
1377 switch (nargin) |
|
1378 { |
712
|
1379 case 0: |
|
1380 break; |
777
|
1381 |
610
|
1382 case 1: |
4481
|
1383 get_dimensions (args(0), fcn, dims); |
610
|
1384 break; |
777
|
1385 |
4563
|
1386 default: |
|
1387 { |
|
1388 dims.resize (nargin); |
4481
|
1389 |
4563
|
1390 for (int i = 0; i < nargin; i++) |
|
1391 { |
4732
|
1392 dims(i) = args(i).is_empty () ? 0 : args(i).int_value (); |
4481
|
1393 |
4563
|
1394 if (error_state) |
|
1395 { |
4732
|
1396 error ("%s: expecting scalar integer arguments", fcn); |
4563
|
1397 break; |
|
1398 } |
|
1399 } |
|
1400 } |
|
1401 break; |
4481
|
1402 } |
|
1403 |
|
1404 if (! error_state) |
|
1405 { |
4946
|
1406 dims.chop_trailing_singletons (); |
4565
|
1407 |
4481
|
1408 check_dimensions (dims, fcn); |
3354
|
1409 |
4946
|
1410 // XXX FIXME XXX -- perhaps this should be made extensible by |
|
1411 // using the class name to lookup a function to call to create |
|
1412 // the new value. |
|
1413 |
|
1414 // Note that automatic narrowing will handle conversion from |
|
1415 // NDArray to scalar. |
|
1416 |
4481
|
1417 if (! error_state) |
|
1418 { |
4946
|
1419 switch (dt) |
|
1420 { |
|
1421 case oct_data_conv::dt_int8: |
|
1422 retval = int8NDArray (dims, val); |
|
1423 break; |
4481
|
1424 |
4946
|
1425 case oct_data_conv::dt_uint8: |
|
1426 retval = uint8NDArray (dims, val); |
|
1427 break; |
|
1428 |
|
1429 case oct_data_conv::dt_int16: |
|
1430 retval = int16NDArray (dims, val); |
|
1431 break; |
|
1432 |
|
1433 case oct_data_conv::dt_uint16: |
|
1434 retval = uint16NDArray (dims, val); |
|
1435 break; |
|
1436 |
|
1437 case oct_data_conv::dt_int32: |
|
1438 retval = int32NDArray (dims, val); |
|
1439 break; |
777
|
1440 |
4946
|
1441 case oct_data_conv::dt_uint32: |
|
1442 retval = uint32NDArray (dims, val); |
|
1443 break; |
|
1444 |
|
1445 case oct_data_conv::dt_int64: |
|
1446 retval = int64NDArray (dims, val); |
|
1447 break; |
4481
|
1448 |
4946
|
1449 case oct_data_conv::dt_uint64: |
|
1450 retval = uint64NDArray (dims, val); |
|
1451 break; |
4481
|
1452 |
4946
|
1453 case oct_data_conv::dt_single: // XXX FIXME XXX |
|
1454 case oct_data_conv::dt_double: |
|
1455 retval = NDArray (dims, val); |
|
1456 break; |
|
1457 |
4986
|
1458 case oct_data_conv::dt_logical: |
|
1459 retval = boolNDArray (dims, val); |
|
1460 break; |
|
1461 |
4946
|
1462 default: |
|
1463 error ("%s: invalid class name", fcn); |
|
1464 break; |
4481
|
1465 } |
|
1466 } |
523
|
1467 } |
|
1468 |
|
1469 return retval; |
|
1470 } |
|
1471 |
3354
|
1472 DEFUN (ones, args, , |
3369
|
1473 "-*- texinfo -*-\n\ |
|
1474 @deftypefn {Built-in Function} {} ones (@var{x})\n\ |
|
1475 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m})\n\ |
4948
|
1476 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
1477 @deftypefnx {Built-in Function} {} ones (@dots{}, @var{class})\n\ |
4481
|
1478 Return a matrix or N-dimensional array whose elements are all 1.\n\ |
|
1479 The arguments are handled the same as the arguments for @code{eye}.\n\ |
3369
|
1480 \n\ |
|
1481 If you need to create a matrix whose values are all the same, you should\n\ |
|
1482 use an expression like\n\ |
|
1483 \n\ |
|
1484 @example\n\ |
|
1485 val_matrix = val * ones (n, m)\n\ |
|
1486 @end example\n\ |
4945
|
1487 \n\ |
|
1488 The optional argument @var{class}, allows @code{ones} to return an array of\n\ |
|
1489 the specified type, like\n\ |
|
1490 \n\ |
|
1491 @example\n\ |
|
1492 val = ones (n,m, \"uint8\")\n\ |
|
1493 @end example\n\ |
3369
|
1494 @end deftypefn") |
523
|
1495 { |
3354
|
1496 return fill_matrix (args, 1.0, "ones"); |
523
|
1497 } |
|
1498 |
3354
|
1499 DEFUN (zeros, args, , |
3369
|
1500 "-*- texinfo -*-\n\ |
|
1501 @deftypefn {Built-in Function} {} zeros (@var{x})\n\ |
|
1502 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m})\n\ |
4948
|
1503 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
1504 @deftypefnx {Built-in Function} {} zeros (@dots{}, @var{class})\n\ |
4481
|
1505 Return a matrix or N-dimensional array whose elements are all 0.\n\ |
|
1506 The arguments are handled the same as the arguments for @code{eye}.\n\ |
4945
|
1507 \n\ |
|
1508 The optional argument @var{class}, allows @code{zeros} to return an array of\n\ |
|
1509 the specified type, like\n\ |
|
1510 \n\ |
|
1511 @example\n\ |
|
1512 val = zeros (n,m, \"uint8\")\n\ |
|
1513 @end example\n\ |
3369
|
1514 @end deftypefn") |
523
|
1515 { |
3354
|
1516 return fill_matrix (args, 0.0, "zeros"); |
|
1517 } |
523
|
1518 |
4946
|
1519 template <class MT> |
|
1520 octave_value |
|
1521 identity_matrix (int nr, int nc) |
|
1522 { |
|
1523 octave_value retval; |
|
1524 |
|
1525 typename octave_array_type_traits<MT>::element_type one (1); |
|
1526 |
|
1527 if (nr == 1 && nc == 1) |
|
1528 retval = one; |
|
1529 else |
|
1530 { |
|
1531 dim_vector dims (nr, nc); |
|
1532 |
|
1533 typename octave_array_type_traits<MT>::element_type zero (0); |
|
1534 |
|
1535 MT m (dims, zero); |
|
1536 |
|
1537 if (nr > 0 && nc > 0) |
|
1538 { |
|
1539 int n = std::min (nr, nc); |
|
1540 |
|
1541 for (int i = 0; i < n; i++) |
|
1542 m(i,i) = one; |
|
1543 } |
|
1544 |
|
1545 retval = m; |
|
1546 } |
|
1547 |
|
1548 return retval; |
|
1549 } |
|
1550 |
5058
|
1551 #define INSTANTIATE_EYE(T) \ |
|
1552 template octave_value identity_matrix<T> (int, int) |
|
1553 |
|
1554 INSTANTIATE_EYE (int8NDArray); |
|
1555 INSTANTIATE_EYE (uint8NDArray); |
|
1556 INSTANTIATE_EYE (int16NDArray); |
|
1557 INSTANTIATE_EYE (uint16NDArray); |
|
1558 INSTANTIATE_EYE (int32NDArray); |
|
1559 INSTANTIATE_EYE (uint32NDArray); |
|
1560 INSTANTIATE_EYE (int64NDArray); |
|
1561 INSTANTIATE_EYE (uint64NDArray); |
|
1562 INSTANTIATE_EYE (NDArray); |
|
1563 INSTANTIATE_EYE (boolNDArray); |
|
1564 |
4945
|
1565 static octave_value |
4948
|
1566 identity_matrix (int nr, int nc, oct_data_conv::data_type dt) |
4945
|
1567 { |
|
1568 octave_value retval; |
|
1569 |
4946
|
1570 // XXX FIXME XXX -- perhaps this should be made extensible by using |
|
1571 // the class name to lookup a function to call to create the new |
|
1572 // value. |
|
1573 |
|
1574 if (! error_state) |
|
1575 { |
|
1576 switch (dt) |
|
1577 { |
|
1578 case oct_data_conv::dt_int8: |
|
1579 retval = identity_matrix<int8NDArray> (nr, nc); |
|
1580 break; |
|
1581 |
|
1582 case oct_data_conv::dt_uint8: |
|
1583 retval = identity_matrix<uint8NDArray> (nr, nc); |
|
1584 break; |
|
1585 |
|
1586 case oct_data_conv::dt_int16: |
|
1587 retval = identity_matrix<int16NDArray> (nr, nc); |
|
1588 break; |
4945
|
1589 |
4946
|
1590 case oct_data_conv::dt_uint16: |
|
1591 retval = identity_matrix<uint16NDArray> (nr, nc); |
|
1592 break; |
|
1593 |
|
1594 case oct_data_conv::dt_int32: |
|
1595 retval = identity_matrix<int32NDArray> (nr, nc); |
|
1596 break; |
|
1597 |
|
1598 case oct_data_conv::dt_uint32: |
|
1599 retval = identity_matrix<uint32NDArray> (nr, nc); |
|
1600 break; |
4945
|
1601 |
4946
|
1602 case oct_data_conv::dt_int64: |
|
1603 retval = identity_matrix<int64NDArray> (nr, nc); |
|
1604 break; |
|
1605 |
|
1606 case oct_data_conv::dt_uint64: |
|
1607 retval = identity_matrix<uint64NDArray> (nr, nc); |
|
1608 break; |
4945
|
1609 |
4946
|
1610 case oct_data_conv::dt_single: // XXX FIXME XXX |
|
1611 case oct_data_conv::dt_double: |
|
1612 retval = identity_matrix<NDArray> (nr, nc); |
|
1613 break; |
4945
|
1614 |
4986
|
1615 case oct_data_conv::dt_logical: |
|
1616 retval = identity_matrix<boolNDArray> (nr, nc); |
|
1617 break; |
|
1618 |
4946
|
1619 default: |
|
1620 error ("eye: invalid class name"); |
|
1621 break; |
4945
|
1622 } |
|
1623 } |
|
1624 |
|
1625 return retval; |
|
1626 } |
|
1627 |
4946
|
1628 #undef INT_EYE_MATRIX |
|
1629 |
1957
|
1630 DEFUN (eye, args, , |
3369
|
1631 "-*- texinfo -*-\n\ |
|
1632 @deftypefn {Built-in Function} {} eye (@var{x})\n\ |
|
1633 @deftypefnx {Built-in Function} {} eye (@var{n}, @var{m})\n\ |
4948
|
1634 @deftypefnx {Built-in Function} {} eye (@dots{}, @var{class})\n\ |
3369
|
1635 Return an identity matrix. If invoked with a single scalar argument,\n\ |
|
1636 @code{eye} returns a square matrix with the dimension specified. If you\n\ |
|
1637 supply two scalar arguments, @code{eye} takes them to be the number of\n\ |
|
1638 rows and columns. If given a vector with two elements, @code{eye} uses\n\ |
|
1639 the values of the elements as the number of rows and columns,\n\ |
|
1640 respectively. For example,\n\ |
|
1641 \n\ |
|
1642 @example\n\ |
|
1643 @group\n\ |
|
1644 eye (3)\n\ |
|
1645 @result{} 1 0 0\n\ |
|
1646 0 1 0\n\ |
|
1647 0 0 1\n\ |
|
1648 @end group\n\ |
|
1649 @end example\n\ |
|
1650 \n\ |
|
1651 The following expressions all produce the same result:\n\ |
|
1652 \n\ |
|
1653 @example\n\ |
|
1654 @group\n\ |
|
1655 eye (2)\n\ |
|
1656 @equiv{}\n\ |
|
1657 eye (2, 2)\n\ |
|
1658 @equiv{}\n\ |
|
1659 eye (size ([1, 2; 3, 4])\n\ |
|
1660 @end group\n\ |
|
1661 @end example\n\ |
|
1662 \n\ |
4945
|
1663 The optional argument @var{class}, allows @code{eye} to return an array of\n\ |
|
1664 the specified type, like\n\ |
|
1665 \n\ |
|
1666 @example\n\ |
|
1667 val = zeros (n,m, \"uint8\")\n\ |
|
1668 @end example\n\ |
|
1669 \n\ |
3369
|
1670 For compatibility with @sc{Matlab}, calling @code{eye} with no arguments\n\ |
|
1671 is equivalent to calling it with an argument of 1.\n\ |
|
1672 @end deftypefn") |
523
|
1673 { |
3354
|
1674 octave_value retval; |
523
|
1675 |
4948
|
1676 int nargin = args.length (); |
4945
|
1677 |
4948
|
1678 oct_data_conv::data_type dt = oct_data_conv::dt_double; |
523
|
1679 |
4945
|
1680 // Check for type information. |
|
1681 |
|
1682 if (nargin > 0 && args(nargin-1).is_string ()) |
|
1683 { |
4948
|
1684 std::string nm = args(nargin-1).string_value (); |
4945
|
1685 nargin--; |
4948
|
1686 |
|
1687 dt = oct_data_conv::string_to_data_type (nm); |
|
1688 |
|
1689 if (error_state) |
|
1690 return retval; |
4945
|
1691 } |
|
1692 |
523
|
1693 switch (nargin) |
|
1694 { |
712
|
1695 case 0: |
4948
|
1696 retval = identity_matrix (1, 1, dt); |
712
|
1697 break; |
777
|
1698 |
610
|
1699 case 1: |
3354
|
1700 { |
5275
|
1701 octave_idx_type nr, nc; |
3354
|
1702 get_dimensions (args(0), "eye", nr, nc); |
|
1703 |
|
1704 if (! error_state) |
4948
|
1705 retval = identity_matrix (nr, nc, dt); |
3354
|
1706 } |
610
|
1707 break; |
777
|
1708 |
523
|
1709 case 2: |
3354
|
1710 { |
5275
|
1711 octave_idx_type nr, nc; |
3354
|
1712 get_dimensions (args(0), args(1), "eye", nr, nc); |
|
1713 |
|
1714 if (! error_state) |
4948
|
1715 retval = identity_matrix (nr, nc, dt); |
3354
|
1716 } |
523
|
1717 break; |
777
|
1718 |
523
|
1719 default: |
|
1720 print_usage ("eye"); |
|
1721 break; |
|
1722 } |
|
1723 |
|
1724 return retval; |
|
1725 } |
|
1726 |
1957
|
1727 DEFUN (linspace, args, , |
3369
|
1728 "-*- texinfo -*-\n\ |
|
1729 @deftypefn {Built-in Function} {} linspace (@var{base}, @var{limit}, @var{n})\n\ |
|
1730 Return a row vector with @var{n} linearly spaced elements between\n\ |
|
1731 @var{base} and @var{limit}. The number of elements, @var{n}, must be\n\ |
|
1732 greater than 1. The @var{base} and @var{limit} are always included in\n\ |
|
1733 the range. If @var{base} is greater than @var{limit}, the elements are\n\ |
|
1734 stored in decreasing order. If the number of points is not specified, a\n\ |
|
1735 value of 100 is used.\n\ |
1100
|
1736 \n\ |
4455
|
1737 The @code{linspace} function always returns a row vector.\n\ |
3369
|
1738 @end deftypefn") |
1100
|
1739 { |
3418
|
1740 octave_value retval; |
1100
|
1741 |
|
1742 int nargin = args.length (); |
|
1743 |
|
1744 int npoints = 100; |
|
1745 |
1940
|
1746 if (nargin != 2 && nargin != 3) |
|
1747 { |
|
1748 print_usage ("linspace"); |
|
1749 return retval; |
|
1750 } |
|
1751 |
1100
|
1752 if (nargin == 3) |
4732
|
1753 npoints = args(2).int_value (); |
1100
|
1754 |
|
1755 if (! error_state) |
|
1756 { |
3322
|
1757 octave_value arg_1 = args(0); |
|
1758 octave_value arg_2 = args(1); |
1100
|
1759 |
3322
|
1760 if (arg_1.is_complex_type () || arg_2.is_complex_type ()) |
|
1761 { |
|
1762 Complex x1 = arg_1.complex_value (); |
|
1763 Complex x2 = arg_2.complex_value (); |
|
1764 |
|
1765 if (! error_state) |
1100
|
1766 { |
3322
|
1767 ComplexRowVector rv = linspace (x1, x2, npoints); |
1100
|
1768 |
|
1769 if (! error_state) |
3418
|
1770 retval = rv; |
1100
|
1771 } |
|
1772 } |
|
1773 else |
3322
|
1774 { |
|
1775 double x1 = arg_1.double_value (); |
|
1776 double x2 = arg_2.double_value (); |
|
1777 |
|
1778 if (! error_state) |
|
1779 { |
|
1780 RowVector rv = linspace (x1, x2, npoints); |
|
1781 |
|
1782 if (! error_state) |
3418
|
1783 retval = rv; |
3322
|
1784 } |
|
1785 } |
1100
|
1786 } |
4732
|
1787 else |
|
1788 error ("linspace: expecting third argument to be an integer"); |
1100
|
1789 |
|
1790 return retval; |
|
1791 } |
|
1792 |
4567
|
1793 DEFUN (reshape, args, , |
|
1794 "-*- texinfo -*-\n\ |
|
1795 @deftypefn {Function File} {} reshape (@var{a}, @var{m}, @var{n}, @dots{})\n\ |
|
1796 @deftypefnx {Function File} {} reshape (@var{a}, @var{siz})\n\ |
|
1797 Return a matrix with the given dimensions whose elements are taken\n\ |
|
1798 from the matrix @var{a}. The elements of the matrix are access in\n\ |
|
1799 column-major order (like Fortran arrays are stored).\n\ |
|
1800 \n\ |
|
1801 For example,\n\ |
|
1802 \n\ |
|
1803 @example\n\ |
|
1804 @group\n\ |
|
1805 reshape ([1, 2, 3, 4], 2, 2)\n\ |
|
1806 @result{} 1 3\n\ |
|
1807 2 4\n\ |
|
1808 @end group\n\ |
|
1809 @end example\n\ |
|
1810 \n\ |
|
1811 @noindent\n\ |
|
1812 Note that the total number of elements in the original\n\ |
|
1813 matrix must match the total number of elements in the new matrix.\n\ |
5013
|
1814 \n\ |
|
1815 A single dimension of the return matrix can be unknown and is flagged\n\ |
|
1816 by an empty argument.\n\ |
4567
|
1817 @end deftypefn") |
|
1818 { |
|
1819 octave_value retval; |
|
1820 |
|
1821 int nargin = args.length (); |
|
1822 |
|
1823 Array<int> new_size; |
|
1824 |
|
1825 if (nargin == 2) |
|
1826 new_size = args(1).int_vector_value (); |
|
1827 else if (nargin > 2) |
|
1828 { |
|
1829 new_size.resize (nargin-1); |
5013
|
1830 int empty_dim = -1; |
|
1831 |
4567
|
1832 for (int i = 1; i < nargin; i++) |
|
1833 { |
5013
|
1834 if (args(i).is_empty ()) |
|
1835 if (empty_dim > 0) |
|
1836 { |
|
1837 error ("reshape: only a single dimension can be unknown"); |
|
1838 break; |
|
1839 } |
|
1840 else |
|
1841 { |
|
1842 empty_dim = i; |
|
1843 new_size(i-1) = 1; |
|
1844 } |
|
1845 else |
|
1846 { |
|
1847 new_size(i-1) = args(i).int_value (); |
4567
|
1848 |
5013
|
1849 if (error_state) |
|
1850 break; |
|
1851 } |
|
1852 } |
|
1853 |
|
1854 if (! error_state && (empty_dim > 0)) |
|
1855 { |
|
1856 int nel = 1; |
|
1857 for (int i = 0; i < nargin - 1; i++) |
|
1858 nel *= new_size(i); |
|
1859 |
|
1860 if (nel == 0) |
|
1861 new_size(empty_dim-1) = 0; |
|
1862 else |
|
1863 { |
|
1864 int size_empty_dim = args(0).numel () / nel; |
|
1865 |
|
1866 if (args(0).numel () != size_empty_dim * nel) |
|
1867 error ("reshape: size is not divisble by the product of known dimensions (= %d)", nel); |
|
1868 else |
|
1869 new_size(empty_dim-1) = size_empty_dim; |
|
1870 } |
4567
|
1871 } |
|
1872 } |
|
1873 else |
|
1874 { |
|
1875 print_usage ("reshape"); |
|
1876 return retval; |
|
1877 } |
|
1878 |
|
1879 if (error_state) |
|
1880 { |
|
1881 error ("reshape: invalid arguments"); |
|
1882 return retval; |
|
1883 } |
|
1884 |
4739
|
1885 // Remove trailing singletons in new_size, but leave at least 2 |
|
1886 // elements. |
|
1887 |
4567
|
1888 int n = new_size.length (); |
|
1889 |
4739
|
1890 while (n > 2) |
|
1891 { |
|
1892 if (new_size(n-1) == 1) |
|
1893 n--; |
|
1894 else |
|
1895 break; |
|
1896 } |
|
1897 |
|
1898 new_size.resize (n); |
|
1899 |
4567
|
1900 if (n < 2) |
|
1901 { |
|
1902 error ("reshape: expecting size to be vector with at least 2 elements"); |
|
1903 return retval; |
|
1904 } |
|
1905 |
|
1906 dim_vector new_dims; |
|
1907 |
|
1908 new_dims.resize (n); |
|
1909 |
5275
|
1910 for (octave_idx_type i = 0; i < n; i++) |
4567
|
1911 new_dims(i) = new_size(i); |
|
1912 |
|
1913 octave_value arg = args(0); |
|
1914 |
|
1915 if (new_dims.numel () == arg.numel ()) |
|
1916 retval = (new_dims == arg.dims ()) ? arg : arg.reshape (new_dims); |
|
1917 else |
|
1918 error ("reshape: size mismatch"); |
|
1919 |
|
1920 return retval; |
|
1921 } |
|
1922 |
4532
|
1923 DEFUN (squeeze, args, , |
|
1924 "-*- texinfo -*-\n\ |
|
1925 @deftypefn {Built-in Function} {} squeeze (@var{x})\n\ |
|
1926 Remove singleton dimensions from @var{x} and return the result.\n\ |
|
1927 @end deftypefn") |
|
1928 { |
|
1929 octave_value retval; |
|
1930 |
|
1931 if (args.length () == 1) |
4545
|
1932 retval = args(0).squeeze (); |
4532
|
1933 else |
|
1934 print_usage ("squeeze"); |
|
1935 |
|
1936 return retval; |
|
1937 } |
|
1938 |
2184
|
1939 void |
|
1940 symbols_of_data (void) |
|
1941 { |
3321
|
1942 |
|
1943 #define IMAGINARY_DOC_STRING "-*- texinfo -*-\n\ |
5333
|
1944 @defvr {Built-in Constant} I\n\ |
|
1945 @defvrx {Built-in Constant} J\n\ |
|
1946 @defvrx {Built-in Constant} i\n\ |
|
1947 @defvrx {Built-in Constant} j\n\ |
3321
|
1948 A pure imaginary number, defined as\n\ |
|
1949 @iftex\n\ |
|
1950 @tex\n\ |
|
1951 $\\sqrt{-1}$.\n\ |
|
1952 @end tex\n\ |
|
1953 @end iftex\n\ |
|
1954 @ifinfo\n\ |
|
1955 @code{sqrt (-1)}.\n\ |
|
1956 @end ifinfo\n\ |
4845
|
1957 These built-in variables behave like functions so you can use the names\n\ |
|
1958 for other purposes. If you use them as variables and assign values to\n\ |
|
1959 them and then clear them, they once again assume their special predefined\n\ |
|
1960 values @xref{Status of Variables}.\n\ |
3321
|
1961 @end defvr" |
|
1962 |
|
1963 #define INFINITY_DOC_STRING "-*- texinfo -*-\n\ |
5333
|
1964 @defvr {Built-in Constant} Inf\n\ |
|
1965 @defvrx {Built-in Constant} inf\n\ |
3321
|
1966 Infinity. This is the result of an operation like 1/0, or an operation\n\ |
|
1967 that results in a floating point overflow.\n\ |
|
1968 @end defvr" |
|
1969 |
|
1970 #define NAN_DOC_STRING "-*- texinfo -*-\n\ |
5333
|
1971 @defvr {Built-in Constant} NaN\n\ |
|
1972 @defvrx {Built-in Constant} nan\n\ |
3321
|
1973 Not a number. This is the result of an operation like\n\ |
|
1974 @iftex\n\ |
|
1975 @tex\n\ |
|
1976 $0/0$, or $\\infty - \\infty$,\n\ |
|
1977 @end tex\n\ |
|
1978 @end iftex\n\ |
|
1979 @ifinfo\n\ |
|
1980 0/0, or @samp{Inf - Inf},\n\ |
|
1981 @end ifinfo\n\ |
|
1982 or any operation with a NaN.\n\ |
|
1983 \n\ |
|
1984 Note that NaN always compares not equal to NaN. This behavior is\n\ |
|
1985 specified by the IEEE standard for floating point arithmetic. To\n\ |
|
1986 find NaN values, you must use the @code{isnan} function.\n\ |
|
1987 @end defvr" |
|
1988 |
3141
|
1989 DEFCONST (I, Complex (0.0, 1.0), |
3321
|
1990 IMAGINARY_DOC_STRING); |
2184
|
1991 |
4102
|
1992 DEFCONST (Inf, lo_ieee_inf_value (), |
3321
|
1993 INFINITY_DOC_STRING); |
2184
|
1994 |
3141
|
1995 DEFCONST (J, Complex (0.0, 1.0), |
3321
|
1996 IMAGINARY_DOC_STRING); |
2184
|
1997 |
4102
|
1998 DEFCONST (NA, lo_ieee_na_value (), |
4025
|
1999 "-*- texinfo -*-\n\ |
5333
|
2000 @defvr {Built-in Constant} NA\n\ |
4025
|
2001 Missing value.\n\ |
|
2002 @end defvr"); |
|
2003 |
4102
|
2004 DEFCONST (NaN, lo_ieee_nan_value (), |
3321
|
2005 NAN_DOC_STRING); |
2184
|
2006 |
|
2007 #if defined (M_E) |
|
2008 double e_val = M_E; |
|
2009 #else |
|
2010 double e_val = exp (1.0); |
|
2011 #endif |
|
2012 |
3141
|
2013 DEFCONST (e, e_val, |
3321
|
2014 "-*- texinfo -*-\n\ |
5333
|
2015 @defvr {Built-in Constant} e\n\ |
3321
|
2016 The base of natural logarithms. The constant\n\ |
|
2017 @iftex\n\ |
|
2018 @tex\n\ |
|
2019 $e$\n\ |
|
2020 @end tex\n\ |
|
2021 @end iftex\n\ |
|
2022 @ifinfo\n\ |
|
2023 @var{e}\n\ |
|
2024 @end ifinfo\n\ |
|
2025 satisfies the equation\n\ |
|
2026 @iftex\n\ |
|
2027 @tex\n\ |
|
2028 $\\log (e) = 1$.\n\ |
|
2029 @end tex\n\ |
|
2030 @end iftex\n\ |
|
2031 @ifinfo\n\ |
|
2032 @code{log} (@var{e}) = 1.\n\ |
|
2033 @end ifinfo\n\ |
|
2034 @end defvr"); |
2184
|
2035 |
3141
|
2036 DEFCONST (eps, DBL_EPSILON, |
3321
|
2037 "-*- texinfo -*-\n\ |
5333
|
2038 @defvr {Built-in Constant} eps\n\ |
3321
|
2039 The machine precision. More precisely, @code{eps} is the largest\n\ |
|
2040 relative spacing between any two adjacent numbers in the machine's\n\ |
|
2041 floating point system. This number is obviously system-dependent. On\n\ |
|
2042 machines that support 64 bit IEEE floating point arithmetic, @code{eps}\n\ |
|
2043 is approximately\n\ |
|
2044 @ifinfo\n\ |
|
2045 2.2204e-16.\n\ |
|
2046 @end ifinfo\n\ |
|
2047 @iftex\n\ |
|
2048 @tex\n\ |
|
2049 $2.2204\\times10^{-16}$.\n\ |
|
2050 @end tex\n\ |
|
2051 @end iftex\n\ |
|
2052 @end defvr"); |
2184
|
2053 |
3258
|
2054 DEFCONST (false, false, |
3443
|
2055 "-*- texinfo -*-\n\ |
5333
|
2056 @defvr {Built-in Constant} false\n\ |
3443
|
2057 Logical false value.\n\ |
5333
|
2058 @seealso{true}\n\ |
3443
|
2059 @end defvr"); |
3258
|
2060 |
3141
|
2061 DEFCONST (i, Complex (0.0, 1.0), |
3321
|
2062 IMAGINARY_DOC_STRING); |
2184
|
2063 |
4102
|
2064 DEFCONST (inf, lo_ieee_inf_value (), |
3321
|
2065 INFINITY_DOC_STRING); |
2184
|
2066 |
3141
|
2067 DEFCONST (j, Complex (0.0, 1.0), |
3321
|
2068 IMAGINARY_DOC_STRING); |
2184
|
2069 |
4102
|
2070 DEFCONST (nan, lo_ieee_nan_value (), |
3321
|
2071 NAN_DOC_STRING); |
2184
|
2072 |
|
2073 #if defined (M_PI) |
|
2074 double pi_val = M_PI; |
|
2075 #else |
|
2076 double pi_val = 4.0 * atan (1.0); |
|
2077 #endif |
|
2078 |
3141
|
2079 DEFCONST (pi, pi_val, |
3321
|
2080 "-*- texinfo -*-\n\ |
5333
|
2081 @defvr {Built-in Constant} pi\n\ |
3321
|
2082 The ratio of the circumference of a circle to its diameter.\n\ |
|
2083 Internally, @code{pi} is computed as @samp{4.0 * atan (1.0)}.\n\ |
|
2084 @end defvr"); |
2184
|
2085 |
3141
|
2086 DEFCONST (realmax, DBL_MAX, |
3321
|
2087 "-*- texinfo -*-\n\ |
5333
|
2088 @defvr {Built-in Constant} realmax\n\ |
3321
|
2089 The largest floating point number that is representable. The actual\n\ |
4303
|
2090 value is system-dependent. On machines that support 64-bit IEEE\n\ |
3321
|
2091 floating point arithmetic, @code{realmax} is approximately\n\ |
|
2092 @ifinfo\n\ |
|
2093 1.7977e+308\n\ |
|
2094 @end ifinfo\n\ |
|
2095 @iftex\n\ |
5333
|
2096 @seealso{realmin}\n\ |
3321
|
2097 @tex\n\ |
|
2098 $1.7977\\times10^{308}$.\n\ |
|
2099 @end tex\n\ |
|
2100 @end iftex\n\ |
|
2101 @end defvr"); |
2184
|
2102 |
3141
|
2103 DEFCONST (realmin, DBL_MIN, |
3321
|
2104 "-*- texinfo -*-\n\ |
5333
|
2105 @defvr {Built-in Constant} realmin\n\ |
4303
|
2106 The smallest normalized floating point number that is representable.\n\ |
|
2107 The actual value is system-dependent. On machines that support\n\ |
|
2108 64-bit IEEE floating point arithmetic, @code{realmin} is approximately\n\ |
3321
|
2109 @ifinfo\n\ |
|
2110 2.2251e-308\n\ |
|
2111 @end ifinfo\n\ |
|
2112 @iftex\n\ |
|
2113 @tex\n\ |
|
2114 $2.2251\\times10^{-308}$.\n\ |
|
2115 @end tex\n\ |
|
2116 @end iftex\n\ |
5333
|
2117 @seealso{realmax}\n\ |
3321
|
2118 @end defvr"); |
2188
|
2119 |
3258
|
2120 DEFCONST (true, true, |
3443
|
2121 "-*- texinfo -*-\n\ |
5333
|
2122 @defvr {Built-in Constant} true\n\ |
3443
|
2123 Logical true value.\n\ |
5333
|
2124 @seealso{false}\n\ |
3443
|
2125 @end defvr"); |
3354
|
2126 |
2184
|
2127 } |
|
2128 |
523
|
2129 /* |
|
2130 ;;; Local Variables: *** |
|
2131 ;;; mode: C++ *** |
|
2132 ;;; End: *** |
|
2133 */ |