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