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