523
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
523
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
523
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
523
|
26 #endif |
|
27 |
2184
|
28 #include <cfloat> |
|
29 #include <cmath> |
|
30 |
1728
|
31 #include <string> |
|
32 |
2184
|
33 #include "lo-ieee.h" |
1755
|
34 #include "str-vec.h" |
4153
|
35 #include "quit.h" |
1755
|
36 |
1352
|
37 #include "defun.h" |
|
38 #include "error.h" |
|
39 #include "gripes.h" |
2366
|
40 #include "ov.h" |
|
41 #include "variables.h" |
1742
|
42 #include "oct-obj.h" |
523
|
43 #include "utils.h" |
4806
|
44 #include "Cell.h" |
|
45 #include "oct-map.h" |
4915
|
46 #include "pt-mat.h" |
523
|
47 |
4015
|
48 #define ANY_ALL(FCN) \ |
|
49 \ |
4233
|
50 octave_value retval; \ |
4015
|
51 \ |
|
52 int nargin = args.length (); \ |
|
53 \ |
4021
|
54 if (nargin == 1 || nargin == 2) \ |
4015
|
55 { \ |
4021
|
56 int dim = (nargin == 1 ? -1 : args(1).int_value (true) - 1); \ |
|
57 \ |
|
58 if (! error_state) \ |
|
59 { \ |
4556
|
60 if (dim >= -1) \ |
4015
|
61 retval = args(0).FCN (dim); \ |
4021
|
62 else \ |
|
63 error (#FCN ": invalid dimension argument = %d", dim + 1); \ |
|
64 } \ |
4015
|
65 else \ |
4021
|
66 error (#FCN ": expecting dimension argument to be an integer"); \ |
4015
|
67 } \ |
4021
|
68 else \ |
|
69 print_usage (#FCN); \ |
4015
|
70 \ |
|
71 return retval |
|
72 |
1957
|
73 DEFUN (all, args, , |
3369
|
74 "-*- texinfo -*-\n\ |
4015
|
75 @deftypefn {Built-in Function} {} all (@var{x}, @var{dim})\n\ |
3369
|
76 The function @code{all} behaves like the function @code{any}, except\n\ |
|
77 that it returns true only if all the elements of a vector, or all the\n\ |
4015
|
78 elements along dimension @var{dim} of a matrix, are nonzero.\n\ |
3369
|
79 @end deftypefn") |
523
|
80 { |
4015
|
81 ANY_ALL (all); |
523
|
82 } |
|
83 |
1957
|
84 DEFUN (any, args, , |
3369
|
85 "-*- texinfo -*-\n\ |
4015
|
86 @deftypefn {Built-in Function} {} any (@var{x}, @var{dim})\n\ |
3369
|
87 For a vector argument, return 1 if any element of the vector is\n\ |
|
88 nonzero.\n\ |
|
89 \n\ |
|
90 For a matrix argument, return a row vector of ones and\n\ |
|
91 zeros with each element indicating whether any of the elements of the\n\ |
|
92 corresponding column of the matrix are nonzero. For example,\n\ |
|
93 \n\ |
|
94 @example\n\ |
|
95 @group\n\ |
|
96 any (eye (2, 4))\n\ |
|
97 @result{} [ 1, 1, 0, 0 ]\n\ |
|
98 @end group\n\ |
|
99 @end example\n\ |
|
100 \n\ |
4015
|
101 If the optional argument @var{dim} is supplied, work along dimension\n\ |
|
102 @var{dim}. For example,\n\ |
3369
|
103 \n\ |
|
104 @example\n\ |
4015
|
105 @group\n\ |
|
106 any (eye (2, 4), 2)\n\ |
|
107 @result{} [ 1; 1 ]\n\ |
|
108 @end group\n\ |
3369
|
109 @end example\n\ |
|
110 @end deftypefn") |
523
|
111 { |
4015
|
112 ANY_ALL (any); |
523
|
113 } |
|
114 |
649
|
115 // These mapping functions may also be useful in other places, eh? |
|
116 |
|
117 typedef double (*d_dd_fcn) (double, double); |
|
118 |
|
119 static Matrix |
2672
|
120 map_d_m (d_dd_fcn f, double x, const Matrix& y) |
649
|
121 { |
5275
|
122 octave_idx_type nr = y.rows (); |
|
123 octave_idx_type nc = y.columns (); |
649
|
124 |
|
125 Matrix retval (nr, nc); |
|
126 |
5275
|
127 for (octave_idx_type j = 0; j < nc; j++) |
|
128 for (octave_idx_type i = 0; i < nr; i++) |
4153
|
129 { |
|
130 OCTAVE_QUIT; |
|
131 retval (i, j) = f (x, y (i, j)); |
|
132 } |
649
|
133 |
|
134 return retval; |
|
135 } |
|
136 |
|
137 static Matrix |
2672
|
138 map_m_d (d_dd_fcn f, const Matrix& x, double y) |
649
|
139 { |
5275
|
140 octave_idx_type nr = x.rows (); |
|
141 octave_idx_type nc = x.columns (); |
649
|
142 |
|
143 Matrix retval (nr, nc); |
|
144 |
5275
|
145 for (octave_idx_type j = 0; j < nc; j++) |
|
146 for (octave_idx_type i = 0; i < nr; i++) |
4153
|
147 { |
|
148 OCTAVE_QUIT; |
|
149 retval (i, j) = f (x (i, j), y); |
|
150 } |
649
|
151 |
|
152 return retval; |
|
153 } |
|
154 |
|
155 static Matrix |
2672
|
156 map_m_m (d_dd_fcn f, const Matrix& x, const Matrix& y) |
649
|
157 { |
5275
|
158 octave_idx_type x_nr = x.rows (); |
|
159 octave_idx_type x_nc = x.columns (); |
649
|
160 |
5275
|
161 octave_idx_type y_nr = y.rows (); |
|
162 octave_idx_type y_nc = y.columns (); |
649
|
163 |
719
|
164 assert (x_nr == y_nr && x_nc == y_nc); |
649
|
165 |
|
166 Matrix retval (x_nr, x_nc); |
|
167 |
5275
|
168 for (octave_idx_type j = 0; j < x_nc; j++) |
|
169 for (octave_idx_type i = 0; i < x_nr; i++) |
4153
|
170 { |
|
171 OCTAVE_QUIT; |
|
172 retval (i, j) = f (x (i, j), y (i, j)); |
|
173 } |
649
|
174 |
|
175 return retval; |
|
176 } |
|
177 |
1957
|
178 DEFUN (atan2, args, , |
3428
|
179 "-*- texinfo -*-\n\ |
|
180 @deftypefn {Mapping Function} {} atan2 (@var{y}, @var{x})\n\ |
|
181 Compute atan (@var{y} / @var{x}) for corresponding elements of @var{y}\n\ |
|
182 and @var{x}. The result is in range -pi to pi.\n\ |
3439
|
183 @end deftypefn") |
649
|
184 { |
4233
|
185 octave_value retval; |
649
|
186 |
712
|
187 int nargin = args.length (); |
|
188 |
|
189 if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
649
|
190 { |
2086
|
191 octave_value arg_y = args(0); |
|
192 octave_value arg_x = args(1); |
649
|
193 |
5275
|
194 octave_idx_type y_nr = arg_y.rows (); |
|
195 octave_idx_type y_nc = arg_y.columns (); |
649
|
196 |
5275
|
197 octave_idx_type x_nr = arg_x.rows (); |
|
198 octave_idx_type x_nc = arg_x.columns (); |
649
|
199 |
|
200 int arg_y_empty = empty_arg ("atan2", y_nr, y_nc); |
|
201 int arg_x_empty = empty_arg ("atan2", x_nr, x_nc); |
|
202 |
719
|
203 if (arg_y_empty > 0 && arg_x_empty > 0) |
4233
|
204 return octave_value (Matrix ()); |
719
|
205 else if (arg_y_empty || arg_x_empty) |
649
|
206 return retval; |
|
207 |
5275
|
208 octave_idx_type y_is_scalar = (y_nr == 1 && y_nc == 1); |
|
209 octave_idx_type x_is_scalar = (x_nr == 1 && x_nc == 1); |
649
|
210 |
|
211 if (y_is_scalar && x_is_scalar) |
|
212 { |
|
213 double y = arg_y.double_value (); |
|
214 |
|
215 if (! error_state) |
|
216 { |
|
217 double x = arg_x.double_value (); |
|
218 |
|
219 if (! error_state) |
|
220 retval = atan2 (y, x); |
|
221 } |
|
222 } |
|
223 else if (y_is_scalar) |
|
224 { |
|
225 double y = arg_y.double_value (); |
|
226 |
|
227 if (! error_state) |
|
228 { |
|
229 Matrix x = arg_x.matrix_value (); |
|
230 |
|
231 if (! error_state) |
2672
|
232 retval = map_d_m (atan2, y, x); |
649
|
233 } |
|
234 } |
|
235 else if (x_is_scalar) |
|
236 { |
|
237 Matrix y = arg_y.matrix_value (); |
|
238 |
|
239 if (! error_state) |
|
240 { |
|
241 double x = arg_x.double_value (); |
|
242 |
|
243 if (! error_state) |
2672
|
244 retval = map_m_d (atan2, y, x); |
649
|
245 } |
|
246 } |
|
247 else if (y_nr == x_nr && y_nc == x_nc) |
|
248 { |
|
249 Matrix y = arg_y.matrix_value (); |
|
250 |
|
251 if (! error_state) |
|
252 { |
|
253 Matrix x = arg_x.matrix_value (); |
|
254 |
|
255 if (! error_state) |
2672
|
256 retval = map_m_m (atan2, y, x); |
649
|
257 } |
|
258 } |
|
259 else |
|
260 error ("atan2: nonconformant matrices"); |
|
261 } |
712
|
262 else |
|
263 print_usage ("atan2"); |
649
|
264 |
|
265 return retval; |
|
266 } |
|
267 |
4311
|
268 DEFUN (fmod, args, , |
|
269 "-*- texinfo -*-\n\ |
|
270 @deftypefn {Mapping Function} {} fmod (@var{x}, @var{y})\n\ |
4685
|
271 Compute the floating point remainder of dividing @var{x} by @var{y}\n\ |
|
272 using the C library function @code{fmod}. The result has the same\n\ |
|
273 sign as @var{x}. If @var{y} is zero, the result implementation-defined.\n\ |
4311
|
274 @end deftypefn") |
|
275 { |
|
276 octave_value retval; |
|
277 |
|
278 int nargin = args.length (); |
|
279 |
|
280 if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
|
281 { |
|
282 octave_value arg_x = args(0); |
|
283 octave_value arg_y = args(1); |
|
284 |
5275
|
285 octave_idx_type y_nr = arg_y.rows (); |
|
286 octave_idx_type y_nc = arg_y.columns (); |
4311
|
287 |
5275
|
288 octave_idx_type x_nr = arg_x.rows (); |
|
289 octave_idx_type x_nc = arg_x.columns (); |
4311
|
290 |
|
291 int arg_y_empty = empty_arg ("fmod", y_nr, y_nc); |
|
292 int arg_x_empty = empty_arg ("fmod", x_nr, x_nc); |
|
293 |
|
294 if (arg_y_empty > 0 && arg_x_empty > 0) |
|
295 return octave_value (Matrix ()); |
|
296 else if (arg_y_empty || arg_x_empty) |
|
297 return retval; |
|
298 |
5275
|
299 octave_idx_type y_is_scalar = (y_nr == 1 && y_nc == 1); |
|
300 octave_idx_type x_is_scalar = (x_nr == 1 && x_nc == 1); |
4311
|
301 |
|
302 if (y_is_scalar && x_is_scalar) |
|
303 { |
|
304 double y = arg_y.double_value (); |
|
305 |
|
306 if (! error_state) |
|
307 { |
|
308 double x = arg_x.double_value (); |
|
309 |
|
310 if (! error_state) |
|
311 retval = fmod (x, y); |
|
312 } |
|
313 } |
|
314 else if (y_is_scalar) |
|
315 { |
|
316 double y = arg_y.double_value (); |
|
317 |
|
318 if (! error_state) |
|
319 { |
|
320 Matrix x = arg_x.matrix_value (); |
|
321 |
|
322 if (! error_state) |
|
323 retval = map_m_d (fmod, x, y); |
|
324 } |
|
325 } |
|
326 else if (x_is_scalar) |
|
327 { |
|
328 Matrix y = arg_y.matrix_value (); |
|
329 |
|
330 if (! error_state) |
|
331 { |
|
332 double x = arg_x.double_value (); |
|
333 |
|
334 if (! error_state) |
|
335 retval = map_d_m (fmod, x, y); |
|
336 } |
|
337 } |
|
338 else if (y_nr == x_nr && y_nc == x_nc) |
|
339 { |
|
340 Matrix y = arg_y.matrix_value (); |
|
341 |
|
342 if (! error_state) |
|
343 { |
|
344 Matrix x = arg_x.matrix_value (); |
|
345 |
|
346 if (! error_state) |
|
347 retval = map_m_m (fmod, x, y); |
|
348 } |
|
349 } |
|
350 else |
|
351 error ("fmod: nonconformant matrices"); |
|
352 } |
|
353 else |
|
354 print_usage ("fmod"); |
|
355 |
|
356 return retval; |
|
357 } |
|
358 |
3723
|
359 #define DATA_REDUCTION(FCN) \ |
|
360 \ |
4233
|
361 octave_value retval; \ |
3723
|
362 \ |
|
363 int nargin = args.length (); \ |
|
364 \ |
|
365 if (nargin == 1 || nargin == 2) \ |
|
366 { \ |
|
367 octave_value arg = args(0); \ |
|
368 \ |
3864
|
369 int dim = (nargin == 1 ? -1 : args(1).int_value (true) - 1); \ |
3723
|
370 \ |
|
371 if (! error_state) \ |
|
372 { \ |
4556
|
373 if (dim >= -1) \ |
3723
|
374 { \ |
|
375 if (arg.is_real_type ()) \ |
|
376 { \ |
4569
|
377 NDArray tmp = arg.array_value (); \ |
3723
|
378 \ |
|
379 if (! error_state) \ |
4233
|
380 retval = tmp.FCN (dim); \ |
3723
|
381 } \ |
|
382 else if (arg.is_complex_type ()) \ |
|
383 { \ |
4569
|
384 ComplexNDArray tmp = arg.complex_array_value (); \ |
3723
|
385 \ |
|
386 if (! error_state) \ |
4233
|
387 retval = tmp.FCN (dim); \ |
3723
|
388 } \ |
|
389 else \ |
|
390 { \ |
|
391 gripe_wrong_type_arg (#FCN, arg); \ |
|
392 return retval; \ |
|
393 } \ |
|
394 } \ |
|
395 else \ |
|
396 error (#FCN ": invalid dimension argument = %d", dim + 1); \ |
|
397 } \ |
|
398 } \ |
|
399 else \ |
|
400 print_usage (#FCN); \ |
|
401 \ |
|
402 return retval |
|
403 |
1957
|
404 DEFUN (cumprod, args, , |
3428
|
405 "-*- texinfo -*-\n\ |
3723
|
406 @deftypefn {Built-in Function} {} cumprod (@var{x}, @var{dim})\n\ |
|
407 Cumulative product of elements along dimension @var{dim}. If\n\ |
|
408 @var{dim} is omitted, it defaults to 1 (column-wise cumulative\n\ |
|
409 products).\n\ |
5061
|
410 \n\ |
|
411 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ |
|
412 return the cumulative product of the elements as a vector with the\n\ |
|
413 same orientation as @var{x}.\n\ |
3428
|
414 @end deftypefn") |
523
|
415 { |
3723
|
416 DATA_REDUCTION (cumprod); |
523
|
417 } |
|
418 |
1957
|
419 DEFUN (cumsum, args, , |
3428
|
420 "-*- texinfo -*-\n\ |
3723
|
421 @deftypefn {Built-in Function} {} cumsum (@var{x}, @var{dim})\n\ |
|
422 Cumulative sum of elements along dimension @var{dim}. If @var{dim}\n\ |
|
423 is omitted, it defaults to 1 (column-wise cumulative sums).\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 sum 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 (cumsum); |
523
|
431 } |
|
432 |
3972
|
433 // XXX FIXME XXX -- we could eliminate some duplicate code here with |
|
434 // some template functions or macros. |
|
435 |
2086
|
436 static octave_value |
5275
|
437 make_diag (const Matrix& v, octave_idx_type k) |
767
|
438 { |
5275
|
439 octave_idx_type nr = v.rows (); |
|
440 octave_idx_type nc = v.columns (); |
767
|
441 assert (nc == 1 || nr == 1); |
|
442 |
2086
|
443 octave_value retval; |
767
|
444 |
5275
|
445 octave_idx_type roff = 0; |
|
446 octave_idx_type coff = 0; |
767
|
447 if (k > 0) |
|
448 { |
|
449 roff = 0; |
|
450 coff = k; |
|
451 } |
|
452 else if (k < 0) |
|
453 { |
|
454 roff = -k; |
|
455 coff = 0; |
|
456 } |
|
457 |
|
458 if (nr == 1) |
|
459 { |
5275
|
460 octave_idx_type n = nc + std::abs (k); |
767
|
461 Matrix m (n, n, 0.0); |
5275
|
462 for (octave_idx_type i = 0; i < nc; i++) |
2305
|
463 m (i+roff, i+coff) = v (0, i); |
4233
|
464 retval = m; |
767
|
465 } |
|
466 else |
|
467 { |
5275
|
468 octave_idx_type n = nr + std::abs (k); |
767
|
469 Matrix m (n, n, 0.0); |
5275
|
470 for (octave_idx_type i = 0; i < nr; i++) |
2305
|
471 m (i+roff, i+coff) = v (i, 0); |
4233
|
472 retval = m; |
767
|
473 } |
|
474 |
|
475 return retval; |
|
476 } |
|
477 |
2086
|
478 static octave_value |
5275
|
479 make_diag (const ComplexMatrix& v, octave_idx_type k) |
767
|
480 { |
5275
|
481 octave_idx_type nr = v.rows (); |
|
482 octave_idx_type nc = v.columns (); |
767
|
483 assert (nc == 1 || nr == 1); |
|
484 |
2086
|
485 octave_value retval; |
767
|
486 |
5275
|
487 octave_idx_type roff = 0; |
|
488 octave_idx_type coff = 0; |
767
|
489 if (k > 0) |
|
490 { |
|
491 roff = 0; |
|
492 coff = k; |
|
493 } |
|
494 else if (k < 0) |
|
495 { |
|
496 roff = -k; |
|
497 coff = 0; |
|
498 } |
|
499 |
|
500 if (nr == 1) |
|
501 { |
5275
|
502 octave_idx_type n = nc + std::abs (k); |
767
|
503 ComplexMatrix m (n, n, 0.0); |
5275
|
504 for (octave_idx_type i = 0; i < nc; i++) |
2305
|
505 m (i+roff, i+coff) = v (0, i); |
4233
|
506 retval = m; |
767
|
507 } |
|
508 else |
|
509 { |
5275
|
510 octave_idx_type n = nr + std::abs (k); |
767
|
511 ComplexMatrix m (n, n, 0.0); |
5275
|
512 for (octave_idx_type i = 0; i < nr; i++) |
2305
|
513 m (i+roff, i+coff) = v (i, 0); |
4233
|
514 retval = m; |
767
|
515 } |
|
516 |
|
517 return retval; |
|
518 } |
|
519 |
2086
|
520 static octave_value |
|
521 make_diag (const octave_value& arg) |
767
|
522 { |
2086
|
523 octave_value retval; |
767
|
524 |
|
525 if (arg.is_real_type ()) |
|
526 { |
|
527 Matrix m = arg.matrix_value (); |
|
528 |
|
529 if (! error_state) |
|
530 { |
5275
|
531 octave_idx_type nr = m.rows (); |
|
532 octave_idx_type nc = m.columns (); |
767
|
533 |
|
534 if (nr == 0 || nc == 0) |
|
535 retval = Matrix (); |
|
536 else if (nr == 1 || nc == 1) |
|
537 retval = make_diag (m, 0); |
|
538 else |
|
539 { |
|
540 ColumnVector v = m.diag (); |
5164
|
541 if (v.numel () > 0) |
767
|
542 retval = v; |
|
543 } |
|
544 } |
|
545 else |
|
546 gripe_wrong_type_arg ("diag", arg); |
|
547 } |
|
548 else if (arg.is_complex_type ()) |
|
549 { |
|
550 ComplexMatrix cm = arg.complex_matrix_value (); |
|
551 |
|
552 if (! error_state) |
|
553 { |
5275
|
554 octave_idx_type nr = cm.rows (); |
|
555 octave_idx_type nc = cm.columns (); |
767
|
556 |
|
557 if (nr == 0 || nc == 0) |
|
558 retval = Matrix (); |
|
559 else if (nr == 1 || nc == 1) |
|
560 retval = make_diag (cm, 0); |
|
561 else |
|
562 { |
|
563 ComplexColumnVector v = cm.diag (); |
5164
|
564 if (v.numel () > 0) |
767
|
565 retval = v; |
|
566 } |
|
567 } |
|
568 else |
|
569 gripe_wrong_type_arg ("diag", arg); |
|
570 } |
|
571 else |
|
572 gripe_wrong_type_arg ("diag", arg); |
|
573 |
|
574 return retval; |
|
575 } |
|
576 |
2086
|
577 static octave_value |
|
578 make_diag (const octave_value& a, const octave_value& b) |
767
|
579 { |
2086
|
580 octave_value retval; |
767
|
581 |
5275
|
582 octave_idx_type k = b.int_value (); |
767
|
583 |
|
584 if (error_state) |
|
585 { |
|
586 error ("diag: invalid second argument"); |
|
587 return retval; |
|
588 } |
|
589 |
|
590 if (a.is_real_type ()) |
|
591 { |
3307
|
592 Matrix m = a.matrix_value (); |
767
|
593 |
3307
|
594 if (! error_state) |
767
|
595 { |
5275
|
596 octave_idx_type nr = m.rows (); |
|
597 octave_idx_type nc = m.columns (); |
767
|
598 |
3972
|
599 if (nr == 1 || nc == 1) |
|
600 retval = make_diag (m, k); |
|
601 else if (nr == 0 || nc == 0) |
767
|
602 retval = Matrix (); |
|
603 else |
|
604 { |
|
605 ColumnVector d = m.diag (k); |
|
606 retval = d; |
|
607 } |
|
608 } |
|
609 } |
|
610 else if (a.is_complex_type ()) |
|
611 { |
3307
|
612 ComplexMatrix cm = a.complex_matrix_value (); |
767
|
613 |
3307
|
614 if (! error_state) |
767
|
615 { |
5275
|
616 octave_idx_type nr = cm.rows (); |
|
617 octave_idx_type nc = cm.columns (); |
767
|
618 |
3972
|
619 if (nr == 1 || nc == 1) |
|
620 retval = make_diag (cm, k); |
|
621 else if (nr == 0 || nc == 0) |
767
|
622 retval = Matrix (); |
|
623 else |
|
624 { |
|
625 ComplexColumnVector d = cm.diag (k); |
|
626 retval = d; |
|
627 } |
|
628 } |
|
629 } |
|
630 else |
|
631 gripe_wrong_type_arg ("diag", a); |
|
632 |
|
633 return retval; |
|
634 } |
|
635 |
1957
|
636 DEFUN (diag, args, , |
3369
|
637 "-*- texinfo -*-\n\ |
|
638 @deftypefn {Built-in Function} {} diag (@var{v}, @var{k})\n\ |
|
639 Return a diagonal matrix with vector @var{v} on diagonal @var{k}. The\n\ |
|
640 second argument is optional. If it is positive, the vector is placed on\n\ |
|
641 the @var{k}-th super-diagonal. If it is negative, it is placed on the\n\ |
|
642 @var{-k}-th sub-diagonal. The default value of @var{k} is 0, and the\n\ |
|
643 vector is placed on the main diagonal. For example,\n\ |
|
644 \n\ |
|
645 @example\n\ |
|
646 @group\n\ |
|
647 diag ([1, 2, 3], 1)\n\ |
|
648 @result{} 0 1 0 0\n\ |
|
649 0 0 2 0\n\ |
|
650 0 0 0 3\n\ |
|
651 0 0 0 0\n\ |
|
652 @end group\n\ |
|
653 @end example\n\ |
|
654 @end deftypefn") |
523
|
655 { |
4233
|
656 octave_value retval; |
523
|
657 |
|
658 int nargin = args.length (); |
|
659 |
712
|
660 if (nargin == 1 && args(0).is_defined ()) |
767
|
661 retval = make_diag (args(0)); |
712
|
662 else if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
767
|
663 retval = make_diag (args(0), args(1)); |
523
|
664 else |
|
665 print_usage ("diag"); |
|
666 |
|
667 return retval; |
|
668 } |
|
669 |
1957
|
670 DEFUN (prod, args, , |
3428
|
671 "-*- texinfo -*-\n\ |
3723
|
672 @deftypefn {Built-in Function} {} prod (@var{x}, @var{dim})\n\ |
|
673 Product of elements along dimension @var{dim}. If @var{dim} is\n\ |
|
674 omitted, it defaults to 1 (column-wise products).\n\ |
5061
|
675 \n\ |
|
676 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ |
|
677 return the product of the elements.\n\ |
3428
|
678 @end deftypefn") |
523
|
679 { |
3723
|
680 DATA_REDUCTION (prod); |
523
|
681 } |
|
682 |
4824
|
683 static octave_value |
|
684 do_cat (const octave_value_list& args, std::string fname) |
4806
|
685 { |
|
686 octave_value retval; |
|
687 |
4824
|
688 int n_args = args.length (); |
4806
|
689 |
4824
|
690 if (n_args > 2) |
|
691 { |
5275
|
692 octave_idx_type dim = args(0).int_value () - 1; |
4806
|
693 |
4824
|
694 if (error_state) |
4806
|
695 { |
4824
|
696 error ("cat: expecting first argument to be a integer"); |
4806
|
697 return retval; |
|
698 } |
|
699 |
4824
|
700 if (dim >= 0) |
|
701 { |
4915
|
702 |
|
703 dim_vector dv = args(1).dims (); |
4824
|
704 |
4915
|
705 for (int i = 2; i < args.length (); i++) |
|
706 { |
|
707 // add_dims constructs a dimension vector which holds the |
4824
|
708 // dimensions of the final array after concatenation. |
4806
|
709 |
4915
|
710 if (! dv.concat (args(i).dims (), dim)) |
4806
|
711 { |
4824
|
712 // Dimensions do not match. |
4915
|
713 error ("cat: dimension mismatch"); |
4806
|
714 return retval; |
|
715 } |
4824
|
716 } |
|
717 |
4915
|
718 // The lines below might seem crazy, since we take a copy |
|
719 // of the first argument, resize it to be empty and then resize |
|
720 // it to be full. This is done since it means that there is no |
|
721 // recopying of data, as would happen if we used a single resize. |
|
722 // It should be noted that resize operation is also significantly |
|
723 // slower than the do_cat_op function, so it makes sense to have an |
|
724 // empty matrix and copy all data. |
4824
|
725 // |
4915
|
726 // We might also start with a empty octave_value using |
|
727 // tmp = octave_value_typeinfo::lookup_type (args(1).type_name()); |
|
728 // and then directly resize. However, for some types there might be |
|
729 // some additional setup needed, and so this should be avoided. |
|
730 octave_value tmp; |
|
731 bool any_strings = false; |
|
732 bool all_strings = true; |
5164
|
733 bool first_non_empty_arg = true; |
4915
|
734 for (int i = 1; i < n_args; i++) |
5164
|
735 if (! args (i).all_zero_dims ()) |
|
736 { |
|
737 if (first_non_empty_arg) |
|
738 { |
|
739 first_non_empty_arg = false; |
|
740 tmp = args (i); |
|
741 } |
|
742 |
|
743 if (args(i).is_string ()) |
|
744 any_strings = true; |
|
745 else |
|
746 all_strings = false; |
|
747 } |
|
748 |
4915
|
749 if (all_strings) |
|
750 tmp = octave_value (charNDArray (dv, Vstring_fill_char), true); |
|
751 else |
5164
|
752 tmp = tmp.resize (dim_vector (0,0)).resize (dv); |
4824
|
753 |
4915
|
754 if (error_state) |
|
755 return retval; |
4824
|
756 |
4915
|
757 Array<int> ra_idx (dv.length (), 0); |
|
758 for (int i = 1; i < n_args; i++) |
|
759 { |
|
760 tmp = do_cat_op (tmp, args (i), ra_idx); |
4824
|
761 |
4915
|
762 if (error_state) |
|
763 return retval; |
4806
|
764 |
4915
|
765 dim_vector dv_tmp = args (i).dims (); |
|
766 ra_idx (dim) += (dim < dv_tmp.length () ? dv_tmp (dim) : 1); |
|
767 } |
4806
|
768 |
4915
|
769 if (any_strings && !all_strings) |
|
770 retval = tmp.convert_to_str (); |
|
771 else |
|
772 retval = tmp; |
4806
|
773 } |
4824
|
774 else print_usage (fname); |
4806
|
775 } |
|
776 else |
4824
|
777 print_usage (fname); |
4806
|
778 |
|
779 return retval; |
|
780 } |
|
781 |
|
782 DEFUN (horzcat, args, , |
4824
|
783 "-*- texinfo -*-\n\ |
4806
|
784 @deftypefn {Built-in Function} {} horzcat (@var{array1}, @var{array2}, @dots{}, @var{arrayN})\n\ |
|
785 Return the horizontal concatenation of N-d array objects, @var{array1},\n\ |
|
786 @var{array2}, @dots{}, @var{arrayN} along dimension 2.\n\ |
|
787 @end deftypefn\n\ |
|
788 @seealso{cat and vertcat}") |
|
789 { |
|
790 octave_value_list args_tmp = args; |
|
791 |
|
792 int dim = 2; |
|
793 |
|
794 octave_value d (dim); |
|
795 |
|
796 args_tmp.prepend (d); |
|
797 |
4824
|
798 return do_cat (args_tmp, "horzcat"); |
4806
|
799 } |
|
800 |
|
801 DEFUN (vertcat, args, , |
|
802 "-*- texinfo -*-\n\ |
|
803 @deftypefn {Built-in Function} {} vertcat (@var{array1}, @var{array2}, @dots{}, @var{arrayN})\n\ |
|
804 Return the vertical concatenation of N-d array objects, @var{array1},\n\ |
|
805 @var{array2}, @dots{}, @var{arrayN} along dimension 1.\n\ |
|
806 @end deftypefn\n\ |
|
807 @seealso{cat and horzcat}") |
|
808 { |
|
809 octave_value_list args_tmp = args; |
|
810 |
|
811 int dim = 1; |
|
812 |
|
813 octave_value d (dim); |
|
814 |
|
815 args_tmp.prepend (d); |
|
816 |
4824
|
817 return do_cat (args_tmp, "vertcat"); |
4806
|
818 } |
|
819 |
4758
|
820 DEFUN (cat, args, , |
|
821 "-*- texinfo -*-\n\ |
|
822 @deftypefn {Built-in Function} {} cat (@var{dim}, @var{array1}, @var{array2}, @dots{}, @var{arrayN})\n\ |
4806
|
823 Return the concatenation of N-d array objects, @var{array1},\n\ |
|
824 @var{array2}, @dots{}, @var{arrayN} along dimension @var{dim}.\n\ |
4758
|
825 \n\ |
|
826 @example\n\ |
|
827 @group\n\ |
|
828 A = ones (2, 2);\n\ |
|
829 B = zeros (2, 2);\n\ |
|
830 cat (2, A, B)\n\ |
|
831 @result{} ans =\n\ |
|
832 \n\ |
|
833 1 1 0 0\n\ |
|
834 1 1 0 0\n\ |
|
835 @end group\n\ |
|
836 @end example\n\ |
|
837 \n\ |
|
838 Alternatively, we can concatenate @var{A} and @var{B} along the\n\ |
|
839 second dimension the following way:\n\ |
|
840 \n\ |
|
841 @example\n\ |
|
842 @group\n\ |
|
843 [A, B].\n\ |
|
844 @end group\n\ |
|
845 @end example\n\ |
|
846 \n\ |
|
847 @var{dim} can be larger than the dimensions of the N-d array objects\n\ |
|
848 and the result will thus have @var{dim} dimensions as the\n\ |
|
849 following example shows:\n\ |
|
850 @example\n\ |
|
851 @group\n\ |
|
852 cat (4, ones(2, 2), zeros (2, 2))\n\ |
|
853 @result{} ans =\n\ |
|
854 \n\ |
|
855 ans(:,:,1,1) =\n\ |
|
856 \n\ |
|
857 1 1\n\ |
|
858 1 1\n\ |
|
859 \n\ |
|
860 ans(:,:,1,2) =\n\ |
|
861 0 0\n\ |
|
862 0 0\n\ |
|
863 @end group\n\ |
|
864 @end example\n\ |
|
865 \n\ |
4806
|
866 @end deftypefn\n\ |
|
867 @seealso{horzcat and vertcat}") |
4758
|
868 { |
4824
|
869 return do_cat (args, "cat"); |
4758
|
870 } |
|
871 |
4593
|
872 static octave_value |
|
873 do_permute (const octave_value_list& args, bool inv, const std::string& fname) |
|
874 { |
|
875 octave_value retval; |
|
876 |
5148
|
877 if (args.length () == 2 && args(1).length () >= args(1).ndims ()) |
4593
|
878 { |
|
879 Array<int> vec = args(1).int_vector_value (); |
|
880 |
5148
|
881 // XXX FIXME XXX -- maybe we shoudl create an idx_vector object |
|
882 // here and pass that to permute? |
|
883 |
|
884 int n = vec.length (); |
|
885 |
|
886 for (int i = 0; i < n; i++) |
|
887 vec(i)--; |
|
888 |
4593
|
889 octave_value ret = args(0).permute (vec, inv); |
|
890 |
|
891 if (! error_state) |
|
892 retval = ret; |
|
893 } |
|
894 else |
|
895 print_usage (fname); |
|
896 |
|
897 return retval; |
|
898 } |
|
899 |
|
900 DEFUN (permute, args, , |
|
901 "-*- texinfo -*-\n\ |
|
902 @deftypefn {Built-in Function} {} permute (@var{a}, @var{perm})\n\ |
|
903 Return the generalized transpose for an N-d array object @var{a}.\n\ |
|
904 The permutation vector @var{perm} must contain the elements\n\ |
|
905 @code{1:ndims(a)} (in any order, but each element must appear just once).\n\ |
|
906 \n\ |
|
907 @end deftypefn\n\ |
|
908 @seealso{ipermute}") |
|
909 { |
|
910 return do_permute (args, false, "permute"); |
|
911 } |
|
912 |
|
913 DEFUN (ipermute, args, , |
|
914 "-*- texinfo -*-\n\ |
|
915 @deftypefn {Built-in Function} {} ipermute (@var{a}, @var{iperm})\n\ |
|
916 The inverse of the @code{permute} function. The expression\n\ |
|
917 \n\ |
|
918 @example\n\ |
|
919 ipermute (permute (a, perm), perm)\n\ |
|
920 @end example\n\ |
|
921 returns the original array @var{a}.\n\ |
|
922 \n\ |
|
923 @end deftypefn\n\ |
|
924 @seealso{permute}") |
|
925 { |
|
926 return do_permute (args, true, "ipermute"); |
|
927 } |
|
928 |
3195
|
929 DEFUN (length, args, , |
3373
|
930 "-*- texinfo -*-\n\ |
|
931 @deftypefn {Built-in Function} {} length (@var{a})\n\ |
4176
|
932 Return the `length' of the object @var{a}. For matrix objects, the\n\ |
3373
|
933 length is the number of rows or columns, whichever is greater (this\n\ |
|
934 odd definition is used for compatibility with Matlab).\n\ |
|
935 @end deftypefn") |
3195
|
936 { |
|
937 octave_value retval; |
|
938 |
|
939 if (args.length () == 1) |
|
940 { |
|
941 int len = args(0).length (); |
|
942 |
|
943 if (! error_state) |
4233
|
944 retval = len; |
3195
|
945 } |
|
946 else |
|
947 print_usage ("length"); |
|
948 |
|
949 return retval; |
|
950 } |
|
951 |
4554
|
952 DEFUN (ndims, args, , |
|
953 "-*- texinfo -*-\n\ |
|
954 @deftypefn {Built-in Function} {} ndims (@var{a})\n\ |
|
955 Returns the number of dimensions of array @var{a}.\n\ |
|
956 For any array, the result will always be larger than or equal to 2.\n\ |
|
957 Trailing singleton dimensions are not counted.\n\ |
|
958 @end deftypefn") |
|
959 { |
|
960 octave_value retval; |
|
961 |
|
962 if (args.length () == 1) |
|
963 { |
|
964 int n_dims = args(0).ndims (); |
|
965 |
|
966 if (! error_state) |
|
967 retval = n_dims; |
|
968 } |
|
969 else |
|
970 print_usage ("ndims"); |
|
971 |
|
972 return retval; |
|
973 } |
|
974 |
4559
|
975 DEFUN (numel, args, , |
|
976 "-*- texinfo -*-\n\ |
|
977 @deftypefn {Built-in Function} {} numel (@var{a})\n\ |
|
978 Returns the number of elements in the object @var{a}.\n\ |
|
979 @end deftypefn") |
|
980 { |
|
981 octave_value retval; |
|
982 |
|
983 if (args.length () == 1) |
|
984 { |
|
985 int numel = args(0).numel (); |
|
986 |
|
987 if (! error_state) |
|
988 { |
|
989 if (numel < 0) |
|
990 numel = 0; |
|
991 |
|
992 retval = numel; |
|
993 } |
|
994 } |
|
995 else |
|
996 print_usage ("numel"); |
|
997 |
|
998 return retval; |
|
999 } |
|
1000 |
1957
|
1001 DEFUN (size, args, nargout, |
3373
|
1002 "-*- texinfo -*-\n\ |
|
1003 @deftypefn {Built-in Function} {} size (@var{a}, @var{n})\n\ |
|
1004 Return the number rows and columns of @var{a}.\n\ |
|
1005 \n\ |
|
1006 With one input argument and one output argument, the result is returned\n\ |
4741
|
1007 in a row vector. If there are multiple output arguments, the number of\n\ |
|
1008 rows is assigned to the first, and the number of columns to the second,\n\ |
|
1009 etc. For example,\n\ |
3373
|
1010 \n\ |
|
1011 @example\n\ |
|
1012 @group\n\ |
|
1013 size ([1, 2; 3, 4; 5, 6])\n\ |
|
1014 @result{} [ 3, 2 ]\n\ |
1031
|
1015 \n\ |
3373
|
1016 [nr, nc] = size ([1, 2; 3, 4; 5, 6])\n\ |
|
1017 @result{} nr = 3\n\ |
|
1018 @result{} nc = 2\n\ |
|
1019 @end group\n\ |
|
1020 @end example\n\ |
|
1021 \n\ |
4741
|
1022 If given a second argument, @code{size} will return the size of the\n\ |
|
1023 corresponding dimension. For example\n\ |
1031
|
1024 \n\ |
3373
|
1025 @example\n\ |
|
1026 size ([1, 2; 3, 4; 5, 6], 2)\n\ |
|
1027 @result{} 2\n\ |
|
1028 @end example\n\ |
|
1029 \n\ |
|
1030 @noindent\n\ |
|
1031 returns the number of columns in the given matrix.\n\ |
|
1032 @end deftypefn") |
523
|
1033 { |
2086
|
1034 octave_value_list retval; |
523
|
1035 |
|
1036 int nargin = args.length (); |
|
1037 |
4513
|
1038 if (nargin == 1) |
523
|
1039 { |
4513
|
1040 dim_vector dimensions = args(0).dims (); |
|
1041 |
|
1042 int ndims = dimensions.length (); |
1031
|
1043 |
4513
|
1044 Matrix m (1, ndims); |
|
1045 |
|
1046 if (nargout > 1) |
523
|
1047 { |
4513
|
1048 while (ndims--) |
|
1049 retval(ndims) = dimensions(ndims); |
523
|
1050 } |
4513
|
1051 else |
712
|
1052 { |
4513
|
1053 for (int i = 0; i < ndims; i++) |
|
1054 m(0, i) = dimensions(i); |
|
1055 |
|
1056 retval(0) = m; |
712
|
1057 } |
1031
|
1058 } |
|
1059 else if (nargin == 2 && nargout < 2) |
|
1060 { |
5275
|
1061 octave_idx_type nd = args(1).int_value (true); |
1031
|
1062 |
|
1063 if (error_state) |
|
1064 error ("size: expecting scalar as second argument"); |
712
|
1065 else |
1031
|
1066 { |
4741
|
1067 dim_vector dv = args(0).dims (); |
|
1068 |
4911
|
1069 if (nd > 0) |
|
1070 { |
|
1071 if (nd <= dv.length ()) |
|
1072 retval(0) = dv(nd-1); |
|
1073 else |
|
1074 retval(0) = 1; |
|
1075 } |
1031
|
1076 else |
4741
|
1077 error ("size: requested dimension (= %d) out of range", nd); |
1031
|
1078 } |
523
|
1079 } |
712
|
1080 else |
|
1081 print_usage ("size"); |
523
|
1082 |
|
1083 return retval; |
|
1084 } |
|
1085 |
1957
|
1086 DEFUN (sum, args, , |
3428
|
1087 "-*- texinfo -*-\n\ |
3723
|
1088 @deftypefn {Built-in Function} {} sum (@var{x}, @var{dim})\n\ |
|
1089 Sum of elements along dimension @var{dim}. If @var{dim} is\n\ |
|
1090 omitted, it defaults to 1 (column-wise sum).\n\ |
5061
|
1091 \n\ |
|
1092 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ |
|
1093 return the sum of the elements.\n\ |
3428
|
1094 @end deftypefn") |
523
|
1095 { |
3723
|
1096 DATA_REDUCTION (sum); |
523
|
1097 } |
|
1098 |
1957
|
1099 DEFUN (sumsq, args, , |
3428
|
1100 "-*- texinfo -*-\n\ |
3723
|
1101 @deftypefn {Built-in Function} {} sumsq (@var{x}, @var{dim})\n\ |
|
1102 Sum of squares of elements along dimension @var{dim}. If @var{dim}\n\ |
|
1103 is omitted, it defaults to 1 (column-wise sum of squares).\n\ |
3095
|
1104 \n\ |
5061
|
1105 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ |
|
1106 return the sum of squares of the elements.\n\ |
|
1107 \n\ |
|
1108 This function is conceptually equivalent to computing\n\ |
3723
|
1109 @example\n\ |
|
1110 sum (x .* conj (x), dim)\n\ |
|
1111 @end example\n\ |
|
1112 but it uses less memory and avoids calling conj if @var{x} is real.\n\ |
3428
|
1113 @end deftypefn") |
523
|
1114 { |
3723
|
1115 DATA_REDUCTION (sumsq); |
523
|
1116 } |
|
1117 |
4028
|
1118 DEFUN (isbool, args, , |
3428
|
1119 "-*- texinfo -*-\n\ |
4028
|
1120 @deftypefn {Built-in Functio} {} isbool (@var{x})\n\ |
3428
|
1121 Return true if @var{x} is a boolean object.\n\ |
3439
|
1122 @end deftypefn") |
3209
|
1123 { |
|
1124 octave_value retval; |
|
1125 |
|
1126 if (args.length () == 1) |
3258
|
1127 retval = args(0).is_bool_type (); |
3209
|
1128 else |
4028
|
1129 print_usage ("isbool"); |
3209
|
1130 |
|
1131 return retval; |
|
1132 } |
|
1133 |
4028
|
1134 DEFALIAS (islogical, isbool); |
3209
|
1135 |
4028
|
1136 DEFUN (iscomplex, args, , |
3428
|
1137 "-*- texinfo -*-\n\ |
4028
|
1138 @deftypefn {Built-in Function} {} iscomplex (@var{x})\n\ |
3428
|
1139 Return true if @var{x} is a complex-valued numeric object.\n\ |
|
1140 @end deftypefn") |
3186
|
1141 { |
|
1142 octave_value retval; |
|
1143 |
|
1144 if (args.length () == 1) |
3258
|
1145 retval = args(0).is_complex_type (); |
3186
|
1146 else |
4028
|
1147 print_usage ("iscomplex"); |
3186
|
1148 |
|
1149 return retval; |
|
1150 } |
|
1151 |
3258
|
1152 DEFUN (isreal, args, , |
3428
|
1153 "-*- texinfo -*-\n\ |
|
1154 @deftypefn {Built-in Function} {} isreal (@var{x})\n\ |
|
1155 Return true if @var{x} is a real-valued numeric object.\n\ |
|
1156 @end deftypefn") |
3258
|
1157 { |
|
1158 octave_value retval; |
|
1159 |
|
1160 if (args.length () == 1) |
|
1161 retval = args(0).is_real_type (); |
|
1162 else |
|
1163 print_usage ("isreal"); |
|
1164 |
|
1165 return retval; |
|
1166 } |
|
1167 |
3202
|
1168 DEFUN (isempty, args, , |
3373
|
1169 "-*- texinfo -*-\n\ |
|
1170 @deftypefn {Built-in Function} {} isempty (@var{a})\n\ |
|
1171 Return 1 if @var{a} is an empty matrix (either the number of rows, or\n\ |
|
1172 the number of columns, or both are zero). Otherwise, return 0.\n\ |
|
1173 @end deftypefn") |
3202
|
1174 { |
4233
|
1175 octave_value retval = false; |
3202
|
1176 |
|
1177 if (args.length () == 1) |
4559
|
1178 retval = args(0).is_empty (); |
3202
|
1179 else |
|
1180 print_usage ("isempty"); |
|
1181 |
|
1182 return retval; |
|
1183 } |
|
1184 |
3206
|
1185 DEFUN (isnumeric, args, , |
3428
|
1186 "-*- texinfo -*-\n\ |
|
1187 @deftypefn {Built-in Function} {} isnumeric (@var{x})\n\ |
|
1188 Return nonzero if @var{x} is a numeric object.\n\ |
|
1189 @end deftypefn") |
3206
|
1190 { |
|
1191 octave_value retval; |
|
1192 |
|
1193 if (args.length () == 1) |
3258
|
1194 retval = args(0).is_numeric_type (); |
3206
|
1195 else |
3238
|
1196 print_usage ("isnumeric"); |
3206
|
1197 |
|
1198 return retval; |
|
1199 } |
|
1200 |
4028
|
1201 DEFUN (islist, args, , |
3526
|
1202 "-*- texinfo -*-\n\ |
4028
|
1203 @deftypefn {Built-in Function} {} islist (@var{x})\n\ |
3428
|
1204 Return nonzero if @var{x} is a list.\n\ |
|
1205 @end deftypefn") |
3204
|
1206 { |
|
1207 octave_value retval; |
|
1208 |
|
1209 if (args.length () == 1) |
3258
|
1210 retval = args(0).is_list (); |
3204
|
1211 else |
4028
|
1212 print_usage ("islist"); |
3204
|
1213 |
|
1214 return retval; |
|
1215 } |
|
1216 |
4028
|
1217 DEFUN (ismatrix, args, , |
3321
|
1218 "-*- texinfo -*-\n\ |
4028
|
1219 @deftypefn {Built-in Function} {} ismatrix (@var{a})\n\ |
3321
|
1220 Return 1 if @var{a} is a matrix. Otherwise, return 0.\n\ |
3333
|
1221 @end deftypefn") |
3202
|
1222 { |
4233
|
1223 octave_value retval = false; |
3202
|
1224 |
|
1225 if (args.length () == 1) |
|
1226 { |
|
1227 octave_value arg = args(0); |
|
1228 |
3212
|
1229 if (arg.is_scalar_type () || arg.is_range ()) |
4233
|
1230 retval = true; |
3202
|
1231 else if (arg.is_matrix_type ()) |
4233
|
1232 retval = (arg.rows () >= 1 && arg.columns () >= 1); |
3202
|
1233 } |
|
1234 else |
4028
|
1235 print_usage ("ismatrix"); |
3202
|
1236 |
|
1237 return retval; |
|
1238 } |
|
1239 |
3354
|
1240 static octave_value |
|
1241 fill_matrix (const octave_value_list& args, double val, const char *fcn) |
523
|
1242 { |
3354
|
1243 octave_value retval; |
523
|
1244 |
|
1245 int nargin = args.length (); |
|
1246 |
4946
|
1247 oct_data_conv::data_type dt = oct_data_conv::dt_double; |
4481
|
1248 |
4946
|
1249 dim_vector dims (1, 1); |
4481
|
1250 |
|
1251 if (nargin > 0 && args(nargin-1).is_string ()) |
|
1252 { |
4946
|
1253 std::string nm = args(nargin-1).string_value (); |
4481
|
1254 nargin--; |
|
1255 |
4946
|
1256 dt = oct_data_conv::string_to_data_type (nm); |
|
1257 |
|
1258 if (error_state) |
|
1259 return retval; |
4481
|
1260 } |
|
1261 |
523
|
1262 switch (nargin) |
|
1263 { |
712
|
1264 case 0: |
|
1265 break; |
777
|
1266 |
610
|
1267 case 1: |
4481
|
1268 get_dimensions (args(0), fcn, dims); |
610
|
1269 break; |
777
|
1270 |
4563
|
1271 default: |
|
1272 { |
|
1273 dims.resize (nargin); |
4481
|
1274 |
4563
|
1275 for (int i = 0; i < nargin; i++) |
|
1276 { |
4732
|
1277 dims(i) = args(i).is_empty () ? 0 : args(i).int_value (); |
4481
|
1278 |
4563
|
1279 if (error_state) |
|
1280 { |
4732
|
1281 error ("%s: expecting scalar integer arguments", fcn); |
4563
|
1282 break; |
|
1283 } |
|
1284 } |
|
1285 } |
|
1286 break; |
4481
|
1287 } |
|
1288 |
|
1289 if (! error_state) |
|
1290 { |
4946
|
1291 dims.chop_trailing_singletons (); |
4565
|
1292 |
4481
|
1293 check_dimensions (dims, fcn); |
3354
|
1294 |
4946
|
1295 // XXX FIXME XXX -- perhaps this should be made extensible by |
|
1296 // using the class name to lookup a function to call to create |
|
1297 // the new value. |
|
1298 |
|
1299 // Note that automatic narrowing will handle conversion from |
|
1300 // NDArray to scalar. |
|
1301 |
4481
|
1302 if (! error_state) |
|
1303 { |
4946
|
1304 switch (dt) |
|
1305 { |
|
1306 case oct_data_conv::dt_int8: |
|
1307 retval = int8NDArray (dims, val); |
|
1308 break; |
4481
|
1309 |
4946
|
1310 case oct_data_conv::dt_uint8: |
|
1311 retval = uint8NDArray (dims, val); |
|
1312 break; |
|
1313 |
|
1314 case oct_data_conv::dt_int16: |
|
1315 retval = int16NDArray (dims, val); |
|
1316 break; |
|
1317 |
|
1318 case oct_data_conv::dt_uint16: |
|
1319 retval = uint16NDArray (dims, val); |
|
1320 break; |
|
1321 |
|
1322 case oct_data_conv::dt_int32: |
|
1323 retval = int32NDArray (dims, val); |
|
1324 break; |
777
|
1325 |
4946
|
1326 case oct_data_conv::dt_uint32: |
|
1327 retval = uint32NDArray (dims, val); |
|
1328 break; |
|
1329 |
|
1330 case oct_data_conv::dt_int64: |
|
1331 retval = int64NDArray (dims, val); |
|
1332 break; |
4481
|
1333 |
4946
|
1334 case oct_data_conv::dt_uint64: |
|
1335 retval = uint64NDArray (dims, val); |
|
1336 break; |
4481
|
1337 |
4946
|
1338 case oct_data_conv::dt_single: // XXX FIXME XXX |
|
1339 case oct_data_conv::dt_double: |
|
1340 retval = NDArray (dims, val); |
|
1341 break; |
|
1342 |
4986
|
1343 case oct_data_conv::dt_logical: |
|
1344 retval = boolNDArray (dims, val); |
|
1345 break; |
|
1346 |
4946
|
1347 default: |
|
1348 error ("%s: invalid class name", fcn); |
|
1349 break; |
4481
|
1350 } |
|
1351 } |
523
|
1352 } |
|
1353 |
|
1354 return retval; |
|
1355 } |
|
1356 |
3354
|
1357 DEFUN (ones, args, , |
3369
|
1358 "-*- texinfo -*-\n\ |
|
1359 @deftypefn {Built-in Function} {} ones (@var{x})\n\ |
|
1360 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m})\n\ |
4948
|
1361 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
1362 @deftypefnx {Built-in Function} {} ones (@dots{}, @var{class})\n\ |
4481
|
1363 Return a matrix or N-dimensional array whose elements are all 1.\n\ |
|
1364 The arguments are handled the same as the arguments for @code{eye}.\n\ |
3369
|
1365 \n\ |
|
1366 If you need to create a matrix whose values are all the same, you should\n\ |
|
1367 use an expression like\n\ |
|
1368 \n\ |
|
1369 @example\n\ |
|
1370 val_matrix = val * ones (n, m)\n\ |
|
1371 @end example\n\ |
4945
|
1372 \n\ |
|
1373 The optional argument @var{class}, allows @code{ones} to return an array of\n\ |
|
1374 the specified type, like\n\ |
|
1375 \n\ |
|
1376 @example\n\ |
|
1377 val = ones (n,m, \"uint8\")\n\ |
|
1378 @end example\n\ |
3369
|
1379 @end deftypefn") |
523
|
1380 { |
3354
|
1381 return fill_matrix (args, 1.0, "ones"); |
523
|
1382 } |
|
1383 |
3354
|
1384 DEFUN (zeros, args, , |
3369
|
1385 "-*- texinfo -*-\n\ |
|
1386 @deftypefn {Built-in Function} {} zeros (@var{x})\n\ |
|
1387 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m})\n\ |
4948
|
1388 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
1389 @deftypefnx {Built-in Function} {} zeros (@dots{}, @var{class})\n\ |
4481
|
1390 Return a matrix or N-dimensional array whose elements are all 0.\n\ |
|
1391 The arguments are handled the same as the arguments for @code{eye}.\n\ |
4945
|
1392 \n\ |
|
1393 The optional argument @var{class}, allows @code{zeros} to return an array of\n\ |
|
1394 the specified type, like\n\ |
|
1395 \n\ |
|
1396 @example\n\ |
|
1397 val = zeros (n,m, \"uint8\")\n\ |
|
1398 @end example\n\ |
3369
|
1399 @end deftypefn") |
523
|
1400 { |
3354
|
1401 return fill_matrix (args, 0.0, "zeros"); |
|
1402 } |
523
|
1403 |
4946
|
1404 template <class MT> |
|
1405 octave_value |
|
1406 identity_matrix (int nr, int nc) |
|
1407 { |
|
1408 octave_value retval; |
|
1409 |
|
1410 typename octave_array_type_traits<MT>::element_type one (1); |
|
1411 |
|
1412 if (nr == 1 && nc == 1) |
|
1413 retval = one; |
|
1414 else |
|
1415 { |
|
1416 dim_vector dims (nr, nc); |
|
1417 |
|
1418 typename octave_array_type_traits<MT>::element_type zero (0); |
|
1419 |
|
1420 MT m (dims, zero); |
|
1421 |
|
1422 if (nr > 0 && nc > 0) |
|
1423 { |
|
1424 int n = std::min (nr, nc); |
|
1425 |
|
1426 for (int i = 0; i < n; i++) |
|
1427 m(i,i) = one; |
|
1428 } |
|
1429 |
|
1430 retval = m; |
|
1431 } |
|
1432 |
|
1433 return retval; |
|
1434 } |
|
1435 |
5058
|
1436 #define INSTANTIATE_EYE(T) \ |
|
1437 template octave_value identity_matrix<T> (int, int) |
|
1438 |
|
1439 INSTANTIATE_EYE (int8NDArray); |
|
1440 INSTANTIATE_EYE (uint8NDArray); |
|
1441 INSTANTIATE_EYE (int16NDArray); |
|
1442 INSTANTIATE_EYE (uint16NDArray); |
|
1443 INSTANTIATE_EYE (int32NDArray); |
|
1444 INSTANTIATE_EYE (uint32NDArray); |
|
1445 INSTANTIATE_EYE (int64NDArray); |
|
1446 INSTANTIATE_EYE (uint64NDArray); |
|
1447 INSTANTIATE_EYE (NDArray); |
|
1448 INSTANTIATE_EYE (boolNDArray); |
|
1449 |
4945
|
1450 static octave_value |
4948
|
1451 identity_matrix (int nr, int nc, oct_data_conv::data_type dt) |
4945
|
1452 { |
|
1453 octave_value retval; |
|
1454 |
4946
|
1455 // XXX FIXME XXX -- perhaps this should be made extensible by using |
|
1456 // the class name to lookup a function to call to create the new |
|
1457 // value. |
|
1458 |
|
1459 if (! error_state) |
|
1460 { |
|
1461 switch (dt) |
|
1462 { |
|
1463 case oct_data_conv::dt_int8: |
|
1464 retval = identity_matrix<int8NDArray> (nr, nc); |
|
1465 break; |
|
1466 |
|
1467 case oct_data_conv::dt_uint8: |
|
1468 retval = identity_matrix<uint8NDArray> (nr, nc); |
|
1469 break; |
|
1470 |
|
1471 case oct_data_conv::dt_int16: |
|
1472 retval = identity_matrix<int16NDArray> (nr, nc); |
|
1473 break; |
4945
|
1474 |
4946
|
1475 case oct_data_conv::dt_uint16: |
|
1476 retval = identity_matrix<uint16NDArray> (nr, nc); |
|
1477 break; |
|
1478 |
|
1479 case oct_data_conv::dt_int32: |
|
1480 retval = identity_matrix<int32NDArray> (nr, nc); |
|
1481 break; |
|
1482 |
|
1483 case oct_data_conv::dt_uint32: |
|
1484 retval = identity_matrix<uint32NDArray> (nr, nc); |
|
1485 break; |
4945
|
1486 |
4946
|
1487 case oct_data_conv::dt_int64: |
|
1488 retval = identity_matrix<int64NDArray> (nr, nc); |
|
1489 break; |
|
1490 |
|
1491 case oct_data_conv::dt_uint64: |
|
1492 retval = identity_matrix<uint64NDArray> (nr, nc); |
|
1493 break; |
4945
|
1494 |
4946
|
1495 case oct_data_conv::dt_single: // XXX FIXME XXX |
|
1496 case oct_data_conv::dt_double: |
|
1497 retval = identity_matrix<NDArray> (nr, nc); |
|
1498 break; |
4945
|
1499 |
4986
|
1500 case oct_data_conv::dt_logical: |
|
1501 retval = identity_matrix<boolNDArray> (nr, nc); |
|
1502 break; |
|
1503 |
4946
|
1504 default: |
|
1505 error ("eye: invalid class name"); |
|
1506 break; |
4945
|
1507 } |
|
1508 } |
|
1509 |
|
1510 return retval; |
|
1511 } |
|
1512 |
4946
|
1513 #undef INT_EYE_MATRIX |
|
1514 |
1957
|
1515 DEFUN (eye, args, , |
3369
|
1516 "-*- texinfo -*-\n\ |
|
1517 @deftypefn {Built-in Function} {} eye (@var{x})\n\ |
|
1518 @deftypefnx {Built-in Function} {} eye (@var{n}, @var{m})\n\ |
4948
|
1519 @deftypefnx {Built-in Function} {} eye (@dots{}, @var{class})\n\ |
3369
|
1520 Return an identity matrix. If invoked with a single scalar argument,\n\ |
|
1521 @code{eye} returns a square matrix with the dimension specified. If you\n\ |
|
1522 supply two scalar arguments, @code{eye} takes them to be the number of\n\ |
|
1523 rows and columns. If given a vector with two elements, @code{eye} uses\n\ |
|
1524 the values of the elements as the number of rows and columns,\n\ |
|
1525 respectively. For example,\n\ |
|
1526 \n\ |
|
1527 @example\n\ |
|
1528 @group\n\ |
|
1529 eye (3)\n\ |
|
1530 @result{} 1 0 0\n\ |
|
1531 0 1 0\n\ |
|
1532 0 0 1\n\ |
|
1533 @end group\n\ |
|
1534 @end example\n\ |
|
1535 \n\ |
|
1536 The following expressions all produce the same result:\n\ |
|
1537 \n\ |
|
1538 @example\n\ |
|
1539 @group\n\ |
|
1540 eye (2)\n\ |
|
1541 @equiv{}\n\ |
|
1542 eye (2, 2)\n\ |
|
1543 @equiv{}\n\ |
|
1544 eye (size ([1, 2; 3, 4])\n\ |
|
1545 @end group\n\ |
|
1546 @end example\n\ |
|
1547 \n\ |
4945
|
1548 The optional argument @var{class}, allows @code{eye} to return an array of\n\ |
|
1549 the specified type, like\n\ |
|
1550 \n\ |
|
1551 @example\n\ |
|
1552 val = zeros (n,m, \"uint8\")\n\ |
|
1553 @end example\n\ |
|
1554 \n\ |
3369
|
1555 For compatibility with @sc{Matlab}, calling @code{eye} with no arguments\n\ |
|
1556 is equivalent to calling it with an argument of 1.\n\ |
|
1557 @end deftypefn") |
523
|
1558 { |
3354
|
1559 octave_value retval; |
523
|
1560 |
4948
|
1561 int nargin = args.length (); |
4945
|
1562 |
4948
|
1563 oct_data_conv::data_type dt = oct_data_conv::dt_double; |
523
|
1564 |
4945
|
1565 // Check for type information. |
|
1566 |
|
1567 if (nargin > 0 && args(nargin-1).is_string ()) |
|
1568 { |
4948
|
1569 std::string nm = args(nargin-1).string_value (); |
4945
|
1570 nargin--; |
4948
|
1571 |
|
1572 dt = oct_data_conv::string_to_data_type (nm); |
|
1573 |
|
1574 if (error_state) |
|
1575 return retval; |
4945
|
1576 } |
|
1577 |
523
|
1578 switch (nargin) |
|
1579 { |
712
|
1580 case 0: |
4948
|
1581 retval = identity_matrix (1, 1, dt); |
712
|
1582 break; |
777
|
1583 |
610
|
1584 case 1: |
3354
|
1585 { |
5275
|
1586 octave_idx_type nr, nc; |
3354
|
1587 get_dimensions (args(0), "eye", nr, nc); |
|
1588 |
|
1589 if (! error_state) |
4948
|
1590 retval = identity_matrix (nr, nc, dt); |
3354
|
1591 } |
610
|
1592 break; |
777
|
1593 |
523
|
1594 case 2: |
3354
|
1595 { |
5275
|
1596 octave_idx_type nr, nc; |
3354
|
1597 get_dimensions (args(0), args(1), "eye", nr, nc); |
|
1598 |
|
1599 if (! error_state) |
4948
|
1600 retval = identity_matrix (nr, nc, dt); |
3354
|
1601 } |
523
|
1602 break; |
777
|
1603 |
523
|
1604 default: |
|
1605 print_usage ("eye"); |
|
1606 break; |
|
1607 } |
|
1608 |
|
1609 return retval; |
|
1610 } |
|
1611 |
1957
|
1612 DEFUN (linspace, args, , |
3369
|
1613 "-*- texinfo -*-\n\ |
|
1614 @deftypefn {Built-in Function} {} linspace (@var{base}, @var{limit}, @var{n})\n\ |
|
1615 Return a row vector with @var{n} linearly spaced elements between\n\ |
|
1616 @var{base} and @var{limit}. The number of elements, @var{n}, must be\n\ |
|
1617 greater than 1. The @var{base} and @var{limit} are always included in\n\ |
|
1618 the range. If @var{base} is greater than @var{limit}, the elements are\n\ |
|
1619 stored in decreasing order. If the number of points is not specified, a\n\ |
|
1620 value of 100 is used.\n\ |
1100
|
1621 \n\ |
4455
|
1622 The @code{linspace} function always returns a row vector.\n\ |
3369
|
1623 @end deftypefn") |
1100
|
1624 { |
3418
|
1625 octave_value retval; |
1100
|
1626 |
|
1627 int nargin = args.length (); |
|
1628 |
|
1629 int npoints = 100; |
|
1630 |
1940
|
1631 if (nargin != 2 && nargin != 3) |
|
1632 { |
|
1633 print_usage ("linspace"); |
|
1634 return retval; |
|
1635 } |
|
1636 |
1100
|
1637 if (nargin == 3) |
4732
|
1638 npoints = args(2).int_value (); |
1100
|
1639 |
|
1640 if (! error_state) |
|
1641 { |
3322
|
1642 octave_value arg_1 = args(0); |
|
1643 octave_value arg_2 = args(1); |
1100
|
1644 |
3322
|
1645 if (arg_1.is_complex_type () || arg_2.is_complex_type ()) |
|
1646 { |
|
1647 Complex x1 = arg_1.complex_value (); |
|
1648 Complex x2 = arg_2.complex_value (); |
|
1649 |
|
1650 if (! error_state) |
1100
|
1651 { |
3322
|
1652 ComplexRowVector rv = linspace (x1, x2, npoints); |
1100
|
1653 |
|
1654 if (! error_state) |
3418
|
1655 retval = rv; |
1100
|
1656 } |
|
1657 } |
|
1658 else |
3322
|
1659 { |
|
1660 double x1 = arg_1.double_value (); |
|
1661 double x2 = arg_2.double_value (); |
|
1662 |
|
1663 if (! error_state) |
|
1664 { |
|
1665 RowVector rv = linspace (x1, x2, npoints); |
|
1666 |
|
1667 if (! error_state) |
3418
|
1668 retval = rv; |
3322
|
1669 } |
|
1670 } |
1100
|
1671 } |
4732
|
1672 else |
|
1673 error ("linspace: expecting third argument to be an integer"); |
1100
|
1674 |
|
1675 return retval; |
|
1676 } |
|
1677 |
4567
|
1678 DEFUN (reshape, args, , |
|
1679 "-*- texinfo -*-\n\ |
|
1680 @deftypefn {Function File} {} reshape (@var{a}, @var{m}, @var{n}, @dots{})\n\ |
|
1681 @deftypefnx {Function File} {} reshape (@var{a}, @var{siz})\n\ |
|
1682 Return a matrix with the given dimensions whose elements are taken\n\ |
|
1683 from the matrix @var{a}. The elements of the matrix are access in\n\ |
|
1684 column-major order (like Fortran arrays are stored).\n\ |
|
1685 \n\ |
|
1686 For example,\n\ |
|
1687 \n\ |
|
1688 @example\n\ |
|
1689 @group\n\ |
|
1690 reshape ([1, 2, 3, 4], 2, 2)\n\ |
|
1691 @result{} 1 3\n\ |
|
1692 2 4\n\ |
|
1693 @end group\n\ |
|
1694 @end example\n\ |
|
1695 \n\ |
|
1696 @noindent\n\ |
|
1697 Note that the total number of elements in the original\n\ |
|
1698 matrix must match the total number of elements in the new matrix.\n\ |
5013
|
1699 \n\ |
|
1700 A single dimension of the return matrix can be unknown and is flagged\n\ |
|
1701 by an empty argument.\n\ |
4567
|
1702 @end deftypefn") |
|
1703 { |
|
1704 octave_value retval; |
|
1705 |
|
1706 int nargin = args.length (); |
|
1707 |
|
1708 Array<int> new_size; |
|
1709 |
|
1710 if (nargin == 2) |
|
1711 new_size = args(1).int_vector_value (); |
|
1712 else if (nargin > 2) |
|
1713 { |
|
1714 new_size.resize (nargin-1); |
5013
|
1715 int empty_dim = -1; |
|
1716 |
4567
|
1717 for (int i = 1; i < nargin; i++) |
|
1718 { |
5013
|
1719 if (args(i).is_empty ()) |
|
1720 if (empty_dim > 0) |
|
1721 { |
|
1722 error ("reshape: only a single dimension can be unknown"); |
|
1723 break; |
|
1724 } |
|
1725 else |
|
1726 { |
|
1727 empty_dim = i; |
|
1728 new_size(i-1) = 1; |
|
1729 } |
|
1730 else |
|
1731 { |
|
1732 new_size(i-1) = args(i).int_value (); |
4567
|
1733 |
5013
|
1734 if (error_state) |
|
1735 break; |
|
1736 } |
|
1737 } |
|
1738 |
|
1739 if (! error_state && (empty_dim > 0)) |
|
1740 { |
|
1741 int nel = 1; |
|
1742 for (int i = 0; i < nargin - 1; i++) |
|
1743 nel *= new_size(i); |
|
1744 |
|
1745 if (nel == 0) |
|
1746 new_size(empty_dim-1) = 0; |
|
1747 else |
|
1748 { |
|
1749 int size_empty_dim = args(0).numel () / nel; |
|
1750 |
|
1751 if (args(0).numel () != size_empty_dim * nel) |
|
1752 error ("reshape: size is not divisble by the product of known dimensions (= %d)", nel); |
|
1753 else |
|
1754 new_size(empty_dim-1) = size_empty_dim; |
|
1755 } |
4567
|
1756 } |
|
1757 } |
|
1758 else |
|
1759 { |
|
1760 print_usage ("reshape"); |
|
1761 return retval; |
|
1762 } |
|
1763 |
|
1764 if (error_state) |
|
1765 { |
|
1766 error ("reshape: invalid arguments"); |
|
1767 return retval; |
|
1768 } |
|
1769 |
4739
|
1770 // Remove trailing singletons in new_size, but leave at least 2 |
|
1771 // elements. |
|
1772 |
4567
|
1773 int n = new_size.length (); |
|
1774 |
4739
|
1775 while (n > 2) |
|
1776 { |
|
1777 if (new_size(n-1) == 1) |
|
1778 n--; |
|
1779 else |
|
1780 break; |
|
1781 } |
|
1782 |
|
1783 new_size.resize (n); |
|
1784 |
4567
|
1785 if (n < 2) |
|
1786 { |
|
1787 error ("reshape: expecting size to be vector with at least 2 elements"); |
|
1788 return retval; |
|
1789 } |
|
1790 |
|
1791 dim_vector new_dims; |
|
1792 |
|
1793 new_dims.resize (n); |
|
1794 |
5275
|
1795 for (octave_idx_type i = 0; i < n; i++) |
4567
|
1796 new_dims(i) = new_size(i); |
|
1797 |
|
1798 octave_value arg = args(0); |
|
1799 |
|
1800 if (new_dims.numel () == arg.numel ()) |
|
1801 retval = (new_dims == arg.dims ()) ? arg : arg.reshape (new_dims); |
|
1802 else |
|
1803 error ("reshape: size mismatch"); |
|
1804 |
|
1805 return retval; |
|
1806 } |
|
1807 |
4532
|
1808 DEFUN (squeeze, args, , |
|
1809 "-*- texinfo -*-\n\ |
|
1810 @deftypefn {Built-in Function} {} squeeze (@var{x})\n\ |
|
1811 Remove singleton dimensions from @var{x} and return the result.\n\ |
|
1812 @end deftypefn") |
|
1813 { |
|
1814 octave_value retval; |
|
1815 |
|
1816 if (args.length () == 1) |
4545
|
1817 retval = args(0).squeeze (); |
4532
|
1818 else |
|
1819 print_usage ("squeeze"); |
|
1820 |
|
1821 return retval; |
|
1822 } |
|
1823 |
2184
|
1824 void |
|
1825 symbols_of_data (void) |
|
1826 { |
3321
|
1827 |
|
1828 #define IMAGINARY_DOC_STRING "-*- texinfo -*-\n\ |
5333
|
1829 @defvr {Built-in Constant} I\n\ |
|
1830 @defvrx {Built-in Constant} J\n\ |
|
1831 @defvrx {Built-in Constant} i\n\ |
|
1832 @defvrx {Built-in Constant} j\n\ |
3321
|
1833 A pure imaginary number, defined as\n\ |
|
1834 @iftex\n\ |
|
1835 @tex\n\ |
|
1836 $\\sqrt{-1}$.\n\ |
|
1837 @end tex\n\ |
|
1838 @end iftex\n\ |
|
1839 @ifinfo\n\ |
|
1840 @code{sqrt (-1)}.\n\ |
|
1841 @end ifinfo\n\ |
4845
|
1842 These built-in variables behave like functions so you can use the names\n\ |
|
1843 for other purposes. If you use them as variables and assign values to\n\ |
|
1844 them and then clear them, they once again assume their special predefined\n\ |
|
1845 values @xref{Status of Variables}.\n\ |
3321
|
1846 @end defvr" |
|
1847 |
|
1848 #define INFINITY_DOC_STRING "-*- texinfo -*-\n\ |
5333
|
1849 @defvr {Built-in Constant} Inf\n\ |
|
1850 @defvrx {Built-in Constant} inf\n\ |
3321
|
1851 Infinity. This is the result of an operation like 1/0, or an operation\n\ |
|
1852 that results in a floating point overflow.\n\ |
|
1853 @end defvr" |
|
1854 |
|
1855 #define NAN_DOC_STRING "-*- texinfo -*-\n\ |
5333
|
1856 @defvr {Built-in Constant} NaN\n\ |
|
1857 @defvrx {Built-in Constant} nan\n\ |
3321
|
1858 Not a number. This is the result of an operation like\n\ |
|
1859 @iftex\n\ |
|
1860 @tex\n\ |
|
1861 $0/0$, or $\\infty - \\infty$,\n\ |
|
1862 @end tex\n\ |
|
1863 @end iftex\n\ |
|
1864 @ifinfo\n\ |
|
1865 0/0, or @samp{Inf - Inf},\n\ |
|
1866 @end ifinfo\n\ |
|
1867 or any operation with a NaN.\n\ |
|
1868 \n\ |
|
1869 Note that NaN always compares not equal to NaN. This behavior is\n\ |
|
1870 specified by the IEEE standard for floating point arithmetic. To\n\ |
|
1871 find NaN values, you must use the @code{isnan} function.\n\ |
|
1872 @end defvr" |
|
1873 |
3141
|
1874 DEFCONST (I, Complex (0.0, 1.0), |
3321
|
1875 IMAGINARY_DOC_STRING); |
2184
|
1876 |
4102
|
1877 DEFCONST (Inf, lo_ieee_inf_value (), |
3321
|
1878 INFINITY_DOC_STRING); |
2184
|
1879 |
3141
|
1880 DEFCONST (J, Complex (0.0, 1.0), |
3321
|
1881 IMAGINARY_DOC_STRING); |
2184
|
1882 |
4102
|
1883 DEFCONST (NA, lo_ieee_na_value (), |
4025
|
1884 "-*- texinfo -*-\n\ |
5333
|
1885 @defvr {Built-in Constant} NA\n\ |
4025
|
1886 Missing value.\n\ |
|
1887 @end defvr"); |
|
1888 |
4102
|
1889 DEFCONST (NaN, lo_ieee_nan_value (), |
3321
|
1890 NAN_DOC_STRING); |
2184
|
1891 |
|
1892 #if defined (M_E) |
|
1893 double e_val = M_E; |
|
1894 #else |
|
1895 double e_val = exp (1.0); |
|
1896 #endif |
|
1897 |
3141
|
1898 DEFCONST (e, e_val, |
3321
|
1899 "-*- texinfo -*-\n\ |
5333
|
1900 @defvr {Built-in Constant} e\n\ |
3321
|
1901 The base of natural logarithms. The constant\n\ |
|
1902 @iftex\n\ |
|
1903 @tex\n\ |
|
1904 $e$\n\ |
|
1905 @end tex\n\ |
|
1906 @end iftex\n\ |
|
1907 @ifinfo\n\ |
|
1908 @var{e}\n\ |
|
1909 @end ifinfo\n\ |
|
1910 satisfies the equation\n\ |
|
1911 @iftex\n\ |
|
1912 @tex\n\ |
|
1913 $\\log (e) = 1$.\n\ |
|
1914 @end tex\n\ |
|
1915 @end iftex\n\ |
|
1916 @ifinfo\n\ |
|
1917 @code{log} (@var{e}) = 1.\n\ |
|
1918 @end ifinfo\n\ |
|
1919 @end defvr"); |
2184
|
1920 |
3141
|
1921 DEFCONST (eps, DBL_EPSILON, |
3321
|
1922 "-*- texinfo -*-\n\ |
5333
|
1923 @defvr {Built-in Constant} eps\n\ |
3321
|
1924 The machine precision. More precisely, @code{eps} is the largest\n\ |
|
1925 relative spacing between any two adjacent numbers in the machine's\n\ |
|
1926 floating point system. This number is obviously system-dependent. On\n\ |
|
1927 machines that support 64 bit IEEE floating point arithmetic, @code{eps}\n\ |
|
1928 is approximately\n\ |
|
1929 @ifinfo\n\ |
|
1930 2.2204e-16.\n\ |
|
1931 @end ifinfo\n\ |
|
1932 @iftex\n\ |
|
1933 @tex\n\ |
|
1934 $2.2204\\times10^{-16}$.\n\ |
|
1935 @end tex\n\ |
|
1936 @end iftex\n\ |
|
1937 @end defvr"); |
2184
|
1938 |
3258
|
1939 DEFCONST (false, false, |
3443
|
1940 "-*- texinfo -*-\n\ |
5333
|
1941 @defvr {Built-in Constant} false\n\ |
3443
|
1942 Logical false value.\n\ |
5333
|
1943 @seealso{true}\n\ |
3443
|
1944 @end defvr"); |
3258
|
1945 |
3141
|
1946 DEFCONST (i, Complex (0.0, 1.0), |
3321
|
1947 IMAGINARY_DOC_STRING); |
2184
|
1948 |
4102
|
1949 DEFCONST (inf, lo_ieee_inf_value (), |
3321
|
1950 INFINITY_DOC_STRING); |
2184
|
1951 |
3141
|
1952 DEFCONST (j, Complex (0.0, 1.0), |
3321
|
1953 IMAGINARY_DOC_STRING); |
2184
|
1954 |
4102
|
1955 DEFCONST (nan, lo_ieee_nan_value (), |
3321
|
1956 NAN_DOC_STRING); |
2184
|
1957 |
|
1958 #if defined (M_PI) |
|
1959 double pi_val = M_PI; |
|
1960 #else |
|
1961 double pi_val = 4.0 * atan (1.0); |
|
1962 #endif |
|
1963 |
3141
|
1964 DEFCONST (pi, pi_val, |
3321
|
1965 "-*- texinfo -*-\n\ |
5333
|
1966 @defvr {Built-in Constant} pi\n\ |
3321
|
1967 The ratio of the circumference of a circle to its diameter.\n\ |
|
1968 Internally, @code{pi} is computed as @samp{4.0 * atan (1.0)}.\n\ |
|
1969 @end defvr"); |
2184
|
1970 |
3141
|
1971 DEFCONST (realmax, DBL_MAX, |
3321
|
1972 "-*- texinfo -*-\n\ |
5333
|
1973 @defvr {Built-in Constant} realmax\n\ |
3321
|
1974 The largest floating point number that is representable. The actual\n\ |
4303
|
1975 value is system-dependent. On machines that support 64-bit IEEE\n\ |
3321
|
1976 floating point arithmetic, @code{realmax} is approximately\n\ |
|
1977 @ifinfo\n\ |
|
1978 1.7977e+308\n\ |
|
1979 @end ifinfo\n\ |
|
1980 @iftex\n\ |
5333
|
1981 @seealso{realmin}\n\ |
3321
|
1982 @tex\n\ |
|
1983 $1.7977\\times10^{308}$.\n\ |
|
1984 @end tex\n\ |
|
1985 @end iftex\n\ |
|
1986 @end defvr"); |
2184
|
1987 |
3141
|
1988 DEFCONST (realmin, DBL_MIN, |
3321
|
1989 "-*- texinfo -*-\n\ |
5333
|
1990 @defvr {Built-in Constant} realmin\n\ |
4303
|
1991 The smallest normalized floating point number that is representable.\n\ |
|
1992 The actual value is system-dependent. On machines that support\n\ |
|
1993 64-bit IEEE floating point arithmetic, @code{realmin} is approximately\n\ |
3321
|
1994 @ifinfo\n\ |
|
1995 2.2251e-308\n\ |
|
1996 @end ifinfo\n\ |
|
1997 @iftex\n\ |
|
1998 @tex\n\ |
|
1999 $2.2251\\times10^{-308}$.\n\ |
|
2000 @end tex\n\ |
|
2001 @end iftex\n\ |
5333
|
2002 @seealso{realmax}\n\ |
3321
|
2003 @end defvr"); |
2188
|
2004 |
3258
|
2005 DEFCONST (true, true, |
3443
|
2006 "-*- texinfo -*-\n\ |
5333
|
2007 @defvr {Built-in Constant} true\n\ |
3443
|
2008 Logical true value.\n\ |
5333
|
2009 @seealso{false}\n\ |
3443
|
2010 @end defvr"); |
3354
|
2011 |
2184
|
2012 } |
|
2013 |
523
|
2014 /* |
|
2015 ;;; Local Variables: *** |
|
2016 ;;; mode: C++ *** |
|
2017 ;;; End: *** |
|
2018 */ |