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