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