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: |
636
|
165 arg2 = args(2); |
515
|
166 // Fall through... |
|
167 case 2: |
636
|
168 arg1 = args(1); |
515
|
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 } |
636
|
185 else if (arg1.is_real_type ()) |
620
|
186 { |
|
187 Matrix m = arg1.matrix_value (); |
636
|
188 |
|
189 if (! error_state) |
|
190 { |
|
191 if (m.rows () == 1) |
|
192 retval(0) = m.row_min (); |
|
193 else |
|
194 retval(0) = tree_constant (m.column_min (), 0); |
|
195 } |
620
|
196 } |
636
|
197 else if (arg1.is_complex_type ()) |
620
|
198 { |
|
199 ComplexMatrix m = arg1.complex_matrix_value (); |
636
|
200 |
|
201 if (! error_state) |
|
202 { |
|
203 if (m.rows () == 1) |
|
204 retval(0) = m.row_min (); |
|
205 else |
|
206 retval(0) = tree_constant (m.column_min (), 0); |
|
207 } |
620
|
208 } |
|
209 else |
|
210 { |
|
211 gripe_wrong_type_arg ("min", arg1); |
|
212 return retval; |
515
|
213 } |
|
214 } |
|
215 else if (nargin == 2 && nargout == 2) |
|
216 { |
620
|
217 if (arg1.is_real_scalar ()) |
515
|
218 { |
620
|
219 retval(1) = 1; |
|
220 retval(0) = arg1.double_value (); |
|
221 } |
|
222 else if (arg1.is_complex_scalar ()) |
|
223 { |
|
224 retval(1) = 1; |
|
225 retval(0) = arg1.complex_value (); |
|
226 } |
636
|
227 else if (arg1.is_real_type ()) |
620
|
228 { |
|
229 Matrix m = arg1.matrix_value (); |
636
|
230 |
|
231 if (! error_state) |
620
|
232 { |
636
|
233 if (m.rows () == 1) |
|
234 { |
|
235 retval(1) = m.row_min_loc (); |
|
236 retval(0) = m.row_min (); |
|
237 } |
|
238 else |
|
239 { |
|
240 retval(1) = tree_constant (m.column_min_loc (), 0); |
|
241 retval(0) = tree_constant (m.column_min (), 0); |
|
242 } |
620
|
243 } |
|
244 } |
636
|
245 else if (arg1.is_complex_type ()) |
620
|
246 { |
|
247 ComplexMatrix m = arg1.complex_matrix_value (); |
636
|
248 |
|
249 if (! error_state) |
620
|
250 { |
636
|
251 if (m.rows () == 1) |
|
252 { |
|
253 retval(1) = m.row_min_loc (); |
|
254 retval(0) = m.row_min (); |
|
255 } |
|
256 else |
|
257 { |
|
258 retval(1) = tree_constant (m.column_min_loc (), 0); |
|
259 retval(0) = tree_constant (m.column_min (), 0); |
|
260 } |
620
|
261 } |
|
262 } |
|
263 else |
|
264 { |
|
265 gripe_wrong_type_arg ("min", arg1); |
|
266 return retval; |
515
|
267 } |
|
268 } |
|
269 else if (nargin == 3) |
|
270 { |
|
271 if (arg1.rows () == arg2.rows () |
|
272 && arg1.columns () == arg2.columns ()) |
|
273 { |
636
|
274 if (arg1.is_real_type () && arg2.is_real_type ()) |
620
|
275 { |
636
|
276 Matrix m1 = arg1.matrix_value (); |
|
277 |
|
278 if (! error_state) |
|
279 { |
|
280 Matrix m2 = arg2.matrix_value (); |
|
281 |
|
282 if (! error_state) |
|
283 { |
|
284 Matrix result = min (m1, m2); |
|
285 if (! error_state) |
|
286 retval(0) = result; |
|
287 } |
|
288 } |
620
|
289 } |
636
|
290 else if (arg1.is_complex_matrix () || arg2.is_complex_type ()) |
620
|
291 { |
636
|
292 ComplexMatrix m1 = arg1.complex_matrix_value (); |
|
293 |
|
294 if (! error_state) |
|
295 { |
|
296 ComplexMatrix m2 = arg2.complex_matrix_value (); |
|
297 |
|
298 if (! error_state) |
|
299 { |
|
300 ComplexMatrix result = min (m1, m2); |
|
301 if (! error_state) |
|
302 retval(0) = result; |
|
303 } |
|
304 } |
620
|
305 } |
|
306 else |
|
307 { |
|
308 gripe_wrong_type_arg ("min", arg1); |
|
309 return retval; |
515
|
310 } |
|
311 } |
|
312 else |
|
313 error ("min: nonconformant matrices"); |
|
314 } |
|
315 else |
|
316 panic_impossible (); |
|
317 |
|
318 return retval; |
|
319 } |
|
320 |
519
|
321 DEFUN_DLD ("max", Fmax, Smax, 3, 2, |
|
322 "max (X): maximum value(s) of a vector (matrix)") |
515
|
323 { |
|
324 Octave_object retval; |
|
325 |
519
|
326 int nargin = args.length (); |
|
327 |
|
328 if (nargin == 1 || nargin > 3 || nargout > 2) |
|
329 { |
|
330 print_usage ("max"); |
|
331 return retval; |
|
332 } |
|
333 |
515
|
334 tree_constant arg1; |
|
335 tree_constant arg2; |
|
336 |
|
337 switch (nargin) |
|
338 { |
|
339 case 3: |
636
|
340 arg2 = args(2); |
515
|
341 // Fall through... |
|
342 case 2: |
636
|
343 arg1 = args(1); |
515
|
344 break; |
|
345 default: |
|
346 panic_impossible (); |
|
347 break; |
|
348 } |
|
349 |
|
350 if (nargin == 2 && (nargout == 1 || nargout == 0)) |
|
351 { |
620
|
352 if (arg1.is_real_scalar ()) |
515
|
353 { |
|
354 retval(0) = arg1.double_value (); |
620
|
355 } |
|
356 else if (arg1.is_complex_scalar ()) |
|
357 { |
515
|
358 retval(0) = arg1.complex_value (); |
620
|
359 } |
|
360 else if (arg1.is_real_matrix ()) |
|
361 { |
|
362 Matrix m = arg1.matrix_value (); |
|
363 if (m.rows () == 1) |
|
364 retval(0) = m.row_max (); |
|
365 else |
|
366 retval(0) = tree_constant (m.column_max (), 0); |
|
367 } |
|
368 else if (arg1.is_complex_matrix ()) |
|
369 { |
|
370 ComplexMatrix m = arg1.complex_matrix_value (); |
|
371 if (m.rows () == 1) |
|
372 retval(0) = m.row_max (); |
|
373 else |
|
374 retval(0) = tree_constant (m.column_max (), 0); |
|
375 } |
|
376 else |
|
377 { |
|
378 gripe_wrong_type_arg ("max", arg1); |
|
379 return retval; |
515
|
380 } |
|
381 } |
|
382 else if (nargin == 2 && nargout == 2) |
|
383 { |
620
|
384 if (arg1.is_real_scalar ()) |
|
385 { |
|
386 retval(1) = 1; |
|
387 retval(0) = arg1.double_value (); |
|
388 } |
|
389 else if (arg1.is_complex_scalar ()) |
|
390 { |
|
391 retval(1) = 1; |
|
392 retval(0) = arg1.complex_value (); |
|
393 } |
|
394 else if (arg1.is_real_matrix ()) |
|
395 { |
|
396 Matrix m = arg1.matrix_value (); |
|
397 if (m.rows () == 1) |
|
398 { |
|
399 retval(1) = m.row_max_loc (); |
|
400 retval(0) = m.row_max (); |
|
401 } |
|
402 else |
|
403 { |
|
404 retval(1) = tree_constant (m.column_max_loc (), 0); |
|
405 retval(0) = tree_constant (m.column_max (), 0); |
|
406 } |
|
407 } |
|
408 else if (arg1.is_complex_matrix ()) |
|
409 { |
|
410 ComplexMatrix m = arg1.complex_matrix_value (); |
|
411 if (m.rows () == 1) |
|
412 { |
|
413 retval(1) = m.row_max_loc (); |
|
414 retval(0) = m.row_max (); |
|
415 } |
|
416 else |
|
417 { |
|
418 retval(1) = tree_constant (m.column_max_loc (), 0); |
|
419 retval(0) = tree_constant (m.column_max (), 0); |
|
420 } |
|
421 } |
|
422 else |
|
423 { |
|
424 gripe_wrong_type_arg ("max", arg1); |
|
425 return retval; |
|
426 } |
515
|
427 } |
|
428 else if (nargin == 3) |
|
429 { |
|
430 if (arg1.rows () == arg2.rows () |
|
431 && arg1.columns () == arg2.columns ()) |
|
432 { |
620
|
433 // XXX FIXME XXX -- I don't think this is quite right. |
|
434 if (arg1.is_real_scalar ()) |
515
|
435 { |
620
|
436 double result; |
|
437 double a_elem = arg1.double_value (); |
|
438 double b_elem = arg2.double_value (); |
|
439 result = MAX (a_elem, b_elem); |
|
440 retval(0) = result; |
|
441 } |
|
442 else if (arg1.is_complex_scalar ()) |
|
443 { |
|
444 Complex result; |
|
445 Complex a_elem = arg1.complex_value (); |
|
446 Complex b_elem = arg2.complex_value (); |
|
447 if (abs (a_elem) > abs (b_elem)) |
|
448 result = a_elem; |
|
449 else |
|
450 result = b_elem; |
|
451 retval(0) = result; |
|
452 } |
|
453 else if (arg1.is_real_matrix ()) |
|
454 { |
|
455 Matrix result; |
|
456 result = max (arg1.matrix_value (), arg2.matrix_value ()); |
|
457 retval(0) = result; |
|
458 } |
|
459 else if (arg1.is_complex_matrix ()) |
|
460 { |
|
461 ComplexMatrix result; |
|
462 result = max (arg1.complex_matrix_value (), |
|
463 arg2.complex_matrix_value ()); |
|
464 retval(0) = result; |
|
465 } |
|
466 else |
|
467 { |
|
468 gripe_wrong_type_arg ("max", arg1); |
|
469 return retval; |
515
|
470 } |
|
471 } |
|
472 else |
|
473 error ("max: nonconformant matrices"); |
|
474 } |
|
475 else |
|
476 panic_impossible (); |
|
477 |
|
478 return retval; |
|
479 } |
|
480 |
|
481 /* |
|
482 ;;; Local Variables: *** |
|
483 ;;; mode: C++ *** |
|
484 ;;; page-delimiter: "^/\\*" *** |
|
485 ;;; End: *** |
|
486 */ |