523
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, |
|
4 2003, 2004, 2005, 2006, 2007 John W. Eaton |
523
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
523
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
523
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
523
|
26 #endif |
|
27 |
7078
|
28 #include "systime.h" |
|
29 |
|
30 #ifdef HAVE_SYS_TYPES_H |
|
31 #include <sys/types.h> |
|
32 #endif |
|
33 |
|
34 #ifdef HAVE_SYS_RESOURCE_H |
|
35 #include <sys/resource.h> |
|
36 #endif |
|
37 |
2184
|
38 #include <cfloat> |
|
39 |
1728
|
40 #include <string> |
|
41 |
2184
|
42 #include "lo-ieee.h" |
7231
|
43 #include "lo-math.h" |
1755
|
44 #include "str-vec.h" |
4153
|
45 #include "quit.h" |
1755
|
46 |
6953
|
47 #include "Cell.h" |
1352
|
48 #include "defun.h" |
|
49 #include "error.h" |
|
50 #include "gripes.h" |
6953
|
51 #include "oct-map.h" |
|
52 #include "oct-obj.h" |
2366
|
53 #include "ov.h" |
5476
|
54 #include "ov-complex.h" |
|
55 #include "ov-cx-mat.h" |
6953
|
56 #include "parse.h" |
|
57 #include "pt-mat.h" |
523
|
58 #include "utils.h" |
6953
|
59 #include "variables.h" |
7045
|
60 #include "pager.h" |
523
|
61 |
4015
|
62 #define ANY_ALL(FCN) \ |
|
63 \ |
4233
|
64 octave_value retval; \ |
4015
|
65 \ |
|
66 int nargin = args.length (); \ |
|
67 \ |
4021
|
68 if (nargin == 1 || nargin == 2) \ |
4015
|
69 { \ |
4021
|
70 int dim = (nargin == 1 ? -1 : args(1).int_value (true) - 1); \ |
|
71 \ |
|
72 if (! error_state) \ |
|
73 { \ |
4556
|
74 if (dim >= -1) \ |
4015
|
75 retval = args(0).FCN (dim); \ |
4021
|
76 else \ |
|
77 error (#FCN ": invalid dimension argument = %d", dim + 1); \ |
|
78 } \ |
4015
|
79 else \ |
4021
|
80 error (#FCN ": expecting dimension argument to be an integer"); \ |
4015
|
81 } \ |
4021
|
82 else \ |
5823
|
83 print_usage (); \ |
4015
|
84 \ |
|
85 return retval |
|
86 |
1957
|
87 DEFUN (all, args, , |
3369
|
88 "-*- texinfo -*-\n\ |
4015
|
89 @deftypefn {Built-in Function} {} all (@var{x}, @var{dim})\n\ |
3369
|
90 The function @code{all} behaves like the function @code{any}, except\n\ |
|
91 that it returns true only if all the elements of a vector, or all the\n\ |
4015
|
92 elements along dimension @var{dim} of a matrix, are nonzero.\n\ |
3369
|
93 @end deftypefn") |
523
|
94 { |
4015
|
95 ANY_ALL (all); |
523
|
96 } |
|
97 |
1957
|
98 DEFUN (any, args, , |
3369
|
99 "-*- texinfo -*-\n\ |
4015
|
100 @deftypefn {Built-in Function} {} any (@var{x}, @var{dim})\n\ |
3369
|
101 For a vector argument, return 1 if any element of the vector is\n\ |
|
102 nonzero.\n\ |
|
103 \n\ |
|
104 For a matrix argument, return a row vector of ones and\n\ |
|
105 zeros with each element indicating whether any of the elements of the\n\ |
|
106 corresponding column of the matrix are nonzero. For example,\n\ |
|
107 \n\ |
|
108 @example\n\ |
|
109 @group\n\ |
|
110 any (eye (2, 4))\n\ |
|
111 @result{} [ 1, 1, 0, 0 ]\n\ |
|
112 @end group\n\ |
|
113 @end example\n\ |
|
114 \n\ |
4015
|
115 If the optional argument @var{dim} is supplied, work along dimension\n\ |
|
116 @var{dim}. For example,\n\ |
3369
|
117 \n\ |
|
118 @example\n\ |
4015
|
119 @group\n\ |
|
120 any (eye (2, 4), 2)\n\ |
|
121 @result{} [ 1; 1 ]\n\ |
|
122 @end group\n\ |
3369
|
123 @end example\n\ |
|
124 @end deftypefn") |
523
|
125 { |
4015
|
126 ANY_ALL (any); |
523
|
127 } |
|
128 |
649
|
129 // These mapping functions may also be useful in other places, eh? |
|
130 |
|
131 typedef double (*d_dd_fcn) (double, double); |
|
132 |
|
133 static Matrix |
2672
|
134 map_d_m (d_dd_fcn f, double x, const Matrix& y) |
649
|
135 { |
5275
|
136 octave_idx_type nr = y.rows (); |
|
137 octave_idx_type nc = y.columns (); |
649
|
138 |
|
139 Matrix retval (nr, nc); |
|
140 |
5275
|
141 for (octave_idx_type j = 0; j < nc; j++) |
|
142 for (octave_idx_type i = 0; i < nr; i++) |
4153
|
143 { |
|
144 OCTAVE_QUIT; |
|
145 retval (i, j) = f (x, y (i, j)); |
|
146 } |
649
|
147 |
|
148 return retval; |
|
149 } |
|
150 |
|
151 static Matrix |
2672
|
152 map_m_d (d_dd_fcn f, const Matrix& x, double y) |
649
|
153 { |
5275
|
154 octave_idx_type nr = x.rows (); |
|
155 octave_idx_type nc = x.columns (); |
649
|
156 |
|
157 Matrix retval (nr, nc); |
|
158 |
5275
|
159 for (octave_idx_type j = 0; j < nc; j++) |
|
160 for (octave_idx_type i = 0; i < nr; i++) |
4153
|
161 { |
|
162 OCTAVE_QUIT; |
|
163 retval (i, j) = f (x (i, j), y); |
|
164 } |
649
|
165 |
|
166 return retval; |
|
167 } |
|
168 |
|
169 static Matrix |
2672
|
170 map_m_m (d_dd_fcn f, const Matrix& x, const Matrix& y) |
649
|
171 { |
5275
|
172 octave_idx_type x_nr = x.rows (); |
|
173 octave_idx_type x_nc = x.columns (); |
649
|
174 |
5275
|
175 octave_idx_type y_nr = y.rows (); |
|
176 octave_idx_type y_nc = y.columns (); |
649
|
177 |
719
|
178 assert (x_nr == y_nr && x_nc == y_nc); |
649
|
179 |
|
180 Matrix retval (x_nr, x_nc); |
|
181 |
5275
|
182 for (octave_idx_type j = 0; j < x_nc; j++) |
|
183 for (octave_idx_type i = 0; i < x_nr; i++) |
4153
|
184 { |
|
185 OCTAVE_QUIT; |
|
186 retval (i, j) = f (x (i, j), y (i, j)); |
|
187 } |
649
|
188 |
|
189 return retval; |
|
190 } |
|
191 |
1957
|
192 DEFUN (atan2, args, , |
3428
|
193 "-*- texinfo -*-\n\ |
|
194 @deftypefn {Mapping Function} {} atan2 (@var{y}, @var{x})\n\ |
|
195 Compute atan (@var{y} / @var{x}) for corresponding elements of @var{y}\n\ |
|
196 and @var{x}. The result is in range -pi to pi.\n\ |
3439
|
197 @end deftypefn") |
649
|
198 { |
4233
|
199 octave_value retval; |
649
|
200 |
712
|
201 int nargin = args.length (); |
|
202 |
|
203 if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
649
|
204 { |
2086
|
205 octave_value arg_y = args(0); |
|
206 octave_value arg_x = args(1); |
649
|
207 |
5275
|
208 octave_idx_type y_nr = arg_y.rows (); |
|
209 octave_idx_type y_nc = arg_y.columns (); |
649
|
210 |
5275
|
211 octave_idx_type x_nr = arg_x.rows (); |
|
212 octave_idx_type x_nc = arg_x.columns (); |
649
|
213 |
|
214 int arg_y_empty = empty_arg ("atan2", y_nr, y_nc); |
|
215 int arg_x_empty = empty_arg ("atan2", x_nr, x_nc); |
|
216 |
719
|
217 if (arg_y_empty > 0 && arg_x_empty > 0) |
4233
|
218 return octave_value (Matrix ()); |
719
|
219 else if (arg_y_empty || arg_x_empty) |
649
|
220 return retval; |
|
221 |
5275
|
222 octave_idx_type y_is_scalar = (y_nr == 1 && y_nc == 1); |
|
223 octave_idx_type x_is_scalar = (x_nr == 1 && x_nc == 1); |
649
|
224 |
|
225 if (y_is_scalar && x_is_scalar) |
|
226 { |
|
227 double y = arg_y.double_value (); |
|
228 |
|
229 if (! error_state) |
|
230 { |
|
231 double x = arg_x.double_value (); |
|
232 |
|
233 if (! error_state) |
|
234 retval = atan2 (y, x); |
|
235 } |
|
236 } |
|
237 else if (y_is_scalar) |
|
238 { |
|
239 double y = arg_y.double_value (); |
|
240 |
|
241 if (! error_state) |
|
242 { |
|
243 Matrix x = arg_x.matrix_value (); |
|
244 |
|
245 if (! error_state) |
2672
|
246 retval = map_d_m (atan2, y, x); |
649
|
247 } |
|
248 } |
|
249 else if (x_is_scalar) |
|
250 { |
|
251 Matrix y = arg_y.matrix_value (); |
|
252 |
|
253 if (! error_state) |
|
254 { |
|
255 double x = arg_x.double_value (); |
|
256 |
|
257 if (! error_state) |
2672
|
258 retval = map_m_d (atan2, y, x); |
649
|
259 } |
|
260 } |
|
261 else if (y_nr == x_nr && y_nc == x_nc) |
|
262 { |
|
263 Matrix y = arg_y.matrix_value (); |
|
264 |
|
265 if (! error_state) |
|
266 { |
|
267 Matrix x = arg_x.matrix_value (); |
|
268 |
|
269 if (! error_state) |
2672
|
270 retval = map_m_m (atan2, y, x); |
649
|
271 } |
|
272 } |
|
273 else |
|
274 error ("atan2: nonconformant matrices"); |
|
275 } |
712
|
276 else |
5823
|
277 print_usage (); |
649
|
278 |
|
279 return retval; |
|
280 } |
|
281 |
4311
|
282 DEFUN (fmod, args, , |
|
283 "-*- texinfo -*-\n\ |
|
284 @deftypefn {Mapping Function} {} fmod (@var{x}, @var{y})\n\ |
4685
|
285 Compute the floating point remainder of dividing @var{x} by @var{y}\n\ |
|
286 using the C library function @code{fmod}. The result has the same\n\ |
|
287 sign as @var{x}. If @var{y} is zero, the result implementation-defined.\n\ |
4311
|
288 @end deftypefn") |
|
289 { |
|
290 octave_value retval; |
|
291 |
|
292 int nargin = args.length (); |
|
293 |
|
294 if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
|
295 { |
|
296 octave_value arg_x = args(0); |
|
297 octave_value arg_y = args(1); |
|
298 |
5275
|
299 octave_idx_type y_nr = arg_y.rows (); |
|
300 octave_idx_type y_nc = arg_y.columns (); |
4311
|
301 |
5275
|
302 octave_idx_type x_nr = arg_x.rows (); |
|
303 octave_idx_type x_nc = arg_x.columns (); |
4311
|
304 |
|
305 int arg_y_empty = empty_arg ("fmod", y_nr, y_nc); |
|
306 int arg_x_empty = empty_arg ("fmod", x_nr, x_nc); |
|
307 |
|
308 if (arg_y_empty > 0 && arg_x_empty > 0) |
|
309 return octave_value (Matrix ()); |
|
310 else if (arg_y_empty || arg_x_empty) |
|
311 return retval; |
|
312 |
5275
|
313 octave_idx_type y_is_scalar = (y_nr == 1 && y_nc == 1); |
|
314 octave_idx_type x_is_scalar = (x_nr == 1 && x_nc == 1); |
4311
|
315 |
|
316 if (y_is_scalar && x_is_scalar) |
|
317 { |
|
318 double y = arg_y.double_value (); |
|
319 |
|
320 if (! error_state) |
|
321 { |
|
322 double x = arg_x.double_value (); |
|
323 |
|
324 if (! error_state) |
|
325 retval = fmod (x, y); |
|
326 } |
|
327 } |
|
328 else if (y_is_scalar) |
|
329 { |
|
330 double y = arg_y.double_value (); |
|
331 |
|
332 if (! error_state) |
|
333 { |
|
334 Matrix x = arg_x.matrix_value (); |
|
335 |
|
336 if (! error_state) |
|
337 retval = map_m_d (fmod, x, y); |
|
338 } |
|
339 } |
|
340 else if (x_is_scalar) |
|
341 { |
|
342 Matrix y = arg_y.matrix_value (); |
|
343 |
|
344 if (! error_state) |
|
345 { |
|
346 double x = arg_x.double_value (); |
|
347 |
|
348 if (! error_state) |
|
349 retval = map_d_m (fmod, x, y); |
|
350 } |
|
351 } |
|
352 else if (y_nr == x_nr && y_nc == x_nc) |
|
353 { |
|
354 Matrix y = arg_y.matrix_value (); |
|
355 |
|
356 if (! error_state) |
|
357 { |
|
358 Matrix x = arg_x.matrix_value (); |
|
359 |
|
360 if (! error_state) |
|
361 retval = map_m_m (fmod, x, y); |
|
362 } |
|
363 } |
|
364 else |
|
365 error ("fmod: nonconformant matrices"); |
|
366 } |
|
367 else |
5823
|
368 print_usage (); |
4311
|
369 |
|
370 return retval; |
|
371 } |
|
372 |
7112
|
373 #define NATIVE_REDUCTION_1(FCN, TYPE, DIM) \ |
|
374 (arg.is_ ## TYPE ## _type ()) \ |
|
375 { \ |
|
376 TYPE ## NDArray tmp = arg. TYPE ##_array_value (); \ |
|
377 \ |
|
378 if (! error_state) \ |
|
379 retval = tmp.FCN (DIM); \ |
|
380 } |
|
381 |
|
382 #define NATIVE_REDUCTION(FCN) \ |
|
383 \ |
|
384 octave_value retval; \ |
|
385 \ |
|
386 int nargin = args.length (); \ |
|
387 \ |
|
388 bool isnative = false; \ |
|
389 \ |
|
390 if (nargin > 1 && args(nargin - 1).is_string ()) \ |
|
391 { \ |
|
392 std::string str = args(nargin - 1).string_value (); \ |
|
393 \ |
|
394 if (! error_state) \ |
|
395 { \ |
|
396 if (str == "native") \ |
|
397 isnative = true; \ |
|
398 else if (str != "double") /* Ignore double as no single type */ \ |
|
399 error ("sum: unrecognized string argument"); \ |
|
400 nargin --; \ |
|
401 } \ |
|
402 } \ |
|
403 \ |
|
404 if (nargin == 1 || nargin == 2) \ |
|
405 { \ |
|
406 octave_value arg = args(0); \ |
|
407 \ |
|
408 int dim = (nargin == 1 ? -1 : args(1).int_value (true) - 1); \ |
|
409 \ |
|
410 if (! error_state) \ |
|
411 { \ |
|
412 if (dim >= -1) \ |
|
413 { \ |
|
414 if (isnative) \ |
|
415 { \ |
|
416 if NATIVE_REDUCTION_1 (FCN, uint8, dim) \ |
|
417 else if NATIVE_REDUCTION_1 (FCN, uint16, dim) \ |
|
418 else if NATIVE_REDUCTION_1 (FCN, uint32, dim) \ |
|
419 else if NATIVE_REDUCTION_1 (FCN, uint64, dim) \ |
|
420 else if NATIVE_REDUCTION_1 (FCN, int8, dim) \ |
|
421 else if NATIVE_REDUCTION_1 (FCN, int16, dim) \ |
|
422 else if NATIVE_REDUCTION_1 (FCN, int32, dim) \ |
|
423 else if NATIVE_REDUCTION_1 (FCN, int64, dim) \ |
|
424 else if NATIVE_REDUCTION_1 (FCN, bool, dim) \ |
|
425 else if (arg.is_char_matrix ()) \ |
|
426 { \ |
|
427 error (#FCN, ": invalid char type"); \ |
|
428 } \ |
|
429 else if (arg.is_complex_type ()) \ |
|
430 { \ |
|
431 ComplexNDArray tmp = arg.complex_array_value (); \ |
|
432 \ |
|
433 if (! error_state) \ |
|
434 retval = tmp.FCN (dim); \ |
|
435 } \ |
|
436 else if (arg.is_real_type ()) \ |
|
437 { \ |
|
438 NDArray tmp = arg.array_value (); \ |
|
439 \ |
|
440 if (! error_state) \ |
|
441 retval = tmp.FCN (dim); \ |
|
442 } \ |
|
443 else \ |
|
444 { \ |
|
445 gripe_wrong_type_arg (#FCN, arg); \ |
|
446 return retval; \ |
|
447 } \ |
|
448 } \ |
|
449 else if (arg.is_real_type ()) \ |
|
450 { \ |
|
451 NDArray tmp = arg.array_value (); \ |
|
452 \ |
|
453 if (! error_state) \ |
|
454 retval = tmp.FCN (dim); \ |
|
455 } \ |
|
456 else if (arg.is_complex_type ()) \ |
|
457 { \ |
|
458 ComplexNDArray tmp = arg.complex_array_value (); \ |
|
459 \ |
|
460 if (! error_state) \ |
|
461 retval = tmp.FCN (dim); \ |
|
462 } \ |
|
463 else \ |
|
464 { \ |
|
465 gripe_wrong_type_arg (#FCN, arg); \ |
|
466 return retval; \ |
|
467 } \ |
|
468 } \ |
|
469 else \ |
|
470 error (#FCN ": invalid dimension argument = %d", dim + 1); \ |
|
471 } \ |
|
472 \ |
|
473 } \ |
|
474 else \ |
|
475 print_usage (); \ |
|
476 \ |
|
477 return retval |
|
478 |
3723
|
479 #define DATA_REDUCTION(FCN) \ |
|
480 \ |
4233
|
481 octave_value retval; \ |
3723
|
482 \ |
|
483 int nargin = args.length (); \ |
|
484 \ |
|
485 if (nargin == 1 || nargin == 2) \ |
|
486 { \ |
|
487 octave_value arg = args(0); \ |
|
488 \ |
3864
|
489 int dim = (nargin == 1 ? -1 : args(1).int_value (true) - 1); \ |
3723
|
490 \ |
|
491 if (! error_state) \ |
|
492 { \ |
4556
|
493 if (dim >= -1) \ |
3723
|
494 { \ |
|
495 if (arg.is_real_type ()) \ |
|
496 { \ |
4569
|
497 NDArray tmp = arg.array_value (); \ |
3723
|
498 \ |
|
499 if (! error_state) \ |
4233
|
500 retval = tmp.FCN (dim); \ |
3723
|
501 } \ |
|
502 else if (arg.is_complex_type ()) \ |
|
503 { \ |
4569
|
504 ComplexNDArray tmp = arg.complex_array_value (); \ |
3723
|
505 \ |
|
506 if (! error_state) \ |
4233
|
507 retval = tmp.FCN (dim); \ |
3723
|
508 } \ |
|
509 else \ |
|
510 { \ |
|
511 gripe_wrong_type_arg (#FCN, arg); \ |
|
512 return retval; \ |
|
513 } \ |
|
514 } \ |
|
515 else \ |
|
516 error (#FCN ": invalid dimension argument = %d", dim + 1); \ |
|
517 } \ |
|
518 } \ |
|
519 else \ |
5823
|
520 print_usage (); \ |
3723
|
521 \ |
|
522 return retval |
|
523 |
1957
|
524 DEFUN (cumprod, args, , |
3428
|
525 "-*- texinfo -*-\n\ |
3723
|
526 @deftypefn {Built-in Function} {} cumprod (@var{x}, @var{dim})\n\ |
|
527 Cumulative product of elements along dimension @var{dim}. If\n\ |
|
528 @var{dim} is omitted, it defaults to 1 (column-wise cumulative\n\ |
|
529 products).\n\ |
5061
|
530 \n\ |
|
531 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ |
|
532 return the cumulative product of the elements as a vector with the\n\ |
|
533 same orientation as @var{x}.\n\ |
3428
|
534 @end deftypefn") |
523
|
535 { |
3723
|
536 DATA_REDUCTION (cumprod); |
523
|
537 } |
|
538 |
1957
|
539 DEFUN (cumsum, args, , |
3428
|
540 "-*- texinfo -*-\n\ |
3723
|
541 @deftypefn {Built-in Function} {} cumsum (@var{x}, @var{dim})\n\ |
|
542 Cumulative sum of elements along dimension @var{dim}. If @var{dim}\n\ |
|
543 is omitted, it defaults to 1 (column-wise cumulative sums).\n\ |
5061
|
544 \n\ |
|
545 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ |
|
546 return the cumulative sum of the elements as a vector with the\n\ |
|
547 same orientation as @var{x}.\n\ |
3428
|
548 @end deftypefn") |
523
|
549 { |
3723
|
550 DATA_REDUCTION (cumsum); |
523
|
551 } |
|
552 |
6979
|
553 template <class T> |
2086
|
554 static octave_value |
6979
|
555 make_diag (const T& v, octave_idx_type k) |
767
|
556 { |
2086
|
557 octave_value retval; |
6979
|
558 dim_vector dv = v.dims (); |
|
559 octave_idx_type nd = dv.length (); |
|
560 |
|
561 if (nd > 2) |
|
562 error ("diag: expecting 2-dimensional matrix"); |
767
|
563 else |
|
564 { |
6979
|
565 octave_idx_type nr = dv (0); |
|
566 octave_idx_type nc = dv (1); |
|
567 |
|
568 if (nr == 0 || nc == 0) |
|
569 retval = T (); |
|
570 else if (nr != 1 && nc != 1) |
|
571 retval = v.diag (k); |
|
572 else |
|
573 { |
|
574 octave_idx_type roff = 0; |
|
575 octave_idx_type coff = 0; |
|
576 if (k > 0) |
|
577 { |
|
578 roff = 0; |
|
579 coff = k; |
|
580 } |
|
581 else if (k < 0) |
|
582 { |
|
583 roff = -k; |
|
584 coff = 0; |
|
585 } |
|
586 |
|
587 if (nr == 1) |
|
588 { |
|
589 octave_idx_type n = nc + std::abs (k); |
|
590 T m (dim_vector (n, n), T::resize_fill_value ()); |
|
591 |
|
592 for (octave_idx_type i = 0; i < nc; i++) |
|
593 m (i+roff, i+coff) = v (0, i); |
|
594 retval = m; |
|
595 } |
|
596 else |
|
597 { |
|
598 octave_idx_type n = nr + std::abs (k); |
|
599 T m (dim_vector (n, n), T::resize_fill_value ()); |
|
600 for (octave_idx_type i = 0; i < nr; i++) |
|
601 m (i+roff, i+coff) = v (i, 0); |
|
602 retval = m; |
|
603 } |
|
604 } |
767
|
605 } |
6979
|
606 |
767
|
607 return retval; |
|
608 } |
|
609 |
6979
|
610 #if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL) |
|
611 static octave_value |
|
612 make_diag (const Matrix& v, octave_idx_type k); |
|
613 |
|
614 static octave_value |
|
615 make_diag (const ComplexMatrix& v, octave_idx_type k); |
|
616 |
|
617 static octave_value |
|
618 make_diag (const charMatrix& v, octave_idx_type k); |
|
619 |
|
620 static octave_value |
|
621 make_diag (const boolMatrix& v, octave_idx_type k); |
|
622 |
|
623 static octave_value |
|
624 make_diag (const int8NDArray& v, octave_idx_type k); |
|
625 |
|
626 static octave_value |
|
627 make_diag (const int16NDArray& v, octave_idx_type k); |
|
628 |
|
629 static octave_value |
|
630 make_diag (const int32NDArray& v, octave_idx_type k); |
|
631 |
|
632 static octave_value |
|
633 make_diag (const int64NDArray& v, octave_idx_type k); |
|
634 |
2086
|
635 static octave_value |
6979
|
636 make_diag (const uint8NDArray& v, octave_idx_type k); |
|
637 |
|
638 static octave_value |
|
639 make_diag (const uint16NDArray& v, octave_idx_type k); |
|
640 |
|
641 static octave_value |
|
642 make_diag (const uint32NDArray& v, octave_idx_type k); |
|
643 |
|
644 static octave_value |
|
645 make_diag (const uint64NDArray& v, octave_idx_type k); |
|
646 #endif |
|
647 |
|
648 static octave_value |
|
649 make_diag (const octave_value& a, octave_idx_type k) |
767
|
650 { |
2086
|
651 octave_value retval; |
6979
|
652 std::string result_type = a.class_name (); |
|
653 |
|
654 if (result_type == "double") |
767
|
655 { |
6979
|
656 if (a.is_real_type ()) |
|
657 { |
|
658 Matrix m = a.matrix_value (); |
|
659 if (!error_state) |
|
660 retval = make_diag (m, k); |
|
661 } |
|
662 else |
|
663 { |
|
664 ComplexMatrix m = a.complex_matrix_value (); |
|
665 if (!error_state) |
|
666 retval = make_diag (m, k); |
|
667 } |
767
|
668 } |
6979
|
669 #if 0 |
|
670 else if (result_type == "single") |
|
671 retval = make_diag (a.single_array_value (), k); |
|
672 #endif |
|
673 else if (result_type == "char") |
767
|
674 { |
6979
|
675 charMatrix m = a.char_matrix_value (); |
|
676 if (!error_state) |
|
677 { |
|
678 retval = make_diag (m, k); |
|
679 if (a.is_sq_string ()) |
|
680 retval = octave_value (retval.char_array_value (), true, '\''); |
|
681 } |
|
682 } |
|
683 else if (result_type == "logical") |
|
684 { |
|
685 boolMatrix m = a.bool_matrix_value (); |
|
686 if (!error_state) |
|
687 retval = make_diag (m, k); |
767
|
688 } |
6979
|
689 else if (result_type == "int8") |
|
690 retval = make_diag (a.int8_array_value (), k); |
|
691 else if (result_type == "int16") |
|
692 retval = make_diag (a.int16_array_value (), k); |
|
693 else if (result_type == "int32") |
|
694 retval = make_diag (a.int32_array_value (), k); |
|
695 else if (result_type == "int64") |
|
696 retval = make_diag (a.int64_array_value (), k); |
|
697 else if (result_type == "uint8") |
|
698 retval = make_diag (a.uint8_array_value (), k); |
|
699 else if (result_type == "uint16") |
|
700 retval = make_diag (a.uint16_array_value (), k); |
|
701 else if (result_type == "uint32") |
|
702 retval = make_diag (a.uint32_array_value (), k); |
|
703 else if (result_type == "uint64") |
|
704 retval = make_diag (a.uint64_array_value (), k); |
767
|
705 else |
6979
|
706 gripe_wrong_type_arg ("diag", a); |
767
|
707 |
|
708 return retval; |
|
709 } |
|
710 |
2086
|
711 static octave_value |
|
712 make_diag (const octave_value& arg) |
767
|
713 { |
6979
|
714 return make_diag (arg, 0); |
767
|
715 } |
|
716 |
2086
|
717 static octave_value |
|
718 make_diag (const octave_value& a, const octave_value& b) |
767
|
719 { |
2086
|
720 octave_value retval; |
767
|
721 |
5275
|
722 octave_idx_type k = b.int_value (); |
767
|
723 |
|
724 if (error_state) |
6979
|
725 error ("diag: invalid second argument"); |
767
|
726 else |
6979
|
727 retval = make_diag (a, k); |
767
|
728 |
|
729 return retval; |
|
730 } |
|
731 |
1957
|
732 DEFUN (diag, args, , |
3369
|
733 "-*- texinfo -*-\n\ |
|
734 @deftypefn {Built-in Function} {} diag (@var{v}, @var{k})\n\ |
|
735 Return a diagonal matrix with vector @var{v} on diagonal @var{k}. The\n\ |
|
736 second argument is optional. If it is positive, the vector is placed on\n\ |
|
737 the @var{k}-th super-diagonal. If it is negative, it is placed on the\n\ |
|
738 @var{-k}-th sub-diagonal. The default value of @var{k} is 0, and the\n\ |
|
739 vector is placed on the main diagonal. For example,\n\ |
|
740 \n\ |
|
741 @example\n\ |
|
742 @group\n\ |
|
743 diag ([1, 2, 3], 1)\n\ |
|
744 @result{} 0 1 0 0\n\ |
|
745 0 0 2 0\n\ |
|
746 0 0 0 3\n\ |
|
747 0 0 0 0\n\ |
|
748 @end group\n\ |
|
749 @end example\n\ |
6772
|
750 \n\ |
|
751 @noindent\n\ |
|
752 Given a matrix argument, instead of a vector, @code{diag} extracts the\n\ |
6774
|
753 @var{k}-th diagonal of the matrix.\n\ |
3369
|
754 @end deftypefn") |
523
|
755 { |
4233
|
756 octave_value retval; |
523
|
757 |
|
758 int nargin = args.length (); |
|
759 |
712
|
760 if (nargin == 1 && args(0).is_defined ()) |
767
|
761 retval = make_diag (args(0)); |
712
|
762 else if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
767
|
763 retval = make_diag (args(0), args(1)); |
523
|
764 else |
5823
|
765 print_usage (); |
523
|
766 |
|
767 return retval; |
|
768 } |
|
769 |
1957
|
770 DEFUN (prod, args, , |
3428
|
771 "-*- texinfo -*-\n\ |
3723
|
772 @deftypefn {Built-in Function} {} prod (@var{x}, @var{dim})\n\ |
|
773 Product of elements along dimension @var{dim}. If @var{dim} is\n\ |
|
774 omitted, it defaults to 1 (column-wise products).\n\ |
5061
|
775 \n\ |
|
776 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ |
|
777 return the product of the elements.\n\ |
3428
|
778 @end deftypefn") |
523
|
779 { |
3723
|
780 DATA_REDUCTION (prod); |
523
|
781 } |
|
782 |
4824
|
783 static octave_value |
|
784 do_cat (const octave_value_list& args, std::string fname) |
4806
|
785 { |
|
786 octave_value retval; |
|
787 |
4824
|
788 int n_args = args.length (); |
4806
|
789 |
5714
|
790 if (n_args == 1) |
|
791 retval = Matrix (); |
|
792 else if (n_args == 2) |
|
793 retval = args(1); |
5507
|
794 else if (n_args > 2) |
4824
|
795 { |
5275
|
796 octave_idx_type dim = args(0).int_value () - 1; |
4806
|
797 |
4824
|
798 if (error_state) |
4806
|
799 { |
4824
|
800 error ("cat: expecting first argument to be a integer"); |
4806
|
801 return retval; |
|
802 } |
|
803 |
4824
|
804 if (dim >= 0) |
|
805 { |
4915
|
806 |
|
807 dim_vector dv = args(1).dims (); |
4824
|
808 |
4915
|
809 for (int i = 2; i < args.length (); i++) |
|
810 { |
|
811 // add_dims constructs a dimension vector which holds the |
4824
|
812 // dimensions of the final array after concatenation. |
4806
|
813 |
4915
|
814 if (! dv.concat (args(i).dims (), dim)) |
4806
|
815 { |
4824
|
816 // Dimensions do not match. |
4915
|
817 error ("cat: dimension mismatch"); |
4806
|
818 return retval; |
|
819 } |
4824
|
820 } |
|
821 |
4915
|
822 // The lines below might seem crazy, since we take a copy |
|
823 // of the first argument, resize it to be empty and then resize |
|
824 // it to be full. This is done since it means that there is no |
|
825 // recopying of data, as would happen if we used a single resize. |
|
826 // It should be noted that resize operation is also significantly |
|
827 // slower than the do_cat_op function, so it makes sense to have an |
|
828 // empty matrix and copy all data. |
4824
|
829 // |
4915
|
830 // We might also start with a empty octave_value using |
|
831 // tmp = octave_value_typeinfo::lookup_type (args(1).type_name()); |
|
832 // and then directly resize. However, for some types there might be |
|
833 // some additional setup needed, and so this should be avoided. |
5533
|
834 |
4915
|
835 octave_value tmp; |
5533
|
836 |
6399
|
837 int i; |
|
838 for (i = 1; i < n_args; i++) |
5533
|
839 { |
|
840 if (! args (i).all_zero_dims ()) |
|
841 { |
|
842 tmp = args (i); |
|
843 break; |
|
844 } |
|
845 } |
5164
|
846 |
6401
|
847 if (i == n_args) |
|
848 retval = Matrix (); |
|
849 else |
4915
|
850 { |
6401
|
851 tmp = tmp.resize (dim_vector (0,0)).resize (dv); |
4824
|
852 |
4915
|
853 if (error_state) |
|
854 return retval; |
4806
|
855 |
6883
|
856 int dv_len = dv.length (); |
|
857 Array<octave_idx_type> ra_idx (dv_len, 0); |
6401
|
858 |
|
859 for (int j = i; j < n_args; j++) |
|
860 { |
6887
|
861 if (args (j). dims (). any_zero ()) |
6883
|
862 continue; |
|
863 |
6401
|
864 tmp = do_cat_op (tmp, args (j), ra_idx); |
|
865 |
|
866 if (error_state) |
|
867 return retval; |
|
868 |
|
869 dim_vector dv_tmp = args (j).dims (); |
|
870 |
6883
|
871 if (dim >= dv_len) |
|
872 { |
|
873 if (j > i) |
|
874 error ("%s: indexing error", fname.c_str ()); |
|
875 break; |
|
876 } |
|
877 else |
|
878 ra_idx (dim) += (dim < dv_tmp.length () ? |
|
879 dv_tmp (dim) : 1); |
6401
|
880 } |
|
881 |
|
882 retval = tmp; |
4915
|
883 } |
4806
|
884 } |
5533
|
885 else |
|
886 error ("%s: invalid dimension argument", fname.c_str ()); |
4806
|
887 } |
|
888 else |
5823
|
889 print_usage (); |
4806
|
890 |
|
891 return retval; |
|
892 } |
|
893 |
|
894 DEFUN (horzcat, args, , |
4824
|
895 "-*- texinfo -*-\n\ |
4806
|
896 @deftypefn {Built-in Function} {} horzcat (@var{array1}, @var{array2}, @dots{}, @var{arrayN})\n\ |
|
897 Return the horizontal concatenation of N-d array objects, @var{array1},\n\ |
|
898 @var{array2}, @dots{}, @var{arrayN} along dimension 2.\n\ |
5642
|
899 @seealso{cat, vertcat}\n\ |
|
900 @end deftypefn") |
4806
|
901 { |
|
902 octave_value_list args_tmp = args; |
|
903 |
|
904 int dim = 2; |
|
905 |
|
906 octave_value d (dim); |
|
907 |
|
908 args_tmp.prepend (d); |
|
909 |
4824
|
910 return do_cat (args_tmp, "horzcat"); |
4806
|
911 } |
|
912 |
|
913 DEFUN (vertcat, args, , |
|
914 "-*- texinfo -*-\n\ |
|
915 @deftypefn {Built-in Function} {} vertcat (@var{array1}, @var{array2}, @dots{}, @var{arrayN})\n\ |
|
916 Return the vertical concatenation of N-d array objects, @var{array1},\n\ |
|
917 @var{array2}, @dots{}, @var{arrayN} along dimension 1.\n\ |
5642
|
918 @seealso{cat, horzcat}\n\ |
|
919 @end deftypefn") |
4806
|
920 { |
|
921 octave_value_list args_tmp = args; |
|
922 |
|
923 int dim = 1; |
|
924 |
|
925 octave_value d (dim); |
|
926 |
|
927 args_tmp.prepend (d); |
|
928 |
4824
|
929 return do_cat (args_tmp, "vertcat"); |
4806
|
930 } |
|
931 |
4758
|
932 DEFUN (cat, args, , |
|
933 "-*- texinfo -*-\n\ |
|
934 @deftypefn {Built-in Function} {} cat (@var{dim}, @var{array1}, @var{array2}, @dots{}, @var{arrayN})\n\ |
4806
|
935 Return the concatenation of N-d array objects, @var{array1},\n\ |
|
936 @var{array2}, @dots{}, @var{arrayN} along dimension @var{dim}.\n\ |
4758
|
937 \n\ |
|
938 @example\n\ |
|
939 @group\n\ |
|
940 A = ones (2, 2);\n\ |
|
941 B = zeros (2, 2);\n\ |
|
942 cat (2, A, B)\n\ |
|
943 @result{} ans =\n\ |
|
944 \n\ |
|
945 1 1 0 0\n\ |
|
946 1 1 0 0\n\ |
|
947 @end group\n\ |
|
948 @end example\n\ |
|
949 \n\ |
|
950 Alternatively, we can concatenate @var{A} and @var{B} along the\n\ |
|
951 second dimension the following way:\n\ |
|
952 \n\ |
|
953 @example\n\ |
|
954 @group\n\ |
|
955 [A, B].\n\ |
|
956 @end group\n\ |
|
957 @end example\n\ |
|
958 \n\ |
|
959 @var{dim} can be larger than the dimensions of the N-d array objects\n\ |
|
960 and the result will thus have @var{dim} dimensions as the\n\ |
|
961 following example shows:\n\ |
|
962 @example\n\ |
|
963 @group\n\ |
|
964 cat (4, ones(2, 2), zeros (2, 2))\n\ |
|
965 @result{} ans =\n\ |
|
966 \n\ |
|
967 ans(:,:,1,1) =\n\ |
|
968 \n\ |
|
969 1 1\n\ |
|
970 1 1\n\ |
|
971 \n\ |
|
972 ans(:,:,1,2) =\n\ |
|
973 0 0\n\ |
|
974 0 0\n\ |
|
975 @end group\n\ |
|
976 @end example\n\ |
5642
|
977 @seealso{horzcat, vertcat}\n\ |
|
978 @end deftypefn") |
4758
|
979 { |
4824
|
980 return do_cat (args, "cat"); |
4758
|
981 } |
|
982 |
4593
|
983 static octave_value |
6959
|
984 do_permute (const octave_value_list& args, bool inv) |
4593
|
985 { |
|
986 octave_value retval; |
|
987 |
5148
|
988 if (args.length () == 2 && args(1).length () >= args(1).ndims ()) |
4593
|
989 { |
|
990 Array<int> vec = args(1).int_vector_value (); |
|
991 |
5775
|
992 // FIXME -- maybe we should create an idx_vector object |
5148
|
993 // here and pass that to permute? |
|
994 |
|
995 int n = vec.length (); |
|
996 |
|
997 for (int i = 0; i < n; i++) |
|
998 vec(i)--; |
|
999 |
4593
|
1000 octave_value ret = args(0).permute (vec, inv); |
|
1001 |
|
1002 if (! error_state) |
|
1003 retval = ret; |
|
1004 } |
|
1005 else |
5823
|
1006 print_usage (); |
4593
|
1007 |
|
1008 return retval; |
|
1009 } |
|
1010 |
|
1011 DEFUN (permute, args, , |
|
1012 "-*- texinfo -*-\n\ |
|
1013 @deftypefn {Built-in Function} {} permute (@var{a}, @var{perm})\n\ |
|
1014 Return the generalized transpose for an N-d array object @var{a}.\n\ |
|
1015 The permutation vector @var{perm} must contain the elements\n\ |
|
1016 @code{1:ndims(a)} (in any order, but each element must appear just once).\n\ |
5642
|
1017 @seealso{ipermute}\n\ |
|
1018 @end deftypefn") |
4593
|
1019 { |
6959
|
1020 return do_permute (args, false); |
4593
|
1021 } |
|
1022 |
|
1023 DEFUN (ipermute, args, , |
|
1024 "-*- texinfo -*-\n\ |
|
1025 @deftypefn {Built-in Function} {} ipermute (@var{a}, @var{iperm})\n\ |
|
1026 The inverse of the @code{permute} function. The expression\n\ |
|
1027 \n\ |
|
1028 @example\n\ |
|
1029 ipermute (permute (a, perm), perm)\n\ |
|
1030 @end example\n\ |
|
1031 returns the original array @var{a}.\n\ |
5642
|
1032 @seealso{permute}\n\ |
|
1033 @end deftypefn") |
4593
|
1034 { |
6959
|
1035 return do_permute (args, true); |
4593
|
1036 } |
|
1037 |
3195
|
1038 DEFUN (length, args, , |
3373
|
1039 "-*- texinfo -*-\n\ |
|
1040 @deftypefn {Built-in Function} {} length (@var{a})\n\ |
4176
|
1041 Return the `length' of the object @var{a}. For matrix objects, the\n\ |
3373
|
1042 length is the number of rows or columns, whichever is greater (this\n\ |
6556
|
1043 odd definition is used for compatibility with @sc{Matlab}).\n\ |
3373
|
1044 @end deftypefn") |
3195
|
1045 { |
|
1046 octave_value retval; |
|
1047 |
|
1048 if (args.length () == 1) |
|
1049 { |
|
1050 int len = args(0).length (); |
|
1051 |
|
1052 if (! error_state) |
4233
|
1053 retval = len; |
3195
|
1054 } |
|
1055 else |
5823
|
1056 print_usage (); |
3195
|
1057 |
|
1058 return retval; |
|
1059 } |
|
1060 |
4554
|
1061 DEFUN (ndims, args, , |
|
1062 "-*- texinfo -*-\n\ |
|
1063 @deftypefn {Built-in Function} {} ndims (@var{a})\n\ |
|
1064 Returns the number of dimensions of array @var{a}.\n\ |
|
1065 For any array, the result will always be larger than or equal to 2.\n\ |
|
1066 Trailing singleton dimensions are not counted.\n\ |
|
1067 @end deftypefn") |
|
1068 { |
|
1069 octave_value retval; |
|
1070 |
|
1071 if (args.length () == 1) |
|
1072 { |
|
1073 int n_dims = args(0).ndims (); |
|
1074 |
|
1075 if (! error_state) |
|
1076 retval = n_dims; |
|
1077 } |
|
1078 else |
5823
|
1079 print_usage (); |
4554
|
1080 |
|
1081 return retval; |
|
1082 } |
|
1083 |
4559
|
1084 DEFUN (numel, args, , |
|
1085 "-*- texinfo -*-\n\ |
|
1086 @deftypefn {Built-in Function} {} numel (@var{a})\n\ |
|
1087 Returns the number of elements in the object @var{a}.\n\ |
5724
|
1088 @seealso{size}\n\ |
4559
|
1089 @end deftypefn") |
|
1090 { |
|
1091 octave_value retval; |
|
1092 |
|
1093 if (args.length () == 1) |
|
1094 { |
|
1095 int numel = args(0).numel (); |
|
1096 |
|
1097 if (! error_state) |
|
1098 { |
|
1099 if (numel < 0) |
|
1100 numel = 0; |
|
1101 |
|
1102 retval = numel; |
|
1103 } |
|
1104 } |
|
1105 else |
5823
|
1106 print_usage (); |
4559
|
1107 |
|
1108 return retval; |
|
1109 } |
|
1110 |
1957
|
1111 DEFUN (size, args, nargout, |
3373
|
1112 "-*- texinfo -*-\n\ |
|
1113 @deftypefn {Built-in Function} {} size (@var{a}, @var{n})\n\ |
|
1114 Return the number rows and columns of @var{a}.\n\ |
|
1115 \n\ |
|
1116 With one input argument and one output argument, the result is returned\n\ |
4741
|
1117 in a row vector. If there are multiple output arguments, the number of\n\ |
|
1118 rows is assigned to the first, and the number of columns to the second,\n\ |
|
1119 etc. For example,\n\ |
3373
|
1120 \n\ |
|
1121 @example\n\ |
|
1122 @group\n\ |
|
1123 size ([1, 2; 3, 4; 5, 6])\n\ |
|
1124 @result{} [ 3, 2 ]\n\ |
1031
|
1125 \n\ |
3373
|
1126 [nr, nc] = size ([1, 2; 3, 4; 5, 6])\n\ |
|
1127 @result{} nr = 3\n\ |
|
1128 @result{} nc = 2\n\ |
|
1129 @end group\n\ |
|
1130 @end example\n\ |
|
1131 \n\ |
4741
|
1132 If given a second argument, @code{size} will return the size of the\n\ |
|
1133 corresponding dimension. For example\n\ |
1031
|
1134 \n\ |
3373
|
1135 @example\n\ |
|
1136 size ([1, 2; 3, 4; 5, 6], 2)\n\ |
|
1137 @result{} 2\n\ |
|
1138 @end example\n\ |
|
1139 \n\ |
|
1140 @noindent\n\ |
|
1141 returns the number of columns in the given matrix.\n\ |
5724
|
1142 @seealso{numel}\n\ |
3373
|
1143 @end deftypefn") |
523
|
1144 { |
2086
|
1145 octave_value_list retval; |
523
|
1146 |
|
1147 int nargin = args.length (); |
|
1148 |
4513
|
1149 if (nargin == 1) |
523
|
1150 { |
4513
|
1151 dim_vector dimensions = args(0).dims (); |
|
1152 |
|
1153 int ndims = dimensions.length (); |
1031
|
1154 |
4513
|
1155 Matrix m (1, ndims); |
|
1156 |
|
1157 if (nargout > 1) |
523
|
1158 { |
5991
|
1159 for (int i = nargout-1; i >= ndims; i--) |
|
1160 retval(i) = 1; |
|
1161 |
6197
|
1162 if (ndims > nargout) |
|
1163 { |
|
1164 octave_idx_type d = 1; |
|
1165 |
|
1166 while (ndims >= nargout) |
|
1167 d *= dimensions(--ndims); |
|
1168 |
|
1169 retval(ndims) = d; |
|
1170 } |
|
1171 |
4513
|
1172 while (ndims--) |
|
1173 retval(ndims) = dimensions(ndims); |
523
|
1174 } |
4513
|
1175 else |
712
|
1176 { |
4513
|
1177 for (int i = 0; i < ndims; i++) |
|
1178 m(0, i) = dimensions(i); |
|
1179 |
|
1180 retval(0) = m; |
712
|
1181 } |
1031
|
1182 } |
|
1183 else if (nargin == 2 && nargout < 2) |
|
1184 { |
5275
|
1185 octave_idx_type nd = args(1).int_value (true); |
1031
|
1186 |
|
1187 if (error_state) |
|
1188 error ("size: expecting scalar as second argument"); |
712
|
1189 else |
1031
|
1190 { |
4741
|
1191 dim_vector dv = args(0).dims (); |
|
1192 |
4911
|
1193 if (nd > 0) |
|
1194 { |
|
1195 if (nd <= dv.length ()) |
|
1196 retval(0) = dv(nd-1); |
|
1197 else |
|
1198 retval(0) = 1; |
|
1199 } |
1031
|
1200 else |
4741
|
1201 error ("size: requested dimension (= %d) out of range", nd); |
1031
|
1202 } |
523
|
1203 } |
712
|
1204 else |
5823
|
1205 print_usage (); |
523
|
1206 |
|
1207 return retval; |
|
1208 } |
|
1209 |
6156
|
1210 DEFUN (size_equal, args, , |
|
1211 "-*- texinfo -*-\n\ |
6561
|
1212 @deftypefn {Built-in Function} {} size_equal (@var{a}, @var{b}, @dots{})\n\ |
|
1213 Return true if the dimensions of all arguments agree.\n\ |
6156
|
1214 Trailing singleton dimensions are ignored.\n\ |
|
1215 @seealso{size, numel}\n\ |
|
1216 @end deftypefn") |
|
1217 { |
|
1218 octave_value retval; |
|
1219 |
6561
|
1220 int nargin = args.length (); |
|
1221 |
|
1222 if (nargin >= 2) |
6156
|
1223 { |
6561
|
1224 retval = true; |
|
1225 |
6156
|
1226 dim_vector a_dims = args(0).dims (); |
|
1227 a_dims.chop_trailing_singletons (); |
6561
|
1228 |
|
1229 for (int i = 1; i < nargin; ++i) |
|
1230 { |
|
1231 dim_vector b_dims = args(i).dims (); |
|
1232 b_dims.chop_trailing_singletons (); |
|
1233 |
|
1234 if (a_dims != b_dims) |
|
1235 { |
|
1236 retval = false; |
|
1237 break; |
|
1238 } |
|
1239 } |
6156
|
1240 } |
|
1241 else |
|
1242 print_usage (); |
|
1243 |
|
1244 return retval; |
|
1245 } |
|
1246 |
5602
|
1247 DEFUN (nnz, args, , |
|
1248 "-*- texinfo -*-\n\ |
6156
|
1249 @deftypefn {Built-in Function} {@var{scalar} =} nnz (@var{a})\n\ |
|
1250 Returns the number of non zero elements in @var{a}.\n\ |
5602
|
1251 @seealso{sparse}\n\ |
|
1252 @end deftypefn") |
|
1253 { |
|
1254 octave_value retval; |
|
1255 |
|
1256 if (args.length () == 1) |
|
1257 retval = args(0).nnz (); |
|
1258 else |
5823
|
1259 print_usage (); |
5602
|
1260 |
|
1261 return retval; |
|
1262 } |
|
1263 |
5604
|
1264 DEFUN (nzmax, args, , |
|
1265 "-*- texinfo -*-\n\ |
6156
|
1266 @deftypefn {Built-in Function} {@var{scalar} =} nzmax (@var{SM})\n\ |
5604
|
1267 Return the amount of storage allocated to the sparse matrix @var{SM}.\n\ |
7001
|
1268 Note that Octave tends to crop unused memory at the first opportunity\n\ |
5604
|
1269 for sparse objects. There are some cases of user created sparse objects\n\ |
|
1270 where the value returned by @dfn{nzmaz} will not be the same as @dfn{nnz},\n\ |
|
1271 but in general they will give the same result.\n\ |
|
1272 @seealso{sparse, spalloc}\n\ |
|
1273 @end deftypefn") |
|
1274 { |
|
1275 octave_value retval; |
|
1276 |
|
1277 if (args.length() == 1) |
|
1278 retval = args(0).nzmax (); |
|
1279 else |
5823
|
1280 print_usage (); |
5604
|
1281 |
|
1282 return retval; |
|
1283 } |
|
1284 |
5677
|
1285 DEFUN (rows, args, , |
|
1286 "-*- texinfo -*-\n\ |
|
1287 @deftypefn {Built-in Function} {} rows (@var{a})\n\ |
|
1288 Return the number of rows of @var{a}.\n\ |
5724
|
1289 @seealso{size, numel, columns, length, isscalar, isvector, ismatrix}\n\ |
5677
|
1290 @end deftypefn") |
|
1291 { |
|
1292 octave_value retval; |
|
1293 |
|
1294 if (args.length () == 1) |
|
1295 retval = args(0).rows (); |
|
1296 else |
5823
|
1297 print_usage (); |
5677
|
1298 |
|
1299 return retval; |
|
1300 } |
|
1301 |
|
1302 DEFUN (columns, args, , |
|
1303 "-*- texinfo -*-\n\ |
|
1304 @deftypefn {Built-in Function} {} columns (@var{a})\n\ |
|
1305 Return the number of columns of @var{a}.\n\ |
5724
|
1306 @seealso{size, numel, rows, length, isscalar, isvector, and ismatrix}\n\ |
5677
|
1307 @end deftypefn") |
|
1308 { |
|
1309 octave_value retval; |
|
1310 |
|
1311 if (args.length () == 1) |
|
1312 retval = args(0).columns (); |
|
1313 else |
5823
|
1314 print_usage (); |
5677
|
1315 |
|
1316 return retval; |
|
1317 } |
|
1318 |
1957
|
1319 DEFUN (sum, args, , |
3428
|
1320 "-*- texinfo -*-\n\ |
3723
|
1321 @deftypefn {Built-in Function} {} sum (@var{x}, @var{dim})\n\ |
7112
|
1322 @deftypefnx {Built-in Function} {} sum (@dots{}, 'native')\n\ |
3723
|
1323 Sum of elements along dimension @var{dim}. If @var{dim} is\n\ |
|
1324 omitted, it defaults to 1 (column-wise sum).\n\ |
5061
|
1325 \n\ |
|
1326 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ |
|
1327 return the sum of the elements.\n\ |
7112
|
1328 \n\ |
|
1329 If the optional argument 'native' is given, then the sum is performed\n\ |
|
1330 in the same type as the original argument, rather than in the default\n\ |
|
1331 double type. For example\n\ |
|
1332 \n\ |
|
1333 @example\n\ |
|
1334 sum ([true, true])\n\ |
|
1335 @result{} 2\n\ |
|
1336 sum ([true, true], 'native')\n\ |
|
1337 @result{} true\n\ |
|
1338 @end example\n\ |
3428
|
1339 @end deftypefn") |
523
|
1340 { |
7112
|
1341 NATIVE_REDUCTION (sum); |
523
|
1342 } |
|
1343 |
7112
|
1344 /* |
|
1345 |
|
1346 %!assert (sum([true,true]), 2) |
|
1347 %!assert (sum([true,true],'native'), true) |
|
1348 %!assert (sum(int8([127,10,-20])), 117); |
|
1349 %!assert (sum(int8([127,10,-20]),'native'), int8(107)); |
|
1350 |
|
1351 */ |
|
1352 |
1957
|
1353 DEFUN (sumsq, args, , |
3428
|
1354 "-*- texinfo -*-\n\ |
3723
|
1355 @deftypefn {Built-in Function} {} sumsq (@var{x}, @var{dim})\n\ |
|
1356 Sum of squares of elements along dimension @var{dim}. If @var{dim}\n\ |
|
1357 is omitted, it defaults to 1 (column-wise sum of squares).\n\ |
3095
|
1358 \n\ |
5061
|
1359 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ |
|
1360 return the sum of squares of the elements.\n\ |
|
1361 \n\ |
|
1362 This function is conceptually equivalent to computing\n\ |
3723
|
1363 @example\n\ |
|
1364 sum (x .* conj (x), dim)\n\ |
|
1365 @end example\n\ |
|
1366 but it uses less memory and avoids calling conj if @var{x} is real.\n\ |
3428
|
1367 @end deftypefn") |
523
|
1368 { |
3723
|
1369 DATA_REDUCTION (sumsq); |
523
|
1370 } |
|
1371 |
6688
|
1372 DEFUN (islogical, args, , |
3428
|
1373 "-*- texinfo -*-\n\ |
7144
|
1374 @deftypefn {Built-in Function} {} islogical (@var{x})\n\ |
6688
|
1375 Return true if @var{x} is a logical object.\n\ |
3439
|
1376 @end deftypefn") |
3209
|
1377 { |
|
1378 octave_value retval; |
|
1379 |
|
1380 if (args.length () == 1) |
3258
|
1381 retval = args(0).is_bool_type (); |
3209
|
1382 else |
5823
|
1383 print_usage (); |
3209
|
1384 |
|
1385 return retval; |
|
1386 } |
|
1387 |
6688
|
1388 DEFALIAS (isbool, islogical); |
3209
|
1389 |
6223
|
1390 DEFUN (isinteger, args, , |
|
1391 "-*- texinfo -*-\n\ |
6230
|
1392 @deftypefn {Built-in Function} {} isinteger (@var{x})\n\ |
6223
|
1393 Return true if @var{x} is an integer object (int8, uint8, int16, etc.).\n\ |
|
1394 Note that @code{isinteger (14)} is false because numeric constants in\n\ |
|
1395 are double precision floating point values.\n\ |
|
1396 @seealso{isreal, isnumeric, class, isa}\n\ |
|
1397 @end deftypefn") |
|
1398 { |
|
1399 octave_value retval; |
|
1400 |
|
1401 if (args.length () == 1) |
|
1402 retval = args(0).is_integer_type (); |
|
1403 else |
|
1404 print_usage (); |
|
1405 |
|
1406 return retval; |
|
1407 } |
|
1408 |
4028
|
1409 DEFUN (iscomplex, args, , |
3428
|
1410 "-*- texinfo -*-\n\ |
4028
|
1411 @deftypefn {Built-in Function} {} iscomplex (@var{x})\n\ |
3428
|
1412 Return true if @var{x} is a complex-valued numeric object.\n\ |
|
1413 @end deftypefn") |
3186
|
1414 { |
|
1415 octave_value retval; |
|
1416 |
|
1417 if (args.length () == 1) |
3258
|
1418 retval = args(0).is_complex_type (); |
3186
|
1419 else |
5823
|
1420 print_usage (); |
3186
|
1421 |
|
1422 return retval; |
|
1423 } |
|
1424 |
5775
|
1425 // FIXME -- perhaps this should be implemented with an |
5476
|
1426 // octave_value member function? |
|
1427 |
|
1428 DEFUN (complex, args, , |
|
1429 "-*- texinfo -*-\n\ |
|
1430 @deftypefn {Built-in Function} {} complex (@var{val})\n\ |
|
1431 @deftypefnx {Built-in Function} {} complex (@var{re}, @var{im})\n\ |
|
1432 Convert @var{x} to a complex value.\n\ |
|
1433 @end deftypefn") |
|
1434 { |
|
1435 octave_value retval; |
|
1436 |
|
1437 int nargin = args.length (); |
|
1438 |
|
1439 if (nargin == 1) |
|
1440 { |
|
1441 octave_value arg = args(0); |
|
1442 |
|
1443 if (arg.is_complex_type ()) |
|
1444 retval = arg; |
|
1445 else |
|
1446 { |
|
1447 if (arg.numel () == 1) |
|
1448 { |
|
1449 Complex val = arg.complex_value (); |
|
1450 |
|
1451 if (! error_state) |
|
1452 retval = octave_value (new octave_complex (val)); |
|
1453 } |
|
1454 else |
|
1455 { |
|
1456 ComplexNDArray val = arg.complex_array_value (); |
|
1457 |
|
1458 if (! error_state) |
|
1459 retval = octave_value (new octave_complex_matrix (val)); |
|
1460 } |
|
1461 |
|
1462 if (error_state) |
|
1463 error ("complex: invalid conversion"); |
|
1464 } |
|
1465 } |
|
1466 else if (nargin == 2) |
|
1467 { |
|
1468 octave_value re = args(0); |
|
1469 octave_value im = args(1); |
|
1470 |
|
1471 if (re.numel () == 1) |
|
1472 { |
|
1473 double re_val = re.double_value (); |
|
1474 |
|
1475 if (im.numel () == 1) |
|
1476 { |
|
1477 double im_val = im.double_value (); |
|
1478 |
|
1479 if (! error_state) |
|
1480 retval = octave_value (new octave_complex (Complex (re_val, im_val))); |
|
1481 } |
|
1482 else |
|
1483 { |
|
1484 const NDArray im_val = im.array_value (); |
|
1485 |
|
1486 if (! error_state) |
|
1487 { |
|
1488 ComplexNDArray result (im_val.dims (), Complex ()); |
|
1489 |
|
1490 for (octave_idx_type i = 0; i < im_val.numel (); i++) |
|
1491 result.xelem (i) = Complex (re_val, im_val(i)); |
|
1492 |
|
1493 retval = octave_value (new octave_complex_matrix (result)); |
|
1494 } |
|
1495 } |
|
1496 } |
|
1497 else |
|
1498 { |
|
1499 const NDArray re_val = re.array_value (); |
|
1500 |
|
1501 if (im.numel () == 1) |
|
1502 { |
|
1503 double im_val = im.double_value (); |
|
1504 |
|
1505 if (! error_state) |
|
1506 { |
|
1507 ComplexNDArray result (re_val.dims (), Complex ()); |
|
1508 |
|
1509 for (octave_idx_type i = 0; i < re_val.numel (); i++) |
|
1510 result.xelem (i) = Complex (re_val(i), im_val); |
|
1511 |
|
1512 retval = octave_value (new octave_complex_matrix (result)); |
|
1513 } |
|
1514 } |
|
1515 else |
|
1516 { |
|
1517 const NDArray im_val = im.array_value (); |
|
1518 |
|
1519 if (! error_state) |
|
1520 { |
|
1521 if (re_val.dims () == im_val.dims ()) |
|
1522 { |
|
1523 ComplexNDArray result (re_val.dims (), Complex ()); |
|
1524 |
|
1525 for (octave_idx_type i = 0; i < re_val.numel (); i++) |
|
1526 result.xelem (i) = Complex (re_val(i), im_val(i)); |
|
1527 |
|
1528 retval = octave_value (new octave_complex_matrix (result)); |
|
1529 } |
|
1530 else |
|
1531 error ("complex: dimension mismatch"); |
|
1532 } |
|
1533 } |
|
1534 } |
|
1535 |
|
1536 if (error_state) |
|
1537 error ("complex: invalid conversion"); |
|
1538 } |
|
1539 else |
5823
|
1540 print_usage (); |
5476
|
1541 |
|
1542 return retval; |
|
1543 } |
|
1544 |
3258
|
1545 DEFUN (isreal, args, , |
3428
|
1546 "-*- texinfo -*-\n\ |
|
1547 @deftypefn {Built-in Function} {} isreal (@var{x})\n\ |
|
1548 Return true if @var{x} is a real-valued numeric object.\n\ |
|
1549 @end deftypefn") |
3258
|
1550 { |
|
1551 octave_value retval; |
|
1552 |
|
1553 if (args.length () == 1) |
|
1554 retval = args(0).is_real_type (); |
|
1555 else |
5823
|
1556 print_usage (); |
3258
|
1557 |
|
1558 return retval; |
|
1559 } |
|
1560 |
3202
|
1561 DEFUN (isempty, args, , |
3373
|
1562 "-*- texinfo -*-\n\ |
|
1563 @deftypefn {Built-in Function} {} isempty (@var{a})\n\ |
|
1564 Return 1 if @var{a} is an empty matrix (either the number of rows, or\n\ |
|
1565 the number of columns, or both are zero). Otherwise, return 0.\n\ |
|
1566 @end deftypefn") |
3202
|
1567 { |
4233
|
1568 octave_value retval = false; |
3202
|
1569 |
|
1570 if (args.length () == 1) |
4559
|
1571 retval = args(0).is_empty (); |
3202
|
1572 else |
5823
|
1573 print_usage (); |
3202
|
1574 |
|
1575 return retval; |
|
1576 } |
|
1577 |
3206
|
1578 DEFUN (isnumeric, args, , |
3428
|
1579 "-*- texinfo -*-\n\ |
|
1580 @deftypefn {Built-in Function} {} isnumeric (@var{x})\n\ |
|
1581 Return nonzero if @var{x} is a numeric object.\n\ |
|
1582 @end deftypefn") |
3206
|
1583 { |
|
1584 octave_value retval; |
|
1585 |
|
1586 if (args.length () == 1) |
3258
|
1587 retval = args(0).is_numeric_type (); |
3206
|
1588 else |
5823
|
1589 print_usage (); |
3206
|
1590 |
|
1591 return retval; |
|
1592 } |
|
1593 |
4028
|
1594 DEFUN (islist, args, , |
3526
|
1595 "-*- texinfo -*-\n\ |
4028
|
1596 @deftypefn {Built-in Function} {} islist (@var{x})\n\ |
3428
|
1597 Return nonzero if @var{x} is a list.\n\ |
|
1598 @end deftypefn") |
3204
|
1599 { |
|
1600 octave_value retval; |
|
1601 |
|
1602 if (args.length () == 1) |
3258
|
1603 retval = args(0).is_list (); |
3204
|
1604 else |
5823
|
1605 print_usage (); |
3204
|
1606 |
|
1607 return retval; |
|
1608 } |
|
1609 |
4028
|
1610 DEFUN (ismatrix, args, , |
3321
|
1611 "-*- texinfo -*-\n\ |
4028
|
1612 @deftypefn {Built-in Function} {} ismatrix (@var{a})\n\ |
3321
|
1613 Return 1 if @var{a} is a matrix. Otherwise, return 0.\n\ |
3333
|
1614 @end deftypefn") |
3202
|
1615 { |
4233
|
1616 octave_value retval = false; |
3202
|
1617 |
|
1618 if (args.length () == 1) |
|
1619 { |
|
1620 octave_value arg = args(0); |
|
1621 |
3212
|
1622 if (arg.is_scalar_type () || arg.is_range ()) |
4233
|
1623 retval = true; |
3202
|
1624 else if (arg.is_matrix_type ()) |
4233
|
1625 retval = (arg.rows () >= 1 && arg.columns () >= 1); |
3202
|
1626 } |
|
1627 else |
5823
|
1628 print_usage (); |
3202
|
1629 |
|
1630 return retval; |
|
1631 } |
|
1632 |
3354
|
1633 static octave_value |
5747
|
1634 fill_matrix (const octave_value_list& args, int val, const char *fcn) |
523
|
1635 { |
3354
|
1636 octave_value retval; |
523
|
1637 |
|
1638 int nargin = args.length (); |
|
1639 |
4946
|
1640 oct_data_conv::data_type dt = oct_data_conv::dt_double; |
4481
|
1641 |
4946
|
1642 dim_vector dims (1, 1); |
4481
|
1643 |
|
1644 if (nargin > 0 && args(nargin-1).is_string ()) |
|
1645 { |
4946
|
1646 std::string nm = args(nargin-1).string_value (); |
4481
|
1647 nargin--; |
|
1648 |
4946
|
1649 dt = oct_data_conv::string_to_data_type (nm); |
|
1650 |
|
1651 if (error_state) |
|
1652 return retval; |
4481
|
1653 } |
|
1654 |
523
|
1655 switch (nargin) |
|
1656 { |
712
|
1657 case 0: |
|
1658 break; |
777
|
1659 |
610
|
1660 case 1: |
4481
|
1661 get_dimensions (args(0), fcn, dims); |
610
|
1662 break; |
777
|
1663 |
4563
|
1664 default: |
|
1665 { |
|
1666 dims.resize (nargin); |
4481
|
1667 |
4563
|
1668 for (int i = 0; i < nargin; i++) |
|
1669 { |
6133
|
1670 dims(i) = args(i).is_empty () ? 0 : args(i).idx_type_value (); |
4481
|
1671 |
4563
|
1672 if (error_state) |
|
1673 { |
4732
|
1674 error ("%s: expecting scalar integer arguments", fcn); |
4563
|
1675 break; |
|
1676 } |
|
1677 } |
|
1678 } |
|
1679 break; |
4481
|
1680 } |
|
1681 |
|
1682 if (! error_state) |
|
1683 { |
4946
|
1684 dims.chop_trailing_singletons (); |
4565
|
1685 |
4481
|
1686 check_dimensions (dims, fcn); |
3354
|
1687 |
5775
|
1688 // FIXME -- perhaps this should be made extensible by |
4946
|
1689 // using the class name to lookup a function to call to create |
|
1690 // the new value. |
|
1691 |
|
1692 // Note that automatic narrowing will handle conversion from |
|
1693 // NDArray to scalar. |
|
1694 |
4481
|
1695 if (! error_state) |
|
1696 { |
4946
|
1697 switch (dt) |
|
1698 { |
|
1699 case oct_data_conv::dt_int8: |
|
1700 retval = int8NDArray (dims, val); |
|
1701 break; |
4481
|
1702 |
4946
|
1703 case oct_data_conv::dt_uint8: |
|
1704 retval = uint8NDArray (dims, val); |
|
1705 break; |
|
1706 |
|
1707 case oct_data_conv::dt_int16: |
|
1708 retval = int16NDArray (dims, val); |
|
1709 break; |
|
1710 |
|
1711 case oct_data_conv::dt_uint16: |
|
1712 retval = uint16NDArray (dims, val); |
|
1713 break; |
|
1714 |
|
1715 case oct_data_conv::dt_int32: |
|
1716 retval = int32NDArray (dims, val); |
|
1717 break; |
777
|
1718 |
4946
|
1719 case oct_data_conv::dt_uint32: |
|
1720 retval = uint32NDArray (dims, val); |
|
1721 break; |
|
1722 |
|
1723 case oct_data_conv::dt_int64: |
|
1724 retval = int64NDArray (dims, val); |
|
1725 break; |
4481
|
1726 |
4946
|
1727 case oct_data_conv::dt_uint64: |
|
1728 retval = uint64NDArray (dims, val); |
|
1729 break; |
4481
|
1730 |
5775
|
1731 case oct_data_conv::dt_single: // FIXME |
4946
|
1732 case oct_data_conv::dt_double: |
|
1733 retval = NDArray (dims, val); |
|
1734 break; |
|
1735 |
4986
|
1736 case oct_data_conv::dt_logical: |
|
1737 retval = boolNDArray (dims, val); |
|
1738 break; |
|
1739 |
4946
|
1740 default: |
|
1741 error ("%s: invalid class name", fcn); |
|
1742 break; |
4481
|
1743 } |
|
1744 } |
523
|
1745 } |
|
1746 |
|
1747 return retval; |
|
1748 } |
|
1749 |
5747
|
1750 static octave_value |
|
1751 fill_matrix (const octave_value_list& args, double val, const char *fcn) |
|
1752 { |
|
1753 octave_value retval; |
|
1754 |
|
1755 int nargin = args.length (); |
|
1756 |
|
1757 oct_data_conv::data_type dt = oct_data_conv::dt_double; |
|
1758 |
|
1759 dim_vector dims (1, 1); |
|
1760 |
|
1761 if (nargin > 0 && args(nargin-1).is_string ()) |
|
1762 { |
|
1763 std::string nm = args(nargin-1).string_value (); |
|
1764 nargin--; |
|
1765 |
|
1766 dt = oct_data_conv::string_to_data_type (nm); |
|
1767 |
|
1768 if (error_state) |
|
1769 return retval; |
|
1770 } |
|
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 { |
|
1810 switch (dt) |
|
1811 { |
5775
|
1812 case oct_data_conv::dt_single: // FIXME |
5747
|
1813 case oct_data_conv::dt_double: |
|
1814 retval = NDArray (dims, val); |
|
1815 break; |
|
1816 |
|
1817 default: |
|
1818 error ("%s: invalid class name", fcn); |
|
1819 break; |
|
1820 } |
|
1821 } |
|
1822 } |
|
1823 |
|
1824 return retval; |
|
1825 } |
|
1826 |
|
1827 static octave_value |
|
1828 fill_matrix (const octave_value_list& args, const Complex& val, |
|
1829 const char *fcn) |
|
1830 { |
|
1831 octave_value retval; |
|
1832 |
|
1833 int nargin = args.length (); |
|
1834 |
|
1835 oct_data_conv::data_type dt = oct_data_conv::dt_double; |
|
1836 |
|
1837 dim_vector dims (1, 1); |
|
1838 |
|
1839 if (nargin > 0 && args(nargin-1).is_string ()) |
|
1840 { |
|
1841 std::string nm = args(nargin-1).string_value (); |
|
1842 nargin--; |
|
1843 |
|
1844 dt = oct_data_conv::string_to_data_type (nm); |
|
1845 |
|
1846 if (error_state) |
|
1847 return retval; |
|
1848 } |
|
1849 |
|
1850 switch (nargin) |
|
1851 { |
|
1852 case 0: |
|
1853 break; |
|
1854 |
|
1855 case 1: |
|
1856 get_dimensions (args(0), fcn, dims); |
|
1857 break; |
|
1858 |
|
1859 default: |
|
1860 { |
|
1861 dims.resize (nargin); |
|
1862 |
|
1863 for (int i = 0; i < nargin; i++) |
|
1864 { |
6133
|
1865 dims(i) = args(i).is_empty () ? 0 : args(i).idx_type_value (); |
5747
|
1866 |
|
1867 if (error_state) |
|
1868 { |
|
1869 error ("%s: expecting scalar integer arguments", fcn); |
|
1870 break; |
|
1871 } |
|
1872 } |
|
1873 } |
|
1874 break; |
|
1875 } |
|
1876 |
|
1877 if (! error_state) |
|
1878 { |
|
1879 dims.chop_trailing_singletons (); |
|
1880 |
|
1881 check_dimensions (dims, fcn); |
|
1882 |
|
1883 // Note that automatic narrowing will handle conversion from |
|
1884 // NDArray to scalar. |
|
1885 |
|
1886 if (! error_state) |
|
1887 { |
|
1888 switch (dt) |
|
1889 { |
5775
|
1890 case oct_data_conv::dt_single: // FIXME |
5747
|
1891 case oct_data_conv::dt_double: |
|
1892 retval = ComplexNDArray (dims, val); |
|
1893 break; |
|
1894 |
|
1895 default: |
|
1896 error ("%s: invalid class name", fcn); |
|
1897 break; |
|
1898 } |
|
1899 } |
|
1900 } |
|
1901 |
|
1902 return retval; |
|
1903 } |
|
1904 |
|
1905 static octave_value |
|
1906 fill_matrix (const octave_value_list& args, bool val, const char *fcn) |
|
1907 { |
|
1908 octave_value retval; |
|
1909 |
|
1910 int nargin = args.length (); |
|
1911 |
|
1912 dim_vector dims (1, 1); |
|
1913 |
|
1914 switch (nargin) |
|
1915 { |
|
1916 case 0: |
|
1917 break; |
|
1918 |
|
1919 case 1: |
|
1920 get_dimensions (args(0), fcn, dims); |
|
1921 break; |
|
1922 |
|
1923 default: |
|
1924 { |
|
1925 dims.resize (nargin); |
|
1926 |
|
1927 for (int i = 0; i < nargin; i++) |
|
1928 { |
6133
|
1929 dims(i) = args(i).is_empty () ? 0 : args(i).idx_type_value (); |
5747
|
1930 |
|
1931 if (error_state) |
|
1932 { |
|
1933 error ("%s: expecting scalar integer arguments", fcn); |
|
1934 break; |
|
1935 } |
|
1936 } |
|
1937 } |
|
1938 break; |
|
1939 } |
|
1940 |
|
1941 if (! error_state) |
|
1942 { |
|
1943 dims.chop_trailing_singletons (); |
|
1944 |
|
1945 check_dimensions (dims, fcn); |
|
1946 |
|
1947 // Note that automatic narrowing will handle conversion from |
|
1948 // NDArray to scalar. |
|
1949 |
|
1950 if (! error_state) |
|
1951 retval = boolNDArray (dims, val); |
|
1952 } |
|
1953 |
|
1954 return retval; |
|
1955 } |
|
1956 |
3354
|
1957 DEFUN (ones, args, , |
3369
|
1958 "-*- texinfo -*-\n\ |
|
1959 @deftypefn {Built-in Function} {} ones (@var{x})\n\ |
|
1960 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m})\n\ |
4948
|
1961 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
1962 @deftypefnx {Built-in Function} {} ones (@dots{}, @var{class})\n\ |
4481
|
1963 Return a matrix or N-dimensional array whose elements are all 1.\n\ |
|
1964 The arguments are handled the same as the arguments for @code{eye}.\n\ |
3369
|
1965 \n\ |
|
1966 If you need to create a matrix whose values are all the same, you should\n\ |
|
1967 use an expression like\n\ |
|
1968 \n\ |
|
1969 @example\n\ |
|
1970 val_matrix = val * ones (n, m)\n\ |
|
1971 @end example\n\ |
4945
|
1972 \n\ |
|
1973 The optional argument @var{class}, allows @code{ones} to return an array of\n\ |
5747
|
1974 the specified type, for example\n\ |
4945
|
1975 \n\ |
|
1976 @example\n\ |
|
1977 val = ones (n,m, \"uint8\")\n\ |
|
1978 @end example\n\ |
3369
|
1979 @end deftypefn") |
523
|
1980 { |
5747
|
1981 return fill_matrix (args, 1, "ones"); |
523
|
1982 } |
|
1983 |
3354
|
1984 DEFUN (zeros, args, , |
3369
|
1985 "-*- texinfo -*-\n\ |
|
1986 @deftypefn {Built-in Function} {} zeros (@var{x})\n\ |
|
1987 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m})\n\ |
4948
|
1988 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
1989 @deftypefnx {Built-in Function} {} zeros (@dots{}, @var{class})\n\ |
4481
|
1990 Return a matrix or N-dimensional array whose elements are all 0.\n\ |
|
1991 The arguments are handled the same as the arguments for @code{eye}.\n\ |
4945
|
1992 \n\ |
|
1993 The optional argument @var{class}, allows @code{zeros} to return an array of\n\ |
5747
|
1994 the specified type, for example\n\ |
4945
|
1995 \n\ |
|
1996 @example\n\ |
|
1997 val = zeros (n,m, \"uint8\")\n\ |
|
1998 @end example\n\ |
3369
|
1999 @end deftypefn") |
523
|
2000 { |
5747
|
2001 return fill_matrix (args, 0, "zeros"); |
|
2002 } |
|
2003 |
|
2004 DEFUN (Inf, args, , |
|
2005 "-*- texinfo -*-\n\ |
|
2006 @deftypefn {Built-in Function} {} Inf (@var{x})\n\ |
|
2007 @deftypefnx {Built-in Function} {} Inf (@var{n}, @var{m})\n\ |
|
2008 @deftypefnx {Built-in Function} {} Inf (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
2009 @deftypefnx {Built-in Function} {} Inf (@dots{}, @var{class})\n\ |
|
2010 Return a matrix or N-dimensional array whose elements are all Infinity.\n\ |
|
2011 The arguments are handled the same as the arguments for @code{eye}.\n\ |
|
2012 The optional argument @var{class} may be either @samp{\"single\"} or\n\ |
5798
|
2013 @samp{\"double\"}. The default is @samp{\"double\"}.\n\ |
5747
|
2014 @end deftypefn") |
|
2015 { |
|
2016 return fill_matrix (args, lo_ieee_inf_value (), "Inf"); |
|
2017 } |
|
2018 |
|
2019 DEFALIAS (inf, Inf); |
|
2020 |
|
2021 DEFUN (NaN, args, , |
|
2022 "-*- texinfo -*-\n\ |
|
2023 @deftypefn {Built-in Function} {} NaN (@var{x})\n\ |
|
2024 @deftypefnx {Built-in Function} {} NaN (@var{n}, @var{m})\n\ |
|
2025 @deftypefnx {Built-in Function} {} NaN (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
2026 @deftypefnx {Built-in Function} {} NaN (@dots{}, @var{class})\n\ |
|
2027 Return a matrix or N-dimensional array whose elements are all NaN\n\ |
|
2028 (Not a Number). The value NaN is the result of an operation like\n\ |
|
2029 @iftex\n\ |
|
2030 @tex\n\ |
|
2031 $0/0$, or $\\infty - \\infty$,\n\ |
|
2032 @end tex\n\ |
|
2033 @end iftex\n\ |
|
2034 @ifinfo\n\ |
|
2035 0/0, or @samp{Inf - Inf},\n\ |
|
2036 @end ifinfo\n\ |
|
2037 or any operation with a NaN.\n\ |
|
2038 \n\ |
|
2039 Note that NaN always compares not equal to NaN. This behavior is\n\ |
|
2040 specified by the IEEE standard for floating point arithmetic. To\n\ |
|
2041 find NaN values, you must use the @code{isnan} function.\n\ |
|
2042 \n\ |
|
2043 The arguments are handled the same as the arguments for @code{eye}.\n\ |
|
2044 The optional argument @var{class} may be either @samp{\"single\"} or\n\ |
5798
|
2045 @samp{\"double\"}. The default is @samp{\"double\"}.\n\ |
5747
|
2046 @end deftypefn") |
|
2047 { |
|
2048 return fill_matrix (args, lo_ieee_nan_value (), "NaN"); |
|
2049 } |
|
2050 |
|
2051 DEFALIAS (nan, NaN); |
|
2052 |
|
2053 DEFUN (e, args, , |
|
2054 "-*- texinfo -*-\n\ |
|
2055 @deftypefn {Built-in Function} {} e (@var{x})\n\ |
|
2056 @deftypefnx {Built-in Function} {} e (@var{n}, @var{m})\n\ |
|
2057 @deftypefnx {Built-in Function} {} e (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
2058 @deftypefnx {Built-in Function} {} e (@dots{}, @var{class})\n\ |
|
2059 Return a matrix or N-dimensional array whose elements are all equal\n\ |
|
2060 to the base of natural logarithms. The constant\n\ |
|
2061 @iftex\n\ |
|
2062 @tex\n\ |
|
2063 $e$\n\ |
|
2064 @end tex\n\ |
|
2065 @end iftex\n\ |
|
2066 @ifinfo\n\ |
|
2067 @var{e}\n\ |
|
2068 @end ifinfo\n\ |
|
2069 satisfies the equation\n\ |
|
2070 @iftex\n\ |
|
2071 @tex\n\ |
|
2072 $\\log (e) = 1$.\n\ |
|
2073 @end tex\n\ |
|
2074 @end iftex\n\ |
|
2075 @ifinfo\n\ |
|
2076 @code{log} (@var{e}) = 1.\n\ |
|
2077 @end ifinfo\n\ |
|
2078 @end deftypefn") |
|
2079 { |
|
2080 #if defined (M_E) |
|
2081 double e_val = M_E; |
|
2082 #else |
|
2083 double e_val = exp (1.0); |
|
2084 #endif |
|
2085 |
|
2086 return fill_matrix (args, e_val, "e"); |
|
2087 } |
|
2088 |
|
2089 DEFUN (eps, args, , |
|
2090 "-*- texinfo -*-\n\ |
|
2091 @deftypefn {Built-in Function} {} eps (@var{x})\n\ |
|
2092 @deftypefnx {Built-in Function} {} eps (@var{n}, @var{m})\n\ |
|
2093 @deftypefnx {Built-in Function} {} eps (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
2094 @deftypefnx {Built-in Function} {} eps (@dots{}, @var{class})\n\ |
|
2095 Return a matrix or N-dimensional array whose elements are all eps,\n\ |
|
2096 the machine precision. More precisely, @code{eps} is the largest\n\ |
|
2097 relative spacing between any two adjacent numbers in the machine's\n\ |
|
2098 floating point system. This number is obviously system-dependent. On\n\ |
|
2099 machines that support 64 bit IEEE floating point arithmetic, @code{eps}\n\ |
|
2100 is approximately\n\ |
|
2101 @ifinfo\n\ |
|
2102 2.2204e-16.\n\ |
|
2103 @end ifinfo\n\ |
|
2104 @iftex\n\ |
|
2105 @tex\n\ |
|
2106 $2.2204\\times10^{-16}$.\n\ |
|
2107 @end tex\n\ |
|
2108 @end iftex\n\ |
|
2109 @end deftypefn") |
|
2110 { |
|
2111 return fill_matrix (args, DBL_EPSILON, "eps"); |
|
2112 } |
|
2113 |
|
2114 DEFUN (pi, args, , |
|
2115 "-*- texinfo -*-\n\ |
|
2116 @deftypefn {Built-in Function} {} pi (@var{x})\n\ |
|
2117 @deftypefnx {Built-in Function} {} pi (@var{n}, @var{m})\n\ |
|
2118 @deftypefnx {Built-in Function} {} pi (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
2119 @deftypefnx {Built-in Function} {} pi (@dots{}, @var{class})\n\ |
|
2120 Return a matrix or N-dimensional array whose elements are all equal\n\ |
|
2121 to the ratio of the circumference of a circle to its diameter.\n\ |
|
2122 Internally, @code{pi} is computed as @samp{4.0 * atan (1.0)}.\n\ |
|
2123 @end deftypefn") |
|
2124 { |
|
2125 #if defined (M_PI) |
|
2126 double pi_val = M_PI; |
|
2127 #else |
|
2128 double pi_val = 4.0 * atan (1.0); |
|
2129 #endif |
|
2130 |
|
2131 return fill_matrix (args, pi_val, "pi"); |
|
2132 } |
|
2133 |
|
2134 DEFUN (realmax, args, , |
|
2135 "-*- texinfo -*-\n\ |
|
2136 @deftypefn {Built-in Function} {} realmax (@var{x})\n\ |
|
2137 @deftypefnx {Built-in Function} {} realmax (@var{n}, @var{m})\n\ |
|
2138 @deftypefnx {Built-in Function} {} realmax (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
2139 @deftypefnx {Built-in Function} {} realmax (@dots{}, @var{class})\n\ |
|
2140 Return a matrix or N-dimensional array whose elements are all equal\n\ |
|
2141 to the largest floating point number that is representable. The actual\n\ |
|
2142 value is system-dependent. On machines that support 64-bit IEEE\n\ |
|
2143 floating point arithmetic, @code{realmax} is approximately\n\ |
|
2144 @ifinfo\n\ |
|
2145 1.7977e+308\n\ |
|
2146 @end ifinfo\n\ |
|
2147 @iftex\n\ |
|
2148 @tex\n\ |
|
2149 $1.7977\\times10^{308}$.\n\ |
|
2150 @end tex\n\ |
|
2151 @end iftex\n\ |
|
2152 @seealso{realmin}\n\ |
|
2153 @end deftypefn") |
|
2154 { |
|
2155 return fill_matrix (args, DBL_MAX, "realmax"); |
|
2156 } |
|
2157 |
|
2158 DEFUN (realmin, args, , |
|
2159 "-*- texinfo -*-\n\ |
|
2160 @deftypefn {Built-in Function} {} realmin (@var{x})\n\ |
|
2161 @deftypefnx {Built-in Function} {} realmin (@var{n}, @var{m})\n\ |
|
2162 @deftypefnx {Built-in Function} {} realmin (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
2163 @deftypefnx {Built-in Function} {} realmin (@dots{}, @var{class})\n\ |
|
2164 Return a matrix or N-dimensional array whose elements are all equal\n\ |
|
2165 to the smallest normalized floating point number that is representable.\n\ |
|
2166 The actual value is system-dependent. On machines that support\n\ |
|
2167 64-bit IEEE floating point arithmetic, @code{realmin} is approximately\n\ |
|
2168 @ifinfo\n\ |
|
2169 2.2251e-308\n\ |
|
2170 @end ifinfo\n\ |
|
2171 @iftex\n\ |
|
2172 @tex\n\ |
|
2173 $2.2251\\times10^{-308}$.\n\ |
|
2174 @end tex\n\ |
|
2175 @end iftex\n\ |
|
2176 @seealso{realmax}\n\ |
|
2177 @end deftypefn") |
|
2178 { |
|
2179 return fill_matrix (args, DBL_MIN, "realmin"); |
|
2180 } |
|
2181 |
|
2182 DEFUN (I, args, , |
|
2183 "-*- texinfo -*-\n\ |
|
2184 @deftypefn {Built-in Function} {} I (@var{x})\n\ |
|
2185 @deftypefnx {Built-in Function} {} I (@var{n}, @var{m})\n\ |
|
2186 @deftypefnx {Built-in Function} {} I (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
2187 @deftypefnx {Built-in Function} {} I (@dots{}, @var{class})\n\ |
|
2188 Return a matrix or N-dimensional array whose elements are all equal\n\ |
|
2189 to the pure imaginary unit, defined as\n\ |
|
2190 @iftex\n\ |
|
2191 @tex\n\ |
|
2192 $\\sqrt{-1}$.\n\ |
|
2193 @end tex\n\ |
|
2194 @end iftex\n\ |
|
2195 @ifinfo\n\ |
|
2196 @code{sqrt (-1)}.\n\ |
|
2197 @end ifinfo\n\ |
7001
|
2198 Since I (also i, J, and j) is a function, you can use the name(s) for\n\ |
5747
|
2199 other purposes.\n\ |
|
2200 @end deftypefn") |
|
2201 { |
|
2202 return fill_matrix (args, Complex (0.0, 1.0), "I"); |
|
2203 } |
|
2204 |
|
2205 DEFALIAS (i, I); |
|
2206 DEFALIAS (J, I); |
|
2207 DEFALIAS (j, I); |
|
2208 |
|
2209 DEFUN (NA, args, , |
|
2210 "-*- texinfo -*-\n\ |
|
2211 @deftypefn {Built-in Function} {} NA (@var{x})\n\ |
|
2212 @deftypefnx {Built-in Function} {} NA (@var{n}, @var{m})\n\ |
|
2213 @deftypefnx {Built-in Function} {} NA (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
2214 @deftypefnx {Built-in Function} {} NA (@dots{}, @var{class})\n\ |
|
2215 Return a matrix or N-dimensional array whose elements are all equal\n\ |
|
2216 to the special constant used to designate missing values.\n\ |
|
2217 @end deftypefn") |
|
2218 { |
|
2219 return fill_matrix (args, lo_ieee_na_value (), "NA"); |
|
2220 } |
|
2221 |
|
2222 DEFUN (false, args, , |
|
2223 "-*- texinfo -*-\n\ |
|
2224 @deftypefn {Built-in Function} {} false (@var{x})\n\ |
|
2225 @deftypefnx {Built-in Function} {} false (@var{n}, @var{m})\n\ |
|
2226 @deftypefnx {Built-in Function} {} false (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
2227 Return a matrix or N-dimensional array whose elements are all logical 0.\n\ |
|
2228 The arguments are handled the same as the arguments for @code{eye}.\n\ |
|
2229 @end deftypefn") |
|
2230 { |
|
2231 return fill_matrix (args, false, "false"); |
|
2232 } |
|
2233 |
|
2234 DEFUN (true, args, , |
|
2235 "-*- texinfo -*-\n\ |
|
2236 @deftypefn {Built-in Function} {} true (@var{x})\n\ |
|
2237 @deftypefnx {Built-in Function} {} true (@var{n}, @var{m})\n\ |
|
2238 @deftypefnx {Built-in Function} {} true (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
2239 Return a matrix or N-dimensional array whose elements are all logical 1.\n\ |
|
2240 The arguments are handled the same as the arguments for @code{eye}.\n\ |
|
2241 @end deftypefn") |
|
2242 { |
|
2243 return fill_matrix (args, true, "true"); |
3354
|
2244 } |
523
|
2245 |
4946
|
2246 template <class MT> |
|
2247 octave_value |
|
2248 identity_matrix (int nr, int nc) |
|
2249 { |
|
2250 octave_value retval; |
|
2251 |
|
2252 typename octave_array_type_traits<MT>::element_type one (1); |
|
2253 |
|
2254 if (nr == 1 && nc == 1) |
|
2255 retval = one; |
|
2256 else |
|
2257 { |
|
2258 dim_vector dims (nr, nc); |
|
2259 |
|
2260 typename octave_array_type_traits<MT>::element_type zero (0); |
|
2261 |
|
2262 MT m (dims, zero); |
|
2263 |
|
2264 if (nr > 0 && nc > 0) |
|
2265 { |
|
2266 int n = std::min (nr, nc); |
|
2267 |
|
2268 for (int i = 0; i < n; i++) |
|
2269 m(i,i) = one; |
|
2270 } |
|
2271 |
|
2272 retval = m; |
|
2273 } |
|
2274 |
|
2275 return retval; |
|
2276 } |
|
2277 |
5058
|
2278 #define INSTANTIATE_EYE(T) \ |
|
2279 template octave_value identity_matrix<T> (int, int) |
|
2280 |
|
2281 INSTANTIATE_EYE (int8NDArray); |
|
2282 INSTANTIATE_EYE (uint8NDArray); |
|
2283 INSTANTIATE_EYE (int16NDArray); |
|
2284 INSTANTIATE_EYE (uint16NDArray); |
|
2285 INSTANTIATE_EYE (int32NDArray); |
|
2286 INSTANTIATE_EYE (uint32NDArray); |
|
2287 INSTANTIATE_EYE (int64NDArray); |
|
2288 INSTANTIATE_EYE (uint64NDArray); |
|
2289 INSTANTIATE_EYE (NDArray); |
|
2290 INSTANTIATE_EYE (boolNDArray); |
|
2291 |
4945
|
2292 static octave_value |
4948
|
2293 identity_matrix (int nr, int nc, oct_data_conv::data_type dt) |
4945
|
2294 { |
|
2295 octave_value retval; |
|
2296 |
5775
|
2297 // FIXME -- perhaps this should be made extensible by using |
4946
|
2298 // the class name to lookup a function to call to create the new |
|
2299 // value. |
|
2300 |
|
2301 if (! error_state) |
|
2302 { |
|
2303 switch (dt) |
|
2304 { |
|
2305 case oct_data_conv::dt_int8: |
|
2306 retval = identity_matrix<int8NDArray> (nr, nc); |
|
2307 break; |
|
2308 |
|
2309 case oct_data_conv::dt_uint8: |
|
2310 retval = identity_matrix<uint8NDArray> (nr, nc); |
|
2311 break; |
|
2312 |
|
2313 case oct_data_conv::dt_int16: |
|
2314 retval = identity_matrix<int16NDArray> (nr, nc); |
|
2315 break; |
4945
|
2316 |
4946
|
2317 case oct_data_conv::dt_uint16: |
|
2318 retval = identity_matrix<uint16NDArray> (nr, nc); |
|
2319 break; |
|
2320 |
|
2321 case oct_data_conv::dt_int32: |
|
2322 retval = identity_matrix<int32NDArray> (nr, nc); |
|
2323 break; |
|
2324 |
|
2325 case oct_data_conv::dt_uint32: |
|
2326 retval = identity_matrix<uint32NDArray> (nr, nc); |
|
2327 break; |
4945
|
2328 |
4946
|
2329 case oct_data_conv::dt_int64: |
|
2330 retval = identity_matrix<int64NDArray> (nr, nc); |
|
2331 break; |
|
2332 |
|
2333 case oct_data_conv::dt_uint64: |
|
2334 retval = identity_matrix<uint64NDArray> (nr, nc); |
|
2335 break; |
4945
|
2336 |
5775
|
2337 case oct_data_conv::dt_single: // FIXME |
4946
|
2338 case oct_data_conv::dt_double: |
|
2339 retval = identity_matrix<NDArray> (nr, nc); |
|
2340 break; |
4945
|
2341 |
4986
|
2342 case oct_data_conv::dt_logical: |
|
2343 retval = identity_matrix<boolNDArray> (nr, nc); |
|
2344 break; |
|
2345 |
4946
|
2346 default: |
|
2347 error ("eye: invalid class name"); |
|
2348 break; |
4945
|
2349 } |
|
2350 } |
|
2351 |
|
2352 return retval; |
|
2353 } |
|
2354 |
4946
|
2355 #undef INT_EYE_MATRIX |
|
2356 |
1957
|
2357 DEFUN (eye, args, , |
3369
|
2358 "-*- texinfo -*-\n\ |
|
2359 @deftypefn {Built-in Function} {} eye (@var{x})\n\ |
|
2360 @deftypefnx {Built-in Function} {} eye (@var{n}, @var{m})\n\ |
4948
|
2361 @deftypefnx {Built-in Function} {} eye (@dots{}, @var{class})\n\ |
3369
|
2362 Return an identity matrix. If invoked with a single scalar argument,\n\ |
|
2363 @code{eye} returns a square matrix with the dimension specified. If you\n\ |
|
2364 supply two scalar arguments, @code{eye} takes them to be the number of\n\ |
|
2365 rows and columns. If given a vector with two elements, @code{eye} uses\n\ |
|
2366 the values of the elements as the number of rows and columns,\n\ |
|
2367 respectively. For example,\n\ |
|
2368 \n\ |
|
2369 @example\n\ |
|
2370 @group\n\ |
|
2371 eye (3)\n\ |
|
2372 @result{} 1 0 0\n\ |
|
2373 0 1 0\n\ |
|
2374 0 0 1\n\ |
|
2375 @end group\n\ |
|
2376 @end example\n\ |
|
2377 \n\ |
|
2378 The following expressions all produce the same result:\n\ |
|
2379 \n\ |
|
2380 @example\n\ |
|
2381 @group\n\ |
|
2382 eye (2)\n\ |
|
2383 @equiv{}\n\ |
|
2384 eye (2, 2)\n\ |
|
2385 @equiv{}\n\ |
|
2386 eye (size ([1, 2; 3, 4])\n\ |
|
2387 @end group\n\ |
|
2388 @end example\n\ |
|
2389 \n\ |
4945
|
2390 The optional argument @var{class}, allows @code{eye} to return an array of\n\ |
|
2391 the specified type, like\n\ |
|
2392 \n\ |
|
2393 @example\n\ |
|
2394 val = zeros (n,m, \"uint8\")\n\ |
|
2395 @end example\n\ |
|
2396 \n\ |
6556
|
2397 Calling @code{eye} with no arguments is equivalent to calling it\n\ |
|
2398 with an argument of 1. This odd definition is for compatibility\n\ |
|
2399 with @sc{Matlab}.\n\ |
3369
|
2400 @end deftypefn") |
523
|
2401 { |
3354
|
2402 octave_value retval; |
523
|
2403 |
4948
|
2404 int nargin = args.length (); |
4945
|
2405 |
4948
|
2406 oct_data_conv::data_type dt = oct_data_conv::dt_double; |
523
|
2407 |
4945
|
2408 // Check for type information. |
|
2409 |
|
2410 if (nargin > 0 && args(nargin-1).is_string ()) |
|
2411 { |
4948
|
2412 std::string nm = args(nargin-1).string_value (); |
4945
|
2413 nargin--; |
4948
|
2414 |
|
2415 dt = oct_data_conv::string_to_data_type (nm); |
|
2416 |
|
2417 if (error_state) |
|
2418 return retval; |
4945
|
2419 } |
|
2420 |
523
|
2421 switch (nargin) |
|
2422 { |
712
|
2423 case 0: |
4948
|
2424 retval = identity_matrix (1, 1, dt); |
712
|
2425 break; |
777
|
2426 |
610
|
2427 case 1: |
3354
|
2428 { |
5275
|
2429 octave_idx_type nr, nc; |
3354
|
2430 get_dimensions (args(0), "eye", nr, nc); |
|
2431 |
|
2432 if (! error_state) |
4948
|
2433 retval = identity_matrix (nr, nc, dt); |
3354
|
2434 } |
610
|
2435 break; |
777
|
2436 |
523
|
2437 case 2: |
3354
|
2438 { |
5275
|
2439 octave_idx_type nr, nc; |
3354
|
2440 get_dimensions (args(0), args(1), "eye", nr, nc); |
|
2441 |
|
2442 if (! error_state) |
4948
|
2443 retval = identity_matrix (nr, nc, dt); |
3354
|
2444 } |
523
|
2445 break; |
777
|
2446 |
523
|
2447 default: |
5823
|
2448 print_usage (); |
523
|
2449 break; |
|
2450 } |
|
2451 |
|
2452 return retval; |
|
2453 } |
|
2454 |
1957
|
2455 DEFUN (linspace, args, , |
3369
|
2456 "-*- texinfo -*-\n\ |
|
2457 @deftypefn {Built-in Function} {} linspace (@var{base}, @var{limit}, @var{n})\n\ |
|
2458 Return a row vector with @var{n} linearly spaced elements between\n\ |
6630
|
2459 @var{base} and @var{limit}. If the number of elements is greater than one,\n\ |
|
2460 then the @var{base} and @var{limit} are always included in\n\ |
3369
|
2461 the range. If @var{base} is greater than @var{limit}, the elements are\n\ |
|
2462 stored in decreasing order. If the number of points is not specified, a\n\ |
|
2463 value of 100 is used.\n\ |
1100
|
2464 \n\ |
4455
|
2465 The @code{linspace} function always returns a row vector.\n\ |
6630
|
2466 \n\ |
|
2467 For compatibility with @sc{Matlab}, return the second argument if\n\ |
|
2468 fewer than two values are requested.\n\ |
3369
|
2469 @end deftypefn") |
1100
|
2470 { |
3418
|
2471 octave_value retval; |
1100
|
2472 |
|
2473 int nargin = args.length (); |
|
2474 |
6133
|
2475 octave_idx_type npoints = 100; |
1100
|
2476 |
1940
|
2477 if (nargin != 2 && nargin != 3) |
|
2478 { |
5823
|
2479 print_usage (); |
1940
|
2480 return retval; |
|
2481 } |
|
2482 |
1100
|
2483 if (nargin == 3) |
6133
|
2484 npoints = args(2).idx_type_value (); |
1100
|
2485 |
|
2486 if (! error_state) |
|
2487 { |
3322
|
2488 octave_value arg_1 = args(0); |
|
2489 octave_value arg_2 = args(1); |
1100
|
2490 |
3322
|
2491 if (arg_1.is_complex_type () || arg_2.is_complex_type ()) |
|
2492 { |
|
2493 Complex x1 = arg_1.complex_value (); |
|
2494 Complex x2 = arg_2.complex_value (); |
|
2495 |
|
2496 if (! error_state) |
1100
|
2497 { |
3322
|
2498 ComplexRowVector rv = linspace (x1, x2, npoints); |
1100
|
2499 |
|
2500 if (! error_state) |
3418
|
2501 retval = rv; |
1100
|
2502 } |
|
2503 } |
|
2504 else |
3322
|
2505 { |
|
2506 double x1 = arg_1.double_value (); |
|
2507 double x2 = arg_2.double_value (); |
|
2508 |
|
2509 if (! error_state) |
|
2510 { |
|
2511 RowVector rv = linspace (x1, x2, npoints); |
|
2512 |
|
2513 if (! error_state) |
3418
|
2514 retval = rv; |
3322
|
2515 } |
|
2516 } |
1100
|
2517 } |
4732
|
2518 else |
|
2519 error ("linspace: expecting third argument to be an integer"); |
1100
|
2520 |
|
2521 return retval; |
|
2522 } |
|
2523 |
5775
|
2524 // FIXME -- should accept dimensions as separate args for N-d |
5734
|
2525 // arrays as well as 1-d and 2-d arrays. |
|
2526 |
5731
|
2527 DEFUN (resize, args, , |
|
2528 "-*- texinfo -*-\n\ |
|
2529 @deftypefn {Built-in Function} {} resize (@var{x}, @var{m})\n\ |
|
2530 @deftypefnx {Built-in Function} {} resize (@var{x}, @var{m}, @var{n})\n\ |
6174
|
2531 Destructively resize @var{x}.\n\ |
|
2532 \n\ |
|
2533 @strong{Values in @var{x} are not preserved as they are with\n\ |
6175
|
2534 @code{reshape}.}\n\ |
6174
|
2535 \n\ |
|
2536 If only @var{m} is supplied and it is a scalar, the dimension of the\n\ |
|
2537 result is @var{m}-by-@var{m}. If @var{m} is a vector, then the\n\ |
|
2538 dimensions of the result are given by the elements of @var{m}.\n\ |
|
2539 If both @var{m} and @var{n} are scalars, then the dimensions of\n\ |
|
2540 the result are @var{m}-by-@var{n}.\n\ |
|
2541 @seealso{reshape}\n\ |
5731
|
2542 @end deftypefn") |
|
2543 { |
|
2544 octave_value retval; |
|
2545 int nargin = args.length (); |
|
2546 |
|
2547 if (nargin == 2) |
|
2548 { |
|
2549 Array<double> vec = args(1).vector_value (); |
|
2550 int ndim = vec.length (); |
|
2551 if (ndim == 1) |
|
2552 { |
|
2553 octave_idx_type m = static_cast<octave_idx_type> (vec(0)); |
|
2554 retval = args(0); |
|
2555 retval = retval.resize (dim_vector (m, m), true); |
|
2556 } |
|
2557 else |
|
2558 { |
|
2559 dim_vector dv; |
|
2560 dv.resize (ndim); |
|
2561 for (int i = 0; i < ndim; i++) |
|
2562 dv(i) = static_cast<octave_idx_type> (vec(i)); |
|
2563 retval = args(0); |
|
2564 retval = retval.resize (dv, true); |
|
2565 } |
|
2566 } |
|
2567 else if (nargin == 3) |
|
2568 { |
|
2569 octave_idx_type m = static_cast<octave_idx_type> |
|
2570 (args(1).scalar_value()); |
|
2571 octave_idx_type n = static_cast<octave_idx_type> |
|
2572 (args(2).scalar_value()); |
|
2573 if (!error_state) |
|
2574 { |
|
2575 retval = args(0); |
|
2576 retval = retval.resize (dim_vector (m, n), true); |
|
2577 } |
|
2578 } |
|
2579 else |
5823
|
2580 print_usage (); |
5731
|
2581 return retval; |
|
2582 } |
|
2583 |
5775
|
2584 // FIXME -- should use octave_idx_type for dimensions. |
5734
|
2585 |
4567
|
2586 DEFUN (reshape, args, , |
|
2587 "-*- texinfo -*-\n\ |
6671
|
2588 @deftypefn {Built-in Function} {} reshape (@var{a}, @var{m}, @var{n}, @dots{})\n\ |
|
2589 @deftypefnx {Built-in Function} {} reshape (@var{a}, @var{siz})\n\ |
4567
|
2590 Return a matrix with the given dimensions whose elements are taken\n\ |
6671
|
2591 from the matrix @var{a}. The elements of the matrix are accessed in\n\ |
4567
|
2592 column-major order (like Fortran arrays are stored).\n\ |
|
2593 \n\ |
|
2594 For example,\n\ |
|
2595 \n\ |
|
2596 @example\n\ |
|
2597 @group\n\ |
|
2598 reshape ([1, 2, 3, 4], 2, 2)\n\ |
|
2599 @result{} 1 3\n\ |
|
2600 2 4\n\ |
|
2601 @end group\n\ |
|
2602 @end example\n\ |
|
2603 \n\ |
|
2604 @noindent\n\ |
|
2605 Note that the total number of elements in the original\n\ |
|
2606 matrix must match the total number of elements in the new matrix.\n\ |
5013
|
2607 \n\ |
|
2608 A single dimension of the return matrix can be unknown and is flagged\n\ |
|
2609 by an empty argument.\n\ |
4567
|
2610 @end deftypefn") |
|
2611 { |
|
2612 octave_value retval; |
|
2613 |
|
2614 int nargin = args.length (); |
|
2615 |
|
2616 Array<int> new_size; |
|
2617 |
|
2618 if (nargin == 2) |
|
2619 new_size = args(1).int_vector_value (); |
|
2620 else if (nargin > 2) |
|
2621 { |
|
2622 new_size.resize (nargin-1); |
5013
|
2623 int empty_dim = -1; |
|
2624 |
4567
|
2625 for (int i = 1; i < nargin; i++) |
|
2626 { |
5013
|
2627 if (args(i).is_empty ()) |
|
2628 if (empty_dim > 0) |
|
2629 { |
|
2630 error ("reshape: only a single dimension can be unknown"); |
|
2631 break; |
|
2632 } |
|
2633 else |
|
2634 { |
|
2635 empty_dim = i; |
|
2636 new_size(i-1) = 1; |
|
2637 } |
|
2638 else |
|
2639 { |
6133
|
2640 new_size(i-1) = args(i).idx_type_value (); |
4567
|
2641 |
5013
|
2642 if (error_state) |
|
2643 break; |
|
2644 } |
|
2645 } |
|
2646 |
|
2647 if (! error_state && (empty_dim > 0)) |
|
2648 { |
|
2649 int nel = 1; |
|
2650 for (int i = 0; i < nargin - 1; i++) |
|
2651 nel *= new_size(i); |
|
2652 |
|
2653 if (nel == 0) |
|
2654 new_size(empty_dim-1) = 0; |
|
2655 else |
|
2656 { |
|
2657 int size_empty_dim = args(0).numel () / nel; |
|
2658 |
|
2659 if (args(0).numel () != size_empty_dim * nel) |
|
2660 error ("reshape: size is not divisble by the product of known dimensions (= %d)", nel); |
|
2661 else |
|
2662 new_size(empty_dim-1) = size_empty_dim; |
|
2663 } |
4567
|
2664 } |
|
2665 } |
|
2666 else |
|
2667 { |
5823
|
2668 print_usage (); |
4567
|
2669 return retval; |
|
2670 } |
|
2671 |
|
2672 if (error_state) |
|
2673 { |
|
2674 error ("reshape: invalid arguments"); |
|
2675 return retval; |
|
2676 } |
|
2677 |
4739
|
2678 // Remove trailing singletons in new_size, but leave at least 2 |
|
2679 // elements. |
|
2680 |
4567
|
2681 int n = new_size.length (); |
|
2682 |
4739
|
2683 while (n > 2) |
|
2684 { |
|
2685 if (new_size(n-1) == 1) |
|
2686 n--; |
|
2687 else |
|
2688 break; |
|
2689 } |
|
2690 |
|
2691 new_size.resize (n); |
|
2692 |
4567
|
2693 if (n < 2) |
|
2694 { |
|
2695 error ("reshape: expecting size to be vector with at least 2 elements"); |
|
2696 return retval; |
|
2697 } |
|
2698 |
|
2699 dim_vector new_dims; |
|
2700 |
|
2701 new_dims.resize (n); |
|
2702 |
5275
|
2703 for (octave_idx_type i = 0; i < n; i++) |
4567
|
2704 new_dims(i) = new_size(i); |
|
2705 |
|
2706 octave_value arg = args(0); |
|
2707 |
|
2708 if (new_dims.numel () == arg.numel ()) |
|
2709 retval = (new_dims == arg.dims ()) ? arg : arg.reshape (new_dims); |
|
2710 else |
|
2711 error ("reshape: size mismatch"); |
|
2712 |
|
2713 return retval; |
|
2714 } |
|
2715 |
4532
|
2716 DEFUN (squeeze, args, , |
|
2717 "-*- texinfo -*-\n\ |
|
2718 @deftypefn {Built-in Function} {} squeeze (@var{x})\n\ |
|
2719 Remove singleton dimensions from @var{x} and return the result.\n\ |
6999
|
2720 Note that for compatibility with @sc{Matlab}, all objects have\n\ |
7007
|
2721 a minimum of two dimensions and row vectors are left unchanged.\n\ |
4532
|
2722 @end deftypefn") |
|
2723 { |
|
2724 octave_value retval; |
|
2725 |
|
2726 if (args.length () == 1) |
4545
|
2727 retval = args(0).squeeze (); |
4532
|
2728 else |
5823
|
2729 print_usage (); |
4532
|
2730 |
|
2731 return retval; |
|
2732 } |
|
2733 |
6953
|
2734 /* |
|
2735 %!shared x |
|
2736 %! x = [1, -3, 4, 5, -7]; |
|
2737 %!assert(norm(x,1), 20); |
|
2738 %!assert(norm(x,2), 10); |
|
2739 %!assert(norm(x,3), 8.24257059961711, -4*eps); |
|
2740 %!assert(norm(x,Inf), 7); |
|
2741 %!assert(norm(x,-Inf), 1); |
|
2742 %!assert(norm(x,"inf"), 7); |
7103
|
2743 %!assert(norm(x,"fro"), 10, -eps); |
6953
|
2744 %!assert(norm(x), 10); |
|
2745 %!assert(norm([1e200, 1]), 1e200); |
|
2746 %!assert(norm([3+4i, 3-4i, sqrt(31)]), 9, -4*eps); |
|
2747 %!shared m |
|
2748 %! m = magic (4); |
|
2749 %!assert(norm(m,1), 34); |
7026
|
2750 %!assert(norm(m,2), 34, -eps); |
6953
|
2751 %!assert(norm(m,Inf), 34); |
|
2752 %!assert(norm(m,"inf"), 34); |
7103
|
2753 %!shared m2, flo, fhi |
7102
|
2754 %! m2 = [1,2;3,4]; |
|
2755 %! flo = 1e-300; |
|
2756 %! fhi = 1e+300; |
7103
|
2757 %!assert (norm(flo*m2,"fro"), sqrt(30)*flo, -eps) |
|
2758 %!assert (norm(fhi*m2,"fro"), sqrt(30)*fhi, -eps) |
6953
|
2759 */ |
|
2760 |
6945
|
2761 // Compute various norms of the vector X. |
|
2762 |
6953
|
2763 DEFUN (norm, args, , |
6508
|
2764 "-*- texinfo -*-\n\ |
6953
|
2765 @deftypefn {Function File} {} norm (@var{a}, @var{p})\n\ |
|
2766 Compute the p-norm of the matrix @var{a}. If the second argument is\n\ |
|
2767 missing, @code{p = 2} is assumed.\n\ |
|
2768 \n\ |
|
2769 If @var{a} is a matrix:\n\ |
|
2770 \n\ |
|
2771 @table @asis\n\ |
|
2772 @item @var{p} = @code{1}\n\ |
|
2773 1-norm, the largest column sum of the absolute values of @var{a}.\n\ |
|
2774 \n\ |
|
2775 @item @var{p} = @code{2}\n\ |
|
2776 Largest singular value of @var{a}.\n\ |
|
2777 \n\ |
7189
|
2778 @item @var{p} = @code{Inf} or @code{\"inf\"}\n\ |
6953
|
2779 @cindex infinity norm\n\ |
|
2780 Infinity norm, the largest row sum of the absolute values of @var{a}.\n\ |
|
2781 \n\ |
|
2782 @item @var{p} = @code{\"fro\"}\n\ |
|
2783 @cindex Frobenius norm\n\ |
|
2784 Frobenius norm of @var{a}, @code{sqrt (sum (diag (@var{a}' * @var{a})))}.\n\ |
|
2785 @end table\n\ |
|
2786 \n\ |
|
2787 If @var{a} is a vector or a scalar:\n\ |
|
2788 \n\ |
|
2789 @table @asis\n\ |
7189
|
2790 @item @var{p} = @code{Inf} or @code{\"inf\"}\n\ |
6953
|
2791 @code{max (abs (@var{a}))}.\n\ |
|
2792 \n\ |
|
2793 @item @var{p} = @code{-Inf}\n\ |
|
2794 @code{min (abs (@var{a}))}.\n\ |
|
2795 \n\ |
7189
|
2796 @item @var{p} = @code{\"fro\"}\n\ |
|
2797 Frobenius norm of @var{a}, @code{sqrt (sumsq (abs (a)))}.\n\ |
|
2798 \n\ |
6953
|
2799 @item other\n\ |
|
2800 p-norm of @var{a}, @code{(sum (abs (@var{a}) .^ @var{p})) ^ (1/@var{p})}.\n\ |
|
2801 @end table\n\ |
|
2802 @seealso{cond, svd}\n\ |
6508
|
2803 @end deftypefn") |
|
2804 { |
6953
|
2805 // Currently only handles vector norms for full double/complex |
|
2806 // vectors internally. Other cases are handled by __norm__.m. |
|
2807 |
|
2808 octave_value_list retval; |
6508
|
2809 |
|
2810 int nargin = args.length (); |
|
2811 |
|
2812 if (nargin == 1 || nargin == 2) |
|
2813 { |
6953
|
2814 octave_value x_arg = args(0); |
|
2815 |
|
2816 if (x_arg.is_empty ()) |
|
2817 retval(0) = 0.0; |
|
2818 else if (x_arg.ndims () == 2) |
6508
|
2819 { |
6953
|
2820 if ((x_arg.rows () == 1 || x_arg.columns () == 1) |
|
2821 && ! (x_arg.is_sparse_type () || x_arg.is_integer_type ())) |
|
2822 { |
7093
|
2823 double p_val = 2; |
|
2824 |
|
2825 if (nargin == 2) |
6953
|
2826 { |
7093
|
2827 octave_value p_arg = args(1); |
|
2828 |
|
2829 if (p_arg.is_string ()) |
|
2830 { |
|
2831 std::string p = args(1).string_value (); |
|
2832 |
|
2833 if (p == "inf") |
|
2834 p_val = octave_Inf; |
|
2835 else if (p == "fro") |
|
2836 p_val = -1; |
|
2837 else |
|
2838 error ("norm: unrecognized norm `%s'", p.c_str ()); |
|
2839 } |
6953
|
2840 else |
7093
|
2841 { |
|
2842 p_val = p_arg.double_value (); |
|
2843 |
|
2844 if (error_state) |
|
2845 error ("norm: unrecognized norm value"); |
|
2846 } |
6953
|
2847 } |
|
2848 |
|
2849 if (! error_state) |
|
2850 { |
|
2851 if (x_arg.is_real_type ()) |
|
2852 { |
|
2853 MArray<double> x (x_arg.array_value ()); |
|
2854 |
|
2855 if (! error_state) |
|
2856 retval(0) = x.norm (p_val); |
|
2857 else |
|
2858 error ("norm: expecting real vector"); |
|
2859 } |
|
2860 else |
|
2861 { |
|
2862 MArray<Complex> x (x_arg.complex_array_value ()); |
|
2863 |
|
2864 if (! error_state) |
|
2865 retval(0) = x.norm (p_val); |
|
2866 else |
|
2867 error ("norm: expecting complex vector"); |
|
2868 } |
|
2869 } |
|
2870 } |
6508
|
2871 else |
6953
|
2872 retval = feval ("__norm__", args); |
6508
|
2873 } |
|
2874 else |
6953
|
2875 error ("norm: only valid for 2-D objects"); |
6508
|
2876 } |
|
2877 else |
|
2878 print_usage (); |
|
2879 |
7269
|
2880 // Should not return a sparse type |
|
2881 if (retval(0).is_sparse_type ()) |
|
2882 { |
|
2883 if (retval(0).type_name () == "sparse matrix") |
|
2884 retval(0) = retval(0).matrix_value (); |
|
2885 else if (retval(0).type_name () == "sparse complex matrix") |
|
2886 retval(0) = retval(0).complex_matrix_value (); |
|
2887 else if (retval(0).type_name () == "sparse bool matrix") |
|
2888 retval(0) = retval(0).bool_matrix_value (); |
|
2889 } |
|
2890 |
6508
|
2891 return retval; |
|
2892 } |
|
2893 |
6518
|
2894 #define UNARY_OP_DEFUN_BODY(F) \ |
|
2895 \ |
|
2896 octave_value retval; \ |
|
2897 \ |
|
2898 if (args.length () == 1) \ |
|
2899 retval = F (args(0)); \ |
|
2900 else \ |
|
2901 print_usage (); \ |
|
2902 \ |
|
2903 return retval |
|
2904 |
|
2905 DEFUN (not, args, , |
|
2906 "-*- texinfo -*-\n\ |
|
2907 @deftypefn {Built-in Function} {} not (@var{x})\n\ |
|
2908 This function is equivalent to @code{! x}.\n\ |
|
2909 @end deftypefn") |
|
2910 { |
|
2911 UNARY_OP_DEFUN_BODY (op_not); |
|
2912 } |
|
2913 |
|
2914 DEFUN (uplus, args, , |
|
2915 "-*- texinfo -*-\n\ |
|
2916 @deftypefn {Built-in Function} {} uplus (@var{x})\n\ |
|
2917 This function is equivalent to @code{+ x}.\n\ |
|
2918 @end deftypefn") |
|
2919 { |
|
2920 UNARY_OP_DEFUN_BODY (op_uplus); |
|
2921 } |
|
2922 |
|
2923 DEFUN (uminus, args, , |
|
2924 "-*- texinfo -*-\n\ |
|
2925 @deftypefn {Built-in Function} {} uminus (@var{x})\n\ |
|
2926 This function is equivalent to @code{- x}.\n\ |
|
2927 @end deftypefn") |
|
2928 { |
|
2929 UNARY_OP_DEFUN_BODY (op_uminus); |
|
2930 } |
|
2931 |
|
2932 DEFUN (transpose, args, , |
|
2933 "-*- texinfo -*-\n\ |
|
2934 @deftypefn {Built-in Function} {} transpose (@var{x})\n\ |
|
2935 This function is equivalent to @code{x.'}.\n\ |
|
2936 @end deftypefn") |
|
2937 { |
|
2938 UNARY_OP_DEFUN_BODY (op_transpose); |
|
2939 } |
|
2940 |
|
2941 DEFUN (ctranspose, args, , |
|
2942 "-*- texinfo -*-\n\ |
|
2943 @deftypefn {Built-in Function} {} ctranspose (@var{x})\n\ |
|
2944 This function is equivalent to @code{x'}.\n\ |
|
2945 @end deftypefn") |
|
2946 { |
|
2947 UNARY_OP_DEFUN_BODY (op_hermitian); |
|
2948 } |
|
2949 |
|
2950 #define BINARY_OP_DEFUN_BODY(F) \ |
|
2951 \ |
|
2952 octave_value retval; \ |
|
2953 \ |
|
2954 if (args.length () == 2) \ |
|
2955 retval = F (args(0), args(1)); \ |
|
2956 else \ |
|
2957 print_usage (); \ |
|
2958 \ |
|
2959 return retval |
|
2960 |
|
2961 DEFUN (plus, args, , |
|
2962 "-*- texinfo -*-\n\ |
|
2963 @deftypefn {Built-in Function} {} plus (@var{x}, @var{y})\n\ |
|
2964 This function is equivalent to @code{x + y}.\n\ |
|
2965 @end deftypefn") |
|
2966 { |
|
2967 BINARY_OP_DEFUN_BODY (op_add); |
|
2968 } |
|
2969 |
|
2970 DEFUN (minus, args, , |
|
2971 "-*- texinfo -*-\n\ |
|
2972 @deftypefn {Built-in Function} {} minus (@var{x}, @var{y})\n\ |
|
2973 This function is equivalent to @code{x - y}.\n\ |
|
2974 @end deftypefn") |
|
2975 { |
|
2976 BINARY_OP_DEFUN_BODY (op_sub); |
|
2977 } |
|
2978 |
|
2979 DEFUN (mtimes, args, , |
|
2980 "-*- texinfo -*-\n\ |
|
2981 @deftypefn {Built-in Function} {} mtimes (@var{x}, @var{y})\n\ |
|
2982 This function is equivalent to @code{x * y}.\n\ |
|
2983 @end deftypefn") |
|
2984 { |
|
2985 BINARY_OP_DEFUN_BODY (op_mul); |
|
2986 } |
|
2987 |
|
2988 DEFUN (mrdivide, args, , |
|
2989 "-*- texinfo -*-\n\ |
|
2990 @deftypefn {Built-in Function} {} mrdivide (@var{x}, @var{y})\n\ |
|
2991 This function is equivalent to @code{x / y}.\n\ |
|
2992 @end deftypefn") |
|
2993 { |
|
2994 BINARY_OP_DEFUN_BODY (op_div); |
|
2995 } |
|
2996 |
|
2997 DEFUN (mpower, args, , |
|
2998 "-*- texinfo -*-\n\ |
|
2999 @deftypefn {Built-in Function} {} mpower (@var{x}, @var{y})\n\ |
|
3000 This function is equivalent to @code{x ^ y}.\n\ |
|
3001 @end deftypefn") |
|
3002 { |
|
3003 BINARY_OP_DEFUN_BODY (op_pow); |
|
3004 } |
|
3005 |
|
3006 DEFUN (mldivide, args, , |
|
3007 "-*- texinfo -*-\n\ |
|
3008 @deftypefn {Built-in Function} {} mldivide (@var{x}, @var{y})\n\ |
|
3009 This function is equivalent to @code{x \\ y}.\n\ |
|
3010 @end deftypefn") |
|
3011 { |
|
3012 BINARY_OP_DEFUN_BODY (op_ldiv); |
|
3013 } |
|
3014 |
|
3015 DEFUN (lt, args, , |
|
3016 "-*- texinfo -*-\n\ |
|
3017 @deftypefn {Built-in Function} {} lt (@var{x}, @var{y})\n\ |
|
3018 This function is equivalent to @code{x < y}.\n\ |
|
3019 @end deftypefn") |
|
3020 { |
|
3021 BINARY_OP_DEFUN_BODY (op_lt); |
|
3022 } |
|
3023 |
|
3024 DEFUN (le, args, , |
|
3025 "-*- texinfo -*-\n\ |
|
3026 @deftypefn {Built-in Function} {} le (@var{x}, @var{y})\n\ |
|
3027 This function is equivalent to @code{x <= y}.\n\ |
|
3028 @end deftypefn") |
|
3029 { |
|
3030 BINARY_OP_DEFUN_BODY (op_le); |
|
3031 } |
|
3032 |
|
3033 DEFUN (eq, args, , |
|
3034 "-*- texinfo -*-\n\ |
|
3035 @deftypefn {Built-in Function} {} eq (@var{x}, @var{y})\n\ |
|
3036 This function is equivalent to @code{x == y}.\n\ |
|
3037 @end deftypefn") |
|
3038 { |
|
3039 BINARY_OP_DEFUN_BODY (op_eq); |
|
3040 } |
|
3041 |
|
3042 DEFUN (ge, args, , |
|
3043 "-*- texinfo -*-\n\ |
|
3044 @deftypefn {Built-in Function} {} ge (@var{x}, @var{y})\n\ |
|
3045 This function is equivalent to @code{x >= y}.\n\ |
|
3046 @end deftypefn") |
|
3047 { |
|
3048 BINARY_OP_DEFUN_BODY (op_ge); |
|
3049 } |
|
3050 |
|
3051 DEFUN (gt, args, , |
|
3052 "-*- texinfo -*-\n\ |
|
3053 @deftypefn {Built-in Function} {} gt (@var{x}, @var{y})\n\ |
|
3054 This function is equivalent to @code{x > y}.\n\ |
|
3055 @end deftypefn") |
|
3056 { |
|
3057 BINARY_OP_DEFUN_BODY (op_gt); |
|
3058 } |
|
3059 |
|
3060 DEFUN (ne, args, , |
|
3061 "-*- texinfo -*-\n\ |
|
3062 @deftypefn {Built-in Function} {} ne (@var{x}, @var{y})\n\ |
|
3063 This function is equivalent to @code{x != y}.\n\ |
|
3064 @end deftypefn") |
|
3065 { |
|
3066 BINARY_OP_DEFUN_BODY (op_ne); |
|
3067 } |
|
3068 |
|
3069 DEFUN (times, args, , |
|
3070 "-*- texinfo -*-\n\ |
|
3071 @deftypefn {Built-in Function} {} times (@var{x}, @var{y})\n\ |
|
3072 This function is equivalent to @code{x .* y}.\n\ |
|
3073 @end deftypefn") |
|
3074 { |
|
3075 BINARY_OP_DEFUN_BODY (op_el_mul); |
|
3076 } |
|
3077 |
|
3078 DEFUN (rdivide, args, , |
|
3079 "-*- texinfo -*-\n\ |
|
3080 @deftypefn {Built-in Function} {} rdivide (@var{x}, @var{y})\n\ |
|
3081 This function is equivalent to @code{x ./ y}.\n\ |
|
3082 @end deftypefn") |
|
3083 { |
|
3084 BINARY_OP_DEFUN_BODY (op_el_div); |
|
3085 } |
|
3086 |
|
3087 DEFUN (power, args, , |
|
3088 "-*- texinfo -*-\n\ |
|
3089 @deftypefn {Built-in Function} {} power (@var{x}, @var{y})\n\ |
|
3090 This function is equivalent to @code{x .\\ y}.\n\ |
|
3091 @end deftypefn") |
|
3092 { |
|
3093 BINARY_OP_DEFUN_BODY (op_el_pow); |
|
3094 } |
|
3095 |
|
3096 DEFUN (ldivide, args, , |
|
3097 "-*- texinfo -*-\n\ |
|
3098 @deftypefn {Built-in Function} {} ldivide (@var{x}, @var{y})\n\ |
|
3099 @end deftypefn") |
|
3100 { |
|
3101 BINARY_OP_DEFUN_BODY (op_el_ldiv); |
|
3102 } |
|
3103 |
|
3104 DEFUN (and, args, , |
|
3105 "-*- texinfo -*-\n\ |
|
3106 @deftypefn {Built-in Function} {} and (@var{x}, @var{y})\n\ |
|
3107 This function is equivalent to @code{x & y}.\n\ |
|
3108 @end deftypefn") |
|
3109 { |
|
3110 BINARY_OP_DEFUN_BODY (op_el_and); |
|
3111 } |
|
3112 |
|
3113 DEFUN (or, args, , |
|
3114 "-*- texinfo -*-\n\ |
|
3115 @deftypefn {Built-in Function} {} or (@var{x}, @var{y})\n\ |
|
3116 This function is equivalent to @code{x | y}.\n\ |
|
3117 @end deftypefn") |
|
3118 { |
|
3119 BINARY_OP_DEFUN_BODY (op_el_or); |
|
3120 } |
|
3121 |
7065
|
3122 static double tic_toc_timestamp = -1.0; |
7045
|
3123 |
|
3124 DEFUN (tic, args, nargout, |
|
3125 "-*- texinfo -*-\n\ |
|
3126 @deftypefn {Built-in Function} {} tic ()\n\ |
|
3127 @deftypefnx {Built-in Function} {} toc ()\n\ |
|
3128 Set or check a wall-clock timer. Calling @code{tic} without an\n\ |
|
3129 output argument sets the timer. Subsequent calls to @code{toc}\n\ |
|
3130 return the number of seconds since the timer was set. For example,\n\ |
|
3131 \n\ |
|
3132 @example\n\ |
|
3133 tic ();\n\ |
|
3134 # many computations later...\n\ |
|
3135 elapsed_time = toc ();\n\ |
|
3136 @end example\n\ |
|
3137 \n\ |
|
3138 @noindent\n\ |
|
3139 will set the variable @code{elapsed_time} to the number of seconds since\n\ |
|
3140 the most recent call to the function @code{tic}.\n\ |
|
3141 \n\ |
|
3142 If called with one output argument then this function returns a scalar\n\ |
|
3143 of type @code{uint64} and the wall-clock timer is not started.\n\ |
|
3144 \n\ |
|
3145 @example\n\ |
|
3146 @group\n\ |
|
3147 t = tic; sleep (5); (double (tic ()) - double (t)) * 1e-6\n\ |
|
3148 @result{} 5\n\ |
|
3149 @end group\n\ |
|
3150 @end example\n\ |
|
3151 \n\ |
|
3152 Nested timing with @code{tic} and @code{toc} is not supported.\n\ |
|
3153 Therefore @code{toc} will always return the elapsed time from the most\n\ |
|
3154 recent call to @code{tic}.\n\ |
|
3155 \n\ |
|
3156 If you are more interested in the CPU time that your process used, you\n\ |
|
3157 should use the @code{cputime} function instead. The @code{tic} and\n\ |
|
3158 @code{toc} functions report the actual wall clock time that elapsed\n\ |
|
3159 between the calls. This may include time spent processing other jobs or\n\ |
|
3160 doing nothing at all. For example,\n\ |
|
3161 \n\ |
|
3162 @example\n\ |
|
3163 @group\n\ |
|
3164 tic (); sleep (5); toc ()\n\ |
|
3165 @result{} 5\n\ |
|
3166 t = cputime (); sleep (5); cputime () - t\n\ |
|
3167 @result{} 0\n\ |
|
3168 @end group\n\ |
|
3169 @end example\n\ |
|
3170 \n\ |
|
3171 @noindent\n\ |
|
3172 (This example also illustrates that the CPU timer may have a fairly\n\ |
|
3173 coarse resolution.)\n\ |
|
3174 @end deftypefn") |
|
3175 { |
|
3176 octave_value retval; |
7065
|
3177 |
7045
|
3178 int nargin = args.length (); |
|
3179 |
|
3180 if (nargin != 0) |
|
3181 warning ("tic: ignoring extra arguments"); |
|
3182 |
7065
|
3183 octave_time now; |
|
3184 |
|
3185 double tmp = now.double_value (); |
|
3186 |
7045
|
3187 if (nargout > 0) |
7065
|
3188 retval = static_cast<octave_uint64> (1e6 * tmp); |
7045
|
3189 else |
7065
|
3190 tic_toc_timestamp = tmp; |
7045
|
3191 |
|
3192 return retval; |
|
3193 } |
|
3194 |
|
3195 DEFUN (toc, args, nargout, |
|
3196 "-*- texinfo -*-\n\ |
|
3197 @deftypefn {Built-in Function} {} toc ()\n\ |
|
3198 See tic.\n\ |
|
3199 @end deftypefn") |
|
3200 { |
|
3201 octave_value retval; |
7065
|
3202 |
7045
|
3203 int nargin = args.length (); |
|
3204 |
|
3205 if (nargin != 0) |
|
3206 warning ("tic: ignoring extra arguments"); |
|
3207 |
7065
|
3208 if (tic_toc_timestamp < 0) |
7045
|
3209 { |
|
3210 warning ("toc called before timer set"); |
|
3211 if (nargout > 0) |
7065
|
3212 retval = Matrix (); |
7045
|
3213 } |
|
3214 else |
7065
|
3215 { |
|
3216 octave_time now; |
|
3217 |
|
3218 double tmp = now.double_value () - tic_toc_timestamp; |
|
3219 |
|
3220 if (nargout > 0) |
|
3221 retval = tmp; |
|
3222 else |
|
3223 octave_stdout << "Elapsed time is " << tmp << " seconds.\n"; |
|
3224 } |
7045
|
3225 |
|
3226 return retval; |
|
3227 } |
|
3228 |
|
3229 DEFUN (cputime, args, , |
|
3230 "-*- texinfo -*-\n\ |
|
3231 @deftypefn {Built-in Function} {[@var{total}, @var{user}, @var{system}] =} cputime ();\n\ |
|
3232 Return the CPU time used by your Octave session. The first output is\n\ |
|
3233 the total time spent executing your process and is equal to the sum of\n\ |
|
3234 second and third outputs, which are the number of CPU seconds spent\n\ |
|
3235 executing in user mode and the number of CPU seconds spent executing in\n\ |
|
3236 system mode, respectively. If your system does not have a way to report\n\ |
|
3237 CPU time usage, @code{cputime} returns 0 for each of its output values.\n\ |
|
3238 Note that because Octave used some CPU time to start, it is reasonable\n\ |
|
3239 to check to see if @code{cputime} works by checking to see if the total\n\ |
|
3240 CPU time used is nonzero.\n\ |
|
3241 @end deftypefn") |
|
3242 { |
|
3243 octave_value_list retval; |
|
3244 int nargin = args.length (); |
|
3245 double usr = 0.0; |
|
3246 double sys = 0.0; |
|
3247 |
|
3248 if (nargin != 0) |
|
3249 warning ("tic: ignoring extra arguments"); |
|
3250 |
|
3251 #if defined (HAVE_GETRUSAGE) |
|
3252 |
|
3253 struct rusage ru; |
|
3254 |
|
3255 getrusage (RUSAGE_SELF, &ru); |
|
3256 |
|
3257 usr = static_cast<double> (ru.ru_utime.tv_sec) + |
|
3258 static_cast<double> (ru.ru_utime.tv_usec) * 1e-6; |
|
3259 |
|
3260 sys = static_cast<double> (ru.ru_stime.tv_sec) + |
|
3261 static_cast<double> (ru.ru_stime.tv_usec) * 1e-6; |
|
3262 |
|
3263 #elif defined (HAVE_TIMES) && defined (HAVE_SYS_TIMES_H) |
|
3264 |
|
3265 struct tms t; |
|
3266 |
|
3267 times (&t); |
|
3268 |
|
3269 unsigned long ticks; |
|
3270 unsigned long seconds; |
|
3271 unsigned long fraction; |
|
3272 |
|
3273 ticks = t.tms_utime + t.tms_cutime; |
|
3274 fraction = ticks % HZ; |
|
3275 seconds = ticks / HZ; |
|
3276 |
|
3277 usr = static_cast<double> (seconds) + static_cast<double>(fraction) / |
|
3278 static_cast<double>(HZ); |
|
3279 |
|
3280 ticks = t.tms_stime + t.tms_cstime; |
|
3281 fraction = ticks % HZ; |
|
3282 seconds = ticks / HZ; |
|
3283 |
|
3284 sys = static_cast<double> (seconds) + static_cast<double>(fraction) / |
|
3285 static_cast<double>(HZ); |
|
3286 |
|
3287 #elif defined (__WIN32__) |
7145
|
3288 |
7045
|
3289 HANDLE hProcess = GetCurrentProcess (); |
|
3290 FILETIME ftCreation, ftExit, ftUser, ftKernel; |
|
3291 GetProcessTimes (hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser); |
|
3292 |
|
3293 int64_t itmp = *(reinterpret_cast<int64_t *> (&ftUser)); |
7145
|
3294 usr = static_cast<double> (itmp) * 1e-7; |
7045
|
3295 |
|
3296 itmp = *(reinterpret_cast<int64_t *> (&ftKernel)); |
7145
|
3297 sys = static_cast<double> (itmp) * 1e-7; |
7045
|
3298 |
|
3299 #endif |
|
3300 |
|
3301 retval (2) = sys; |
|
3302 retval (1) = usr; |
|
3303 retval (0) = sys + usr; |
|
3304 |
|
3305 return retval; |
|
3306 } |
|
3307 |
7433
|
3308 DEFUN (sort, args, nargout, |
|
3309 "-*- texinfo -*-\n\ |
|
3310 @deftypefn {Loadable Function} {[@var{s}, @var{i}] =} sort (@var{x})\n\ |
|
3311 @deftypefnx {Loadable Function} {[@var{s}, @var{i}] =} sort (@var{x}, @var{dim})\n\ |
|
3312 @deftypefnx {Loadable Function} {[@var{s}, @var{i}] =} sort (@var{x}, @var{mode})\n\ |
|
3313 @deftypefnx {Loadable Function} {[@var{s}, @var{i}] =} sort (@var{x}, @var{dim}, @var{mode})\n\ |
|
3314 Return a copy of @var{x} with the elements arranged in increasing\n\ |
|
3315 order. For matrices, @code{sort} orders the elements in each column.\n\ |
|
3316 \n\ |
|
3317 For example,\n\ |
|
3318 \n\ |
|
3319 @example\n\ |
|
3320 @group\n\ |
|
3321 sort ([1, 2; 2, 3; 3, 1])\n\ |
|
3322 @result{} 1 1\n\ |
|
3323 2 2\n\ |
|
3324 3 3\n\ |
|
3325 @end group\n\ |
|
3326 @end example\n\ |
|
3327 \n\ |
|
3328 The @code{sort} function may also be used to produce a matrix\n\ |
|
3329 containing the original row indices of the elements in the sorted\n\ |
|
3330 matrix. For example,\n\ |
|
3331 \n\ |
|
3332 @example\n\ |
|
3333 @group\n\ |
|
3334 [s, i] = sort ([1, 2; 2, 3; 3, 1])\n\ |
|
3335 @result{} s = 1 1\n\ |
|
3336 2 2\n\ |
|
3337 3 3\n\ |
|
3338 @result{} i = 1 3\n\ |
|
3339 2 1\n\ |
|
3340 3 2\n\ |
|
3341 @end group\n\ |
|
3342 @end example\n\ |
|
3343 \n\ |
|
3344 If the optional argument @var{dim} is given, then the matrix is sorted\n\ |
|
3345 along the dimension defined by @var{dim}. The optional argument @code{mode}\n\ |
|
3346 defines the order in which the values will be sorted. Valid values of\n\ |
|
3347 @code{mode} are `ascend' or `descend'.\n\ |
|
3348 \n\ |
|
3349 For equal elements, the indices are such that the equal elements are listed\n\ |
|
3350 in the order that appeared in the original list.\n\ |
|
3351 \n\ |
|
3352 The @code{sort} function may also be used to sort strings and cell arrays\n\ |
|
3353 of strings, in which case the dictionary order of the strings is used.\n\ |
|
3354 \n\ |
|
3355 The algorithm used in @code{sort} is optimized for the sorting of partially\n\ |
|
3356 ordered lists.\n\ |
|
3357 @end deftypefn") |
|
3358 { |
|
3359 octave_value_list retval; |
|
3360 |
|
3361 int nargin = args.length (); |
|
3362 sortmode smode = ASCENDING; |
|
3363 |
|
3364 if (nargin < 1 || nargin > 3) |
|
3365 { |
|
3366 print_usage (); |
|
3367 return retval; |
|
3368 } |
|
3369 |
|
3370 bool return_idx = nargout > 1; |
|
3371 |
|
3372 octave_value arg = args(0); |
|
3373 |
|
3374 int dim = 0; |
|
3375 if (nargin > 1) |
|
3376 { |
|
3377 if (args(1).is_string ()) |
|
3378 { |
|
3379 std::string mode = args(1).string_value(); |
|
3380 if (mode == "ascend") |
|
3381 smode = ASCENDING; |
|
3382 else if (mode == "descend") |
|
3383 smode = DESCENDING; |
|
3384 else |
|
3385 { |
|
3386 error ("sort: mode must be either \"ascend\" or \"descend\""); |
|
3387 return retval; |
|
3388 } |
|
3389 } |
|
3390 else |
|
3391 dim = args(1).nint_value () - 1; |
|
3392 } |
|
3393 |
|
3394 if (nargin > 2) |
|
3395 { |
|
3396 if (args(1).is_string ()) |
|
3397 { |
|
3398 print_usage (); |
|
3399 return retval; |
|
3400 } |
|
3401 |
|
3402 if (! args(2).is_string ()) |
|
3403 { |
|
3404 error ("sort: mode must be a string"); |
|
3405 return retval; |
|
3406 } |
|
3407 std::string mode = args(2).string_value(); |
|
3408 if (mode == "ascend") |
|
3409 smode = ASCENDING; |
|
3410 else if (mode == "descend") |
|
3411 smode = DESCENDING; |
|
3412 else |
|
3413 { |
|
3414 error ("sort: mode must be either \"ascend\" or \"descend\""); |
|
3415 return retval; |
|
3416 } |
|
3417 } |
|
3418 |
|
3419 dim_vector dv = arg.dims (); |
|
3420 if (error_state) |
|
3421 { |
|
3422 gripe_wrong_type_arg ("sort", arg); |
|
3423 return retval; |
|
3424 } |
|
3425 if (nargin == 1 || args(1).is_string ()) |
|
3426 { |
|
3427 // Find first non singleton dimension |
|
3428 for (int i = 0; i < dv.length (); i++) |
|
3429 if (dv(i) > 1) |
|
3430 { |
|
3431 dim = i; |
|
3432 break; |
|
3433 } |
|
3434 } |
|
3435 else |
|
3436 { |
|
3437 if (dim < 0 || dim > dv.length () - 1) |
|
3438 { |
|
3439 error ("sort: dim must be a valid dimension"); |
|
3440 return retval; |
|
3441 } |
|
3442 } |
|
3443 |
|
3444 if (return_idx) |
|
3445 { |
|
3446 Array<octave_idx_type> sidx; |
|
3447 |
|
3448 retval (0) = arg.sort (sidx, dim, smode); |
|
3449 |
|
3450 octave_idx_type *ps = sidx.fortran_vec (); |
|
3451 NDArray midx (sidx.dims ()); |
|
3452 double *pm = midx.fortran_vec (); |
|
3453 |
|
3454 for (octave_idx_type i = 0; i < sidx.numel (); i++) |
|
3455 pm [i] = static_cast<double> |
|
3456 (ps [i] + static_cast<octave_idx_type> (1)); |
|
3457 |
|
3458 retval (1) = midx; |
|
3459 } |
|
3460 else |
|
3461 retval(0) = arg.sort (dim, smode); |
|
3462 |
|
3463 return retval; |
|
3464 } |
|
3465 |
|
3466 /* |
|
3467 |
|
3468 %% Double |
|
3469 %!assert (sort ([NaN, 1, -1, 2, Inf]), [-1, 1, 2, Inf, NaN]) |
|
3470 %!assert (sort ([NaN, 1, -1, 2, Inf], 1), [NaN, 1, -1, 2, Inf]) |
|
3471 %!assert (sort ([NaN, 1, -1, 2, Inf], 2), [-1, 1, 2, Inf, NaN]) |
|
3472 %!error (sort ([NaN, 1, -1, 2, Inf], 3)) |
|
3473 %!assert (sort ([NaN, 1, -1, 2, Inf], "ascend"), [-1, 1, 2, Inf, NaN]) |
|
3474 %!assert (sort ([NaN, 1, -1, 2, Inf], 2, "ascend"), [-1, 1, 2, Inf, NaN]) |
|
3475 %!assert (sort ([NaN, 1, -1, 2, Inf], "descend"), [NaN, Inf, 2, 1, -1]) |
|
3476 %!assert (sort ([NaN, 1, -1, 2, Inf], 2, "descend"), [NaN, Inf, 2, 1, -1]) |
|
3477 %!assert (sort ([3, 1, 7, 5; 8, 2, 6, 4]), [3, 1, 6, 4; 8, 2, 7, 5]) |
|
3478 %!assert (sort ([3, 1, 7, 5; 8, 2, 6, 4], 1), [3, 1, 6, 4; 8, 2, 7, 5]) |
|
3479 %!assert (sort ([3, 1, 7, 5; 8, 2, 6, 4], 2), [1, 3, 5, 7; 2, 4, 6, 8]) |
|
3480 %!assert (sort (1), 1) |
|
3481 |
|
3482 %!test |
|
3483 %! [v, i] = sort ([NaN, 1, -1, Inf, 1]); |
|
3484 %! assert (v, [-1, 1, 1, Inf, NaN]) |
|
3485 %! assert (i, [3, 2, 5, 4, 1]) |
|
3486 |
|
3487 %% Complex |
|
3488 %!assert (sort ([NaN, 1i, -1, 2, Inf]), [1i, -1, 2, Inf, NaN]) |
|
3489 %!assert (sort ([NaN, 1i, -1, 2, Inf], 1), [NaN, 1i, -1, 2, Inf]) |
|
3490 %!assert (sort ([NaN, 1i, -1, 2, Inf], 2), [1i, -1, 2, Inf, NaN]) |
|
3491 %!error (sort ([NaN, 1i, -1, 2, Inf], 3)) |
|
3492 %!assert (sort ([NaN, 1i, -1, 2, Inf], "ascend"), [1i, -1, 2, Inf, NaN]) |
|
3493 %!assert (sort ([NaN, 1i, -1, 2, Inf], 2, "ascend"), [1i, -1, 2, Inf, NaN]) |
|
3494 %!assert (sort ([NaN, 1i, -1, 2, Inf], "descend"), [NaN, Inf, 2, -1, 1i]) |
|
3495 %!assert (sort ([NaN, 1i, -1, 2, Inf], 2, "descend"), [NaN, Inf, 2, -1, 1i]) |
|
3496 %!assert (sort ([3, 1i, 7, 5; 8, 2, 6, 4]), [3, 1i, 6, 4; 8, 2, 7, 5]) |
|
3497 %!assert (sort ([3, 1i, 7, 5; 8, 2, 6, 4], 1), [3, 1i, 6, 4; 8, 2, 7, 5]) |
|
3498 %!assert (sort ([3, 1i, 7, 5; 8, 2, 6, 4], 2), [1i, 3, 5, 7; 2, 4, 6, 8]) |
|
3499 %!assert (sort (1i), 1i) |
|
3500 |
|
3501 %!test |
|
3502 %! [v, i] = sort ([NaN, 1i, -1, Inf, 1, 1i]); |
|
3503 %! assert (v, [1, 1i, 1i, -1, Inf, NaN]) |
|
3504 %! assert (i, [5, 2, 6, 3, 4, 1]) |
|
3505 |
|
3506 %% Bool |
|
3507 %!assert (sort ([true, false, true, false]), [false, false, true, true]) |
|
3508 %!assert (sort ([true, false, true, false], 1), [true, false, true, false]) |
|
3509 %!assert (sort ([true, false, true, false], 2), [false, false, true, true]) |
|
3510 %!error (sort ([true, false, true, false], 3)) |
|
3511 %!assert (sort ([true, false, true, false], "ascend"), [false, false, true, true]) |
|
3512 %!assert (sort ([true, false, true, false], 2, "ascend"), [false, false, true, true]) |
|
3513 %!assert (sort ([true, false, true, false], "descend"), [true, true, false, false]) |
|
3514 %!assert (sort ([true, false, true, false], 2, "descend"), [true, true, false, false]) |
|
3515 %!assert (sort (true), true) |
|
3516 |
|
3517 %!test |
|
3518 %! [v, i] = sort ([true, false, true, false]); |
|
3519 %! assert (v, [false, false, true, true]) |
|
3520 %! assert (i, [2, 4, 1, 3]) |
|
3521 |
|
3522 %% Sparse Double |
|
3523 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf])), sparse ([-1, 0, 0, 1, 2, Inf, NaN])) |
|
3524 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf]), 1), sparse ([0, NaN, 1, 0, -1, 2, Inf])) |
|
3525 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf]), 2), sparse ([-1, 0, 0, 1, 2, Inf, NaN])) |
|
3526 %!error (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf]), 3)) |
|
3527 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf]), "ascend"), sparse ([-1, 0, 0, 1, 2, Inf, NaN])) |
|
3528 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf]), 2, "ascend"), sparse ([-1, 0, 0, 1, 2, Inf, NaN])) |
|
3529 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf]), "descend"), sparse ([NaN, Inf, 2, 1, 0, 0, -1])) |
|
3530 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf]), 2, "descend"), sparse ([NaN, Inf, 2, 1, 0, 0, -1])) |
|
3531 |
|
3532 %!shared a |
|
3533 %! a = randn (10, 10); |
|
3534 %! a (a < 0) = 0; |
|
3535 %!assert (sort (sparse (a)), sparse (sort (a))) |
|
3536 %!assert (sort (sparse (a), 1), sparse (sort (a, 1))) |
|
3537 %!assert (sort (sparse (a), 2), sparse (sort (a, 2))) |
|
3538 %!test |
|
3539 %! [v, i] = sort (a); |
|
3540 %! [vs, is] = sort (sparse (a)); |
|
3541 %! assert (vs, sparse (v)) |
|
3542 %! assert (is, i) |
|
3543 |
|
3544 %% Sparse Complex |
|
3545 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf])), sparse ([0, 0, 1i, -1, 2, Inf, NaN])) |
|
3546 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf]), 1), sparse ([0, NaN, 1i, 0, -1, 2, Inf])) |
|
3547 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf]), 2), sparse ([0, 0, 1i, -1, 2, Inf, NaN])) |
|
3548 %!error (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf]), 3)) |
|
3549 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf]), "ascend"), sparse ([0, 0, 1i, -1, 2, Inf, NaN])) |
|
3550 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf]), 2, "ascend"), sparse ([0, 0, 1i, -1, 2, Inf, NaN])) |
|
3551 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf]), "descend"), sparse ([NaN, Inf, 2, -1, 1i, 0, 0])) |
|
3552 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf]), 2, "descend"), sparse ([NaN, Inf, 2, -1, 1i, 0, 0])) |
|
3553 |
|
3554 %!shared a |
|
3555 %! a = randn (10, 10); |
|
3556 %! a (a < 0) = 0; |
|
3557 %! a = 1i * a; |
|
3558 %!assert (sort (sparse (a)), sparse (sort (a))) |
|
3559 %!assert (sort (sparse (a), 1), sparse (sort (a, 1))) |
|
3560 %!assert (sort (sparse (a), 2), sparse (sort (a, 2))) |
|
3561 %!test |
|
3562 %! [v, i] = sort (a); |
|
3563 %! [vs, is] = sort (sparse (a)); |
|
3564 %! assert (vs, sparse (v)) |
|
3565 %! assert (is, i) |
|
3566 |
|
3567 %% Sparse Bool |
|
3568 %!assert (sort (sparse ([true, false, true, false])), sparse ([false, false, true, true])) |
|
3569 %!assert (sort (sparse([true, false, true, false]), 1), sparse ([true, false, true, false])) |
|
3570 %!assert (sort (sparse ([true, false, true, false]), 2), sparse ([false, false, true, true])) |
|
3571 %!error (sort (sparse ([true, false, true, false], 3))) |
|
3572 %!assert (sort (sparse ([true, false, true, false]), "ascend"), sparse([false, false, true, true])) |
|
3573 %!assert (sort (sparse ([true, false, true, false]), 2, "ascend"), sparse([false, false, true, true])) |
|
3574 %!assert (sort (sparse ([true, false, true, false]), "descend"), sparse ([true, true, false, false])) |
|
3575 %!assert (sort (sparse ([true, false, true, false]), 2, "descend"), sparse([true, true, false, false])) |
|
3576 |
|
3577 %!test |
|
3578 %! [v, i] = sort (sparse([true, false, true, false])); |
|
3579 %! assert (v, sparse([false, false, true, true])) |
|
3580 %! assert (i, [2, 4, 1, 3]) |
|
3581 |
|
3582 %% Cell string array |
|
3583 %!shared a, b, c |
|
3584 %! a = {"Alice", "Cecile", "Eric", "Barry", "David"}; |
|
3585 %! b = {"Alice", "Barry", "Cecile", "David", "Eric"}; |
|
3586 %! c = {"Eric", "David", "Cecile", "Barry", "Alice"}; |
|
3587 %!assert (sort (a), b); |
|
3588 %!assert (sort (a, 1), a) |
|
3589 %!assert (sort (a, 2), b) |
|
3590 %!error (sort (a, 3)) |
|
3591 %!assert (sort (a, "ascend"), b) |
|
3592 %!assert (sort (a, 2, "ascend"), b) |
|
3593 %!assert (sort (a, "descend"), c) |
|
3594 %!assert (sort (a, 2, "descend"), c) |
|
3595 |
|
3596 %!test |
|
3597 %! [v, i] = sort (a); |
|
3598 %! assert (i, [1, 4, 2, 5, 3]) |
|
3599 |
|
3600 */ |
|
3601 |
523
|
3602 /* |
|
3603 ;;; Local Variables: *** |
|
3604 ;;; mode: C++ *** |
|
3605 ;;; End: *** |
|
3606 */ |