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