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 (); |
|
540 if (v.capacity () > 0) |
|
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 (); |
|
563 if (v.capacity () > 0) |
|
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; |
|
732 for (int i = 1; i < n_args; i++) |
|
733 if (args(i).is_string ()) |
|
734 any_strings = true; |
|
735 else |
|
736 all_strings = false; |
4806
|
737 |
4915
|
738 if (all_strings) |
|
739 tmp = octave_value (charNDArray (dv, Vstring_fill_char), true); |
|
740 else |
|
741 tmp = args(1).resize (dim_vector (0,0)).resize (dv); |
4824
|
742 |
4915
|
743 if (error_state) |
|
744 return retval; |
4824
|
745 |
4915
|
746 Array<int> ra_idx (dv.length (), 0); |
|
747 for (int i = 1; i < n_args; i++) |
|
748 { |
|
749 tmp = do_cat_op (tmp, args (i), ra_idx); |
4824
|
750 |
4915
|
751 if (error_state) |
|
752 return retval; |
4806
|
753 |
4915
|
754 dim_vector dv_tmp = args (i).dims (); |
|
755 ra_idx (dim) += (dim < dv_tmp.length () ? dv_tmp (dim) : 1); |
|
756 } |
4806
|
757 |
4915
|
758 if (any_strings && !all_strings) |
|
759 retval = tmp.convert_to_str (); |
|
760 else |
|
761 retval = tmp; |
4806
|
762 } |
4824
|
763 else print_usage (fname); |
4806
|
764 } |
|
765 else |
4824
|
766 print_usage (fname); |
4806
|
767 |
|
768 return retval; |
|
769 } |
|
770 |
|
771 DEFUN (horzcat, args, , |
4824
|
772 "-*- texinfo -*-\n\ |
4806
|
773 @deftypefn {Built-in Function} {} horzcat (@var{array1}, @var{array2}, @dots{}, @var{arrayN})\n\ |
|
774 Return the horizontal concatenation of N-d array objects, @var{array1},\n\ |
|
775 @var{array2}, @dots{}, @var{arrayN} along dimension 2.\n\ |
|
776 @end deftypefn\n\ |
|
777 @seealso{cat and vertcat}") |
|
778 { |
|
779 octave_value_list args_tmp = args; |
|
780 |
|
781 int dim = 2; |
|
782 |
|
783 octave_value d (dim); |
|
784 |
|
785 args_tmp.prepend (d); |
|
786 |
4824
|
787 return do_cat (args_tmp, "horzcat"); |
4806
|
788 } |
|
789 |
|
790 DEFUN (vertcat, args, , |
|
791 "-*- texinfo -*-\n\ |
|
792 @deftypefn {Built-in Function} {} vertcat (@var{array1}, @var{array2}, @dots{}, @var{arrayN})\n\ |
|
793 Return the vertical concatenation of N-d array objects, @var{array1},\n\ |
|
794 @var{array2}, @dots{}, @var{arrayN} along dimension 1.\n\ |
|
795 @end deftypefn\n\ |
|
796 @seealso{cat and horzcat}") |
|
797 { |
|
798 octave_value_list args_tmp = args; |
|
799 |
|
800 int dim = 1; |
|
801 |
|
802 octave_value d (dim); |
|
803 |
|
804 args_tmp.prepend (d); |
|
805 |
4824
|
806 return do_cat (args_tmp, "vertcat"); |
4806
|
807 } |
|
808 |
4758
|
809 DEFUN (cat, args, , |
|
810 "-*- texinfo -*-\n\ |
|
811 @deftypefn {Built-in Function} {} cat (@var{dim}, @var{array1}, @var{array2}, @dots{}, @var{arrayN})\n\ |
4806
|
812 Return the concatenation of N-d array objects, @var{array1},\n\ |
|
813 @var{array2}, @dots{}, @var{arrayN} along dimension @var{dim}.\n\ |
4758
|
814 \n\ |
|
815 @example\n\ |
|
816 @group\n\ |
|
817 A = ones (2, 2);\n\ |
|
818 B = zeros (2, 2);\n\ |
|
819 cat (2, A, B)\n\ |
|
820 @result{} ans =\n\ |
|
821 \n\ |
|
822 1 1 0 0\n\ |
|
823 1 1 0 0\n\ |
|
824 @end group\n\ |
|
825 @end example\n\ |
|
826 \n\ |
|
827 Alternatively, we can concatenate @var{A} and @var{B} along the\n\ |
|
828 second dimension the following way:\n\ |
|
829 \n\ |
|
830 @example\n\ |
|
831 @group\n\ |
|
832 [A, B].\n\ |
|
833 @end group\n\ |
|
834 @end example\n\ |
|
835 \n\ |
|
836 @var{dim} can be larger than the dimensions of the N-d array objects\n\ |
|
837 and the result will thus have @var{dim} dimensions as the\n\ |
|
838 following example shows:\n\ |
|
839 @example\n\ |
|
840 @group\n\ |
|
841 cat (4, ones(2, 2), zeros (2, 2))\n\ |
|
842 @result{} ans =\n\ |
|
843 \n\ |
|
844 ans(:,:,1,1) =\n\ |
|
845 \n\ |
|
846 1 1\n\ |
|
847 1 1\n\ |
|
848 \n\ |
|
849 ans(:,:,1,2) =\n\ |
|
850 0 0\n\ |
|
851 0 0\n\ |
|
852 @end group\n\ |
|
853 @end example\n\ |
|
854 \n\ |
4806
|
855 @end deftypefn\n\ |
|
856 @seealso{horzcat and vertcat}") |
4758
|
857 { |
4824
|
858 return do_cat (args, "cat"); |
4758
|
859 } |
|
860 |
4593
|
861 static octave_value |
|
862 do_permute (const octave_value_list& args, bool inv, const std::string& fname) |
|
863 { |
|
864 octave_value retval; |
|
865 |
5148
|
866 if (args.length () == 2 && args(1).length () >= args(1).ndims ()) |
4593
|
867 { |
|
868 Array<int> vec = args(1).int_vector_value (); |
|
869 |
5148
|
870 // XXX FIXME XXX -- maybe we shoudl create an idx_vector object |
|
871 // here and pass that to permute? |
|
872 |
|
873 int n = vec.length (); |
|
874 |
|
875 for (int i = 0; i < n; i++) |
|
876 vec(i)--; |
|
877 |
4593
|
878 octave_value ret = args(0).permute (vec, inv); |
|
879 |
|
880 if (! error_state) |
|
881 retval = ret; |
|
882 } |
|
883 else |
|
884 print_usage (fname); |
|
885 |
|
886 return retval; |
|
887 } |
|
888 |
|
889 DEFUN (permute, args, , |
|
890 "-*- texinfo -*-\n\ |
|
891 @deftypefn {Built-in Function} {} permute (@var{a}, @var{perm})\n\ |
|
892 Return the generalized transpose for an N-d array object @var{a}.\n\ |
|
893 The permutation vector @var{perm} must contain the elements\n\ |
|
894 @code{1:ndims(a)} (in any order, but each element must appear just once).\n\ |
|
895 \n\ |
|
896 @end deftypefn\n\ |
|
897 @seealso{ipermute}") |
|
898 { |
|
899 return do_permute (args, false, "permute"); |
|
900 } |
|
901 |
|
902 DEFUN (ipermute, args, , |
|
903 "-*- texinfo -*-\n\ |
|
904 @deftypefn {Built-in Function} {} ipermute (@var{a}, @var{iperm})\n\ |
|
905 The inverse of the @code{permute} function. The expression\n\ |
|
906 \n\ |
|
907 @example\n\ |
|
908 ipermute (permute (a, perm), perm)\n\ |
|
909 @end example\n\ |
|
910 returns the original array @var{a}.\n\ |
|
911 \n\ |
|
912 @end deftypefn\n\ |
|
913 @seealso{permute}") |
|
914 { |
|
915 return do_permute (args, true, "ipermute"); |
|
916 } |
|
917 |
3195
|
918 DEFUN (length, args, , |
3373
|
919 "-*- texinfo -*-\n\ |
|
920 @deftypefn {Built-in Function} {} length (@var{a})\n\ |
4176
|
921 Return the `length' of the object @var{a}. For matrix objects, the\n\ |
3373
|
922 length is the number of rows or columns, whichever is greater (this\n\ |
|
923 odd definition is used for compatibility with Matlab).\n\ |
|
924 @end deftypefn") |
3195
|
925 { |
|
926 octave_value retval; |
|
927 |
|
928 if (args.length () == 1) |
|
929 { |
|
930 int len = args(0).length (); |
|
931 |
|
932 if (! error_state) |
4233
|
933 retval = len; |
3195
|
934 } |
|
935 else |
|
936 print_usage ("length"); |
|
937 |
|
938 return retval; |
|
939 } |
|
940 |
4554
|
941 DEFUN (ndims, args, , |
|
942 "-*- texinfo -*-\n\ |
|
943 @deftypefn {Built-in Function} {} ndims (@var{a})\n\ |
|
944 Returns the number of dimensions of array @var{a}.\n\ |
|
945 For any array, the result will always be larger than or equal to 2.\n\ |
|
946 Trailing singleton dimensions are not counted.\n\ |
|
947 @end deftypefn") |
|
948 { |
|
949 octave_value retval; |
|
950 |
|
951 if (args.length () == 1) |
|
952 { |
|
953 int n_dims = args(0).ndims (); |
|
954 |
|
955 if (! error_state) |
|
956 retval = n_dims; |
|
957 } |
|
958 else |
|
959 print_usage ("ndims"); |
|
960 |
|
961 return retval; |
|
962 } |
|
963 |
4559
|
964 DEFUN (numel, args, , |
|
965 "-*- texinfo -*-\n\ |
|
966 @deftypefn {Built-in Function} {} numel (@var{a})\n\ |
|
967 Returns the number of elements in the object @var{a}.\n\ |
|
968 @end deftypefn") |
|
969 { |
|
970 octave_value retval; |
|
971 |
|
972 if (args.length () == 1) |
|
973 { |
|
974 int numel = args(0).numel (); |
|
975 |
|
976 if (! error_state) |
|
977 { |
|
978 if (numel < 0) |
|
979 numel = 0; |
|
980 |
|
981 retval = numel; |
|
982 } |
|
983 } |
|
984 else |
|
985 print_usage ("numel"); |
|
986 |
|
987 return retval; |
|
988 } |
|
989 |
1957
|
990 DEFUN (size, args, nargout, |
3373
|
991 "-*- texinfo -*-\n\ |
|
992 @deftypefn {Built-in Function} {} size (@var{a}, @var{n})\n\ |
|
993 Return the number rows and columns of @var{a}.\n\ |
|
994 \n\ |
|
995 With one input argument and one output argument, the result is returned\n\ |
4741
|
996 in a row vector. If there are multiple output arguments, the number of\n\ |
|
997 rows is assigned to the first, and the number of columns to the second,\n\ |
|
998 etc. For example,\n\ |
3373
|
999 \n\ |
|
1000 @example\n\ |
|
1001 @group\n\ |
|
1002 size ([1, 2; 3, 4; 5, 6])\n\ |
|
1003 @result{} [ 3, 2 ]\n\ |
1031
|
1004 \n\ |
3373
|
1005 [nr, nc] = size ([1, 2; 3, 4; 5, 6])\n\ |
|
1006 @result{} nr = 3\n\ |
|
1007 @result{} nc = 2\n\ |
|
1008 @end group\n\ |
|
1009 @end example\n\ |
|
1010 \n\ |
4741
|
1011 If given a second argument, @code{size} will return the size of the\n\ |
|
1012 corresponding dimension. For example\n\ |
1031
|
1013 \n\ |
3373
|
1014 @example\n\ |
|
1015 size ([1, 2; 3, 4; 5, 6], 2)\n\ |
|
1016 @result{} 2\n\ |
|
1017 @end example\n\ |
|
1018 \n\ |
|
1019 @noindent\n\ |
|
1020 returns the number of columns in the given matrix.\n\ |
|
1021 @end deftypefn") |
523
|
1022 { |
2086
|
1023 octave_value_list retval; |
523
|
1024 |
|
1025 int nargin = args.length (); |
|
1026 |
4513
|
1027 if (nargin == 1) |
523
|
1028 { |
4513
|
1029 dim_vector dimensions = args(0).dims (); |
|
1030 |
|
1031 int ndims = dimensions.length (); |
1031
|
1032 |
4513
|
1033 Matrix m (1, ndims); |
|
1034 |
|
1035 if (nargout > 1) |
523
|
1036 { |
4513
|
1037 while (ndims--) |
|
1038 retval(ndims) = dimensions(ndims); |
523
|
1039 } |
4513
|
1040 else |
712
|
1041 { |
4513
|
1042 for (int i = 0; i < ndims; i++) |
|
1043 m(0, i) = dimensions(i); |
|
1044 |
|
1045 retval(0) = m; |
712
|
1046 } |
1031
|
1047 } |
|
1048 else if (nargin == 2 && nargout < 2) |
|
1049 { |
4732
|
1050 int nd = args(1).int_value (true); |
1031
|
1051 |
|
1052 if (error_state) |
|
1053 error ("size: expecting scalar as second argument"); |
712
|
1054 else |
1031
|
1055 { |
4741
|
1056 dim_vector dv = args(0).dims (); |
|
1057 |
4911
|
1058 if (nd > 0) |
|
1059 { |
|
1060 if (nd <= dv.length ()) |
|
1061 retval(0) = dv(nd-1); |
|
1062 else |
|
1063 retval(0) = 1; |
|
1064 } |
1031
|
1065 else |
4741
|
1066 error ("size: requested dimension (= %d) out of range", nd); |
1031
|
1067 } |
523
|
1068 } |
712
|
1069 else |
|
1070 print_usage ("size"); |
523
|
1071 |
|
1072 return retval; |
|
1073 } |
|
1074 |
1957
|
1075 DEFUN (sum, args, , |
3428
|
1076 "-*- texinfo -*-\n\ |
3723
|
1077 @deftypefn {Built-in Function} {} sum (@var{x}, @var{dim})\n\ |
|
1078 Sum of elements along dimension @var{dim}. If @var{dim} is\n\ |
|
1079 omitted, it defaults to 1 (column-wise sum).\n\ |
5061
|
1080 \n\ |
|
1081 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ |
|
1082 return the sum of the elements.\n\ |
3428
|
1083 @end deftypefn") |
523
|
1084 { |
3723
|
1085 DATA_REDUCTION (sum); |
523
|
1086 } |
|
1087 |
1957
|
1088 DEFUN (sumsq, args, , |
3428
|
1089 "-*- texinfo -*-\n\ |
3723
|
1090 @deftypefn {Built-in Function} {} sumsq (@var{x}, @var{dim})\n\ |
|
1091 Sum of squares of elements along dimension @var{dim}. If @var{dim}\n\ |
|
1092 is omitted, it defaults to 1 (column-wise sum of squares).\n\ |
3095
|
1093 \n\ |
5061
|
1094 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ |
|
1095 return the sum of squares of the elements.\n\ |
|
1096 \n\ |
|
1097 This function is conceptually equivalent to computing\n\ |
3723
|
1098 @example\n\ |
|
1099 sum (x .* conj (x), dim)\n\ |
|
1100 @end example\n\ |
|
1101 but it uses less memory and avoids calling conj if @var{x} is real.\n\ |
3428
|
1102 @end deftypefn") |
523
|
1103 { |
3723
|
1104 DATA_REDUCTION (sumsq); |
523
|
1105 } |
|
1106 |
4028
|
1107 DEFUN (isbool, args, , |
3428
|
1108 "-*- texinfo -*-\n\ |
4028
|
1109 @deftypefn {Built-in Functio} {} isbool (@var{x})\n\ |
3428
|
1110 Return true if @var{x} is a boolean object.\n\ |
3439
|
1111 @end deftypefn") |
3209
|
1112 { |
|
1113 octave_value retval; |
|
1114 |
|
1115 if (args.length () == 1) |
3258
|
1116 retval = args(0).is_bool_type (); |
3209
|
1117 else |
4028
|
1118 print_usage ("isbool"); |
3209
|
1119 |
|
1120 return retval; |
|
1121 } |
|
1122 |
4028
|
1123 DEFALIAS (islogical, isbool); |
3209
|
1124 |
4028
|
1125 DEFUN (iscomplex, args, , |
3428
|
1126 "-*- texinfo -*-\n\ |
4028
|
1127 @deftypefn {Built-in Function} {} iscomplex (@var{x})\n\ |
3428
|
1128 Return true if @var{x} is a complex-valued numeric object.\n\ |
|
1129 @end deftypefn") |
3186
|
1130 { |
|
1131 octave_value retval; |
|
1132 |
|
1133 if (args.length () == 1) |
3258
|
1134 retval = args(0).is_complex_type (); |
3186
|
1135 else |
4028
|
1136 print_usage ("iscomplex"); |
3186
|
1137 |
|
1138 return retval; |
|
1139 } |
|
1140 |
3258
|
1141 DEFUN (isreal, args, , |
3428
|
1142 "-*- texinfo -*-\n\ |
|
1143 @deftypefn {Built-in Function} {} isreal (@var{x})\n\ |
|
1144 Return true if @var{x} is a real-valued numeric object.\n\ |
|
1145 @end deftypefn") |
3258
|
1146 { |
|
1147 octave_value retval; |
|
1148 |
|
1149 if (args.length () == 1) |
|
1150 retval = args(0).is_real_type (); |
|
1151 else |
|
1152 print_usage ("isreal"); |
|
1153 |
|
1154 return retval; |
|
1155 } |
|
1156 |
3202
|
1157 DEFUN (isempty, args, , |
3373
|
1158 "-*- texinfo -*-\n\ |
|
1159 @deftypefn {Built-in Function} {} isempty (@var{a})\n\ |
|
1160 Return 1 if @var{a} is an empty matrix (either the number of rows, or\n\ |
|
1161 the number of columns, or both are zero). Otherwise, return 0.\n\ |
|
1162 @end deftypefn") |
3202
|
1163 { |
4233
|
1164 octave_value retval = false; |
3202
|
1165 |
|
1166 if (args.length () == 1) |
4559
|
1167 retval = args(0).is_empty (); |
3202
|
1168 else |
|
1169 print_usage ("isempty"); |
|
1170 |
|
1171 return retval; |
|
1172 } |
|
1173 |
3206
|
1174 DEFUN (isnumeric, args, , |
3428
|
1175 "-*- texinfo -*-\n\ |
|
1176 @deftypefn {Built-in Function} {} isnumeric (@var{x})\n\ |
|
1177 Return nonzero if @var{x} is a numeric object.\n\ |
|
1178 @end deftypefn") |
3206
|
1179 { |
|
1180 octave_value retval; |
|
1181 |
|
1182 if (args.length () == 1) |
3258
|
1183 retval = args(0).is_numeric_type (); |
3206
|
1184 else |
3238
|
1185 print_usage ("isnumeric"); |
3206
|
1186 |
|
1187 return retval; |
|
1188 } |
|
1189 |
4028
|
1190 DEFUN (islist, args, , |
3526
|
1191 "-*- texinfo -*-\n\ |
4028
|
1192 @deftypefn {Built-in Function} {} islist (@var{x})\n\ |
3428
|
1193 Return nonzero if @var{x} is a list.\n\ |
|
1194 @end deftypefn") |
3204
|
1195 { |
|
1196 octave_value retval; |
|
1197 |
|
1198 if (args.length () == 1) |
3258
|
1199 retval = args(0).is_list (); |
3204
|
1200 else |
4028
|
1201 print_usage ("islist"); |
3204
|
1202 |
|
1203 return retval; |
|
1204 } |
|
1205 |
4028
|
1206 DEFUN (ismatrix, args, , |
3321
|
1207 "-*- texinfo -*-\n\ |
4028
|
1208 @deftypefn {Built-in Function} {} ismatrix (@var{a})\n\ |
3321
|
1209 Return 1 if @var{a} is a matrix. Otherwise, return 0.\n\ |
3333
|
1210 @end deftypefn") |
3202
|
1211 { |
4233
|
1212 octave_value retval = false; |
3202
|
1213 |
|
1214 if (args.length () == 1) |
|
1215 { |
|
1216 octave_value arg = args(0); |
|
1217 |
3212
|
1218 if (arg.is_scalar_type () || arg.is_range ()) |
4233
|
1219 retval = true; |
3202
|
1220 else if (arg.is_matrix_type ()) |
4233
|
1221 retval = (arg.rows () >= 1 && arg.columns () >= 1); |
3202
|
1222 } |
|
1223 else |
4028
|
1224 print_usage ("ismatrix"); |
3202
|
1225 |
|
1226 return retval; |
|
1227 } |
|
1228 |
3354
|
1229 static octave_value |
|
1230 fill_matrix (const octave_value_list& args, double val, const char *fcn) |
523
|
1231 { |
3354
|
1232 octave_value retval; |
523
|
1233 |
|
1234 int nargin = args.length (); |
|
1235 |
4946
|
1236 oct_data_conv::data_type dt = oct_data_conv::dt_double; |
4481
|
1237 |
4946
|
1238 dim_vector dims (1, 1); |
4481
|
1239 |
|
1240 if (nargin > 0 && args(nargin-1).is_string ()) |
|
1241 { |
4946
|
1242 std::string nm = args(nargin-1).string_value (); |
4481
|
1243 nargin--; |
|
1244 |
4946
|
1245 dt = oct_data_conv::string_to_data_type (nm); |
|
1246 |
|
1247 if (error_state) |
|
1248 return retval; |
4481
|
1249 } |
|
1250 |
523
|
1251 switch (nargin) |
|
1252 { |
712
|
1253 case 0: |
|
1254 break; |
777
|
1255 |
610
|
1256 case 1: |
4481
|
1257 get_dimensions (args(0), fcn, dims); |
610
|
1258 break; |
777
|
1259 |
4563
|
1260 default: |
|
1261 { |
|
1262 dims.resize (nargin); |
4481
|
1263 |
4563
|
1264 for (int i = 0; i < nargin; i++) |
|
1265 { |
4732
|
1266 dims(i) = args(i).is_empty () ? 0 : args(i).int_value (); |
4481
|
1267 |
4563
|
1268 if (error_state) |
|
1269 { |
4732
|
1270 error ("%s: expecting scalar integer arguments", fcn); |
4563
|
1271 break; |
|
1272 } |
|
1273 } |
|
1274 } |
|
1275 break; |
4481
|
1276 } |
|
1277 |
|
1278 if (! error_state) |
|
1279 { |
4946
|
1280 dims.chop_trailing_singletons (); |
4565
|
1281 |
4481
|
1282 check_dimensions (dims, fcn); |
3354
|
1283 |
4946
|
1284 // XXX FIXME XXX -- perhaps this should be made extensible by |
|
1285 // using the class name to lookup a function to call to create |
|
1286 // the new value. |
|
1287 |
|
1288 // Note that automatic narrowing will handle conversion from |
|
1289 // NDArray to scalar. |
|
1290 |
4481
|
1291 if (! error_state) |
|
1292 { |
4946
|
1293 switch (dt) |
|
1294 { |
|
1295 case oct_data_conv::dt_int8: |
|
1296 retval = int8NDArray (dims, val); |
|
1297 break; |
4481
|
1298 |
4946
|
1299 case oct_data_conv::dt_uint8: |
|
1300 retval = uint8NDArray (dims, val); |
|
1301 break; |
|
1302 |
|
1303 case oct_data_conv::dt_int16: |
|
1304 retval = int16NDArray (dims, val); |
|
1305 break; |
|
1306 |
|
1307 case oct_data_conv::dt_uint16: |
|
1308 retval = uint16NDArray (dims, val); |
|
1309 break; |
|
1310 |
|
1311 case oct_data_conv::dt_int32: |
|
1312 retval = int32NDArray (dims, val); |
|
1313 break; |
777
|
1314 |
4946
|
1315 case oct_data_conv::dt_uint32: |
|
1316 retval = uint32NDArray (dims, val); |
|
1317 break; |
|
1318 |
|
1319 case oct_data_conv::dt_int64: |
|
1320 retval = int64NDArray (dims, val); |
|
1321 break; |
4481
|
1322 |
4946
|
1323 case oct_data_conv::dt_uint64: |
|
1324 retval = uint64NDArray (dims, val); |
|
1325 break; |
4481
|
1326 |
4946
|
1327 case oct_data_conv::dt_single: // XXX FIXME XXX |
|
1328 case oct_data_conv::dt_double: |
|
1329 retval = NDArray (dims, val); |
|
1330 break; |
|
1331 |
4986
|
1332 case oct_data_conv::dt_logical: |
|
1333 retval = boolNDArray (dims, val); |
|
1334 break; |
|
1335 |
4946
|
1336 default: |
|
1337 error ("%s: invalid class name", fcn); |
|
1338 break; |
4481
|
1339 } |
|
1340 } |
523
|
1341 } |
|
1342 |
|
1343 return retval; |
|
1344 } |
|
1345 |
3354
|
1346 DEFUN (ones, args, , |
3369
|
1347 "-*- texinfo -*-\n\ |
|
1348 @deftypefn {Built-in Function} {} ones (@var{x})\n\ |
|
1349 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m})\n\ |
4948
|
1350 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
1351 @deftypefnx {Built-in Function} {} ones (@dots{}, @var{class})\n\ |
4481
|
1352 Return a matrix or N-dimensional array whose elements are all 1.\n\ |
|
1353 The arguments are handled the same as the arguments for @code{eye}.\n\ |
3369
|
1354 \n\ |
|
1355 If you need to create a matrix whose values are all the same, you should\n\ |
|
1356 use an expression like\n\ |
|
1357 \n\ |
|
1358 @example\n\ |
|
1359 val_matrix = val * ones (n, m)\n\ |
|
1360 @end example\n\ |
4945
|
1361 \n\ |
|
1362 The optional argument @var{class}, allows @code{ones} to return an array of\n\ |
|
1363 the specified type, like\n\ |
|
1364 \n\ |
|
1365 @example\n\ |
|
1366 val = ones (n,m, \"uint8\")\n\ |
|
1367 @end example\n\ |
3369
|
1368 @end deftypefn") |
523
|
1369 { |
3354
|
1370 return fill_matrix (args, 1.0, "ones"); |
523
|
1371 } |
|
1372 |
3354
|
1373 DEFUN (zeros, args, , |
3369
|
1374 "-*- texinfo -*-\n\ |
|
1375 @deftypefn {Built-in Function} {} zeros (@var{x})\n\ |
|
1376 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m})\n\ |
4948
|
1377 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
|
1378 @deftypefnx {Built-in Function} {} zeros (@dots{}, @var{class})\n\ |
4481
|
1379 Return a matrix or N-dimensional array whose elements are all 0.\n\ |
|
1380 The arguments are handled the same as the arguments for @code{eye}.\n\ |
4945
|
1381 \n\ |
|
1382 The optional argument @var{class}, allows @code{zeros} to return an array of\n\ |
|
1383 the specified type, like\n\ |
|
1384 \n\ |
|
1385 @example\n\ |
|
1386 val = zeros (n,m, \"uint8\")\n\ |
|
1387 @end example\n\ |
3369
|
1388 @end deftypefn") |
523
|
1389 { |
3354
|
1390 return fill_matrix (args, 0.0, "zeros"); |
|
1391 } |
523
|
1392 |
4946
|
1393 template <class MT> |
|
1394 octave_value |
|
1395 identity_matrix (int nr, int nc) |
|
1396 { |
|
1397 octave_value retval; |
|
1398 |
|
1399 typename octave_array_type_traits<MT>::element_type one (1); |
|
1400 |
|
1401 if (nr == 1 && nc == 1) |
|
1402 retval = one; |
|
1403 else |
|
1404 { |
|
1405 dim_vector dims (nr, nc); |
|
1406 |
|
1407 typename octave_array_type_traits<MT>::element_type zero (0); |
|
1408 |
|
1409 MT m (dims, zero); |
|
1410 |
|
1411 if (nr > 0 && nc > 0) |
|
1412 { |
|
1413 int n = std::min (nr, nc); |
|
1414 |
|
1415 for (int i = 0; i < n; i++) |
|
1416 m(i,i) = one; |
|
1417 } |
|
1418 |
|
1419 retval = m; |
|
1420 } |
|
1421 |
|
1422 return retval; |
|
1423 } |
|
1424 |
5058
|
1425 #define INSTANTIATE_EYE(T) \ |
|
1426 template octave_value identity_matrix<T> (int, int) |
|
1427 |
|
1428 INSTANTIATE_EYE (int8NDArray); |
|
1429 INSTANTIATE_EYE (uint8NDArray); |
|
1430 INSTANTIATE_EYE (int16NDArray); |
|
1431 INSTANTIATE_EYE (uint16NDArray); |
|
1432 INSTANTIATE_EYE (int32NDArray); |
|
1433 INSTANTIATE_EYE (uint32NDArray); |
|
1434 INSTANTIATE_EYE (int64NDArray); |
|
1435 INSTANTIATE_EYE (uint64NDArray); |
|
1436 INSTANTIATE_EYE (NDArray); |
|
1437 INSTANTIATE_EYE (boolNDArray); |
|
1438 |
4945
|
1439 static octave_value |
4948
|
1440 identity_matrix (int nr, int nc, oct_data_conv::data_type dt) |
4945
|
1441 { |
|
1442 octave_value retval; |
|
1443 |
4946
|
1444 // XXX FIXME XXX -- perhaps this should be made extensible by using |
|
1445 // the class name to lookup a function to call to create the new |
|
1446 // value. |
|
1447 |
|
1448 if (! error_state) |
|
1449 { |
|
1450 switch (dt) |
|
1451 { |
|
1452 case oct_data_conv::dt_int8: |
|
1453 retval = identity_matrix<int8NDArray> (nr, nc); |
|
1454 break; |
|
1455 |
|
1456 case oct_data_conv::dt_uint8: |
|
1457 retval = identity_matrix<uint8NDArray> (nr, nc); |
|
1458 break; |
|
1459 |
|
1460 case oct_data_conv::dt_int16: |
|
1461 retval = identity_matrix<int16NDArray> (nr, nc); |
|
1462 break; |
4945
|
1463 |
4946
|
1464 case oct_data_conv::dt_uint16: |
|
1465 retval = identity_matrix<uint16NDArray> (nr, nc); |
|
1466 break; |
|
1467 |
|
1468 case oct_data_conv::dt_int32: |
|
1469 retval = identity_matrix<int32NDArray> (nr, nc); |
|
1470 break; |
|
1471 |
|
1472 case oct_data_conv::dt_uint32: |
|
1473 retval = identity_matrix<uint32NDArray> (nr, nc); |
|
1474 break; |
4945
|
1475 |
4946
|
1476 case oct_data_conv::dt_int64: |
|
1477 retval = identity_matrix<int64NDArray> (nr, nc); |
|
1478 break; |
|
1479 |
|
1480 case oct_data_conv::dt_uint64: |
|
1481 retval = identity_matrix<uint64NDArray> (nr, nc); |
|
1482 break; |
4945
|
1483 |
4946
|
1484 case oct_data_conv::dt_single: // XXX FIXME XXX |
|
1485 case oct_data_conv::dt_double: |
|
1486 retval = identity_matrix<NDArray> (nr, nc); |
|
1487 break; |
4945
|
1488 |
4986
|
1489 case oct_data_conv::dt_logical: |
|
1490 retval = identity_matrix<boolNDArray> (nr, nc); |
|
1491 break; |
|
1492 |
4946
|
1493 default: |
|
1494 error ("eye: invalid class name"); |
|
1495 break; |
4945
|
1496 } |
|
1497 } |
|
1498 |
|
1499 return retval; |
|
1500 } |
|
1501 |
4946
|
1502 #undef INT_EYE_MATRIX |
|
1503 |
1957
|
1504 DEFUN (eye, args, , |
3369
|
1505 "-*- texinfo -*-\n\ |
|
1506 @deftypefn {Built-in Function} {} eye (@var{x})\n\ |
|
1507 @deftypefnx {Built-in Function} {} eye (@var{n}, @var{m})\n\ |
4948
|
1508 @deftypefnx {Built-in Function} {} eye (@dots{}, @var{class})\n\ |
3369
|
1509 Return an identity matrix. If invoked with a single scalar argument,\n\ |
|
1510 @code{eye} returns a square matrix with the dimension specified. If you\n\ |
|
1511 supply two scalar arguments, @code{eye} takes them to be the number of\n\ |
|
1512 rows and columns. If given a vector with two elements, @code{eye} uses\n\ |
|
1513 the values of the elements as the number of rows and columns,\n\ |
|
1514 respectively. For example,\n\ |
|
1515 \n\ |
|
1516 @example\n\ |
|
1517 @group\n\ |
|
1518 eye (3)\n\ |
|
1519 @result{} 1 0 0\n\ |
|
1520 0 1 0\n\ |
|
1521 0 0 1\n\ |
|
1522 @end group\n\ |
|
1523 @end example\n\ |
|
1524 \n\ |
|
1525 The following expressions all produce the same result:\n\ |
|
1526 \n\ |
|
1527 @example\n\ |
|
1528 @group\n\ |
|
1529 eye (2)\n\ |
|
1530 @equiv{}\n\ |
|
1531 eye (2, 2)\n\ |
|
1532 @equiv{}\n\ |
|
1533 eye (size ([1, 2; 3, 4])\n\ |
|
1534 @end group\n\ |
|
1535 @end example\n\ |
|
1536 \n\ |
4945
|
1537 The optional argument @var{class}, allows @code{eye} to return an array of\n\ |
|
1538 the specified type, like\n\ |
|
1539 \n\ |
|
1540 @example\n\ |
|
1541 val = zeros (n,m, \"uint8\")\n\ |
|
1542 @end example\n\ |
|
1543 \n\ |
3369
|
1544 For compatibility with @sc{Matlab}, calling @code{eye} with no arguments\n\ |
|
1545 is equivalent to calling it with an argument of 1.\n\ |
|
1546 @end deftypefn") |
523
|
1547 { |
3354
|
1548 octave_value retval; |
523
|
1549 |
4948
|
1550 int nargin = args.length (); |
4945
|
1551 |
4948
|
1552 oct_data_conv::data_type dt = oct_data_conv::dt_double; |
523
|
1553 |
4945
|
1554 // Check for type information. |
|
1555 |
|
1556 if (nargin > 0 && args(nargin-1).is_string ()) |
|
1557 { |
4948
|
1558 std::string nm = args(nargin-1).string_value (); |
4945
|
1559 nargin--; |
4948
|
1560 |
|
1561 dt = oct_data_conv::string_to_data_type (nm); |
|
1562 |
|
1563 if (error_state) |
|
1564 return retval; |
4945
|
1565 } |
|
1566 |
523
|
1567 switch (nargin) |
|
1568 { |
712
|
1569 case 0: |
4948
|
1570 retval = identity_matrix (1, 1, dt); |
712
|
1571 break; |
777
|
1572 |
610
|
1573 case 1: |
3354
|
1574 { |
|
1575 int nr, nc; |
|
1576 get_dimensions (args(0), "eye", nr, nc); |
|
1577 |
|
1578 if (! error_state) |
4948
|
1579 retval = identity_matrix (nr, nc, dt); |
3354
|
1580 } |
610
|
1581 break; |
777
|
1582 |
523
|
1583 case 2: |
3354
|
1584 { |
|
1585 int nr, nc; |
|
1586 get_dimensions (args(0), args(1), "eye", nr, nc); |
|
1587 |
|
1588 if (! error_state) |
4948
|
1589 retval = identity_matrix (nr, nc, dt); |
3354
|
1590 } |
523
|
1591 break; |
777
|
1592 |
523
|
1593 default: |
|
1594 print_usage ("eye"); |
|
1595 break; |
|
1596 } |
|
1597 |
|
1598 return retval; |
|
1599 } |
|
1600 |
1957
|
1601 DEFUN (linspace, args, , |
3369
|
1602 "-*- texinfo -*-\n\ |
|
1603 @deftypefn {Built-in Function} {} linspace (@var{base}, @var{limit}, @var{n})\n\ |
|
1604 Return a row vector with @var{n} linearly spaced elements between\n\ |
|
1605 @var{base} and @var{limit}. The number of elements, @var{n}, must be\n\ |
|
1606 greater than 1. The @var{base} and @var{limit} are always included in\n\ |
|
1607 the range. If @var{base} is greater than @var{limit}, the elements are\n\ |
|
1608 stored in decreasing order. If the number of points is not specified, a\n\ |
|
1609 value of 100 is used.\n\ |
1100
|
1610 \n\ |
4455
|
1611 The @code{linspace} function always returns a row vector.\n\ |
3369
|
1612 @end deftypefn") |
1100
|
1613 { |
3418
|
1614 octave_value retval; |
1100
|
1615 |
|
1616 int nargin = args.length (); |
|
1617 |
|
1618 int npoints = 100; |
|
1619 |
1940
|
1620 if (nargin != 2 && nargin != 3) |
|
1621 { |
|
1622 print_usage ("linspace"); |
|
1623 return retval; |
|
1624 } |
|
1625 |
1100
|
1626 if (nargin == 3) |
4732
|
1627 npoints = args(2).int_value (); |
1100
|
1628 |
|
1629 if (! error_state) |
|
1630 { |
3322
|
1631 octave_value arg_1 = args(0); |
|
1632 octave_value arg_2 = args(1); |
1100
|
1633 |
3322
|
1634 if (arg_1.is_complex_type () || arg_2.is_complex_type ()) |
|
1635 { |
|
1636 Complex x1 = arg_1.complex_value (); |
|
1637 Complex x2 = arg_2.complex_value (); |
|
1638 |
|
1639 if (! error_state) |
1100
|
1640 { |
3322
|
1641 ComplexRowVector rv = linspace (x1, x2, npoints); |
1100
|
1642 |
|
1643 if (! error_state) |
3418
|
1644 retval = rv; |
1100
|
1645 } |
|
1646 } |
|
1647 else |
3322
|
1648 { |
|
1649 double x1 = arg_1.double_value (); |
|
1650 double x2 = arg_2.double_value (); |
|
1651 |
|
1652 if (! error_state) |
|
1653 { |
|
1654 RowVector rv = linspace (x1, x2, npoints); |
|
1655 |
|
1656 if (! error_state) |
3418
|
1657 retval = rv; |
3322
|
1658 } |
|
1659 } |
1100
|
1660 } |
4732
|
1661 else |
|
1662 error ("linspace: expecting third argument to be an integer"); |
1100
|
1663 |
|
1664 return retval; |
|
1665 } |
|
1666 |
4567
|
1667 DEFUN (reshape, args, , |
|
1668 "-*- texinfo -*-\n\ |
|
1669 @deftypefn {Function File} {} reshape (@var{a}, @var{m}, @var{n}, @dots{})\n\ |
|
1670 @deftypefnx {Function File} {} reshape (@var{a}, @var{siz})\n\ |
|
1671 Return a matrix with the given dimensions whose elements are taken\n\ |
|
1672 from the matrix @var{a}. The elements of the matrix are access in\n\ |
|
1673 column-major order (like Fortran arrays are stored).\n\ |
|
1674 \n\ |
|
1675 For example,\n\ |
|
1676 \n\ |
|
1677 @example\n\ |
|
1678 @group\n\ |
|
1679 reshape ([1, 2, 3, 4], 2, 2)\n\ |
|
1680 @result{} 1 3\n\ |
|
1681 2 4\n\ |
|
1682 @end group\n\ |
|
1683 @end example\n\ |
|
1684 \n\ |
|
1685 @noindent\n\ |
|
1686 Note that the total number of elements in the original\n\ |
|
1687 matrix must match the total number of elements in the new matrix.\n\ |
5013
|
1688 \n\ |
|
1689 A single dimension of the return matrix can be unknown and is flagged\n\ |
|
1690 by an empty argument.\n\ |
4567
|
1691 @end deftypefn") |
|
1692 { |
|
1693 octave_value retval; |
|
1694 |
|
1695 int nargin = args.length (); |
|
1696 |
|
1697 Array<int> new_size; |
|
1698 |
|
1699 if (nargin == 2) |
|
1700 new_size = args(1).int_vector_value (); |
|
1701 else if (nargin > 2) |
|
1702 { |
|
1703 new_size.resize (nargin-1); |
5013
|
1704 int empty_dim = -1; |
|
1705 |
4567
|
1706 for (int i = 1; i < nargin; i++) |
|
1707 { |
5013
|
1708 if (args(i).is_empty ()) |
|
1709 if (empty_dim > 0) |
|
1710 { |
|
1711 error ("reshape: only a single dimension can be unknown"); |
|
1712 break; |
|
1713 } |
|
1714 else |
|
1715 { |
|
1716 empty_dim = i; |
|
1717 new_size(i-1) = 1; |
|
1718 } |
|
1719 else |
|
1720 { |
|
1721 new_size(i-1) = args(i).int_value (); |
4567
|
1722 |
5013
|
1723 if (error_state) |
|
1724 break; |
|
1725 } |
|
1726 } |
|
1727 |
|
1728 if (! error_state && (empty_dim > 0)) |
|
1729 { |
|
1730 int nel = 1; |
|
1731 for (int i = 0; i < nargin - 1; i++) |
|
1732 nel *= new_size(i); |
|
1733 |
|
1734 if (nel == 0) |
|
1735 new_size(empty_dim-1) = 0; |
|
1736 else |
|
1737 { |
|
1738 int size_empty_dim = args(0).numel () / nel; |
|
1739 |
|
1740 if (args(0).numel () != size_empty_dim * nel) |
|
1741 error ("reshape: size is not divisble by the product of known dimensions (= %d)", nel); |
|
1742 else |
|
1743 new_size(empty_dim-1) = size_empty_dim; |
|
1744 } |
4567
|
1745 } |
|
1746 } |
|
1747 else |
|
1748 { |
|
1749 print_usage ("reshape"); |
|
1750 return retval; |
|
1751 } |
|
1752 |
|
1753 if (error_state) |
|
1754 { |
|
1755 error ("reshape: invalid arguments"); |
|
1756 return retval; |
|
1757 } |
|
1758 |
4739
|
1759 // Remove trailing singletons in new_size, but leave at least 2 |
|
1760 // elements. |
|
1761 |
4567
|
1762 int n = new_size.length (); |
|
1763 |
4739
|
1764 while (n > 2) |
|
1765 { |
|
1766 if (new_size(n-1) == 1) |
|
1767 n--; |
|
1768 else |
|
1769 break; |
|
1770 } |
|
1771 |
|
1772 new_size.resize (n); |
|
1773 |
4567
|
1774 if (n < 2) |
|
1775 { |
|
1776 error ("reshape: expecting size to be vector with at least 2 elements"); |
|
1777 return retval; |
|
1778 } |
|
1779 |
|
1780 dim_vector new_dims; |
|
1781 |
|
1782 new_dims.resize (n); |
|
1783 |
|
1784 for (int i = 0; i < n; i++) |
|
1785 new_dims(i) = new_size(i); |
|
1786 |
|
1787 octave_value arg = args(0); |
|
1788 |
|
1789 if (new_dims.numel () == arg.numel ()) |
|
1790 retval = (new_dims == arg.dims ()) ? arg : arg.reshape (new_dims); |
|
1791 else |
|
1792 error ("reshape: size mismatch"); |
|
1793 |
|
1794 return retval; |
|
1795 } |
|
1796 |
4532
|
1797 DEFUN (squeeze, args, , |
|
1798 "-*- texinfo -*-\n\ |
|
1799 @deftypefn {Built-in Function} {} squeeze (@var{x})\n\ |
|
1800 Remove singleton dimensions from @var{x} and return the result.\n\ |
|
1801 @end deftypefn") |
|
1802 { |
|
1803 octave_value retval; |
|
1804 |
|
1805 if (args.length () == 1) |
4545
|
1806 retval = args(0).squeeze (); |
4532
|
1807 else |
|
1808 print_usage ("squeeze"); |
|
1809 |
|
1810 return retval; |
|
1811 } |
|
1812 |
2184
|
1813 void |
|
1814 symbols_of_data (void) |
|
1815 { |
3321
|
1816 |
|
1817 #define IMAGINARY_DOC_STRING "-*- texinfo -*-\n\ |
|
1818 @defvr {Built-in Variable} I\n\ |
|
1819 @defvrx {Built-in Variable} J\n\ |
|
1820 @defvrx {Built-in Variable} i\n\ |
|
1821 @defvrx {Built-in Variable} j\n\ |
|
1822 A pure imaginary number, defined as\n\ |
|
1823 @iftex\n\ |
|
1824 @tex\n\ |
|
1825 $\\sqrt{-1}$.\n\ |
|
1826 @end tex\n\ |
|
1827 @end iftex\n\ |
|
1828 @ifinfo\n\ |
|
1829 @code{sqrt (-1)}.\n\ |
|
1830 @end ifinfo\n\ |
4845
|
1831 These built-in variables behave like functions so you can use the names\n\ |
|
1832 for other purposes. If you use them as variables and assign values to\n\ |
|
1833 them and then clear them, they once again assume their special predefined\n\ |
|
1834 values @xref{Status of Variables}.\n\ |
3321
|
1835 @end defvr" |
|
1836 |
|
1837 #define INFINITY_DOC_STRING "-*- texinfo -*-\n\ |
|
1838 @defvr {Built-in Variable} Inf\n\ |
|
1839 @defvrx {Built-in Variable} inf\n\ |
|
1840 Infinity. This is the result of an operation like 1/0, or an operation\n\ |
|
1841 that results in a floating point overflow.\n\ |
|
1842 @end defvr" |
|
1843 |
|
1844 #define NAN_DOC_STRING "-*- texinfo -*-\n\ |
|
1845 @defvr {Built-in Variable} NaN\n\ |
|
1846 @defvrx {Built-in Variable} nan\n\ |
|
1847 Not a number. This is the result of an operation like\n\ |
|
1848 @iftex\n\ |
|
1849 @tex\n\ |
|
1850 $0/0$, or $\\infty - \\infty$,\n\ |
|
1851 @end tex\n\ |
|
1852 @end iftex\n\ |
|
1853 @ifinfo\n\ |
|
1854 0/0, or @samp{Inf - Inf},\n\ |
|
1855 @end ifinfo\n\ |
|
1856 or any operation with a NaN.\n\ |
|
1857 \n\ |
|
1858 Note that NaN always compares not equal to NaN. This behavior is\n\ |
|
1859 specified by the IEEE standard for floating point arithmetic. To\n\ |
|
1860 find NaN values, you must use the @code{isnan} function.\n\ |
|
1861 @end defvr" |
|
1862 |
3141
|
1863 DEFCONST (I, Complex (0.0, 1.0), |
3321
|
1864 IMAGINARY_DOC_STRING); |
2184
|
1865 |
4102
|
1866 DEFCONST (Inf, lo_ieee_inf_value (), |
3321
|
1867 INFINITY_DOC_STRING); |
2184
|
1868 |
3141
|
1869 DEFCONST (J, Complex (0.0, 1.0), |
3321
|
1870 IMAGINARY_DOC_STRING); |
2184
|
1871 |
4102
|
1872 DEFCONST (NA, lo_ieee_na_value (), |
4025
|
1873 "-*- texinfo -*-\n\ |
|
1874 @defvr {Built-in Variable} NA\n\ |
|
1875 Missing value.\n\ |
|
1876 @end defvr"); |
|
1877 |
4102
|
1878 DEFCONST (NaN, lo_ieee_nan_value (), |
3321
|
1879 NAN_DOC_STRING); |
2184
|
1880 |
|
1881 #if defined (M_E) |
|
1882 double e_val = M_E; |
|
1883 #else |
|
1884 double e_val = exp (1.0); |
|
1885 #endif |
|
1886 |
3141
|
1887 DEFCONST (e, e_val, |
3321
|
1888 "-*- texinfo -*-\n\ |
|
1889 @defvr {Built-in Variable} e\n\ |
|
1890 The base of natural logarithms. The constant\n\ |
|
1891 @iftex\n\ |
|
1892 @tex\n\ |
|
1893 $e$\n\ |
|
1894 @end tex\n\ |
|
1895 @end iftex\n\ |
|
1896 @ifinfo\n\ |
|
1897 @var{e}\n\ |
|
1898 @end ifinfo\n\ |
|
1899 satisfies the equation\n\ |
|
1900 @iftex\n\ |
|
1901 @tex\n\ |
|
1902 $\\log (e) = 1$.\n\ |
|
1903 @end tex\n\ |
|
1904 @end iftex\n\ |
|
1905 @ifinfo\n\ |
|
1906 @code{log} (@var{e}) = 1.\n\ |
|
1907 @end ifinfo\n\ |
|
1908 @end defvr"); |
2184
|
1909 |
3141
|
1910 DEFCONST (eps, DBL_EPSILON, |
3321
|
1911 "-*- texinfo -*-\n\ |
|
1912 @defvr {Built-in Variable} eps\n\ |
|
1913 The machine precision. More precisely, @code{eps} is the largest\n\ |
|
1914 relative spacing between any two adjacent numbers in the machine's\n\ |
|
1915 floating point system. This number is obviously system-dependent. On\n\ |
|
1916 machines that support 64 bit IEEE floating point arithmetic, @code{eps}\n\ |
|
1917 is approximately\n\ |
|
1918 @ifinfo\n\ |
|
1919 2.2204e-16.\n\ |
|
1920 @end ifinfo\n\ |
|
1921 @iftex\n\ |
|
1922 @tex\n\ |
|
1923 $2.2204\\times10^{-16}$.\n\ |
|
1924 @end tex\n\ |
|
1925 @end iftex\n\ |
|
1926 @end defvr"); |
2184
|
1927 |
3258
|
1928 DEFCONST (false, false, |
3443
|
1929 "-*- texinfo -*-\n\ |
|
1930 @defvr {Built-in Variable} false\n\ |
|
1931 Logical false value.\n\ |
|
1932 @end defvr"); |
3258
|
1933 |
3141
|
1934 DEFCONST (i, Complex (0.0, 1.0), |
3321
|
1935 IMAGINARY_DOC_STRING); |
2184
|
1936 |
4102
|
1937 DEFCONST (inf, lo_ieee_inf_value (), |
3321
|
1938 INFINITY_DOC_STRING); |
2184
|
1939 |
3141
|
1940 DEFCONST (j, Complex (0.0, 1.0), |
3321
|
1941 IMAGINARY_DOC_STRING); |
2184
|
1942 |
4102
|
1943 DEFCONST (nan, lo_ieee_nan_value (), |
3321
|
1944 NAN_DOC_STRING); |
2184
|
1945 |
|
1946 #if defined (M_PI) |
|
1947 double pi_val = M_PI; |
|
1948 #else |
|
1949 double pi_val = 4.0 * atan (1.0); |
|
1950 #endif |
|
1951 |
3141
|
1952 DEFCONST (pi, pi_val, |
3321
|
1953 "-*- texinfo -*-\n\ |
|
1954 @defvr {Built-in Variable} pi\n\ |
|
1955 The ratio of the circumference of a circle to its diameter.\n\ |
|
1956 Internally, @code{pi} is computed as @samp{4.0 * atan (1.0)}.\n\ |
|
1957 @end defvr"); |
2184
|
1958 |
3141
|
1959 DEFCONST (realmax, DBL_MAX, |
3321
|
1960 "-*- texinfo -*-\n\ |
|
1961 @defvr {Built-in Variable} realmax\n\ |
|
1962 The largest floating point number that is representable. The actual\n\ |
4303
|
1963 value is system-dependent. On machines that support 64-bit IEEE\n\ |
3321
|
1964 floating point arithmetic, @code{realmax} is approximately\n\ |
|
1965 @ifinfo\n\ |
|
1966 1.7977e+308\n\ |
|
1967 @end ifinfo\n\ |
|
1968 @iftex\n\ |
|
1969 @tex\n\ |
|
1970 $1.7977\\times10^{308}$.\n\ |
|
1971 @end tex\n\ |
|
1972 @end iftex\n\ |
|
1973 @end defvr"); |
2184
|
1974 |
3141
|
1975 DEFCONST (realmin, DBL_MIN, |
3321
|
1976 "-*- texinfo -*-\n\ |
|
1977 @defvr {Built-in Variable} realmin\n\ |
4303
|
1978 The smallest normalized floating point number that is representable.\n\ |
|
1979 The actual value is system-dependent. On machines that support\n\ |
|
1980 64-bit IEEE floating point arithmetic, @code{realmin} is approximately\n\ |
3321
|
1981 @ifinfo\n\ |
|
1982 2.2251e-308\n\ |
|
1983 @end ifinfo\n\ |
|
1984 @iftex\n\ |
|
1985 @tex\n\ |
|
1986 $2.2251\\times10^{-308}$.\n\ |
|
1987 @end tex\n\ |
|
1988 @end iftex\n\ |
|
1989 @end defvr"); |
2188
|
1990 |
3258
|
1991 DEFCONST (true, true, |
3443
|
1992 "-*- texinfo -*-\n\ |
|
1993 @defvr {Built-in Variable} true\n\ |
|
1994 Logical true value.\n\ |
|
1995 @end defvr"); |
3354
|
1996 |
2184
|
1997 } |
|
1998 |
523
|
1999 /* |
|
2000 ;;; Local Variables: *** |
|
2001 ;;; mode: C++ *** |
|
2002 ;;; End: *** |
|
2003 */ |