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