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, |
|
277 "min (X): minimum value(s) of a vector (matrix)") |
|
278 { |
|
279 octave_value_list retval; |
|
280 |
|
281 int nargin = args.length (); |
|
282 |
|
283 if (nargin < 1 || nargin > 2 || nargout > 2) |
|
284 { |
|
285 print_usage ("min"); |
|
286 return retval; |
|
287 } |
|
288 |
|
289 octave_value arg1; |
|
290 octave_value arg2; |
|
291 |
|
292 switch (nargin) |
|
293 { |
|
294 case 2: |
|
295 arg2 = args(1); |
|
296 // Fall through... |
|
297 |
|
298 case 1: |
|
299 arg1 = args(0); |
|
300 break; |
|
301 |
|
302 default: |
|
303 panic_impossible (); |
|
304 break; |
|
305 } |
|
306 |
|
307 if (nargin == 1 && (nargout == 1 || nargout == 0)) |
|
308 { |
|
309 if (arg1.is_real_type ()) |
|
310 { |
|
311 Matrix m = arg1.matrix_value (); |
|
312 |
|
313 if (! error_state) |
|
314 { |
|
315 if (m.rows () == 1) |
|
316 retval(0) = m.row_min (); |
|
317 else |
|
318 retval(0) = octave_value (m.column_min (), 0); |
|
319 } |
|
320 } |
|
321 else if (arg1.is_complex_type ()) |
|
322 { |
|
323 ComplexMatrix m = arg1.complex_matrix_value (); |
|
324 |
|
325 if (! error_state) |
|
326 { |
|
327 if (m.rows () == 1) |
|
328 retval(0) = m.row_min (); |
|
329 else |
|
330 retval(0) = octave_value (m.column_min (), 0); |
|
331 } |
|
332 } |
|
333 else |
|
334 gripe_wrong_type_arg ("min", arg1); |
|
335 } |
|
336 else if (nargin == 1 && nargout == 2) |
|
337 { |
|
338 Array<int> index; |
|
339 |
|
340 if (arg1.is_real_type ()) |
|
341 { |
|
342 Matrix m = arg1.matrix_value (); |
|
343 |
|
344 if (! error_state) |
|
345 { |
|
346 retval.resize (2); |
|
347 |
|
348 if (m.rows () == 1) |
|
349 retval(0) = m.row_min (index); |
|
350 else |
|
351 retval(0) = octave_value (m.column_min (index), 0); |
|
352 } |
|
353 } |
|
354 else if (arg1.is_complex_type ()) |
|
355 { |
|
356 ComplexMatrix m = arg1.complex_matrix_value (); |
|
357 |
|
358 if (! error_state) |
|
359 { |
|
360 retval.resize (2); |
|
361 |
|
362 if (m.rows () == 1) |
|
363 retval(0) = m.row_min (index); |
|
364 else |
|
365 retval(0) = octave_value (m.column_min (index), 0); |
|
366 } |
|
367 } |
|
368 else |
|
369 gripe_wrong_type_arg ("min", arg1); |
|
370 |
|
371 int len = index.length (); |
|
372 |
|
373 if (len > 0) |
|
374 { |
|
375 RowVector idx (len); |
|
376 |
|
377 for (int i = 0; i < len; i++) |
|
378 { |
|
379 int tmp = index.elem (i) + 1; |
|
380 idx.elem (i) = (tmp <= 0) |
|
381 ? octave_NaN : static_cast<double> (tmp); |
|
382 } |
|
383 |
|
384 retval(1) = octave_value (idx, 0); |
|
385 } |
|
386 } |
|
387 else if (nargin == 2) |
|
388 { |
|
389 int arg1_is_scalar = arg1.is_scalar_type (); |
|
390 int arg2_is_scalar = arg2.is_scalar_type (); |
|
391 |
|
392 int arg1_is_complex = arg1.is_complex_type (); |
|
393 int arg2_is_complex = arg2.is_complex_type (); |
|
394 |
|
395 if (arg1_is_scalar) |
|
396 { |
|
397 if (arg1_is_complex || arg2_is_complex) |
|
398 { |
|
399 Complex c1 = arg1.complex_value (); |
|
400 ComplexMatrix m2 = arg2.complex_matrix_value (); |
|
401 if (! error_state) |
|
402 { |
|
403 ComplexMatrix result = min (c1, m2); |
|
404 if (! error_state) |
|
405 retval(0) = result; |
|
406 } |
|
407 } |
|
408 else |
|
409 { |
|
410 double d1 = arg1.double_value (); |
|
411 Matrix m2 = arg2.matrix_value (); |
|
412 |
|
413 if (! error_state) |
|
414 { |
|
415 Matrix result = min (d1, m2); |
|
416 if (! error_state) |
|
417 retval(0) = result; |
|
418 } |
|
419 } |
|
420 } |
|
421 else if (arg2_is_scalar) |
|
422 { |
|
423 if (arg1_is_complex || arg2_is_complex) |
|
424 { |
|
425 ComplexMatrix m1 = arg1.complex_matrix_value (); |
|
426 |
|
427 if (! error_state) |
|
428 { |
|
429 Complex c2 = arg2.complex_value (); |
|
430 ComplexMatrix result = min (m1, c2); |
|
431 if (! error_state) |
|
432 retval(0) = result; |
|
433 } |
|
434 } |
|
435 else |
|
436 { |
|
437 Matrix m1 = arg1.matrix_value (); |
|
438 |
|
439 if (! error_state) |
|
440 { |
|
441 double d2 = arg2.double_value (); |
|
442 Matrix result = min (m1, d2); |
|
443 if (! error_state) |
|
444 retval(0) = result; |
|
445 } |
|
446 } |
|
447 } |
|
448 else |
|
449 { |
|
450 if (arg1_is_complex || arg2_is_complex) |
|
451 { |
|
452 ComplexMatrix m1 = arg1.complex_matrix_value (); |
|
453 |
|
454 if (! error_state) |
|
455 { |
|
456 ComplexMatrix m2 = arg2.complex_matrix_value (); |
|
457 |
|
458 if (! error_state) |
|
459 { |
|
460 ComplexMatrix result = min (m1, m2); |
|
461 if (! error_state) |
|
462 retval(0) = result; |
|
463 } |
|
464 } |
|
465 } |
|
466 else |
|
467 { |
|
468 Matrix m1 = arg1.matrix_value (); |
|
469 |
|
470 if (! error_state) |
|
471 { |
|
472 Matrix m2 = arg2.matrix_value (); |
|
473 |
|
474 if (! error_state) |
|
475 { |
|
476 Matrix result = min (m1, m2); |
|
477 if (! error_state) |
|
478 retval(0) = result; |
|
479 } |
|
480 } |
|
481 } |
|
482 } |
|
483 } |
|
484 else |
|
485 panic_impossible (); |
|
486 |
|
487 return retval; |
|
488 } |
|
489 |
|
490 DEFUN_DLD (max, args, nargout, |
|
491 "max (X): maximum value(s) of a vector (matrix)") |
|
492 { |
|
493 octave_value_list retval; |
|
494 |
|
495 int nargin = args.length (); |
|
496 |
|
497 if (nargin < 1 || nargin > 2 || nargout > 2) |
|
498 { |
|
499 print_usage ("max"); |
|
500 return retval; |
|
501 } |
|
502 |
|
503 octave_value arg1; |
|
504 octave_value arg2; |
|
505 |
|
506 switch (nargin) |
|
507 { |
|
508 case 2: |
|
509 arg2 = args(1); |
|
510 // Fall through... |
|
511 |
|
512 case 1: |
|
513 arg1 = args(0); |
|
514 break; |
|
515 |
|
516 default: |
|
517 panic_impossible (); |
|
518 break; |
|
519 } |
|
520 |
|
521 if (nargin == 1 && (nargout == 1 || nargout == 0)) |
|
522 { |
|
523 if (arg1.is_real_type ()) |
|
524 { |
|
525 Matrix m = arg1.matrix_value (); |
|
526 |
|
527 if (! error_state) |
|
528 { |
|
529 if (m.rows () == 1) |
|
530 retval(0) = m.row_max (); |
|
531 else |
|
532 retval(0) = octave_value (m.column_max (), 0); |
|
533 } |
|
534 } |
|
535 else if (arg1.is_complex_type ()) |
|
536 { |
|
537 ComplexMatrix m = arg1.complex_matrix_value (); |
|
538 |
|
539 if (! error_state) |
|
540 { |
|
541 if (m.rows () == 1) |
|
542 retval(0) = m.row_max (); |
|
543 else |
|
544 retval(0) = octave_value (m.column_max (), 0); |
|
545 } |
|
546 } |
|
547 else |
|
548 gripe_wrong_type_arg ("max", arg1); |
|
549 } |
|
550 else if (nargin == 1 && nargout == 2) |
|
551 { |
|
552 Array<int> index; |
|
553 |
|
554 if (arg1.is_real_type ()) |
|
555 { |
|
556 Matrix m = arg1.matrix_value (); |
|
557 |
|
558 if (! error_state) |
|
559 { |
|
560 retval.resize (2); |
|
561 |
|
562 if (m.rows () == 1) |
|
563 retval(0) = m.row_max (index); |
|
564 else |
|
565 retval(0) = octave_value (m.column_max (index), 0); |
|
566 } |
|
567 } |
|
568 else if (arg1.is_complex_type ()) |
|
569 { |
|
570 ComplexMatrix m = arg1.complex_matrix_value (); |
|
571 |
|
572 if (! error_state) |
|
573 { |
|
574 retval.resize (2); |
|
575 |
|
576 if (m.rows () == 1) |
|
577 retval(0) = m.row_max (index); |
|
578 else |
|
579 retval(0) = octave_value (m.column_max (index), 0); |
|
580 } |
|
581 } |
|
582 else |
|
583 gripe_wrong_type_arg ("max", arg1); |
|
584 |
|
585 int len = index.length (); |
|
586 |
|
587 if (len > 0) |
|
588 { |
|
589 RowVector idx (len); |
|
590 |
|
591 for (int i = 0; i < len; i++) |
|
592 { |
|
593 int tmp = index.elem (i) + 1; |
|
594 idx.elem (i) = (tmp <= 0) |
|
595 ? octave_NaN : static_cast<double> (tmp); |
|
596 } |
|
597 |
|
598 retval(1) = octave_value (idx, 0); |
|
599 } |
|
600 } |
|
601 else if (nargin == 2) |
|
602 { |
|
603 int arg1_is_scalar = arg1.is_scalar_type (); |
|
604 int arg2_is_scalar = arg2.is_scalar_type (); |
|
605 |
|
606 int arg1_is_complex = arg1.is_complex_type (); |
|
607 int arg2_is_complex = arg2.is_complex_type (); |
|
608 |
|
609 if (arg1_is_scalar) |
|
610 { |
|
611 if (arg1_is_complex || arg2_is_complex) |
|
612 { |
|
613 Complex c1 = arg1.complex_value (); |
|
614 ComplexMatrix m2 = arg2.complex_matrix_value (); |
|
615 if (! error_state) |
|
616 { |
|
617 ComplexMatrix result = max (c1, m2); |
|
618 if (! error_state) |
|
619 retval(0) = result; |
|
620 } |
|
621 } |
|
622 else |
|
623 { |
|
624 double d1 = arg1.double_value (); |
|
625 Matrix m2 = arg2.matrix_value (); |
|
626 |
|
627 if (! error_state) |
|
628 { |
|
629 Matrix result = max (d1, m2); |
|
630 if (! error_state) |
|
631 retval(0) = result; |
|
632 } |
|
633 } |
|
634 } |
|
635 else if (arg2_is_scalar) |
|
636 { |
|
637 if (arg1_is_complex || arg2_is_complex) |
|
638 { |
|
639 ComplexMatrix m1 = arg1.complex_matrix_value (); |
|
640 |
|
641 if (! error_state) |
|
642 { |
|
643 Complex c2 = arg2.complex_value (); |
|
644 ComplexMatrix result = max (m1, c2); |
|
645 if (! error_state) |
|
646 retval(0) = result; |
|
647 } |
|
648 } |
|
649 else |
|
650 { |
|
651 Matrix m1 = arg1.matrix_value (); |
|
652 |
|
653 if (! error_state) |
|
654 { |
|
655 double d2 = arg2.double_value (); |
|
656 Matrix result = max (m1, d2); |
|
657 if (! error_state) |
|
658 retval(0) = result; |
|
659 } |
|
660 } |
|
661 } |
|
662 else |
|
663 { |
|
664 if (arg1_is_complex || arg2_is_complex) |
|
665 { |
|
666 ComplexMatrix m1 = arg1.complex_matrix_value (); |
|
667 |
|
668 if (! error_state) |
|
669 { |
|
670 ComplexMatrix m2 = arg2.complex_matrix_value (); |
|
671 |
|
672 if (! error_state) |
|
673 { |
|
674 ComplexMatrix result = max (m1, m2); |
|
675 if (! error_state) |
|
676 retval(0) = result; |
|
677 } |
|
678 } |
|
679 } |
|
680 else |
|
681 { |
|
682 Matrix m1 = arg1.matrix_value (); |
|
683 |
|
684 if (! error_state) |
|
685 { |
|
686 Matrix m2 = arg2.matrix_value (); |
|
687 |
|
688 if (! error_state) |
|
689 { |
|
690 Matrix result = max (m1, m2); |
|
691 if (! error_state) |
|
692 retval(0) = result; |
|
693 } |
|
694 } |
|
695 } |
|
696 } |
|
697 } |
|
698 else |
|
699 panic_impossible (); |
|
700 |
|
701 return retval; |
|
702 } |
|
703 |
|
704 /* |
|
705 ;;; Local Variables: *** |
|
706 ;;; mode: C++ *** |
|
707 ;;; End: *** |
|
708 */ |