2928
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
|
4 2005, 2006, 2007 John W. Eaton |
2928
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
2928
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
2928
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
3145
|
28 #include <cmath> |
|
29 |
2928
|
30 #include "lo-ieee.h" |
3248
|
31 #include "lo-mappers.h" |
4844
|
32 #include "dNDArray.h" |
|
33 #include "CNDArray.h" |
4153
|
34 #include "quit.h" |
2928
|
35 |
|
36 #include "defun-dld.h" |
|
37 #include "error.h" |
|
38 #include "gripes.h" |
|
39 #include "oct-obj.h" |
|
40 |
4844
|
41 #include "ov-cx-mat.h" |
|
42 |
7189
|
43 #define MINMAX_DOUBLE_BODY(FCN) \ |
|
44 { \ |
6711
|
45 bool single_arg = (nargin == 1) || (arg2.is_empty() && nargin == 3); \ |
4844
|
46 \ |
|
47 if (single_arg && (nargout == 1 || nargout == 0)) \ |
3747
|
48 { \ |
|
49 if (arg1.is_real_type ()) \ |
|
50 { \ |
4844
|
51 NDArray m = arg1.array_value (); \ |
3747
|
52 \ |
|
53 if (! error_state) \ |
|
54 { \ |
4844
|
55 NDArray n = m. FCN (dim); \ |
|
56 retval(0) = n; \ |
3747
|
57 } \ |
|
58 } \ |
|
59 else if (arg1.is_complex_type ()) \ |
|
60 { \ |
4844
|
61 ComplexNDArray m = arg1.complex_array_value (); \ |
3747
|
62 \ |
|
63 if (! error_state) \ |
|
64 { \ |
4844
|
65 ComplexNDArray n = m. FCN (dim); \ |
|
66 retval(0) = n; \ |
3747
|
67 } \ |
|
68 } \ |
|
69 else \ |
|
70 gripe_wrong_type_arg (#FCN, arg1); \ |
|
71 } \ |
4844
|
72 else if (single_arg && nargout == 2) \ |
3747
|
73 { \ |
5275
|
74 ArrayN<octave_idx_type> index; \ |
3747
|
75 \ |
|
76 if (arg1.is_real_type ()) \ |
|
77 { \ |
4844
|
78 NDArray m = arg1.array_value (); \ |
3747
|
79 \ |
|
80 if (! error_state) \ |
|
81 { \ |
4844
|
82 NDArray n = m. FCN (index, dim); \ |
|
83 retval(0) = n; \ |
3747
|
84 } \ |
|
85 } \ |
|
86 else if (arg1.is_complex_type ()) \ |
|
87 { \ |
4844
|
88 ComplexNDArray m = arg1.complex_array_value (); \ |
3747
|
89 \ |
|
90 if (! error_state) \ |
|
91 { \ |
4844
|
92 ComplexNDArray n = m. FCN (index, dim); \ |
|
93 retval(0) = n; \ |
3747
|
94 } \ |
|
95 } \ |
|
96 else \ |
|
97 gripe_wrong_type_arg (#FCN, arg1); \ |
|
98 \ |
5275
|
99 octave_idx_type len = index.numel (); \ |
3747
|
100 \ |
|
101 if (len > 0) \ |
|
102 { \ |
4102
|
103 double nan_val = lo_ieee_nan_value (); \ |
|
104 \ |
4844
|
105 NDArray idx (index.dims ()); \ |
3747
|
106 \ |
5275
|
107 for (octave_idx_type i = 0; i < len; i++) \ |
3747
|
108 { \ |
4153
|
109 OCTAVE_QUIT; \ |
3747
|
110 int tmp = index.elem (i) + 1; \ |
|
111 idx.elem (i) = (tmp <= 0) \ |
4102
|
112 ? nan_val : static_cast<double> (tmp); \ |
3747
|
113 } \ |
|
114 \ |
|
115 retval(1) = idx; \ |
|
116 } \ |
|
117 else \ |
4844
|
118 retval(1) = NDArray (); \ |
3747
|
119 } \ |
4844
|
120 else \ |
3747
|
121 { \ |
|
122 int arg1_is_scalar = arg1.is_scalar_type (); \ |
|
123 int arg2_is_scalar = arg2.is_scalar_type (); \ |
|
124 \ |
|
125 int arg1_is_complex = arg1.is_complex_type (); \ |
|
126 int arg2_is_complex = arg2.is_complex_type (); \ |
|
127 \ |
|
128 if (arg1_is_scalar) \ |
|
129 { \ |
|
130 if (arg1_is_complex || arg2_is_complex) \ |
|
131 { \ |
|
132 Complex c1 = arg1.complex_value (); \ |
4844
|
133 ComplexNDArray m2 = arg2.complex_array_value (); \ |
3747
|
134 if (! error_state) \ |
|
135 { \ |
4844
|
136 ComplexNDArray result = FCN (c1, m2); \ |
3747
|
137 if (! error_state) \ |
|
138 retval(0) = result; \ |
|
139 } \ |
|
140 } \ |
|
141 else \ |
|
142 { \ |
|
143 double d1 = arg1.double_value (); \ |
4844
|
144 NDArray m2 = arg2.array_value (); \ |
3747
|
145 \ |
|
146 if (! error_state) \ |
|
147 { \ |
4844
|
148 NDArray result = FCN (d1, m2); \ |
3747
|
149 if (! error_state) \ |
|
150 retval(0) = result; \ |
|
151 } \ |
|
152 } \ |
|
153 } \ |
|
154 else if (arg2_is_scalar) \ |
|
155 { \ |
|
156 if (arg1_is_complex || arg2_is_complex) \ |
|
157 { \ |
4844
|
158 ComplexNDArray m1 = arg1.complex_array_value (); \ |
3747
|
159 \ |
|
160 if (! error_state) \ |
|
161 { \ |
|
162 Complex c2 = arg2.complex_value (); \ |
4844
|
163 ComplexNDArray result = FCN (m1, c2); \ |
3747
|
164 if (! error_state) \ |
|
165 retval(0) = result; \ |
|
166 } \ |
|
167 } \ |
|
168 else \ |
|
169 { \ |
4844
|
170 NDArray m1 = arg1.array_value (); \ |
3747
|
171 \ |
|
172 if (! error_state) \ |
|
173 { \ |
|
174 double d2 = arg2.double_value (); \ |
4844
|
175 NDArray result = FCN (m1, d2); \ |
3747
|
176 if (! error_state) \ |
|
177 retval(0) = result; \ |
|
178 } \ |
|
179 } \ |
|
180 } \ |
|
181 else \ |
|
182 { \ |
|
183 if (arg1_is_complex || arg2_is_complex) \ |
|
184 { \ |
4844
|
185 ComplexNDArray m1 = arg1.complex_array_value (); \ |
3747
|
186 \ |
|
187 if (! error_state) \ |
|
188 { \ |
4844
|
189 ComplexNDArray m2 = arg2.complex_array_value (); \ |
3747
|
190 \ |
|
191 if (! error_state) \ |
|
192 { \ |
4844
|
193 ComplexNDArray result = FCN (m1, m2); \ |
3747
|
194 if (! error_state) \ |
|
195 retval(0) = result; \ |
|
196 } \ |
|
197 } \ |
|
198 } \ |
|
199 else \ |
|
200 { \ |
4844
|
201 NDArray m1 = arg1.array_value (); \ |
3747
|
202 \ |
|
203 if (! error_state) \ |
|
204 { \ |
4844
|
205 NDArray m2 = arg2.array_value (); \ |
3747
|
206 \ |
|
207 if (! error_state) \ |
|
208 { \ |
4844
|
209 NDArray result = FCN (m1, m2); \ |
3747
|
210 if (! error_state) \ |
|
211 retval(0) = result; \ |
|
212 } \ |
|
213 } \ |
|
214 } \ |
|
215 } \ |
|
216 } \ |
7189
|
217 } |
|
218 |
|
219 #define MINMAX_INT_BODY(FCN, TYP) \ |
|
220 { \ |
|
221 bool single_arg = (nargin == 1) || (arg2.is_empty() && nargin == 3); \ |
3747
|
222 \ |
7189
|
223 if (single_arg && (nargout == 1 || nargout == 0)) \ |
|
224 { \ |
|
225 TYP ## NDArray m = arg1. TYP ## _array_value (); \ |
|
226 \ |
|
227 if (! error_state) \ |
|
228 { \ |
|
229 TYP ## NDArray n = m. FCN (dim); \ |
|
230 retval(0) = n; \ |
|
231 } \ |
|
232 } \ |
|
233 else if (single_arg && nargout == 2) \ |
|
234 { \ |
|
235 ArrayN<octave_idx_type> index; \ |
|
236 \ |
|
237 TYP ## NDArray m = arg1. TYP ## _array_value (); \ |
|
238 \ |
|
239 if (! error_state) \ |
|
240 { \ |
|
241 TYP ## NDArray n = m. FCN (index, dim); \ |
|
242 retval(0) = n; \ |
|
243 } \ |
|
244 \ |
|
245 octave_idx_type len = index.numel (); \ |
|
246 \ |
|
247 if (len > 0) \ |
|
248 { \ |
|
249 double nan_val = lo_ieee_nan_value (); \ |
|
250 \ |
|
251 NDArray idx (index.dims ()); \ |
|
252 \ |
|
253 for (octave_idx_type i = 0; i < len; i++) \ |
|
254 { \ |
|
255 OCTAVE_QUIT; \ |
|
256 int tmp = index.elem (i) + 1; \ |
|
257 idx.elem (i) = (tmp <= 0) \ |
|
258 ? nan_val : static_cast<double> (tmp); \ |
|
259 } \ |
|
260 \ |
|
261 retval(1) = idx; \ |
|
262 } \ |
|
263 else \ |
|
264 retval(1) = NDArray (); \ |
|
265 } \ |
|
266 else \ |
|
267 { \ |
|
268 int arg1_is_scalar = arg1.is_scalar_type (); \ |
|
269 int arg2_is_scalar = arg2.is_scalar_type (); \ |
|
270 \ |
|
271 if (arg1_is_scalar) \ |
|
272 { \ |
|
273 octave_ ## TYP d1 = arg1. TYP ## _scalar_value (); \ |
|
274 TYP ## NDArray m2 = arg2. TYP ## _array_value (); \ |
|
275 \ |
|
276 if (! error_state) \ |
|
277 { \ |
|
278 TYP ## NDArray result = FCN (d1, m2); \ |
|
279 if (! error_state) \ |
|
280 retval(0) = result; \ |
|
281 } \ |
|
282 } \ |
|
283 else if (arg2_is_scalar) \ |
|
284 { \ |
|
285 TYP ## NDArray m1 = arg1. TYP ## _array_value (); \ |
|
286 \ |
|
287 if (! error_state) \ |
|
288 { \ |
|
289 octave_ ## TYP d2 = arg2. TYP ## _scalar_value (); \ |
|
290 TYP ## NDArray result = FCN (m1, d2); \ |
|
291 if (! error_state) \ |
|
292 retval(0) = result; \ |
|
293 } \ |
|
294 } \ |
|
295 else \ |
|
296 { \ |
|
297 TYP ## NDArray m1 = arg1. TYP ## _array_value (); \ |
|
298 \ |
|
299 if (! error_state) \ |
|
300 { \ |
|
301 TYP ## NDArray m2 = arg2. TYP ## _array_value (); \ |
|
302 \ |
|
303 if (! error_state) \ |
|
304 { \ |
|
305 TYP ## NDArray result = FCN (m1, m2); \ |
|
306 if (! error_state) \ |
|
307 retval(0) = result; \ |
|
308 } \ |
|
309 } \ |
|
310 } \ |
|
311 } \ |
|
312 } |
|
313 |
|
314 #define MINMAX_BODY(FCN) \ |
|
315 \ |
|
316 octave_value_list retval; \ |
|
317 \ |
|
318 int nargin = args.length (); \ |
|
319 \ |
|
320 if (nargin < 1 || nargin > 3 || nargout > 2) \ |
|
321 { \ |
|
322 print_usage (); \ |
|
323 return retval; \ |
|
324 } \ |
|
325 \ |
|
326 octave_value arg1; \ |
|
327 octave_value arg2; \ |
|
328 octave_value arg3; \ |
|
329 \ |
|
330 switch (nargin) \ |
|
331 { \ |
|
332 case 3: \ |
|
333 arg3 = args(2); \ |
|
334 \ |
|
335 case 2: \ |
|
336 arg2 = args(1); \ |
|
337 \ |
|
338 case 1: \ |
|
339 arg1 = args(0); \ |
|
340 break; \ |
|
341 \ |
|
342 default: \ |
|
343 panic_impossible (); \ |
|
344 break; \ |
|
345 } \ |
|
346 \ |
|
347 int dim; \ |
|
348 dim_vector dv = arg1.dims (); \ |
|
349 if (error_state) \ |
|
350 { \ |
|
351 gripe_wrong_type_arg (#FCN, arg1); \ |
|
352 return retval; \ |
|
353 } \ |
|
354 \ |
|
355 if (nargin == 3) \ |
|
356 { \ |
|
357 dim = arg3.nint_value () - 1; \ |
|
358 if (dim < 0 || dim >= dv.length ()) \ |
|
359 { \ |
|
360 error ("%s: invalid dimension", #FCN); \ |
|
361 return retval; \ |
|
362 } \ |
|
363 } \ |
|
364 else \ |
|
365 { \ |
|
366 dim = 0; \ |
|
367 while ((dim < dv.length ()) && (dv (dim) <= 1)) \ |
|
368 dim++; \ |
|
369 if (dim == dv.length ()) \ |
|
370 dim = 0; \ |
|
371 } \ |
|
372 \ |
|
373 if (arg1.is_integer_type ()) \ |
|
374 { \ |
|
375 if (arg1.is_uint8_type ()) \ |
|
376 MINMAX_INT_BODY (FCN, uint8) \ |
|
377 else if (arg1.is_uint16_type ()) \ |
|
378 MINMAX_INT_BODY (FCN, uint16) \ |
|
379 else if (arg1.is_uint32_type ()) \ |
|
380 MINMAX_INT_BODY (FCN, uint32) \ |
|
381 else if (arg1.is_uint64_type ()) \ |
|
382 MINMAX_INT_BODY (FCN, uint64) \ |
|
383 else if (arg1.is_int8_type ()) \ |
|
384 MINMAX_INT_BODY (FCN, int8) \ |
|
385 else if (arg1.is_int16_type ()) \ |
|
386 MINMAX_INT_BODY (FCN, int16) \ |
|
387 else if (arg1.is_int32_type ()) \ |
|
388 MINMAX_INT_BODY (FCN, int32) \ |
|
389 else if (arg1.is_int64_type ()) \ |
|
390 MINMAX_INT_BODY (FCN, int64) \ |
|
391 } \ |
|
392 else \ |
|
393 MINMAX_DOUBLE_BODY (FCN) \ |
|
394 \ |
|
395 return retval; |
3747
|
396 |
2928
|
397 DEFUN_DLD (min, args, nargout, |
3443
|
398 "-*- texinfo -*-\n\ |
4844
|
399 @deftypefn {Mapping Function} {} min (@var{x}, @var{y}, @var{dim})\n\ |
4522
|
400 @deftypefnx {Mapping Function} {[@var{w}, @var{iw}] =} min (@var{x})\n\ |
|
401 @cindex Utility Functions\n\ |
3443
|
402 For a vector argument, return the minimum value. For a matrix\n\ |
|
403 argument, return the minimum value from each column, as a row\n\ |
4844
|
404 vector, or over the dimension @var{dim} if defined. For two matrices\n\ |
|
405 (or a matrix and scalar), return the pair-wise minimum.\n\ |
4522
|
406 Thus,\n\ |
3443
|
407 \n\ |
|
408 @example\n\ |
|
409 min (min (@var{x}))\n\ |
|
410 @end example\n\ |
|
411 \n\ |
|
412 @noindent\n\ |
4522
|
413 returns the smallest element of @var{x}, and\n\ |
|
414 \n\ |
|
415 @example\n\ |
|
416 @group\n\ |
|
417 min (2:5, pi)\n\ |
|
418 @result{} 2.0000 3.0000 3.1416 3.1416\n\ |
|
419 @end group\n\ |
|
420 @end example\n\ |
|
421 @noindent\n\ |
|
422 compares each element of the range @code{2:5} with @code{pi}, and\n\ |
|
423 returns a row vector of the minimum values.\n\ |
3443
|
424 \n\ |
|
425 For complex arguments, the magnitude of the elements are used for\n\ |
3657
|
426 comparison.\n\ |
|
427 \n\ |
4522
|
428 If called with one input and two output arguments,\n\ |
|
429 @code{min} also returns the first index of the\n\ |
3657
|
430 minimum value(s). Thus,\n\ |
4522
|
431 \n\ |
3775
|
432 @example\n\ |
4522
|
433 @group\n\ |
3657
|
434 [x, ix] = min ([1, 3, 0, 2, 5])\n\ |
4522
|
435 @result{} x = 0\n\ |
|
436 ix = 3\n\ |
|
437 @end group\n\ |
3657
|
438 @end example\n\ |
4522
|
439 @end deftypefn") |
2928
|
440 { |
3747
|
441 MINMAX_BODY (min); |
2928
|
442 } |
|
443 |
|
444 DEFUN_DLD (max, args, nargout, |
3443
|
445 "-*- texinfo -*-\n\ |
4844
|
446 @deftypefn {Mapping Function} {} max (@var{x}, @var{y}, @var{dim})\n\ |
4522
|
447 @deftypefnx {Mapping Function} {[@var{w}, @var{iw}] =} max (@var{x})\n\ |
|
448 @cindex Utility Functions\n\ |
3443
|
449 For a vector argument, return the maximum value. For a matrix\n\ |
|
450 argument, return the maximum value from each column, as a row\n\ |
4844
|
451 vector, or over the dimension @var{dim} if defined. For two matrices\n\ |
|
452 (or a matrix and scalar), return the pair-wise maximum.\n\ |
4522
|
453 Thus,\n\ |
3443
|
454 \n\ |
|
455 @example\n\ |
|
456 max (max (@var{x}))\n\ |
|
457 @end example\n\ |
|
458 \n\ |
|
459 @noindent\n\ |
4522
|
460 returns the largest element of @var{x}, and\n\ |
|
461 \n\ |
|
462 @example\n\ |
|
463 @group\n\ |
|
464 max (2:5, pi)\n\ |
|
465 @result{} 3.1416 3.1416 4.0000 5.0000\n\ |
|
466 @end group\n\ |
|
467 @end example\n\ |
|
468 @noindent\n\ |
|
469 compares each element of the range @code{2:5} with @code{pi}, and\n\ |
|
470 returns a row vector of the maximum values.\n\ |
3443
|
471 \n\ |
|
472 For complex arguments, the magnitude of the elements are used for\n\ |
3775
|
473 comparison.\n\ |
3657
|
474 \n\ |
4522
|
475 If called with one input and two output arguments,\n\ |
|
476 @code{max} also returns the first index of the\n\ |
3657
|
477 maximum value(s). Thus,\n\ |
4522
|
478 \n\ |
3775
|
479 @example\n\ |
4522
|
480 @group\n\ |
|
481 [x, ix] = max ([1, 3, 5, 2, 5])\n\ |
|
482 @result{} x = 5\n\ |
|
483 ix = 3\n\ |
|
484 @end group\n\ |
3657
|
485 @end example\n\ |
4522
|
486 @end deftypefn") |
2928
|
487 { |
3747
|
488 MINMAX_BODY (max); |
2928
|
489 } |
|
490 |
|
491 /* |
|
492 ;;; Local Variables: *** |
|
493 ;;; mode: C++ *** |
|
494 ;;; End: *** |
|
495 */ |