515
|
1 // f-minmax.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1994 John W. Eaton |
|
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 |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
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 |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
|
26 #endif |
|
27 |
|
28 #include <math.h> |
|
29 |
|
30 #include "tree-const.h" |
|
31 #include "error.h" |
544
|
32 #include "help.h" |
519
|
33 #include "defun-dld.h" |
515
|
34 |
|
35 #ifndef MAX |
|
36 #define MAX(a,b) ((a) > (b) ? (a) : (b)) |
|
37 #endif |
|
38 |
|
39 #ifndef MIN |
|
40 #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
|
41 #endif |
|
42 |
|
43 static Matrix |
|
44 min (const Matrix& a, const Matrix& b) |
|
45 { |
|
46 int nr = a.rows (); |
|
47 int nc = a.columns (); |
|
48 if (nr != b.rows () || nc != b.columns ()) |
|
49 { |
|
50 error ("two-arg min expecting args of same size"); |
|
51 return Matrix (); |
|
52 } |
|
53 |
|
54 Matrix result (nr, nc); |
|
55 |
|
56 for (int j = 0; j < nc; j++) |
|
57 for (int i = 0; i < nr; i++) |
|
58 { |
|
59 double a_elem = a.elem (i, j); |
|
60 double b_elem = b.elem (i, j); |
|
61 result.elem (i, j) = MIN (a_elem, b_elem); |
|
62 } |
|
63 |
|
64 return result; |
|
65 } |
|
66 |
|
67 static ComplexMatrix |
|
68 min (const ComplexMatrix& a, const ComplexMatrix& b) |
|
69 { |
|
70 int nr = a.rows (); |
|
71 int nc = a.columns (); |
|
72 if (nr != b.rows () || nc != b.columns ()) |
|
73 { |
|
74 error ("two-arg min expecting args of same size"); |
|
75 return ComplexMatrix (); |
|
76 } |
|
77 |
|
78 ComplexMatrix result (nr, nc); |
|
79 |
|
80 for (int j = 0; j < nc; j++) |
|
81 for (int i = 0; i < nr; i++) |
|
82 { |
|
83 double abs_a_elem = abs (a.elem (i, j)); |
|
84 double abs_b_elem = abs (b.elem (i, j)); |
|
85 if (abs_a_elem < abs_b_elem) |
|
86 result.elem (i, j) = a.elem (i, j); |
|
87 else |
|
88 result.elem (i, j) = b.elem (i, j); |
|
89 } |
|
90 |
|
91 return result; |
|
92 } |
|
93 |
|
94 static Matrix |
|
95 max (const Matrix& a, const Matrix& b) |
|
96 { |
|
97 int nr = a.rows (); |
|
98 int nc = a.columns (); |
|
99 if (nr != b.rows () || nc != b.columns ()) |
|
100 { |
|
101 error ("two-arg max expecting args of same size"); |
|
102 return Matrix (); |
|
103 } |
|
104 |
|
105 Matrix result (nr, nc); |
|
106 |
|
107 for (int j = 0; j < nc; j++) |
|
108 for (int i = 0; i < nr; i++) |
|
109 { |
|
110 double a_elem = a.elem (i, j); |
|
111 double b_elem = b.elem (i, j); |
|
112 result.elem (i, j) = MAX (a_elem, b_elem); |
|
113 } |
|
114 |
|
115 return result; |
|
116 } |
|
117 |
|
118 static ComplexMatrix |
|
119 max (const ComplexMatrix& a, const ComplexMatrix& b) |
|
120 { |
|
121 int nr = a.rows (); |
|
122 int nc = a.columns (); |
|
123 if (nr != b.rows () || nc != b.columns ()) |
|
124 { |
|
125 error ("two-arg max expecting args of same size"); |
|
126 return ComplexMatrix (); |
|
127 } |
|
128 |
|
129 ComplexMatrix result (nr, nc); |
|
130 |
|
131 for (int j = 0; j < nc; j++) |
|
132 for (int i = 0; i < nr; i++) |
|
133 { |
|
134 double abs_a_elem = abs (a.elem (i, j)); |
|
135 double abs_b_elem = abs (b.elem (i, j)); |
|
136 if (abs_a_elem > abs_b_elem) |
|
137 result.elem (i, j) = a.elem (i, j); |
|
138 else |
|
139 result.elem (i, j) = b.elem (i, j); |
|
140 } |
|
141 |
|
142 return result; |
|
143 } |
|
144 |
519
|
145 |
|
146 DEFUN_DLD ("min", Fmin, Smin, 3, 2, |
|
147 "min (X): minimum value(s) of a vector (matrix)") |
515
|
148 { |
|
149 Octave_object retval; |
|
150 |
519
|
151 int nargin = args.length (); |
|
152 |
|
153 if (nargin == 1 || nargin > 3 || nargout > 2) |
|
154 { |
|
155 print_usage ("min"); |
|
156 return retval; |
|
157 } |
|
158 |
515
|
159 tree_constant arg1; |
|
160 tree_constant arg2; |
|
161 |
|
162 switch (nargin) |
|
163 { |
|
164 case 3: |
|
165 arg2 = args(2).make_numeric (); |
|
166 // Fall through... |
|
167 case 2: |
|
168 arg1 = args(1).make_numeric (); |
|
169 break; |
|
170 default: |
|
171 panic_impossible (); |
|
172 break; |
|
173 } |
|
174 |
|
175 if (nargin == 2 && (nargout == 1 || nargout == 0)) |
|
176 { |
620
|
177 if (arg1.is_real_scalar ()) |
515
|
178 { |
|
179 retval(0) = arg1.double_value (); |
620
|
180 } |
|
181 else if (arg1.is_complex_scalar ()) |
|
182 { |
515
|
183 retval(0) = arg1.complex_value (); |
620
|
184 } |
|
185 else if (arg1.is_real_matrix ()) |
|
186 { |
|
187 Matrix m = arg1.matrix_value (); |
|
188 if (m.rows () == 1) |
|
189 retval(0) = m.row_min (); |
|
190 else |
|
191 retval(0) = tree_constant (m.column_min (), 0); |
|
192 } |
|
193 else if (arg1.is_complex_matrix ()) |
|
194 { |
|
195 ComplexMatrix m = arg1.complex_matrix_value (); |
|
196 if (m.rows () == 1) |
|
197 retval(0) = m.row_min (); |
|
198 else |
|
199 retval(0) = tree_constant (m.column_min (), 0); |
|
200 } |
|
201 else |
|
202 { |
|
203 gripe_wrong_type_arg ("min", arg1); |
|
204 return retval; |
515
|
205 } |
|
206 } |
|
207 else if (nargin == 2 && nargout == 2) |
|
208 { |
620
|
209 if (arg1.is_real_scalar ()) |
515
|
210 { |
620
|
211 retval(1) = 1; |
|
212 retval(0) = arg1.double_value (); |
|
213 } |
|
214 else if (arg1.is_complex_scalar ()) |
|
215 { |
|
216 retval(1) = 1; |
|
217 retval(0) = arg1.complex_value (); |
|
218 } |
|
219 else if (arg1.is_real_matrix ()) |
|
220 { |
|
221 Matrix m = arg1.matrix_value (); |
|
222 if (m.rows () == 1) |
|
223 { |
|
224 retval(1) = m.row_min_loc (); |
|
225 retval(0) = m.row_min (); |
|
226 } |
|
227 else |
|
228 { |
|
229 retval(1) = tree_constant (m.column_min_loc (), 0); |
|
230 retval(0) = tree_constant (m.column_min (), 0); |
|
231 } |
|
232 } |
|
233 else if (arg1.is_complex_matrix ()) |
|
234 { |
|
235 ComplexMatrix m = arg1.complex_matrix_value (); |
|
236 if (m.rows () == 1) |
|
237 { |
|
238 retval(1) = m.row_min_loc (); |
|
239 retval(0) = m.row_min (); |
|
240 } |
|
241 else |
|
242 { |
|
243 retval(1) = tree_constant (m.column_min_loc (), 0); |
|
244 retval(0) = tree_constant (m.column_min (), 0); |
|
245 } |
|
246 } |
|
247 else |
|
248 { |
|
249 gripe_wrong_type_arg ("min", arg1); |
|
250 return retval; |
515
|
251 } |
|
252 } |
|
253 else if (nargin == 3) |
|
254 { |
|
255 if (arg1.rows () == arg2.rows () |
|
256 && arg1.columns () == arg2.columns ()) |
|
257 { |
620
|
258 // XXX FIXME XXX -- I don't think this is quite right. |
|
259 if (arg1.is_real_scalar ()) |
515
|
260 { |
620
|
261 double result; |
|
262 double a_elem = arg1.double_value (); |
|
263 double b_elem = arg2.double_value (); |
|
264 result = MIN (a_elem, b_elem); |
|
265 retval(0) = result; |
|
266 } |
|
267 else if (arg1.is_complex_scalar ()) |
|
268 { |
|
269 Complex result; |
|
270 Complex a_elem = arg1.complex_value (); |
|
271 Complex b_elem = arg2.complex_value (); |
|
272 if (abs (a_elem) < abs (b_elem)) |
|
273 result = a_elem; |
|
274 else |
|
275 result = b_elem; |
|
276 retval(0) = result; |
|
277 } |
|
278 else if (arg1.is_real_matrix ()) |
|
279 { |
|
280 Matrix result; |
|
281 result = min (arg1.matrix_value (), arg2.matrix_value ()); |
|
282 retval(0) = result; |
|
283 } |
|
284 else if (arg1.is_complex_matrix ()) |
|
285 { |
|
286 ComplexMatrix result; |
|
287 result = min (arg1.complex_matrix_value (), |
|
288 arg2.complex_matrix_value ()); |
|
289 retval(0) = result; |
|
290 } |
|
291 else |
|
292 { |
|
293 gripe_wrong_type_arg ("min", arg1); |
|
294 return retval; |
515
|
295 } |
|
296 } |
|
297 else |
|
298 error ("min: nonconformant matrices"); |
|
299 } |
|
300 else |
|
301 panic_impossible (); |
|
302 |
|
303 return retval; |
|
304 } |
|
305 |
519
|
306 DEFUN_DLD ("max", Fmax, Smax, 3, 2, |
|
307 "max (X): maximum value(s) of a vector (matrix)") |
515
|
308 { |
|
309 Octave_object retval; |
|
310 |
519
|
311 int nargin = args.length (); |
|
312 |
|
313 if (nargin == 1 || nargin > 3 || nargout > 2) |
|
314 { |
|
315 print_usage ("max"); |
|
316 return retval; |
|
317 } |
|
318 |
515
|
319 tree_constant arg1; |
|
320 tree_constant arg2; |
|
321 |
|
322 switch (nargin) |
|
323 { |
|
324 case 3: |
|
325 arg2 = args(2).make_numeric (); |
|
326 // Fall through... |
|
327 case 2: |
|
328 arg1 = args(1).make_numeric (); |
|
329 break; |
|
330 default: |
|
331 panic_impossible (); |
|
332 break; |
|
333 } |
|
334 |
|
335 if (nargin == 2 && (nargout == 1 || nargout == 0)) |
|
336 { |
620
|
337 if (arg1.is_real_scalar ()) |
515
|
338 { |
|
339 retval(0) = arg1.double_value (); |
620
|
340 } |
|
341 else if (arg1.is_complex_scalar ()) |
|
342 { |
515
|
343 retval(0) = arg1.complex_value (); |
620
|
344 } |
|
345 else if (arg1.is_real_matrix ()) |
|
346 { |
|
347 Matrix m = arg1.matrix_value (); |
|
348 if (m.rows () == 1) |
|
349 retval(0) = m.row_max (); |
|
350 else |
|
351 retval(0) = tree_constant (m.column_max (), 0); |
|
352 } |
|
353 else if (arg1.is_complex_matrix ()) |
|
354 { |
|
355 ComplexMatrix m = arg1.complex_matrix_value (); |
|
356 if (m.rows () == 1) |
|
357 retval(0) = m.row_max (); |
|
358 else |
|
359 retval(0) = tree_constant (m.column_max (), 0); |
|
360 } |
|
361 else |
|
362 { |
|
363 gripe_wrong_type_arg ("max", arg1); |
|
364 return retval; |
515
|
365 } |
|
366 } |
|
367 else if (nargin == 2 && nargout == 2) |
|
368 { |
620
|
369 if (arg1.is_real_scalar ()) |
|
370 { |
|
371 retval(1) = 1; |
|
372 retval(0) = arg1.double_value (); |
|
373 } |
|
374 else if (arg1.is_complex_scalar ()) |
|
375 { |
|
376 retval(1) = 1; |
|
377 retval(0) = arg1.complex_value (); |
|
378 } |
|
379 else if (arg1.is_real_matrix ()) |
|
380 { |
|
381 Matrix m = arg1.matrix_value (); |
|
382 if (m.rows () == 1) |
|
383 { |
|
384 retval(1) = m.row_max_loc (); |
|
385 retval(0) = m.row_max (); |
|
386 } |
|
387 else |
|
388 { |
|
389 retval(1) = tree_constant (m.column_max_loc (), 0); |
|
390 retval(0) = tree_constant (m.column_max (), 0); |
|
391 } |
|
392 } |
|
393 else if (arg1.is_complex_matrix ()) |
|
394 { |
|
395 ComplexMatrix m = arg1.complex_matrix_value (); |
|
396 if (m.rows () == 1) |
|
397 { |
|
398 retval(1) = m.row_max_loc (); |
|
399 retval(0) = m.row_max (); |
|
400 } |
|
401 else |
|
402 { |
|
403 retval(1) = tree_constant (m.column_max_loc (), 0); |
|
404 retval(0) = tree_constant (m.column_max (), 0); |
|
405 } |
|
406 } |
|
407 else |
|
408 { |
|
409 gripe_wrong_type_arg ("max", arg1); |
|
410 return retval; |
|
411 } |
515
|
412 } |
|
413 else if (nargin == 3) |
|
414 { |
|
415 if (arg1.rows () == arg2.rows () |
|
416 && arg1.columns () == arg2.columns ()) |
|
417 { |
620
|
418 // XXX FIXME XXX -- I don't think this is quite right. |
|
419 if (arg1.is_real_scalar ()) |
515
|
420 { |
620
|
421 double result; |
|
422 double a_elem = arg1.double_value (); |
|
423 double b_elem = arg2.double_value (); |
|
424 result = MAX (a_elem, b_elem); |
|
425 retval(0) = result; |
|
426 } |
|
427 else if (arg1.is_complex_scalar ()) |
|
428 { |
|
429 Complex result; |
|
430 Complex a_elem = arg1.complex_value (); |
|
431 Complex b_elem = arg2.complex_value (); |
|
432 if (abs (a_elem) > abs (b_elem)) |
|
433 result = a_elem; |
|
434 else |
|
435 result = b_elem; |
|
436 retval(0) = result; |
|
437 } |
|
438 else if (arg1.is_real_matrix ()) |
|
439 { |
|
440 Matrix result; |
|
441 result = max (arg1.matrix_value (), arg2.matrix_value ()); |
|
442 retval(0) = result; |
|
443 } |
|
444 else if (arg1.is_complex_matrix ()) |
|
445 { |
|
446 ComplexMatrix result; |
|
447 result = max (arg1.complex_matrix_value (), |
|
448 arg2.complex_matrix_value ()); |
|
449 retval(0) = result; |
|
450 } |
|
451 else |
|
452 { |
|
453 gripe_wrong_type_arg ("max", arg1); |
|
454 return retval; |
515
|
455 } |
|
456 } |
|
457 else |
|
458 error ("max: nonconformant matrices"); |
|
459 } |
|
460 else |
|
461 panic_impossible (); |
|
462 |
|
463 return retval; |
|
464 } |
|
465 |
|
466 /* |
|
467 ;;; Local Variables: *** |
|
468 ;;; mode: C++ *** |
|
469 ;;; page-delimiter: "^/\\*" *** |
|
470 ;;; End: *** |
|
471 */ |