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