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 |
3747
|
43 #define MINMAX_BODY(FCN) \ |
|
44 \ |
|
45 octave_value_list retval; \ |
|
46 \ |
|
47 int nargin = args.length (); \ |
|
48 \ |
4844
|
49 if (nargin < 1 || nargin > 3 || nargout > 2) \ |
3747
|
50 { \ |
5823
|
51 print_usage (); \ |
3747
|
52 return retval; \ |
|
53 } \ |
|
54 \ |
|
55 octave_value arg1; \ |
|
56 octave_value arg2; \ |
4844
|
57 octave_value arg3; \ |
3747
|
58 \ |
|
59 switch (nargin) \ |
|
60 { \ |
4844
|
61 case 3: \ |
|
62 arg3 = args(2); \ |
|
63 \ |
3747
|
64 case 2: \ |
|
65 arg2 = args(1); \ |
|
66 \ |
|
67 case 1: \ |
|
68 arg1 = args(0); \ |
|
69 break; \ |
|
70 \ |
|
71 default: \ |
|
72 panic_impossible (); \ |
|
73 break; \ |
|
74 } \ |
|
75 \ |
4844
|
76 int dim; \ |
5540
|
77 dim_vector dv = arg1.dims (); \ |
4844
|
78 if (error_state) \ |
|
79 { \ |
|
80 gripe_wrong_type_arg (#FCN, arg1); \ |
|
81 return retval; \ |
|
82 } \ |
|
83 \ |
|
84 if (nargin == 3) \ |
|
85 { \ |
|
86 dim = arg3.nint_value () - 1; \ |
|
87 if (dim < 0 || dim >= dv.length ()) \ |
|
88 { \ |
|
89 error ("%s: invalid dimension", #FCN); \ |
|
90 return retval; \ |
|
91 } \ |
|
92 } \ |
|
93 else \ |
|
94 { \ |
|
95 dim = 0; \ |
|
96 while ((dim < dv.length ()) && (dv (dim) <= 1)) \ |
|
97 dim++; \ |
|
98 if (dim == dv.length ()) \ |
|
99 dim = 0; \ |
|
100 } \ |
|
101 \ |
6711
|
102 bool single_arg = (nargin == 1) || (arg2.is_empty() && nargin == 3); \ |
4844
|
103 \ |
|
104 if (single_arg && (nargout == 1 || nargout == 0)) \ |
3747
|
105 { \ |
|
106 if (arg1.is_real_type ()) \ |
|
107 { \ |
4844
|
108 NDArray m = arg1.array_value (); \ |
3747
|
109 \ |
|
110 if (! error_state) \ |
|
111 { \ |
4844
|
112 NDArray n = m. FCN (dim); \ |
|
113 retval(0) = n; \ |
3747
|
114 } \ |
|
115 } \ |
|
116 else if (arg1.is_complex_type ()) \ |
|
117 { \ |
4844
|
118 ComplexNDArray m = arg1.complex_array_value (); \ |
3747
|
119 \ |
|
120 if (! error_state) \ |
|
121 { \ |
4844
|
122 ComplexNDArray n = m. FCN (dim); \ |
|
123 retval(0) = n; \ |
3747
|
124 } \ |
|
125 } \ |
|
126 else \ |
|
127 gripe_wrong_type_arg (#FCN, arg1); \ |
|
128 } \ |
4844
|
129 else if (single_arg && nargout == 2) \ |
3747
|
130 { \ |
5275
|
131 ArrayN<octave_idx_type> index; \ |
3747
|
132 \ |
|
133 if (arg1.is_real_type ()) \ |
|
134 { \ |
4844
|
135 NDArray m = arg1.array_value (); \ |
3747
|
136 \ |
|
137 if (! error_state) \ |
|
138 { \ |
4844
|
139 NDArray n = m. FCN (index, dim); \ |
|
140 retval(0) = n; \ |
3747
|
141 } \ |
|
142 } \ |
|
143 else if (arg1.is_complex_type ()) \ |
|
144 { \ |
4844
|
145 ComplexNDArray m = arg1.complex_array_value (); \ |
3747
|
146 \ |
|
147 if (! error_state) \ |
|
148 { \ |
4844
|
149 ComplexNDArray n = m. FCN (index, dim); \ |
|
150 retval(0) = n; \ |
3747
|
151 } \ |
|
152 } \ |
|
153 else \ |
|
154 gripe_wrong_type_arg (#FCN, arg1); \ |
|
155 \ |
5275
|
156 octave_idx_type len = index.numel (); \ |
3747
|
157 \ |
|
158 if (len > 0) \ |
|
159 { \ |
4102
|
160 double nan_val = lo_ieee_nan_value (); \ |
|
161 \ |
4844
|
162 NDArray idx (index.dims ()); \ |
3747
|
163 \ |
5275
|
164 for (octave_idx_type i = 0; i < len; i++) \ |
3747
|
165 { \ |
4153
|
166 OCTAVE_QUIT; \ |
3747
|
167 int tmp = index.elem (i) + 1; \ |
|
168 idx.elem (i) = (tmp <= 0) \ |
4102
|
169 ? nan_val : static_cast<double> (tmp); \ |
3747
|
170 } \ |
|
171 \ |
|
172 retval(1) = idx; \ |
|
173 } \ |
|
174 else \ |
4844
|
175 retval(1) = NDArray (); \ |
3747
|
176 } \ |
4844
|
177 else \ |
3747
|
178 { \ |
|
179 int arg1_is_scalar = arg1.is_scalar_type (); \ |
|
180 int arg2_is_scalar = arg2.is_scalar_type (); \ |
|
181 \ |
|
182 int arg1_is_complex = arg1.is_complex_type (); \ |
|
183 int arg2_is_complex = arg2.is_complex_type (); \ |
|
184 \ |
|
185 if (arg1_is_scalar) \ |
|
186 { \ |
|
187 if (arg1_is_complex || arg2_is_complex) \ |
|
188 { \ |
|
189 Complex c1 = arg1.complex_value (); \ |
4844
|
190 ComplexNDArray m2 = arg2.complex_array_value (); \ |
3747
|
191 if (! error_state) \ |
|
192 { \ |
4844
|
193 ComplexNDArray result = FCN (c1, m2); \ |
3747
|
194 if (! error_state) \ |
|
195 retval(0) = result; \ |
|
196 } \ |
|
197 } \ |
|
198 else \ |
|
199 { \ |
|
200 double d1 = arg1.double_value (); \ |
4844
|
201 NDArray m2 = arg2.array_value (); \ |
3747
|
202 \ |
|
203 if (! error_state) \ |
|
204 { \ |
4844
|
205 NDArray result = FCN (d1, m2); \ |
3747
|
206 if (! error_state) \ |
|
207 retval(0) = result; \ |
|
208 } \ |
|
209 } \ |
|
210 } \ |
|
211 else if (arg2_is_scalar) \ |
|
212 { \ |
|
213 if (arg1_is_complex || arg2_is_complex) \ |
|
214 { \ |
4844
|
215 ComplexNDArray m1 = arg1.complex_array_value (); \ |
3747
|
216 \ |
|
217 if (! error_state) \ |
|
218 { \ |
|
219 Complex c2 = arg2.complex_value (); \ |
4844
|
220 ComplexNDArray result = FCN (m1, c2); \ |
3747
|
221 if (! error_state) \ |
|
222 retval(0) = result; \ |
|
223 } \ |
|
224 } \ |
|
225 else \ |
|
226 { \ |
4844
|
227 NDArray m1 = arg1.array_value (); \ |
3747
|
228 \ |
|
229 if (! error_state) \ |
|
230 { \ |
|
231 double d2 = arg2.double_value (); \ |
4844
|
232 NDArray result = FCN (m1, d2); \ |
3747
|
233 if (! error_state) \ |
|
234 retval(0) = result; \ |
|
235 } \ |
|
236 } \ |
|
237 } \ |
|
238 else \ |
|
239 { \ |
|
240 if (arg1_is_complex || arg2_is_complex) \ |
|
241 { \ |
4844
|
242 ComplexNDArray m1 = arg1.complex_array_value (); \ |
3747
|
243 \ |
|
244 if (! error_state) \ |
|
245 { \ |
4844
|
246 ComplexNDArray m2 = arg2.complex_array_value (); \ |
3747
|
247 \ |
|
248 if (! error_state) \ |
|
249 { \ |
4844
|
250 ComplexNDArray result = FCN (m1, m2); \ |
3747
|
251 if (! error_state) \ |
|
252 retval(0) = result; \ |
|
253 } \ |
|
254 } \ |
|
255 } \ |
|
256 else \ |
|
257 { \ |
4844
|
258 NDArray m1 = arg1.array_value (); \ |
3747
|
259 \ |
|
260 if (! error_state) \ |
|
261 { \ |
4844
|
262 NDArray m2 = arg2.array_value (); \ |
3747
|
263 \ |
|
264 if (! error_state) \ |
|
265 { \ |
4844
|
266 NDArray result = FCN (m1, m2); \ |
3747
|
267 if (! error_state) \ |
|
268 retval(0) = result; \ |
|
269 } \ |
|
270 } \ |
|
271 } \ |
|
272 } \ |
|
273 } \ |
|
274 \ |
|
275 return retval |
|
276 |
2928
|
277 DEFUN_DLD (min, args, nargout, |
3443
|
278 "-*- texinfo -*-\n\ |
4844
|
279 @deftypefn {Mapping Function} {} min (@var{x}, @var{y}, @var{dim})\n\ |
4522
|
280 @deftypefnx {Mapping Function} {[@var{w}, @var{iw}] =} min (@var{x})\n\ |
|
281 @cindex Utility Functions\n\ |
3443
|
282 For a vector argument, return the minimum value. For a matrix\n\ |
|
283 argument, return the minimum value from each column, as a row\n\ |
4844
|
284 vector, or over the dimension @var{dim} if defined. For two matrices\n\ |
|
285 (or a matrix and scalar), return the pair-wise minimum.\n\ |
4522
|
286 Thus,\n\ |
3443
|
287 \n\ |
|
288 @example\n\ |
|
289 min (min (@var{x}))\n\ |
|
290 @end example\n\ |
|
291 \n\ |
|
292 @noindent\n\ |
4522
|
293 returns the smallest element of @var{x}, and\n\ |
|
294 \n\ |
|
295 @example\n\ |
|
296 @group\n\ |
|
297 min (2:5, pi)\n\ |
|
298 @result{} 2.0000 3.0000 3.1416 3.1416\n\ |
|
299 @end group\n\ |
|
300 @end example\n\ |
|
301 @noindent\n\ |
|
302 compares each element of the range @code{2:5} with @code{pi}, and\n\ |
|
303 returns a row vector of the minimum values.\n\ |
3443
|
304 \n\ |
|
305 For complex arguments, the magnitude of the elements are used for\n\ |
3657
|
306 comparison.\n\ |
|
307 \n\ |
4522
|
308 If called with one input and two output arguments,\n\ |
|
309 @code{min} also returns the first index of the\n\ |
3657
|
310 minimum value(s). Thus,\n\ |
4522
|
311 \n\ |
3775
|
312 @example\n\ |
4522
|
313 @group\n\ |
3657
|
314 [x, ix] = min ([1, 3, 0, 2, 5])\n\ |
4522
|
315 @result{} x = 0\n\ |
|
316 ix = 3\n\ |
|
317 @end group\n\ |
3657
|
318 @end example\n\ |
4522
|
319 @end deftypefn") |
2928
|
320 { |
3747
|
321 MINMAX_BODY (min); |
2928
|
322 } |
|
323 |
|
324 DEFUN_DLD (max, args, nargout, |
3443
|
325 "-*- texinfo -*-\n\ |
4844
|
326 @deftypefn {Mapping Function} {} max (@var{x}, @var{y}, @var{dim})\n\ |
4522
|
327 @deftypefnx {Mapping Function} {[@var{w}, @var{iw}] =} max (@var{x})\n\ |
|
328 @cindex Utility Functions\n\ |
3443
|
329 For a vector argument, return the maximum value. For a matrix\n\ |
|
330 argument, return the maximum value from each column, as a row\n\ |
4844
|
331 vector, or over the dimension @var{dim} if defined. For two matrices\n\ |
|
332 (or a matrix and scalar), return the pair-wise maximum.\n\ |
4522
|
333 Thus,\n\ |
3443
|
334 \n\ |
|
335 @example\n\ |
|
336 max (max (@var{x}))\n\ |
|
337 @end example\n\ |
|
338 \n\ |
|
339 @noindent\n\ |
4522
|
340 returns the largest element of @var{x}, and\n\ |
|
341 \n\ |
|
342 @example\n\ |
|
343 @group\n\ |
|
344 max (2:5, pi)\n\ |
|
345 @result{} 3.1416 3.1416 4.0000 5.0000\n\ |
|
346 @end group\n\ |
|
347 @end example\n\ |
|
348 @noindent\n\ |
|
349 compares each element of the range @code{2:5} with @code{pi}, and\n\ |
|
350 returns a row vector of the maximum values.\n\ |
3443
|
351 \n\ |
|
352 For complex arguments, the magnitude of the elements are used for\n\ |
3775
|
353 comparison.\n\ |
3657
|
354 \n\ |
4522
|
355 If called with one input and two output arguments,\n\ |
|
356 @code{max} also returns the first index of the\n\ |
3657
|
357 maximum value(s). Thus,\n\ |
4522
|
358 \n\ |
3775
|
359 @example\n\ |
4522
|
360 @group\n\ |
|
361 [x, ix] = max ([1, 3, 5, 2, 5])\n\ |
|
362 @result{} x = 5\n\ |
|
363 ix = 3\n\ |
|
364 @end group\n\ |
3657
|
365 @end example\n\ |
4522
|
366 @end deftypefn") |
2928
|
367 { |
3747
|
368 MINMAX_BODY (max); |
2928
|
369 } |
|
370 |
|
371 /* |
|
372 ;;; Local Variables: *** |
|
373 ;;; mode: C++ *** |
|
374 ;;; End: *** |
|
375 */ |