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