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