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" |
2928
|
31 |
|
32 #include "defun-dld.h" |
|
33 #include "error.h" |
|
34 #include "gripes.h" |
|
35 #include "oct-obj.h" |
|
36 |
|
37 // XXX FIXME XXX -- it would be nice to share code among the min/max |
|
38 // functions below. |
|
39 |
3747
|
40 #define EMPTY_RETURN_CHECK(T) \ |
|
41 if (nr == 0 || nc == 0) \ |
|
42 return T (0, 0) |
|
43 |
2928
|
44 static Matrix |
|
45 min (double d, const Matrix& m) |
|
46 { |
|
47 int nr = m.rows (); |
|
48 int nc = m.columns (); |
|
49 |
3747
|
50 EMPTY_RETURN_CHECK (Matrix); |
|
51 |
2928
|
52 Matrix result (nr, nc); |
|
53 |
|
54 for (int j = 0; j < nc; j++) |
|
55 for (int i = 0; i < nr; i++) |
3248
|
56 result (i, j) = xmin (d, m (i, j)); |
2928
|
57 |
|
58 return result; |
|
59 } |
|
60 |
|
61 static Matrix |
|
62 min (const Matrix& m, double d) |
|
63 { |
|
64 int nr = m.rows (); |
|
65 int nc = m.columns (); |
|
66 |
3747
|
67 EMPTY_RETURN_CHECK (Matrix); |
|
68 |
2928
|
69 Matrix result (nr, nc); |
|
70 |
|
71 for (int j = 0; j < nc; j++) |
|
72 for (int i = 0; i < nr; i++) |
3248
|
73 result (i, j) = xmin (m (i, j), d); |
2928
|
74 |
|
75 return result; |
|
76 } |
|
77 |
|
78 static ComplexMatrix |
|
79 min (const Complex& c, const ComplexMatrix& m) |
|
80 { |
|
81 int nr = m.rows (); |
|
82 int nc = m.columns (); |
|
83 |
3747
|
84 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
85 |
2928
|
86 ComplexMatrix result (nr, nc); |
|
87 |
|
88 for (int j = 0; j < nc; j++) |
3248
|
89 for (int i = 0; i < nr; i++) |
|
90 result (i, j) = xmin (c, m (i, j)); |
2928
|
91 |
|
92 return result; |
|
93 } |
|
94 |
|
95 static ComplexMatrix |
|
96 min (const ComplexMatrix& m, const Complex& c) |
|
97 { |
|
98 int nr = m.rows (); |
|
99 int nc = m.columns (); |
|
100 |
3747
|
101 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
102 |
2928
|
103 ComplexMatrix result (nr, nc); |
|
104 |
|
105 for (int j = 0; j < nc; j++) |
|
106 for (int i = 0; i < nr; i++) |
3248
|
107 result (i, j) = xmin (m (i, j), c); |
2928
|
108 |
|
109 return result; |
|
110 } |
|
111 |
|
112 static Matrix |
|
113 min (const Matrix& a, const Matrix& b) |
|
114 { |
|
115 int nr = a.rows (); |
|
116 int nc = a.columns (); |
3747
|
117 |
2928
|
118 if (nr != b.rows () || nc != b.columns ()) |
|
119 { |
|
120 error ("two-arg min expecting args of same size"); |
|
121 return Matrix (); |
|
122 } |
|
123 |
3747
|
124 EMPTY_RETURN_CHECK (Matrix); |
|
125 |
2928
|
126 Matrix result (nr, nc); |
|
127 |
|
128 for (int j = 0; j < nc; j++) |
|
129 for (int i = 0; i < nr; i++) |
3248
|
130 result (i, j) = xmin (a (i, j), b (i, j)); |
2928
|
131 |
|
132 return result; |
|
133 } |
|
134 |
|
135 static ComplexMatrix |
|
136 min (const ComplexMatrix& a, const ComplexMatrix& b) |
|
137 { |
|
138 int nr = a.rows (); |
|
139 int nc = a.columns (); |
3747
|
140 |
2928
|
141 if (nr != b.rows () || nc != b.columns ()) |
|
142 { |
|
143 error ("two-arg min expecting args of same size"); |
|
144 return ComplexMatrix (); |
|
145 } |
|
146 |
3747
|
147 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
148 |
2928
|
149 ComplexMatrix result (nr, nc); |
|
150 |
|
151 for (int j = 0; j < nc; j++) |
|
152 { |
|
153 int columns_are_real_only = 1; |
|
154 for (int i = 0; i < nr; i++) |
3243
|
155 if (imag (a (i, j)) != 0.0 || imag (b (i, j)) != 0.0) |
2928
|
156 { |
|
157 columns_are_real_only = 0; |
|
158 break; |
|
159 } |
|
160 |
|
161 if (columns_are_real_only) |
|
162 { |
|
163 for (int i = 0; i < nr; i++) |
3248
|
164 result (i, j) = xmin (real (a (i, j)), real (b (i, j))); |
2928
|
165 } |
|
166 else |
|
167 { |
|
168 for (int i = 0; i < nr; i++) |
3248
|
169 result (i, j) = xmin (a (i, j), b (i, j)); |
2928
|
170 } |
|
171 } |
|
172 |
|
173 return result; |
|
174 } |
|
175 |
|
176 static Matrix |
|
177 max (double d, const Matrix& m) |
|
178 { |
|
179 int nr = m.rows (); |
|
180 int nc = m.columns (); |
|
181 |
3747
|
182 EMPTY_RETURN_CHECK (Matrix); |
|
183 |
2928
|
184 Matrix result (nr, nc); |
|
185 |
|
186 for (int j = 0; j < nc; j++) |
|
187 for (int i = 0; i < nr; i++) |
3248
|
188 result (i, j) = xmax (d, m (i, j)); |
2928
|
189 |
|
190 return result; |
|
191 } |
|
192 |
|
193 static Matrix |
|
194 max (const Matrix& m, double d) |
|
195 { |
|
196 int nr = m.rows (); |
|
197 int nc = m.columns (); |
|
198 |
3747
|
199 EMPTY_RETURN_CHECK (Matrix); |
|
200 |
2928
|
201 Matrix result (nr, nc); |
|
202 |
|
203 for (int j = 0; j < nc; j++) |
|
204 for (int i = 0; i < nr; i++) |
3248
|
205 result (i, j) = xmax (m (i, j), d); |
2928
|
206 |
|
207 return result; |
|
208 } |
|
209 |
|
210 static ComplexMatrix |
|
211 max (const Complex& c, const ComplexMatrix& m) |
|
212 { |
|
213 int nr = m.rows (); |
|
214 int nc = m.columns (); |
|
215 |
3747
|
216 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
217 |
2928
|
218 ComplexMatrix result (nr, nc); |
|
219 |
|
220 for (int j = 0; j < nc; j++) |
|
221 for (int i = 0; i < nr; i++) |
3248
|
222 result (i, j) = xmax (c, m (i, j)); |
2928
|
223 |
|
224 return result; |
|
225 } |
|
226 |
|
227 static ComplexMatrix |
|
228 max (const ComplexMatrix& m, const Complex& c) |
|
229 { |
|
230 int nr = m.rows (); |
|
231 int nc = m.columns (); |
|
232 |
3747
|
233 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
234 |
2928
|
235 ComplexMatrix result (nr, nc); |
|
236 |
|
237 for (int j = 0; j < nc; j++) |
|
238 for (int i = 0; i < nr; i++) |
3248
|
239 result (i, j) = xmax (m (i, j), c); |
2928
|
240 |
|
241 return result; |
|
242 } |
|
243 |
|
244 static Matrix |
|
245 max (const Matrix& a, const Matrix& b) |
|
246 { |
|
247 int nr = a.rows (); |
|
248 int nc = a.columns (); |
3747
|
249 |
2928
|
250 if (nr != b.rows () || nc != b.columns ()) |
|
251 { |
|
252 error ("two-arg max expecting args of same size"); |
|
253 return Matrix (); |
|
254 } |
|
255 |
3747
|
256 EMPTY_RETURN_CHECK (Matrix); |
|
257 |
2928
|
258 Matrix result (nr, nc); |
|
259 |
|
260 for (int j = 0; j < nc; j++) |
|
261 for (int i = 0; i < nr; i++) |
3248
|
262 result (i, j) = xmax (a (i, j), b (i, j)); |
2928
|
263 |
|
264 return result; |
|
265 } |
|
266 |
|
267 static ComplexMatrix |
|
268 max (const ComplexMatrix& a, const ComplexMatrix& b) |
|
269 { |
|
270 int nr = a.rows (); |
|
271 int nc = a.columns (); |
3747
|
272 |
2928
|
273 if (nr != b.rows () || nc != b.columns ()) |
|
274 { |
|
275 error ("two-arg max expecting args of same size"); |
|
276 return ComplexMatrix (); |
|
277 } |
|
278 |
3747
|
279 EMPTY_RETURN_CHECK (ComplexMatrix); |
|
280 |
2928
|
281 ComplexMatrix result (nr, nc); |
|
282 |
|
283 for (int j = 0; j < nc; j++) |
|
284 { |
|
285 int columns_are_real_only = 1; |
|
286 for (int i = 0; i < nr; i++) |
3243
|
287 if (imag (a (i, j)) != 0.0 || imag (b (i, j)) != 0.0) |
2928
|
288 { |
|
289 columns_are_real_only = 0; |
|
290 break; |
|
291 } |
|
292 |
|
293 if (columns_are_real_only) |
|
294 { |
|
295 for (int i = 0; i < nr; i++) |
3248
|
296 result (i, j) = xmax (real (a (i, j)), real (b (i, j))); |
2928
|
297 } |
|
298 else |
|
299 { |
|
300 for (int i = 0; i < nr; i++) |
3248
|
301 result (i, j) = xmax (a (i, j), b (i, j)); |
2928
|
302 } |
|
303 } |
|
304 |
|
305 return result; |
|
306 } |
|
307 |
3747
|
308 #define MINMAX_BODY(FCN) \ |
|
309 \ |
|
310 octave_value_list retval; \ |
|
311 \ |
|
312 int nargin = args.length (); \ |
|
313 \ |
|
314 if (nargin < 1 || nargin > 2 || nargout > 2) \ |
|
315 { \ |
|
316 print_usage (#FCN); \ |
|
317 return retval; \ |
|
318 } \ |
|
319 \ |
|
320 octave_value arg1; \ |
|
321 octave_value arg2; \ |
|
322 \ |
|
323 switch (nargin) \ |
|
324 { \ |
|
325 case 2: \ |
|
326 arg2 = args(1); \ |
|
327 \ |
|
328 case 1: \ |
|
329 arg1 = args(0); \ |
|
330 break; \ |
|
331 \ |
|
332 default: \ |
|
333 panic_impossible (); \ |
|
334 break; \ |
|
335 } \ |
|
336 \ |
|
337 if (nargin == 1 && (nargout == 1 || nargout == 0)) \ |
|
338 { \ |
|
339 if (arg1.is_real_type ()) \ |
|
340 { \ |
|
341 Matrix m = arg1.matrix_value (); \ |
|
342 \ |
|
343 if (! error_state) \ |
|
344 { \ |
|
345 if (m.rows () == 1) \ |
|
346 retval(0) = m.row_ ## FCN (); \ |
|
347 else \ |
|
348 { \ |
|
349 if (m.rows () == 0 || m.columns () == 0) \ |
|
350 retval(0) = Matrix (); \ |
|
351 else \ |
|
352 retval(0) = m.column_ ## FCN (); \ |
|
353 } \ |
|
354 } \ |
|
355 } \ |
|
356 else if (arg1.is_complex_type ()) \ |
|
357 { \ |
|
358 ComplexMatrix m = arg1.complex_matrix_value (); \ |
|
359 \ |
|
360 if (! error_state) \ |
|
361 { \ |
|
362 if (m.rows () == 1) \ |
|
363 retval(0) = m.row_ ## FCN (); \ |
|
364 else \ |
|
365 { \ |
|
366 if (m.rows () == 0 || m.columns () == 0) \ |
|
367 retval(0) = Matrix (); \ |
|
368 else \ |
|
369 retval(0) = m.column_ ## FCN (); \ |
|
370 } \ |
|
371 } \ |
|
372 } \ |
|
373 else \ |
|
374 gripe_wrong_type_arg (#FCN, arg1); \ |
|
375 } \ |
|
376 else if (nargin == 1 && nargout == 2) \ |
|
377 { \ |
|
378 Array<int> index; \ |
|
379 \ |
|
380 if (arg1.is_real_type ()) \ |
|
381 { \ |
|
382 Matrix m = arg1.matrix_value (); \ |
|
383 \ |
|
384 if (! error_state) \ |
|
385 { \ |
|
386 retval.resize (2); \ |
|
387 \ |
|
388 if (m.rows () == 1) \ |
|
389 retval(0) = m.row_ ## FCN (index); \ |
|
390 else \ |
|
391 { \ |
|
392 if (m.rows () == 0 || m.columns () == 0) \ |
|
393 retval(0) = Matrix (); \ |
|
394 else \ |
|
395 retval(0) = m.column_ ## FCN (index); \ |
|
396 } \ |
|
397 } \ |
|
398 } \ |
|
399 else if (arg1.is_complex_type ()) \ |
|
400 { \ |
|
401 ComplexMatrix m = arg1.complex_matrix_value (); \ |
|
402 \ |
|
403 if (! error_state) \ |
|
404 { \ |
|
405 retval.resize (2); \ |
|
406 \ |
|
407 if (m.rows () == 1) \ |
|
408 retval(0) = m.row_ ## FCN (index); \ |
|
409 else \ |
|
410 { \ |
|
411 if (m.rows () == 0 || m.columns () == 0) \ |
|
412 retval(0) = Matrix (); \ |
|
413 else \ |
|
414 retval(0) = m.column_ ## FCN (index); \ |
|
415 } \ |
|
416 } \ |
|
417 } \ |
|
418 else \ |
|
419 gripe_wrong_type_arg (#FCN, arg1); \ |
|
420 \ |
|
421 int len = index.length (); \ |
|
422 \ |
|
423 if (len > 0) \ |
|
424 { \ |
|
425 RowVector idx (len); \ |
|
426 \ |
|
427 for (int i = 0; i < len; i++) \ |
|
428 { \ |
|
429 int tmp = index.elem (i) + 1; \ |
|
430 idx.elem (i) = (tmp <= 0) \ |
|
431 ? octave_NaN : static_cast<double> (tmp); \ |
|
432 } \ |
|
433 \ |
|
434 retval(1) = idx; \ |
|
435 } \ |
|
436 else \ |
|
437 retval(1) = Matrix (); \ |
|
438 } \ |
|
439 else if (nargin == 2) \ |
|
440 { \ |
|
441 int arg1_is_scalar = arg1.is_scalar_type (); \ |
|
442 int arg2_is_scalar = arg2.is_scalar_type (); \ |
|
443 \ |
|
444 int arg1_is_complex = arg1.is_complex_type (); \ |
|
445 int arg2_is_complex = arg2.is_complex_type (); \ |
|
446 \ |
|
447 if (arg1_is_scalar) \ |
|
448 { \ |
|
449 if (arg1_is_complex || arg2_is_complex) \ |
|
450 { \ |
|
451 Complex c1 = arg1.complex_value (); \ |
|
452 ComplexMatrix m2 = arg2.complex_matrix_value (); \ |
|
453 if (! error_state) \ |
|
454 { \ |
|
455 ComplexMatrix result = FCN (c1, m2); \ |
|
456 if (! error_state) \ |
|
457 retval(0) = result; \ |
|
458 } \ |
|
459 } \ |
|
460 else \ |
|
461 { \ |
|
462 double d1 = arg1.double_value (); \ |
|
463 Matrix m2 = arg2.matrix_value (); \ |
|
464 \ |
|
465 if (! error_state) \ |
|
466 { \ |
|
467 Matrix result = FCN (d1, m2); \ |
|
468 if (! error_state) \ |
|
469 retval(0) = result; \ |
|
470 } \ |
|
471 } \ |
|
472 } \ |
|
473 else if (arg2_is_scalar) \ |
|
474 { \ |
|
475 if (arg1_is_complex || arg2_is_complex) \ |
|
476 { \ |
|
477 ComplexMatrix m1 = arg1.complex_matrix_value (); \ |
|
478 \ |
|
479 if (! error_state) \ |
|
480 { \ |
|
481 Complex c2 = arg2.complex_value (); \ |
|
482 ComplexMatrix result = FCN (m1, c2); \ |
|
483 if (! error_state) \ |
|
484 retval(0) = result; \ |
|
485 } \ |
|
486 } \ |
|
487 else \ |
|
488 { \ |
|
489 Matrix m1 = arg1.matrix_value (); \ |
|
490 \ |
|
491 if (! error_state) \ |
|
492 { \ |
|
493 double d2 = arg2.double_value (); \ |
|
494 Matrix result = FCN (m1, d2); \ |
|
495 if (! error_state) \ |
|
496 retval(0) = result; \ |
|
497 } \ |
|
498 } \ |
|
499 } \ |
|
500 else \ |
|
501 { \ |
|
502 if (arg1_is_complex || arg2_is_complex) \ |
|
503 { \ |
|
504 ComplexMatrix m1 = arg1.complex_matrix_value (); \ |
|
505 \ |
|
506 if (! error_state) \ |
|
507 { \ |
|
508 ComplexMatrix m2 = arg2.complex_matrix_value (); \ |
|
509 \ |
|
510 if (! error_state) \ |
|
511 { \ |
|
512 ComplexMatrix result = FCN (m1, m2); \ |
|
513 if (! error_state) \ |
|
514 retval(0) = result; \ |
|
515 } \ |
|
516 } \ |
|
517 } \ |
|
518 else \ |
|
519 { \ |
|
520 Matrix m1 = arg1.matrix_value (); \ |
|
521 \ |
|
522 if (! error_state) \ |
|
523 { \ |
|
524 Matrix m2 = arg2.matrix_value (); \ |
|
525 \ |
|
526 if (! error_state) \ |
|
527 { \ |
|
528 Matrix result = FCN (m1, m2); \ |
|
529 if (! error_state) \ |
|
530 retval(0) = result; \ |
|
531 } \ |
|
532 } \ |
|
533 } \ |
|
534 } \ |
|
535 } \ |
|
536 else \ |
|
537 panic_impossible (); \ |
|
538 \ |
|
539 return retval |
|
540 |
2928
|
541 DEFUN_DLD (min, args, nargout, |
3443
|
542 "-*- texinfo -*-\n\ |
|
543 For a vector argument, return the minimum value. For a matrix\n\ |
|
544 argument, return the minimum value from each column, as a row\n\ |
|
545 vector. Thus,\n\ |
|
546 \n\ |
|
547 @example\n\ |
|
548 min (min (@var{x}))\n\ |
|
549 @end example\n\ |
|
550 \n\ |
|
551 @noindent\n\ |
|
552 returns the smallest element of @var{x}.\n\ |
|
553 \n\ |
|
554 For complex arguments, the magnitude of the elements are used for\n\ |
3657
|
555 comparison.\n\ |
|
556 \n\ |
|
557 If called with two output arguments, also returns the index of the\n\ |
|
558 minimum value(s). Thus,\n\ |
|
559 @example\n |
|
560 [x, ix] = min ([1, 3, 0, 2, 5])\n\ |
|
561 @end example\n\ |
|
562 \n\ |
|
563 @noindent\n\ |
|
564 returns @var{x} = 0 and @var{ix} = 3.") |
2928
|
565 { |
3747
|
566 MINMAX_BODY (min); |
2928
|
567 } |
|
568 |
|
569 DEFUN_DLD (max, args, nargout, |
3443
|
570 "-*- texinfo -*-\n\ |
|
571 For a vector argument, return the maximum value. For a matrix\n\ |
|
572 argument, return the maximum value from each column, as a row\n\ |
|
573 vector. Thus,\n\ |
|
574 \n\ |
|
575 @example\n\ |
|
576 max (max (@var{x}))\n\ |
|
577 @end example\n\ |
|
578 \n\ |
|
579 @noindent\n\ |
|
580 returns the largest element of @var{x}.\n\ |
|
581 \n\ |
|
582 For complex arguments, the magnitude of the elements are used for\n\ |
3657
|
583 comparison. |
|
584 \n\ |
|
585 If called with two output arguments, also returns the index of the\n\ |
|
586 maximum value(s). Thus,\n\ |
|
587 @example\n |
|
588 [x, ix] = max([1, 3, 5, 2, 5])\n\ |
|
589 @end example\n\ |
|
590 \n\ |
|
591 @noindent\n\ |
|
592 returns @var{x} = 5 and @var{ix} = 3.") |
2928
|
593 { |
3747
|
594 MINMAX_BODY (max); |
2928
|
595 } |
|
596 |
|
597 /* |
|
598 ;;; Local Variables: *** |
|
599 ;;; mode: C++ *** |
|
600 ;;; End: *** |
|
601 */ |