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 |
5775
|
435 // FIXME -- we could eliminate some duplicate code here with |
3972
|
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 |
5714
|
692 if (n_args == 1) |
|
693 retval = Matrix (); |
|
694 else if (n_args == 2) |
|
695 retval = args(1); |
5507
|
696 else if (n_args > 2) |
4824
|
697 { |
5275
|
698 octave_idx_type dim = args(0).int_value () - 1; |
4806
|
699 |
4824
|
700 if (error_state) |
4806
|
701 { |
4824
|
702 error ("cat: expecting first argument to be a integer"); |
4806
|
703 return retval; |
|
704 } |
|
705 |
4824
|
706 if (dim >= 0) |
|
707 { |
4915
|
708 |
|
709 dim_vector dv = args(1).dims (); |
4824
|
710 |
4915
|
711 for (int i = 2; i < args.length (); i++) |
|
712 { |
|
713 // add_dims constructs a dimension vector which holds the |
4824
|
714 // dimensions of the final array after concatenation. |
4806
|
715 |
4915
|
716 if (! dv.concat (args(i).dims (), dim)) |
4806
|
717 { |
4824
|
718 // Dimensions do not match. |
4915
|
719 error ("cat: dimension mismatch"); |
4806
|
720 return retval; |
|
721 } |
4824
|
722 } |
|
723 |
4915
|
724 // The lines below might seem crazy, since we take a copy |
|
725 // of the first argument, resize it to be empty and then resize |
|
726 // it to be full. This is done since it means that there is no |
|
727 // recopying of data, as would happen if we used a single resize. |
|
728 // It should be noted that resize operation is also significantly |
|
729 // slower than the do_cat_op function, so it makes sense to have an |
|
730 // empty matrix and copy all data. |
4824
|
731 // |
4915
|
732 // We might also start with a empty octave_value using |
|
733 // tmp = octave_value_typeinfo::lookup_type (args(1).type_name()); |
|
734 // and then directly resize. However, for some types there might be |
|
735 // some additional setup needed, and so this should be avoided. |
5533
|
736 |
4915
|
737 octave_value tmp; |
5533
|
738 |
4915
|
739 for (int i = 1; i < n_args; i++) |
5533
|
740 { |
|
741 if (! args (i).all_zero_dims ()) |
|
742 { |
|
743 tmp = args (i); |
|
744 break; |
|
745 } |
|
746 } |
5164
|
747 |
5533
|
748 tmp = tmp.resize (dim_vector (0,0)).resize (dv); |
4824
|
749 |
4915
|
750 if (error_state) |
|
751 return retval; |
4824
|
752 |
4915
|
753 Array<int> ra_idx (dv.length (), 0); |
5533
|
754 |
4915
|
755 for (int i = 1; i < n_args; i++) |
|
756 { |
|
757 tmp = do_cat_op (tmp, args (i), ra_idx); |
4824
|
758 |
4915
|
759 if (error_state) |
|
760 return retval; |
4806
|
761 |
4915
|
762 dim_vector dv_tmp = args (i).dims (); |
5533
|
763 |
4915
|
764 ra_idx (dim) += (dim < dv_tmp.length () ? dv_tmp (dim) : 1); |
|
765 } |
4806
|
766 |
5533
|
767 retval = tmp; |
4806
|
768 } |
5533
|
769 else |
|
770 error ("%s: invalid dimension argument", fname.c_str ()); |
4806
|
771 } |
|
772 else |
4824
|
773 print_usage (fname); |
4806
|
774 |
|
775 return retval; |
|
776 } |
|
777 |
|
778 DEFUN (horzcat, args, , |
4824
|
779 "-*- texinfo -*-\n\ |
4806
|
780 @deftypefn {Built-in Function} {} horzcat (@var{array1}, @var{array2}, @dots{}, @var{arrayN})\n\ |
|
781 Return the horizontal concatenation of N-d array objects, @var{array1},\n\ |
|
782 @var{array2}, @dots{}, @var{arrayN} along dimension 2.\n\ |
5642
|
783 @seealso{cat, vertcat}\n\ |
|
784 @end deftypefn") |
4806
|
785 { |
|
786 octave_value_list args_tmp = args; |
|
787 |
|
788 int dim = 2; |
|
789 |
|
790 octave_value d (dim); |
|
791 |
|
792 args_tmp.prepend (d); |
|
793 |
4824
|
794 return do_cat (args_tmp, "horzcat"); |
4806
|
795 } |
|
796 |
|
797 DEFUN (vertcat, args, , |
|
798 "-*- texinfo -*-\n\ |
|
799 @deftypefn {Built-in Function} {} vertcat (@var{array1}, @var{array2}, @dots{}, @var{arrayN})\n\ |
|
800 Return the vertical concatenation of N-d array objects, @var{array1},\n\ |
|
801 @var{array2}, @dots{}, @var{arrayN} along dimension 1.\n\ |
5642
|
802 @seealso{cat, horzcat}\n\ |
|
803 @end deftypefn") |
4806
|
804 { |
|
805 octave_value_list args_tmp = args; |
|
806 |
|
807 int dim = 1; |
|
808 |
|
809 octave_value d (dim); |
|
810 |
|
811 args_tmp.prepend (d); |
|
812 |
4824
|
813 return do_cat (args_tmp, "vertcat"); |
4806
|
814 } |
|
815 |
4758
|
816 DEFUN (cat, args, , |
|
817 "-*- texinfo -*-\n\ |
|
818 @deftypefn {Built-in Function} {} cat (@var{dim}, @var{array1}, @var{array2}, @dots{}, @var{arrayN})\n\ |
4806
|
819 Return the concatenation of N-d array objects, @var{array1},\n\ |
|
820 @var{array2}, @dots{}, @var{arrayN} along dimension @var{dim}.\n\ |
4758
|
821 \n\ |
|
822 @example\n\ |
|
823 @group\n\ |
|
824 A = ones (2, 2);\n\ |
|
825 B = zeros (2, 2);\n\ |
|
826 cat (2, A, B)\n\ |
|
827 @result{} ans =\n\ |
|
828 \n\ |
|
829 1 1 0 0\n\ |
|
830 1 1 0 0\n\ |
|
831 @end group\n\ |
|
832 @end example\n\ |
|
833 \n\ |
|
834 Alternatively, we can concatenate @var{A} and @var{B} along the\n\ |
|
835 second dimension the following way:\n\ |
|
836 \n\ |
|
837 @example\n\ |
|
838 @group\n\ |
|
839 [A, B].\n\ |
|
840 @end group\n\ |
|
841 @end example\n\ |
|
842 \n\ |
|
843 @var{dim} can be larger than the dimensions of the N-d array objects\n\ |
|
844 and the result will thus have @var{dim} dimensions as the\n\ |
|
845 following example shows:\n\ |
|
846 @example\n\ |
|
847 @group\n\ |
|
848 cat (4, ones(2, 2), zeros (2, 2))\n\ |
|
849 @result{} ans =\n\ |
|
850 \n\ |
|
851 ans(:,:,1,1) =\n\ |
|
852 \n\ |
|
853 1 1\n\ |
|
854 1 1\n\ |
|
855 \n\ |
|
856 ans(:,:,1,2) =\n\ |
|
857 0 0\n\ |
|
858 0 0\n\ |
|
859 @end group\n\ |
|
860 @end example\n\ |
5642
|
861 @seealso{horzcat, vertcat}\n\ |
|
862 @end deftypefn") |
4758
|
863 { |
4824
|
864 return do_cat (args, "cat"); |
4758
|
865 } |
|
866 |
4593
|
867 static octave_value |
|
868 do_permute (const octave_value_list& args, bool inv, const std::string& fname) |
|
869 { |
|
870 octave_value retval; |
|
871 |
5148
|
872 if (args.length () == 2 && args(1).length () >= args(1).ndims ()) |
4593
|
873 { |
|
874 Array<int> vec = args(1).int_vector_value (); |
|
875 |
5775
|
876 // FIXME -- maybe we should create an idx_vector object |
5148
|
877 // here and pass that to permute? |
|
878 |
|
879 int n = vec.length (); |
|
880 |
|
881 for (int i = 0; i < n; i++) |
|
882 vec(i)--; |
|
883 |
4593
|
884 octave_value ret = args(0).permute (vec, inv); |
|
885 |
|
886 if (! error_state) |
|
887 retval = ret; |
|
888 } |
|
889 else |
|
890 print_usage (fname); |
|
891 |
|
892 return retval; |
|
893 } |
|
894 |
|
895 DEFUN (permute, args, , |
|
896 "-*- texinfo -*-\n\ |
|
897 @deftypefn {Built-in Function} {} permute (@var{a}, @var{perm})\n\ |
|
898 Return the generalized transpose for an N-d array object @var{a}.\n\ |
|
899 The permutation vector @var{perm} must contain the elements\n\ |
|
900 @code{1:ndims(a)} (in any order, but each element must appear just once).\n\ |
5642
|
901 @seealso{ipermute}\n\ |
|
902 @end deftypefn") |
4593
|
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\ |
5642
|
916 @seealso{permute}\n\ |
|
917 @end deftypefn") |
4593
|
918 { |
|
919 return do_permute (args, true, "ipermute"); |
|
920 } |
|
921 |
3195
|
922 DEFUN (length, args, , |
3373
|
923 "-*- texinfo -*-\n\ |
|
924 @deftypefn {Built-in Function} {} length (@var{a})\n\ |
4176
|
925 Return the `length' of the object @var{a}. For matrix objects, the\n\ |
3373
|
926 length is the number of rows or columns, whichever is greater (this\n\ |
|
927 odd definition is used for compatibility with Matlab).\n\ |
|
928 @end deftypefn") |
3195
|
929 { |
|
930 octave_value retval; |
|
931 |
|
932 if (args.length () == 1) |
|
933 { |
|
934 int len = args(0).length (); |
|
935 |
|
936 if (! error_state) |
4233
|
937 retval = len; |
3195
|
938 } |
|
939 else |
|
940 print_usage ("length"); |
|
941 |
|
942 return retval; |
|
943 } |
|
944 |
4554
|
945 DEFUN (ndims, args, , |
|
946 "-*- texinfo -*-\n\ |
|
947 @deftypefn {Built-in Function} {} ndims (@var{a})\n\ |
|
948 Returns the number of dimensions of array @var{a}.\n\ |
|
949 For any array, the result will always be larger than or equal to 2.\n\ |
|
950 Trailing singleton dimensions are not counted.\n\ |
|
951 @end deftypefn") |
|
952 { |
|
953 octave_value retval; |
|
954 |
|
955 if (args.length () == 1) |
|
956 { |
|
957 int n_dims = args(0).ndims (); |
|
958 |
|
959 if (! error_state) |
|
960 retval = n_dims; |
|
961 } |
|
962 else |
|
963 print_usage ("ndims"); |
|
964 |
|
965 return retval; |
|
966 } |
|
967 |
4559
|
968 DEFUN (numel, args, , |
|
969 "-*- texinfo -*-\n\ |
|
970 @deftypefn {Built-in Function} {} numel (@var{a})\n\ |
|
971 Returns the number of elements in the object @var{a}.\n\ |
5724
|
972 @seealso{size}\n\ |
4559
|
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\ |
5724
|
1026 @seealso{numel}\n\ |
3373
|
1027 @end deftypefn") |
523
|
1028 { |
2086
|
1029 octave_value_list retval; |
523
|
1030 |
|
1031 int nargin = args.length (); |
|
1032 |
4513
|
1033 if (nargin == 1) |
523
|
1034 { |
4513
|
1035 dim_vector dimensions = args(0).dims (); |
|
1036 |
|
1037 int ndims = dimensions.length (); |
1031
|
1038 |
4513
|
1039 Matrix m (1, ndims); |
|
1040 |
|
1041 if (nargout > 1) |
523
|
1042 { |
4513
|
1043 while (ndims--) |
|
1044 retval(ndims) = dimensions(ndims); |
523
|
1045 } |
4513
|
1046 else |
712
|
1047 { |
4513
|
1048 for (int i = 0; i < ndims; i++) |
|
1049 m(0, i) = dimensions(i); |
|
1050 |
|
1051 retval(0) = m; |
712
|
1052 } |
1031
|
1053 } |
|
1054 else if (nargin == 2 && nargout < 2) |
|
1055 { |
5275
|
1056 octave_idx_type nd = args(1).int_value (true); |
1031
|
1057 |
|
1058 if (error_state) |
|
1059 error ("size: expecting scalar as second argument"); |
712
|
1060 else |
1031
|
1061 { |
4741
|
1062 dim_vector dv = args(0).dims (); |
|
1063 |
4911
|
1064 if (nd > 0) |
|
1065 { |
|
1066 if (nd <= dv.length ()) |
|
1067 retval(0) = dv(nd-1); |
|
1068 else |
|
1069 retval(0) = 1; |
|
1070 } |
1031
|
1071 else |
4741
|
1072 error ("size: requested dimension (= %d) out of range", nd); |
1031
|
1073 } |
523
|
1074 } |
712
|
1075 else |
|
1076 print_usage ("size"); |
523
|
1077 |
|
1078 return retval; |
|
1079 } |
|
1080 |
5602
|
1081 DEFUN (nnz, args, , |
|
1082 "-*- texinfo -*-\n\ |
|
1083 @deftypefn {Loadable Function} {@var{scalar} =} nnz (@var{a})\n\ |
|
1084 returns number of non zero elements in @var{a}.\n\ |
|
1085 @seealso{sparse}\n\ |
|
1086 @end deftypefn") |
|
1087 { |
|
1088 octave_value retval; |
|
1089 |
|
1090 if (args.length () == 1) |
|
1091 retval = args(0).nnz (); |
|
1092 else |
|
1093 print_usage ("nnz"); |
|
1094 |
|
1095 return retval; |
|
1096 } |
|
1097 |
5604
|
1098 DEFUN (nzmax, args, , |
|
1099 "-*- texinfo -*-\n\ |
|
1100 @deftypefn {Loadable Function} {@var{scalar} =} nzmax (@var{SM})\n\ |
|
1101 Return the amount of storage allocated to the sparse matrix @var{SM}.\n\ |
|
1102 Note that Octave tends to crop unused memory at the first oppurtunity\n\ |
|
1103 for sparse objects. There are some cases of user created sparse objects\n\ |
|
1104 where the value returned by @dfn{nzmaz} will not be the same as @dfn{nnz},\n\ |
|
1105 but in general they will give the same result.\n\ |
|
1106 @seealso{sparse, spalloc}\n\ |
|
1107 @end deftypefn") |
|
1108 { |
|
1109 octave_value retval; |
|
1110 |
|
1111 if (args.length() == 1) |
|
1112 retval = args(0).nzmax (); |
|
1113 else |
|
1114 print_usage ("nzmax"); |
|
1115 |
|
1116 return retval; |
|
1117 } |
|
1118 |
5677
|
1119 DEFUN (rows, args, , |
|
1120 "-*- texinfo -*-\n\ |
|
1121 @deftypefn {Built-in Function} {} rows (@var{a})\n\ |
|
1122 Return the number of rows of @var{a}.\n\ |
5724
|
1123 @seealso{size, numel, columns, length, isscalar, isvector, ismatrix}\n\ |
5677
|
1124 @end deftypefn") |
|
1125 { |
|
1126 octave_value retval; |
|
1127 |
|
1128 if (args.length () == 1) |
|
1129 retval = args(0).rows (); |
|
1130 else |
|
1131 print_usage ("rows"); |
|
1132 |
|
1133 return retval; |
|
1134 } |
|
1135 |
|
1136 DEFUN (columns, args, , |
|
1137 "-*- texinfo -*-\n\ |
|
1138 @deftypefn {Built-in Function} {} columns (@var{a})\n\ |
|
1139 Return the number of columns of @var{a}.\n\ |
5724
|
1140 @seealso{size, numel, rows, length, isscalar, isvector, and ismatrix}\n\ |
5677
|
1141 @end deftypefn") |
|
1142 { |
|
1143 octave_value retval; |
|
1144 |
|
1145 if (args.length () == 1) |
|
1146 retval = args(0).columns (); |
|
1147 else |
|
1148 print_usage ("columns"); |
|
1149 |
|
1150 return retval; |
|
1151 } |
|
1152 |
1957
|
1153 DEFUN (sum, args, , |
3428
|
1154 "-*- texinfo -*-\n\ |
3723
|
1155 @deftypefn {Built-in Function} {} sum (@var{x}, @var{dim})\n\ |
|
1156 Sum of elements along dimension @var{dim}. If @var{dim} is\n\ |
|
1157 omitted, it defaults to 1 (column-wise sum).\n\ |
5061
|
1158 \n\ |
|
1159 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ |
|
1160 return the sum of the elements.\n\ |
3428
|
1161 @end deftypefn") |
523
|
1162 { |
3723
|
1163 DATA_REDUCTION (sum); |
523
|
1164 } |
|
1165 |
1957
|
1166 DEFUN (sumsq, args, , |
3428
|
1167 "-*- texinfo -*-\n\ |
3723
|
1168 @deftypefn {Built-in Function} {} sumsq (@var{x}, @var{dim})\n\ |
|
1169 Sum of squares of elements along dimension @var{dim}. If @var{dim}\n\ |
|
1170 is omitted, it defaults to 1 (column-wise sum of squares).\n\ |
3095
|
1171 \n\ |
5061
|
1172 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ |
|
1173 return the sum of squares of the elements.\n\ |
|
1174 \n\ |
|
1175 This function is conceptually equivalent to computing\n\ |
3723
|
1176 @example\n\ |
|
1177 sum (x .* conj (x), dim)\n\ |
|
1178 @end example\n\ |
|
1179 but it uses less memory and avoids calling conj if @var{x} is real.\n\ |
3428
|
1180 @end deftypefn") |
523
|
1181 { |
3723
|
1182 DATA_REDUCTION (sumsq); |
523
|
1183 } |
|
1184 |
4028
|
1185 DEFUN (isbool, args, , |
3428
|
1186 "-*- texinfo -*-\n\ |
4028
|
1187 @deftypefn {Built-in Functio} {} isbool (@var{x})\n\ |
3428
|
1188 Return true if @var{x} is a boolean object.\n\ |
3439
|
1189 @end deftypefn") |
3209
|
1190 { |
|
1191 octave_value retval; |
|
1192 |
|
1193 if (args.length () == 1) |
3258
|
1194 retval = args(0).is_bool_type (); |
3209
|
1195 else |
4028
|
1196 print_usage ("isbool"); |
3209
|
1197 |
|
1198 return retval; |
|
1199 } |
|
1200 |
4028
|
1201 DEFALIAS (islogical, isbool); |
3209
|
1202 |
4028
|
1203 DEFUN (iscomplex, args, , |
3428
|
1204 "-*- texinfo -*-\n\ |
4028
|
1205 @deftypefn {Built-in Function} {} iscomplex (@var{x})\n\ |
3428
|
1206 Return true if @var{x} is a complex-valued numeric object.\n\ |
|
1207 @end deftypefn") |
3186
|
1208 { |
|
1209 octave_value retval; |
|
1210 |
|
1211 if (args.length () == 1) |
3258
|
1212 retval = args(0).is_complex_type (); |
3186
|
1213 else |
4028
|
1214 print_usage ("iscomplex"); |
3186
|
1215 |
|
1216 return retval; |
|
1217 } |
|
1218 |
5775
|
1219 // FIXME -- perhaps this should be implemented with an |
5476
|
1220 // octave_value member function? |
|
1221 |
|
1222 DEFUN (complex, args, , |
|
1223 "-*- texinfo -*-\n\ |
|
1224 @deftypefn {Built-in Function} {} complex (@var{val})\n\ |
|
1225 @deftypefnx {Built-in Function} {} complex (@var{re}, @var{im})\n\ |
|
1226 Convert @var{x} to a complex value.\n\ |
|
1227 @end deftypefn") |
|
1228 { |
|
1229 octave_value retval; |
|
1230 |
|
1231 int nargin = args.length (); |
|
1232 |
|
1233 if (nargin == 1) |
|
1234 { |
|
1235 octave_value arg = args(0); |
|
1236 |
|
1237 if (arg.is_complex_type ()) |
|
1238 retval = arg; |
|
1239 else |
|
1240 { |
|
1241 if (arg.numel () == 1) |
|
1242 { |
|
1243 Complex val = arg.complex_value (); |
|
1244 |
|
1245 if (! error_state) |
|
1246 retval = octave_value (new octave_complex (val)); |
|
1247 } |
|
1248 else |
|
1249 { |
|
1250 ComplexNDArray val = arg.complex_array_value (); |
|
1251 |
|
1252 if (! error_state) |
|
1253 retval = octave_value (new octave_complex_matrix (val)); |
|
1254 } |
|
1255 |
|
1256 if (error_state) |
|
1257 error ("complex: invalid conversion"); |
|
1258 } |
|
1259 } |
|
1260 else if (nargin == 2) |
|
1261 { |
|
1262 octave_value re = args(0); |
|
1263 octave_value im = args(1); |
|
1264 |
|
1265 if (re.numel () == 1) |
|
1266 { |
|
1267 double re_val = re.double_value (); |
|
1268 |
|
1269 if (im.numel () == 1) |
|
1270 { |
|
1271 double im_val = im.double_value (); |
|
1272 |
|
1273 if (! error_state) |
|
1274 retval = octave_value (new octave_complex (Complex (re_val, im_val))); |
|
1275 } |
|
1276 else |
|
1277 { |
|
1278 const NDArray im_val = im.array_value (); |
|
1279 |
|
1280 if (! error_state) |
|
1281 { |
|
1282 ComplexNDArray result (im_val.dims (), Complex ()); |
|
1283 |
|
1284 for (octave_idx_type i = 0; i < im_val.numel (); i++) |
|
1285 result.xelem (i) = Complex (re_val, im_val(i)); |
|
1286 |
|
1287 retval = octave_value (new octave_complex_matrix (result)); |
|
1288 } |
|
1289 } |
|
1290 } |
|
1291 else |
|
1292 { |
|
1293 const NDArray re_val = re.array_value (); |
|
1294 |
|
1295 if (im.numel () == 1) |
|
1296 { |
|
1297 double im_val = im.double_value (); |
|
1298 |
|
1299 if (! error_state) |
|
1300 { |
|
1301 ComplexNDArray result (re_val.dims (), Complex ()); |
|
1302 |
|
1303 for (octave_idx_type i = 0; i < re_val.numel (); i++) |
|
1304 result.xelem (i) = Complex (re_val(i), im_val); |
|
1305 |
|
1306 retval = octave_value (new octave_complex_matrix (result)); |
|
1307 } |
|
1308 } |
|
1309 else |
|
1310 { |
|
1311 const NDArray im_val = im.array_value (); |
|
1312 |
|
1313 if (! error_state) |
|
1314 { |
|
1315 if (re_val.dims () == im_val.dims ()) |
|
1316 { |
|
1317 ComplexNDArray result (re_val.dims (), Complex ()); |
|
1318 |
|
1319 for (octave_idx_type i = 0; i < re_val.numel (); i++) |
|
1320 result.xelem (i) = Complex (re_val(i), im_val(i)); |
|
1321 |
|
1322 retval = octave_value (new octave_complex_matrix (result)); |
|
1323 } |
|
1324 else |
|
1325 error ("complex: dimension mismatch"); |
|
1326 } |
|
1327 } |
|
1328 } |
|
1329 |
|
1330 if (error_state) |
|
1331 error ("complex: invalid conversion"); |
|
1332 } |
|
1333 else |
|
1334 print_usage ("complex"); |
|
1335 |
|
1336 return retval; |
|
1337 } |
|
1338 |
|
1339 |
3258
|
1340 DEFUN (isreal, args, , |
3428
|
1341 "-*- texinfo -*-\n\ |
|
1342 @deftypefn {Built-in Function} {} isreal (@var{x})\n\ |
|
1343 Return true if @var{x} is a real-valued numeric object.\n\ |
|
1344 @end deftypefn") |
3258
|
1345 { |
|
1346 octave_value retval; |
|
1347 |
|
1348 if (args.length () == 1) |
|
1349 retval = args(0).is_real_type (); |
|
1350 else |
|
1351 print_usage ("isreal"); |
|
1352 |
|
1353 return retval; |
|
1354 } |
|
1355 |
3202
|
1356 DEFUN (isempty, args, , |
3373
|
1357 "-*- texinfo -*-\n\ |
|
1358 @deftypefn {Built-in Function} {} isempty (@var{a})\n\ |
|
1359 Return 1 if @var{a} is an empty matrix (either the number of rows, or\n\ |
|
1360 the number of columns, or both are zero). Otherwise, return 0.\n\ |
|
1361 @end deftypefn") |
3202
|
1362 { |
4233
|
1363 octave_value retval = false; |
3202
|
1364 |
|
1365 if (args.length () == 1) |
4559
|
1366 retval = args(0).is_empty (); |
3202
|
1367 else |
|
1368 print_usage ("isempty"); |
|
1369 |
|
1370 return retval; |
|
1371 } |
|
1372 |
3206
|
1373 DEFUN (isnumeric, args, , |
3428
|
1374 "-*- texinfo -*-\n\ |
|
1375 @deftypefn {Built-in Function} {} isnumeric (@var{x})\n\ |
|
1376 Return nonzero if @var{x} is a numeric object.\n\ |
|
1377 @end deftypefn") |
3206
|
1378 { |
|
1379 octave_value retval; |
|
1380 |
|
1381 if (args.length () == 1) |
3258
|
1382 retval = args(0).is_numeric_type (); |
3206
|
1383 else |
3238
|
1384 print_usage ("isnumeric"); |
3206
|
1385 |
|
1386 return retval; |
|
1387 } |
|
1388 |
4028
|
1389 DEFUN (islist, args, , |
3526
|
1390 "-*- texinfo -*-\n\ |
4028
|
1391 @deftypefn {Built-in Function} {} islist (@var{x})\n\ |
3428
|
1392 Return nonzero if @var{x} is a list.\n\ |
|
1393 @end deftypefn") |
3204
|
1394 { |
|
1395 octave_value retval; |
|
1396 |
|
1397 if (args.length () == 1) |
3258
|
1398 retval = args(0).is_list (); |
3204
|
1399 else |
4028
|
1400 print_usage ("islist"); |
3204
|
1401 |
|
1402 return retval; |
|
1403 } |
|
1404 |
4028
|
1405 DEFUN (ismatrix, args, , |
3321
|
1406 "-*- texinfo -*-\n\ |
4028
|
1407 @deftypefn {Built-in Function} {} ismatrix (@var{a})\n\ |
3321
|
1408 Return 1 if @var{a} is a matrix. Otherwise, return 0.\n\ |
3333
|
1409 @end deftypefn") |
3202
|
1410 { |
4233
|
1411 octave_value retval = false; |
3202
|
1412 |
|
1413 if (args.length () == 1) |
|
1414 { |
|
1415 octave_value arg = args(0); |
|
1416 |
3212
|
1417 if (arg.is_scalar_type () || arg.is_range ()) |
4233
|
1418 retval = true; |
3202
|
1419 else if (arg.is_matrix_type ()) |
4233
|
1420 retval = (arg.rows () >= 1 && arg.columns () >= 1); |
3202
|
1421 } |
|
1422 else |
4028
|
1423 print_usage ("ismatrix"); |
3202
|
1424 |
|
1425 return retval; |
|
1426 } |
|
1427 |
3354
|
1428 static octave_value |
5747
|
1429 fill_matrix (const octave_value_list& args, int val, const char *fcn) |
523
|
1430 { |
3354
|
1431 octave_value retval; |
523
|
1432 |
|
1433 int nargin = args.length (); |
|
1434 |
4946
|
1435 oct_data_conv::data_type dt = oct_data_conv::dt_double; |
4481
|
1436 |
4946
|
1437 dim_vector dims (1, 1); |
4481
|
1438 |
|
1439 if (nargin > 0 && args(nargin-1).is_string ()) |
|
1440 { |
4946
|
1441 std::string nm = args(nargin-1).string_value (); |
4481
|
1442 nargin--; |
|
1443 |
4946
|
1444 dt = oct_data_conv::string_to_data_type (nm); |
|
1445 |
|
1446 if (error_state) |
|
1447 return retval; |
4481
|
1448 } |
|
1449 |
523
|
1450 switch (nargin) |
|
1451 { |
712
|
1452 case 0: |
|
1453 break; |
777
|
1454 |
610
|
1455 case 1: |
4481
|
1456 get_dimensions (args(0), fcn, dims); |
610
|
1457 break; |
777
|
1458 |
4563
|
1459 default: |
|
1460 { |
|
1461 dims.resize (nargin); |
4481
|
1462 |
4563
|
1463 for (int i = 0; i < nargin; i++) |
|
1464 { |
4732
|
1465 dims(i) = args(i).is_empty () ? 0 : args(i).int_value (); |
4481
|
1466 |
4563
|
1467 if (error_state) |
|
1468 { |
4732
|
1469 error ("%s: expecting scalar integer arguments", fcn); |
4563
|
1470 break; |
|
1471 } |
|
1472 } |
|
1473 } |
|
1474 break; |
4481
|
1475 } |
|
1476 |
|
1477 if (! error_state) |
|
1478 { |
4946
|
1479 dims.chop_trailing_singletons (); |
4565
|
1480 |
4481
|
1481 check_dimensions (dims, fcn); |
3354
|
1482 |
5775
|
1483 // FIXME -- perhaps this should be made extensible by |
4946
|
1484 // using the class name to lookup a function to call to create |
|
1485 // the new value. |
|
1486 |
|
1487 // Note that automatic narrowing will handle conversion from |
|
1488 // NDArray to scalar. |
|
1489 |
4481
|
1490 if (! error_state) |
|
1491 { |
4946
|
1492 switch (dt) |
|
1493 { |
|
1494 case oct_data_conv::dt_int8: |
|
1495 retval = int8NDArray (dims, val); |
|
1496 break; |
4481
|
1497 |
4946
|
1498 case oct_data_conv::dt_uint8: |
|
1499 retval = uint8NDArray (dims, val); |
|
1500 break; |
|
1501 |
|
1502 case oct_data_conv::dt_int16: |
|
1503 retval = int16NDArray (dims, val); |
|
1504 break; |
|
1505 |
|
1506 case oct_data_conv::dt_uint16: |
|
1507 retval = uint16NDArray (dims, val); |
|
1508 break; |
|
1509 |
|
1510 case oct_data_conv::dt_int32: |
|
1511 retval = int32NDArray (dims, val); |
|
1512 break; |
777
|
1513 |
4946
|
1514 case oct_data_conv::dt_uint32: |
|
1515 retval = uint32NDArray (dims, val); |
|
1516 break; |
|
1517 |
|
1518 case oct_data_conv::dt_int64: |
|
1519 retval = int64NDArray (dims, val); |
|
1520 break; |
4481
|
1521 |
4946
|
1522 case oct_data_conv::dt_uint64: |
|
1523 retval = uint64NDArray (dims, val); |
|
1524 break; |
4481
|
1525 |
5775
|
1526 case oct_data_conv::dt_single: // FIXME |
4946
|
1527 case oct_data_conv::dt_double: |
|
1528 retval = NDArray (dims, val); |
|
1529 break; |
|
1530 |
4986
|
1531 case oct_data_conv::dt_logical: |
|
1532 retval = boolNDArray (dims, val); |
|
1533 break; |
|
1534 |
4946
|
1535 default: |
|
1536 error ("%s: invalid class name", fcn); |
|
1537 break; |
4481
|
1538 } |
|
1539 } |
523
|
1540 } |
|
1541 |
|
1542 return retval; |
|
1543 } |
|
1544 |
5747
|
1545 static octave_value |
|
1546 fill_matrix (const octave_value_list& args, double val, const char *fcn) |
|
1547 { |
|
1548 octave_value retval; |
|
1549 |
|
1550 int nargin = args.length (); |
|
1551 |
|
1552 oct_data_conv::data_type dt = oct_data_conv::dt_double; |
|
1553 |
|
1554 dim_vector dims (1, 1); |
|
1555 |
|
1556 if (nargin > 0 && args(nargin-1).is_string ()) |
|
1557 { |
|
1558 std::string nm = args(nargin-1).string_value (); |
|
1559 nargin--; |
|
1560 |
|
1561 dt = oct_data_conv::string_to_data_type (nm); |
|
1562 |
|
1563 if (error_state) |
|
1564 return retval; |
|
1565 } |
|
1566 |
|
1567 switch (nargin) |
|
1568 { |
|
1569 case 0: |
|
1570 break; |
|
1571 |
|
1572 case 1: |
|
1573 get_dimensions (args(0), fcn, dims); |
|
1574 break; |
|
1575 |
|
1576 default: |
|
1577 { |
|
1578 dims.resize (nargin); |
|
1579 |
|
1580 for (int i = 0; i < nargin; i++) |
|
1581 { |
|
1582 dims(i) = args(i).is_empty () ? 0 : args(i).int_value (); |
|
1583 |
|
1584 if (error_state) |
|
1585 { |
|
1586 error ("%s: expecting scalar integer arguments", fcn); |
|
1587 break; |
|
1588 } |
|
1589 } |
|
1590 } |
|
1591 break; |
|
1592 } |
|
1593 |
|
1594 if (! error_state) |
|
1595 { |
|
1596 dims.chop_trailing_singletons (); |
|
1597 |
|
1598 check_dimensions (dims, fcn); |
|
1599 |
|
1600 // Note that automatic narrowing will handle conversion from |
|
1601 // NDArray to scalar. |
|
1602 |
|
1603 if (! error_state) |
|
1604 { |
|
1605 switch (dt) |
|
1606 { |
5775
|
1607 case oct_data_conv::dt_single: // FIXME |
5747
|
1608 case oct_data_conv::dt_double: |
|
1609 retval = NDArray (dims, val); |
|
1610 break; |
|
1611 |
|
1612 default: |
|
1613 error ("%s: invalid class name", fcn); |
|
1614 break; |
|
1615 } |
|
1616 } |
|
1617 } |
|
1618 |
|
1619 return retval; |
|
1620 } |
|
1621 |
|
1622 static octave_value |
|
1623 fill_matrix (const octave_value_list& args, const Complex& val, |
|
1624 const char *fcn) |
|
1625 { |
|
1626 octave_value retval; |
|
1627 |
|
1628 int nargin = args.length (); |
|
1629 |
|
1630 oct_data_conv::data_type dt = oct_data_conv::dt_double; |
|
1631 |
|
1632 dim_vector dims (1, 1); |
|
1633 |
|
1634 if (nargin > 0 && args(nargin-1).is_string ()) |
|
1635 { |
|
1636 std::string nm = args(nargin-1).string_value (); |
|
1637 nargin--; |
|
1638 |
|
1639 dt = oct_data_conv::string_to_data_type (nm); |
|
1640 |
|
1641 if (error_state) |
|
1642 return retval; |
|
1643 } |
|
1644 |
|
1645 switch (nargin) |
|
1646 { |
|
1647 case 0: |
|
1648 break; |
|
1649 |
|
1650 case 1: |
|
1651 get_dimensions (args(0), fcn, dims); |
|
1652 break; |
|
1653 |
|
1654 default: |
|
1655 { |
|
1656 dims.resize (nargin); |
|
1657 |
|
1658 for (int i = 0; i < nargin; i++) |
|
1659 { |
|
1660 dims(i) = args(i).is_empty () ? 0 : args(i).int_value (); |
|
1661 |
|
1662 if (error_state) |
|
1663 { |
|
1664 error ("%s: expecting scalar integer arguments", fcn); |
|
1665 break; |
|
1666 } |
|
1667 } |
|
1668 } |
|
1669 break; |
|
1670 } |
|
1671 |
|
1672 if (! error_state) |
|
1673 { |
|
1674 dims.chop_trailing_singletons (); |
|
1675 |
|
1676 check_dimensions (dims, fcn); |
|
1677 |
|
1678 // Note that automatic narrowing will handle conversion from |
|
1679 // NDArray to scalar. |
|
1680 |
|
1681 if (! error_state) |
|
1682 { |
|
1683 switch (dt) |
|
1684 { |
5775
|
1685 case oct_data_conv::dt_single: // FIXME |
5747
|
1686 case oct_data_conv::dt_double: |
|
1687 retval = ComplexNDArray (dims, val); |
|
1688 break; |
|
1689 |
|
1690 default: |
|
1691 error ("%s: invalid class name", fcn); |
|
1692 break; |
|
1693 } |
|
1694 } |
|
1695 } |
|
1696 |
|
1697 return retval; |
|
1698 } |
|
1699 |
|
1700 static octave_value |
|
1701 fill_matrix (const octave_value_list& args, bool val, const char *fcn) |
|
1702 { |
|
1703 octave_value retval; |
|
1704 |
|
1705 int nargin = args.length (); |
|
1706 |
|
1707 dim_vector dims (1, 1); |
|
1708 |
|
1709 switch (nargin) |
|
1710 { |
|
1711 case 0: |
|
1712 break; |
|
1713 |
|
1714 case 1: |
|
1715 get_dimensions (args(0), fcn, dims); |
|
1716 break; |
|
1717 |
|
1718 default: |
|
1719 { |
|
1720 dims.resize (nargin); |
|
1721 |
|
1722 for (int i = 0; i < nargin; i++) |
|
1723 { |
|
1724 dims(i) = args(i).is_empty () ? 0 : args(i).int_value (); |
|
1725 |
|
1726 if (error_state) |
|
1727 { |
|
1728 error ("%s: expecting scalar integer arguments", fcn); |
|
1729 break; |
|
1730 } |
|
1731 } |
|
1732 } |
|
1733 break; |
|
1734 } |
|
1735 |
|
1736 if (! error_state) |
|
1737 { |
|
1738 dims.chop_trailing_singletons (); |
|
1739 |
|
1740 check_dimensions (dims, fcn); |
|
1741 |
|
1742 // Note that automatic narrowing will handle conversion from |
|
1743 // NDArray to scalar. |
|
1744 |
|
1745 if (! error_state) |
|
1746 retval = boolNDArray (dims, val); |
|
1747 } |
|
1748 |
|
1749 return retval; |
|
1750 } |
|
1751 |
3354
|
1752 DEFUN (ones, args, , |
3369
|
1753 "-*- texinfo -*-\n\ |
|
1754 @deftypefn {Built-in Function} {} ones (@var{x})\n\ |
|
1755 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m})\n\ |
4948
|
1756 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
1757 @deftypefnx {Built-in Function} {} ones (@dots{}, @var{class})\n\ |
4481
|
1758 Return a matrix or N-dimensional array whose elements are all 1.\n\ |
|
1759 The arguments are handled the same as the arguments for @code{eye}.\n\ |
3369
|
1760 \n\ |
|
1761 If you need to create a matrix whose values are all the same, you should\n\ |
|
1762 use an expression like\n\ |
|
1763 \n\ |
|
1764 @example\n\ |
|
1765 val_matrix = val * ones (n, m)\n\ |
|
1766 @end example\n\ |
4945
|
1767 \n\ |
|
1768 The optional argument @var{class}, allows @code{ones} to return an array of\n\ |
5747
|
1769 the specified type, for example\n\ |
4945
|
1770 \n\ |
|
1771 @example\n\ |
|
1772 val = ones (n,m, \"uint8\")\n\ |
|
1773 @end example\n\ |
3369
|
1774 @end deftypefn") |
523
|
1775 { |
5747
|
1776 return fill_matrix (args, 1, "ones"); |
523
|
1777 } |
|
1778 |
3354
|
1779 DEFUN (zeros, args, , |
3369
|
1780 "-*- texinfo -*-\n\ |
|
1781 @deftypefn {Built-in Function} {} zeros (@var{x})\n\ |
|
1782 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m})\n\ |
4948
|
1783 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
1784 @deftypefnx {Built-in Function} {} zeros (@dots{}, @var{class})\n\ |
4481
|
1785 Return a matrix or N-dimensional array whose elements are all 0.\n\ |
|
1786 The arguments are handled the same as the arguments for @code{eye}.\n\ |
4945
|
1787 \n\ |
|
1788 The optional argument @var{class}, allows @code{zeros} to return an array of\n\ |
5747
|
1789 the specified type, for example\n\ |
4945
|
1790 \n\ |
|
1791 @example\n\ |
|
1792 val = zeros (n,m, \"uint8\")\n\ |
|
1793 @end example\n\ |
3369
|
1794 @end deftypefn") |
523
|
1795 { |
5747
|
1796 return fill_matrix (args, 0, "zeros"); |
|
1797 } |
|
1798 |
|
1799 DEFUN (Inf, args, , |
|
1800 "-*- texinfo -*-\n\ |
|
1801 @deftypefn {Built-in Function} {} Inf (@var{x})\n\ |
|
1802 @deftypefnx {Built-in Function} {} Inf (@var{n}, @var{m})\n\ |
|
1803 @deftypefnx {Built-in Function} {} Inf (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
1804 @deftypefnx {Built-in Function} {} Inf (@dots{}, @var{class})\n\ |
|
1805 Return a matrix or N-dimensional array whose elements are all Infinity.\n\ |
|
1806 The arguments are handled the same as the arguments for @code{eye}.\n\ |
|
1807 The optional argument @var{class} may be either @samp{\"single\"} or\n\ |
5798
|
1808 @samp{\"double\"}. The default is @samp{\"double\"}.\n\ |
5747
|
1809 @end deftypefn") |
|
1810 { |
|
1811 return fill_matrix (args, lo_ieee_inf_value (), "Inf"); |
|
1812 } |
|
1813 |
|
1814 DEFALIAS (inf, Inf); |
|
1815 |
|
1816 DEFUN (NaN, args, , |
|
1817 "-*- texinfo -*-\n\ |
|
1818 @deftypefn {Built-in Function} {} NaN (@var{x})\n\ |
|
1819 @deftypefnx {Built-in Function} {} NaN (@var{n}, @var{m})\n\ |
|
1820 @deftypefnx {Built-in Function} {} NaN (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
1821 @deftypefnx {Built-in Function} {} NaN (@dots{}, @var{class})\n\ |
|
1822 Return a matrix or N-dimensional array whose elements are all NaN\n\ |
|
1823 (Not a Number). The value NaN is the result of an operation like\n\ |
|
1824 @iftex\n\ |
|
1825 @tex\n\ |
|
1826 $0/0$, or $\\infty - \\infty$,\n\ |
|
1827 @end tex\n\ |
|
1828 @end iftex\n\ |
|
1829 @ifinfo\n\ |
|
1830 0/0, or @samp{Inf - Inf},\n\ |
|
1831 @end ifinfo\n\ |
|
1832 or any operation with a NaN.\n\ |
|
1833 \n\ |
|
1834 Note that NaN always compares not equal to NaN. This behavior is\n\ |
|
1835 specified by the IEEE standard for floating point arithmetic. To\n\ |
|
1836 find NaN values, you must use the @code{isnan} function.\n\ |
|
1837 \n\ |
|
1838 The arguments are handled the same as the arguments for @code{eye}.\n\ |
|
1839 The optional argument @var{class} may be either @samp{\"single\"} or\n\ |
5798
|
1840 @samp{\"double\"}. The default is @samp{\"double\"}.\n\ |
5747
|
1841 @end deftypefn") |
|
1842 { |
|
1843 return fill_matrix (args, lo_ieee_nan_value (), "NaN"); |
|
1844 } |
|
1845 |
|
1846 DEFALIAS (nan, NaN); |
|
1847 |
|
1848 DEFUN (e, args, , |
|
1849 "-*- texinfo -*-\n\ |
|
1850 @deftypefn {Built-in Function} {} e (@var{x})\n\ |
|
1851 @deftypefnx {Built-in Function} {} e (@var{n}, @var{m})\n\ |
|
1852 @deftypefnx {Built-in Function} {} e (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
1853 @deftypefnx {Built-in Function} {} e (@dots{}, @var{class})\n\ |
|
1854 Return a matrix or N-dimensional array whose elements are all equal\n\ |
|
1855 to the base of natural logarithms. The constant\n\ |
|
1856 @iftex\n\ |
|
1857 @tex\n\ |
|
1858 $e$\n\ |
|
1859 @end tex\n\ |
|
1860 @end iftex\n\ |
|
1861 @ifinfo\n\ |
|
1862 @var{e}\n\ |
|
1863 @end ifinfo\n\ |
|
1864 satisfies the equation\n\ |
|
1865 @iftex\n\ |
|
1866 @tex\n\ |
|
1867 $\\log (e) = 1$.\n\ |
|
1868 @end tex\n\ |
|
1869 @end iftex\n\ |
|
1870 @ifinfo\n\ |
|
1871 @code{log} (@var{e}) = 1.\n\ |
|
1872 @end ifinfo\n\ |
|
1873 @end deftypefn") |
|
1874 { |
|
1875 #if defined (M_E) |
|
1876 double e_val = M_E; |
|
1877 #else |
|
1878 double e_val = exp (1.0); |
|
1879 #endif |
|
1880 |
|
1881 return fill_matrix (args, e_val, "e"); |
|
1882 } |
|
1883 |
|
1884 DEFUN (eps, args, , |
|
1885 "-*- texinfo -*-\n\ |
|
1886 @deftypefn {Built-in Function} {} eps (@var{x})\n\ |
|
1887 @deftypefnx {Built-in Function} {} eps (@var{n}, @var{m})\n\ |
|
1888 @deftypefnx {Built-in Function} {} eps (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
1889 @deftypefnx {Built-in Function} {} eps (@dots{}, @var{class})\n\ |
|
1890 Return a matrix or N-dimensional array whose elements are all eps,\n\ |
|
1891 the machine precision. More precisely, @code{eps} is the largest\n\ |
|
1892 relative spacing between any two adjacent numbers in the machine's\n\ |
|
1893 floating point system. This number is obviously system-dependent. On\n\ |
|
1894 machines that support 64 bit IEEE floating point arithmetic, @code{eps}\n\ |
|
1895 is approximately\n\ |
|
1896 @ifinfo\n\ |
|
1897 2.2204e-16.\n\ |
|
1898 @end ifinfo\n\ |
|
1899 @iftex\n\ |
|
1900 @tex\n\ |
|
1901 $2.2204\\times10^{-16}$.\n\ |
|
1902 @end tex\n\ |
|
1903 @end iftex\n\ |
|
1904 @end deftypefn") |
|
1905 { |
|
1906 return fill_matrix (args, DBL_EPSILON, "eps"); |
|
1907 } |
|
1908 |
|
1909 DEFUN (pi, args, , |
|
1910 "-*- texinfo -*-\n\ |
|
1911 @deftypefn {Built-in Function} {} pi (@var{x})\n\ |
|
1912 @deftypefnx {Built-in Function} {} pi (@var{n}, @var{m})\n\ |
|
1913 @deftypefnx {Built-in Function} {} pi (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
1914 @deftypefnx {Built-in Function} {} pi (@dots{}, @var{class})\n\ |
|
1915 Return a matrix or N-dimensional array whose elements are all equal\n\ |
|
1916 to the ratio of the circumference of a circle to its diameter.\n\ |
|
1917 Internally, @code{pi} is computed as @samp{4.0 * atan (1.0)}.\n\ |
|
1918 @end deftypefn") |
|
1919 { |
|
1920 #if defined (M_PI) |
|
1921 double pi_val = M_PI; |
|
1922 #else |
|
1923 double pi_val = 4.0 * atan (1.0); |
|
1924 #endif |
|
1925 |
|
1926 return fill_matrix (args, pi_val, "pi"); |
|
1927 } |
|
1928 |
|
1929 DEFUN (realmax, args, , |
|
1930 "-*- texinfo -*-\n\ |
|
1931 @deftypefn {Built-in Function} {} realmax (@var{x})\n\ |
|
1932 @deftypefnx {Built-in Function} {} realmax (@var{n}, @var{m})\n\ |
|
1933 @deftypefnx {Built-in Function} {} realmax (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
1934 @deftypefnx {Built-in Function} {} realmax (@dots{}, @var{class})\n\ |
|
1935 Return a matrix or N-dimensional array whose elements are all equal\n\ |
|
1936 to the largest floating point number that is representable. The actual\n\ |
|
1937 value is system-dependent. On machines that support 64-bit IEEE\n\ |
|
1938 floating point arithmetic, @code{realmax} is approximately\n\ |
|
1939 @ifinfo\n\ |
|
1940 1.7977e+308\n\ |
|
1941 @end ifinfo\n\ |
|
1942 @iftex\n\ |
|
1943 @tex\n\ |
|
1944 $1.7977\\times10^{308}$.\n\ |
|
1945 @end tex\n\ |
|
1946 @end iftex\n\ |
|
1947 @seealso{realmin}\n\ |
|
1948 @end deftypefn") |
|
1949 { |
|
1950 return fill_matrix (args, DBL_MAX, "realmax"); |
|
1951 } |
|
1952 |
|
1953 DEFUN (realmin, args, , |
|
1954 "-*- texinfo -*-\n\ |
|
1955 @deftypefn {Built-in Function} {} realmin (@var{x})\n\ |
|
1956 @deftypefnx {Built-in Function} {} realmin (@var{n}, @var{m})\n\ |
|
1957 @deftypefnx {Built-in Function} {} realmin (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
1958 @deftypefnx {Built-in Function} {} realmin (@dots{}, @var{class})\n\ |
|
1959 Return a matrix or N-dimensional array whose elements are all equal\n\ |
|
1960 to the smallest normalized floating point number that is representable.\n\ |
|
1961 The actual value is system-dependent. On machines that support\n\ |
|
1962 64-bit IEEE floating point arithmetic, @code{realmin} is approximately\n\ |
|
1963 @ifinfo\n\ |
|
1964 2.2251e-308\n\ |
|
1965 @end ifinfo\n\ |
|
1966 @iftex\n\ |
|
1967 @tex\n\ |
|
1968 $2.2251\\times10^{-308}$.\n\ |
|
1969 @end tex\n\ |
|
1970 @end iftex\n\ |
|
1971 @seealso{realmax}\n\ |
|
1972 @end deftypefn") |
|
1973 { |
|
1974 return fill_matrix (args, DBL_MIN, "realmin"); |
|
1975 } |
|
1976 |
|
1977 DEFUN (I, args, , |
|
1978 "-*- texinfo -*-\n\ |
|
1979 @deftypefn {Built-in Function} {} I (@var{x})\n\ |
|
1980 @deftypefnx {Built-in Function} {} I (@var{n}, @var{m})\n\ |
|
1981 @deftypefnx {Built-in Function} {} I (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
1982 @deftypefnx {Built-in Function} {} I (@dots{}, @var{class})\n\ |
|
1983 Return a matrix or N-dimensional array whose elements are all equal\n\ |
|
1984 to the pure imaginary unit, defined as\n\ |
|
1985 @iftex\n\ |
|
1986 @tex\n\ |
|
1987 $\\sqrt{-1}$.\n\ |
|
1988 @end tex\n\ |
|
1989 @end iftex\n\ |
|
1990 @ifinfo\n\ |
|
1991 @code{sqrt (-1)}.\n\ |
|
1992 @end ifinfo\n\ |
|
1993 Since I (also i, J, and J) is a function, you can use the name(s) for\n\ |
|
1994 other purposes.\n\ |
|
1995 @end deftypefn") |
|
1996 { |
|
1997 return fill_matrix (args, Complex (0.0, 1.0), "I"); |
|
1998 } |
|
1999 |
|
2000 DEFALIAS (i, I); |
|
2001 DEFALIAS (J, I); |
|
2002 DEFALIAS (j, I); |
|
2003 |
|
2004 DEFUN (NA, args, , |
|
2005 "-*- texinfo -*-\n\ |
|
2006 @deftypefn {Built-in Function} {} NA (@var{x})\n\ |
|
2007 @deftypefnx {Built-in Function} {} NA (@var{n}, @var{m})\n\ |
|
2008 @deftypefnx {Built-in Function} {} NA (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
2009 @deftypefnx {Built-in Function} {} NA (@dots{}, @var{class})\n\ |
|
2010 Return a matrix or N-dimensional array whose elements are all equal\n\ |
|
2011 to the special constant used to designate missing values.\n\ |
|
2012 @end deftypefn") |
|
2013 { |
|
2014 return fill_matrix (args, lo_ieee_na_value (), "NA"); |
|
2015 } |
|
2016 |
|
2017 DEFUN (false, args, , |
|
2018 "-*- texinfo -*-\n\ |
|
2019 @deftypefn {Built-in Function} {} false (@var{x})\n\ |
|
2020 @deftypefnx {Built-in Function} {} false (@var{n}, @var{m})\n\ |
|
2021 @deftypefnx {Built-in Function} {} false (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
2022 Return a matrix or N-dimensional array whose elements are all logical 0.\n\ |
|
2023 The arguments are handled the same as the arguments for @code{eye}.\n\ |
|
2024 @end deftypefn") |
|
2025 { |
|
2026 return fill_matrix (args, false, "false"); |
|
2027 } |
|
2028 |
|
2029 DEFUN (true, args, , |
|
2030 "-*- texinfo -*-\n\ |
|
2031 @deftypefn {Built-in Function} {} true (@var{x})\n\ |
|
2032 @deftypefnx {Built-in Function} {} true (@var{n}, @var{m})\n\ |
|
2033 @deftypefnx {Built-in Function} {} true (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
2034 Return a matrix or N-dimensional array whose elements are all logical 1.\n\ |
|
2035 The arguments are handled the same as the arguments for @code{eye}.\n\ |
|
2036 @end deftypefn") |
|
2037 { |
|
2038 return fill_matrix (args, true, "true"); |
3354
|
2039 } |
523
|
2040 |
4946
|
2041 template <class MT> |
|
2042 octave_value |
|
2043 identity_matrix (int nr, int nc) |
|
2044 { |
|
2045 octave_value retval; |
|
2046 |
|
2047 typename octave_array_type_traits<MT>::element_type one (1); |
|
2048 |
|
2049 if (nr == 1 && nc == 1) |
|
2050 retval = one; |
|
2051 else |
|
2052 { |
|
2053 dim_vector dims (nr, nc); |
|
2054 |
|
2055 typename octave_array_type_traits<MT>::element_type zero (0); |
|
2056 |
|
2057 MT m (dims, zero); |
|
2058 |
|
2059 if (nr > 0 && nc > 0) |
|
2060 { |
|
2061 int n = std::min (nr, nc); |
|
2062 |
|
2063 for (int i = 0; i < n; i++) |
|
2064 m(i,i) = one; |
|
2065 } |
|
2066 |
|
2067 retval = m; |
|
2068 } |
|
2069 |
|
2070 return retval; |
|
2071 } |
|
2072 |
5058
|
2073 #define INSTANTIATE_EYE(T) \ |
|
2074 template octave_value identity_matrix<T> (int, int) |
|
2075 |
|
2076 INSTANTIATE_EYE (int8NDArray); |
|
2077 INSTANTIATE_EYE (uint8NDArray); |
|
2078 INSTANTIATE_EYE (int16NDArray); |
|
2079 INSTANTIATE_EYE (uint16NDArray); |
|
2080 INSTANTIATE_EYE (int32NDArray); |
|
2081 INSTANTIATE_EYE (uint32NDArray); |
|
2082 INSTANTIATE_EYE (int64NDArray); |
|
2083 INSTANTIATE_EYE (uint64NDArray); |
|
2084 INSTANTIATE_EYE (NDArray); |
|
2085 INSTANTIATE_EYE (boolNDArray); |
|
2086 |
4945
|
2087 static octave_value |
4948
|
2088 identity_matrix (int nr, int nc, oct_data_conv::data_type dt) |
4945
|
2089 { |
|
2090 octave_value retval; |
|
2091 |
5775
|
2092 // FIXME -- perhaps this should be made extensible by using |
4946
|
2093 // the class name to lookup a function to call to create the new |
|
2094 // value. |
|
2095 |
|
2096 if (! error_state) |
|
2097 { |
|
2098 switch (dt) |
|
2099 { |
|
2100 case oct_data_conv::dt_int8: |
|
2101 retval = identity_matrix<int8NDArray> (nr, nc); |
|
2102 break; |
|
2103 |
|
2104 case oct_data_conv::dt_uint8: |
|
2105 retval = identity_matrix<uint8NDArray> (nr, nc); |
|
2106 break; |
|
2107 |
|
2108 case oct_data_conv::dt_int16: |
|
2109 retval = identity_matrix<int16NDArray> (nr, nc); |
|
2110 break; |
4945
|
2111 |
4946
|
2112 case oct_data_conv::dt_uint16: |
|
2113 retval = identity_matrix<uint16NDArray> (nr, nc); |
|
2114 break; |
|
2115 |
|
2116 case oct_data_conv::dt_int32: |
|
2117 retval = identity_matrix<int32NDArray> (nr, nc); |
|
2118 break; |
|
2119 |
|
2120 case oct_data_conv::dt_uint32: |
|
2121 retval = identity_matrix<uint32NDArray> (nr, nc); |
|
2122 break; |
4945
|
2123 |
4946
|
2124 case oct_data_conv::dt_int64: |
|
2125 retval = identity_matrix<int64NDArray> (nr, nc); |
|
2126 break; |
|
2127 |
|
2128 case oct_data_conv::dt_uint64: |
|
2129 retval = identity_matrix<uint64NDArray> (nr, nc); |
|
2130 break; |
4945
|
2131 |
5775
|
2132 case oct_data_conv::dt_single: // FIXME |
4946
|
2133 case oct_data_conv::dt_double: |
|
2134 retval = identity_matrix<NDArray> (nr, nc); |
|
2135 break; |
4945
|
2136 |
4986
|
2137 case oct_data_conv::dt_logical: |
|
2138 retval = identity_matrix<boolNDArray> (nr, nc); |
|
2139 break; |
|
2140 |
4946
|
2141 default: |
|
2142 error ("eye: invalid class name"); |
|
2143 break; |
4945
|
2144 } |
|
2145 } |
|
2146 |
|
2147 return retval; |
|
2148 } |
|
2149 |
4946
|
2150 #undef INT_EYE_MATRIX |
|
2151 |
1957
|
2152 DEFUN (eye, args, , |
3369
|
2153 "-*- texinfo -*-\n\ |
|
2154 @deftypefn {Built-in Function} {} eye (@var{x})\n\ |
|
2155 @deftypefnx {Built-in Function} {} eye (@var{n}, @var{m})\n\ |
4948
|
2156 @deftypefnx {Built-in Function} {} eye (@dots{}, @var{class})\n\ |
3369
|
2157 Return an identity matrix. If invoked with a single scalar argument,\n\ |
|
2158 @code{eye} returns a square matrix with the dimension specified. If you\n\ |
|
2159 supply two scalar arguments, @code{eye} takes them to be the number of\n\ |
|
2160 rows and columns. If given a vector with two elements, @code{eye} uses\n\ |
|
2161 the values of the elements as the number of rows and columns,\n\ |
|
2162 respectively. For example,\n\ |
|
2163 \n\ |
|
2164 @example\n\ |
|
2165 @group\n\ |
|
2166 eye (3)\n\ |
|
2167 @result{} 1 0 0\n\ |
|
2168 0 1 0\n\ |
|
2169 0 0 1\n\ |
|
2170 @end group\n\ |
|
2171 @end example\n\ |
|
2172 \n\ |
|
2173 The following expressions all produce the same result:\n\ |
|
2174 \n\ |
|
2175 @example\n\ |
|
2176 @group\n\ |
|
2177 eye (2)\n\ |
|
2178 @equiv{}\n\ |
|
2179 eye (2, 2)\n\ |
|
2180 @equiv{}\n\ |
|
2181 eye (size ([1, 2; 3, 4])\n\ |
|
2182 @end group\n\ |
|
2183 @end example\n\ |
|
2184 \n\ |
4945
|
2185 The optional argument @var{class}, allows @code{eye} to return an array of\n\ |
|
2186 the specified type, like\n\ |
|
2187 \n\ |
|
2188 @example\n\ |
|
2189 val = zeros (n,m, \"uint8\")\n\ |
|
2190 @end example\n\ |
|
2191 \n\ |
3369
|
2192 For compatibility with @sc{Matlab}, calling @code{eye} with no arguments\n\ |
|
2193 is equivalent to calling it with an argument of 1.\n\ |
|
2194 @end deftypefn") |
523
|
2195 { |
3354
|
2196 octave_value retval; |
523
|
2197 |
4948
|
2198 int nargin = args.length (); |
4945
|
2199 |
4948
|
2200 oct_data_conv::data_type dt = oct_data_conv::dt_double; |
523
|
2201 |
4945
|
2202 // Check for type information. |
|
2203 |
|
2204 if (nargin > 0 && args(nargin-1).is_string ()) |
|
2205 { |
4948
|
2206 std::string nm = args(nargin-1).string_value (); |
4945
|
2207 nargin--; |
4948
|
2208 |
|
2209 dt = oct_data_conv::string_to_data_type (nm); |
|
2210 |
|
2211 if (error_state) |
|
2212 return retval; |
4945
|
2213 } |
|
2214 |
523
|
2215 switch (nargin) |
|
2216 { |
712
|
2217 case 0: |
4948
|
2218 retval = identity_matrix (1, 1, dt); |
712
|
2219 break; |
777
|
2220 |
610
|
2221 case 1: |
3354
|
2222 { |
5275
|
2223 octave_idx_type nr, nc; |
3354
|
2224 get_dimensions (args(0), "eye", nr, nc); |
|
2225 |
|
2226 if (! error_state) |
4948
|
2227 retval = identity_matrix (nr, nc, dt); |
3354
|
2228 } |
610
|
2229 break; |
777
|
2230 |
523
|
2231 case 2: |
3354
|
2232 { |
5275
|
2233 octave_idx_type nr, nc; |
3354
|
2234 get_dimensions (args(0), args(1), "eye", nr, nc); |
|
2235 |
|
2236 if (! error_state) |
4948
|
2237 retval = identity_matrix (nr, nc, dt); |
3354
|
2238 } |
523
|
2239 break; |
777
|
2240 |
523
|
2241 default: |
|
2242 print_usage ("eye"); |
|
2243 break; |
|
2244 } |
|
2245 |
|
2246 return retval; |
|
2247 } |
|
2248 |
1957
|
2249 DEFUN (linspace, args, , |
3369
|
2250 "-*- texinfo -*-\n\ |
|
2251 @deftypefn {Built-in Function} {} linspace (@var{base}, @var{limit}, @var{n})\n\ |
|
2252 Return a row vector with @var{n} linearly spaced elements between\n\ |
|
2253 @var{base} and @var{limit}. The number of elements, @var{n}, must be\n\ |
|
2254 greater than 1. The @var{base} and @var{limit} are always included in\n\ |
|
2255 the range. If @var{base} is greater than @var{limit}, the elements are\n\ |
|
2256 stored in decreasing order. If the number of points is not specified, a\n\ |
|
2257 value of 100 is used.\n\ |
1100
|
2258 \n\ |
4455
|
2259 The @code{linspace} function always returns a row vector.\n\ |
3369
|
2260 @end deftypefn") |
1100
|
2261 { |
3418
|
2262 octave_value retval; |
1100
|
2263 |
|
2264 int nargin = args.length (); |
|
2265 |
|
2266 int npoints = 100; |
|
2267 |
1940
|
2268 if (nargin != 2 && nargin != 3) |
|
2269 { |
|
2270 print_usage ("linspace"); |
|
2271 return retval; |
|
2272 } |
|
2273 |
1100
|
2274 if (nargin == 3) |
4732
|
2275 npoints = args(2).int_value (); |
1100
|
2276 |
|
2277 if (! error_state) |
|
2278 { |
3322
|
2279 octave_value arg_1 = args(0); |
|
2280 octave_value arg_2 = args(1); |
1100
|
2281 |
3322
|
2282 if (arg_1.is_complex_type () || arg_2.is_complex_type ()) |
|
2283 { |
|
2284 Complex x1 = arg_1.complex_value (); |
|
2285 Complex x2 = arg_2.complex_value (); |
|
2286 |
|
2287 if (! error_state) |
1100
|
2288 { |
3322
|
2289 ComplexRowVector rv = linspace (x1, x2, npoints); |
1100
|
2290 |
|
2291 if (! error_state) |
3418
|
2292 retval = rv; |
1100
|
2293 } |
|
2294 } |
|
2295 else |
3322
|
2296 { |
|
2297 double x1 = arg_1.double_value (); |
|
2298 double x2 = arg_2.double_value (); |
|
2299 |
|
2300 if (! error_state) |
|
2301 { |
|
2302 RowVector rv = linspace (x1, x2, npoints); |
|
2303 |
|
2304 if (! error_state) |
3418
|
2305 retval = rv; |
3322
|
2306 } |
|
2307 } |
1100
|
2308 } |
4732
|
2309 else |
|
2310 error ("linspace: expecting third argument to be an integer"); |
1100
|
2311 |
|
2312 return retval; |
|
2313 } |
|
2314 |
5775
|
2315 // FIXME -- should accept dimensions as separate args for N-d |
5734
|
2316 // arrays as well as 1-d and 2-d arrays. |
|
2317 |
5731
|
2318 DEFUN (resize, args, , |
|
2319 "-*- texinfo -*-\n\ |
|
2320 @deftypefn {Built-in Function} {} resize (@var{x}, @var{m})\n\ |
|
2321 @deftypefnx {Built-in Function} {} resize (@var{x}, @var{m}, @var{n})\n\ |
|
2322 Resize @var{x} to be dimension @var{m}-by-@var{n} where @var{m}\n\ |
|
2323 and @var{n} are scalar. If @var{n} is not supplied, then resize @var{x}\n\ |
|
2324 to be dimension @var{m}-by-@var{m} if @var{m} is scalar, or if\n\ |
|
2325 @var{m} is a vector, then the values of this vector respresent the\n\ |
|
2326 dimensions of the resized matrix.\n\ |
|
2327 @end deftypefn") |
|
2328 { |
|
2329 octave_value retval; |
|
2330 int nargin = args.length (); |
|
2331 |
|
2332 if (nargin == 2) |
|
2333 { |
|
2334 Array<double> vec = args(1).vector_value (); |
|
2335 int ndim = vec.length (); |
|
2336 if (ndim == 1) |
|
2337 { |
|
2338 octave_idx_type m = static_cast<octave_idx_type> (vec(0)); |
|
2339 retval = args(0); |
|
2340 retval = retval.resize (dim_vector (m, m), true); |
|
2341 } |
|
2342 else |
|
2343 { |
|
2344 dim_vector dv; |
|
2345 dv.resize (ndim); |
|
2346 for (int i = 0; i < ndim; i++) |
|
2347 dv(i) = static_cast<octave_idx_type> (vec(i)); |
|
2348 retval = args(0); |
|
2349 retval = retval.resize (dv, true); |
|
2350 } |
|
2351 } |
|
2352 else if (nargin == 3) |
|
2353 { |
|
2354 octave_idx_type m = static_cast<octave_idx_type> |
|
2355 (args(1).scalar_value()); |
|
2356 octave_idx_type n = static_cast<octave_idx_type> |
|
2357 (args(2).scalar_value()); |
|
2358 if (!error_state) |
|
2359 { |
|
2360 retval = args(0); |
|
2361 retval = retval.resize (dim_vector (m, n), true); |
|
2362 } |
|
2363 } |
|
2364 else |
|
2365 print_usage ("resize"); |
|
2366 return retval; |
|
2367 } |
|
2368 |
5775
|
2369 // FIXME -- should use octave_idx_type for dimensions. |
5734
|
2370 |
4567
|
2371 DEFUN (reshape, args, , |
|
2372 "-*- texinfo -*-\n\ |
|
2373 @deftypefn {Function File} {} reshape (@var{a}, @var{m}, @var{n}, @dots{})\n\ |
|
2374 @deftypefnx {Function File} {} reshape (@var{a}, @var{siz})\n\ |
|
2375 Return a matrix with the given dimensions whose elements are taken\n\ |
|
2376 from the matrix @var{a}. The elements of the matrix are access in\n\ |
|
2377 column-major order (like Fortran arrays are stored).\n\ |
|
2378 \n\ |
|
2379 For example,\n\ |
|
2380 \n\ |
|
2381 @example\n\ |
|
2382 @group\n\ |
|
2383 reshape ([1, 2, 3, 4], 2, 2)\n\ |
|
2384 @result{} 1 3\n\ |
|
2385 2 4\n\ |
|
2386 @end group\n\ |
|
2387 @end example\n\ |
|
2388 \n\ |
|
2389 @noindent\n\ |
|
2390 Note that the total number of elements in the original\n\ |
|
2391 matrix must match the total number of elements in the new matrix.\n\ |
5013
|
2392 \n\ |
|
2393 A single dimension of the return matrix can be unknown and is flagged\n\ |
|
2394 by an empty argument.\n\ |
4567
|
2395 @end deftypefn") |
|
2396 { |
|
2397 octave_value retval; |
|
2398 |
|
2399 int nargin = args.length (); |
|
2400 |
|
2401 Array<int> new_size; |
|
2402 |
|
2403 if (nargin == 2) |
|
2404 new_size = args(1).int_vector_value (); |
|
2405 else if (nargin > 2) |
|
2406 { |
|
2407 new_size.resize (nargin-1); |
5013
|
2408 int empty_dim = -1; |
|
2409 |
4567
|
2410 for (int i = 1; i < nargin; i++) |
|
2411 { |
5013
|
2412 if (args(i).is_empty ()) |
|
2413 if (empty_dim > 0) |
|
2414 { |
|
2415 error ("reshape: only a single dimension can be unknown"); |
|
2416 break; |
|
2417 } |
|
2418 else |
|
2419 { |
|
2420 empty_dim = i; |
|
2421 new_size(i-1) = 1; |
|
2422 } |
|
2423 else |
|
2424 { |
|
2425 new_size(i-1) = args(i).int_value (); |
4567
|
2426 |
5013
|
2427 if (error_state) |
|
2428 break; |
|
2429 } |
|
2430 } |
|
2431 |
|
2432 if (! error_state && (empty_dim > 0)) |
|
2433 { |
|
2434 int nel = 1; |
|
2435 for (int i = 0; i < nargin - 1; i++) |
|
2436 nel *= new_size(i); |
|
2437 |
|
2438 if (nel == 0) |
|
2439 new_size(empty_dim-1) = 0; |
|
2440 else |
|
2441 { |
|
2442 int size_empty_dim = args(0).numel () / nel; |
|
2443 |
|
2444 if (args(0).numel () != size_empty_dim * nel) |
|
2445 error ("reshape: size is not divisble by the product of known dimensions (= %d)", nel); |
|
2446 else |
|
2447 new_size(empty_dim-1) = size_empty_dim; |
|
2448 } |
4567
|
2449 } |
|
2450 } |
|
2451 else |
|
2452 { |
|
2453 print_usage ("reshape"); |
|
2454 return retval; |
|
2455 } |
|
2456 |
|
2457 if (error_state) |
|
2458 { |
|
2459 error ("reshape: invalid arguments"); |
|
2460 return retval; |
|
2461 } |
|
2462 |
4739
|
2463 // Remove trailing singletons in new_size, but leave at least 2 |
|
2464 // elements. |
|
2465 |
4567
|
2466 int n = new_size.length (); |
|
2467 |
4739
|
2468 while (n > 2) |
|
2469 { |
|
2470 if (new_size(n-1) == 1) |
|
2471 n--; |
|
2472 else |
|
2473 break; |
|
2474 } |
|
2475 |
|
2476 new_size.resize (n); |
|
2477 |
4567
|
2478 if (n < 2) |
|
2479 { |
|
2480 error ("reshape: expecting size to be vector with at least 2 elements"); |
|
2481 return retval; |
|
2482 } |
|
2483 |
|
2484 dim_vector new_dims; |
|
2485 |
|
2486 new_dims.resize (n); |
|
2487 |
5275
|
2488 for (octave_idx_type i = 0; i < n; i++) |
4567
|
2489 new_dims(i) = new_size(i); |
|
2490 |
|
2491 octave_value arg = args(0); |
|
2492 |
|
2493 if (new_dims.numel () == arg.numel ()) |
|
2494 retval = (new_dims == arg.dims ()) ? arg : arg.reshape (new_dims); |
|
2495 else |
|
2496 error ("reshape: size mismatch"); |
|
2497 |
|
2498 return retval; |
|
2499 } |
|
2500 |
4532
|
2501 DEFUN (squeeze, args, , |
|
2502 "-*- texinfo -*-\n\ |
|
2503 @deftypefn {Built-in Function} {} squeeze (@var{x})\n\ |
|
2504 Remove singleton dimensions from @var{x} and return the result.\n\ |
|
2505 @end deftypefn") |
|
2506 { |
|
2507 octave_value retval; |
|
2508 |
|
2509 if (args.length () == 1) |
4545
|
2510 retval = args(0).squeeze (); |
4532
|
2511 else |
|
2512 print_usage ("squeeze"); |
|
2513 |
|
2514 return retval; |
|
2515 } |
|
2516 |
523
|
2517 /* |
|
2518 ;;; Local Variables: *** |
|
2519 ;;; mode: C++ *** |
|
2520 ;;; End: *** |
|
2521 */ |