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