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