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