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" |
4309
|
31 #include "dMatrix.h" |
|
32 #include "CMatrix.h" |
4153
|
33 #include "quit.h" |
2928
|
34 |
|
35 #include "defun-dld.h" |
|
36 #include "error.h" |
|
37 #include "gripes.h" |
|
38 #include "oct-obj.h" |
|
39 |
3747
|
40 #define MINMAX_BODY(FCN) \ |
|
41 \ |
|
42 octave_value_list retval; \ |
|
43 \ |
|
44 int nargin = args.length (); \ |
|
45 \ |
|
46 if (nargin < 1 || nargin > 2 || nargout > 2) \ |
|
47 { \ |
|
48 print_usage (#FCN); \ |
|
49 return retval; \ |
|
50 } \ |
|
51 \ |
|
52 octave_value arg1; \ |
|
53 octave_value arg2; \ |
|
54 \ |
|
55 switch (nargin) \ |
|
56 { \ |
|
57 case 2: \ |
|
58 arg2 = args(1); \ |
|
59 \ |
|
60 case 1: \ |
|
61 arg1 = args(0); \ |
|
62 break; \ |
|
63 \ |
|
64 default: \ |
|
65 panic_impossible (); \ |
|
66 break; \ |
|
67 } \ |
|
68 \ |
|
69 if (nargin == 1 && (nargout == 1 || nargout == 0)) \ |
|
70 { \ |
|
71 if (arg1.is_real_type ()) \ |
|
72 { \ |
|
73 Matrix m = arg1.matrix_value (); \ |
|
74 \ |
|
75 if (! error_state) \ |
|
76 { \ |
|
77 if (m.rows () == 1) \ |
|
78 retval(0) = m.row_ ## FCN (); \ |
|
79 else \ |
|
80 { \ |
|
81 if (m.rows () == 0 || m.columns () == 0) \ |
|
82 retval(0) = Matrix (); \ |
|
83 else \ |
|
84 retval(0) = m.column_ ## FCN (); \ |
|
85 } \ |
|
86 } \ |
|
87 } \ |
|
88 else if (arg1.is_complex_type ()) \ |
|
89 { \ |
|
90 ComplexMatrix m = arg1.complex_matrix_value (); \ |
|
91 \ |
|
92 if (! error_state) \ |
|
93 { \ |
|
94 if (m.rows () == 1) \ |
|
95 retval(0) = m.row_ ## FCN (); \ |
|
96 else \ |
|
97 { \ |
|
98 if (m.rows () == 0 || m.columns () == 0) \ |
|
99 retval(0) = Matrix (); \ |
|
100 else \ |
|
101 retval(0) = m.column_ ## FCN (); \ |
|
102 } \ |
|
103 } \ |
|
104 } \ |
|
105 else \ |
|
106 gripe_wrong_type_arg (#FCN, arg1); \ |
|
107 } \ |
|
108 else if (nargin == 1 && nargout == 2) \ |
|
109 { \ |
|
110 Array<int> index; \ |
|
111 \ |
|
112 if (arg1.is_real_type ()) \ |
|
113 { \ |
|
114 Matrix m = arg1.matrix_value (); \ |
|
115 \ |
|
116 if (! error_state) \ |
|
117 { \ |
|
118 retval.resize (2); \ |
|
119 \ |
|
120 if (m.rows () == 1) \ |
|
121 retval(0) = m.row_ ## FCN (index); \ |
|
122 else \ |
|
123 { \ |
|
124 if (m.rows () == 0 || m.columns () == 0) \ |
|
125 retval(0) = Matrix (); \ |
|
126 else \ |
|
127 retval(0) = m.column_ ## FCN (index); \ |
|
128 } \ |
|
129 } \ |
|
130 } \ |
|
131 else if (arg1.is_complex_type ()) \ |
|
132 { \ |
|
133 ComplexMatrix m = arg1.complex_matrix_value (); \ |
|
134 \ |
|
135 if (! error_state) \ |
|
136 { \ |
|
137 retval.resize (2); \ |
|
138 \ |
|
139 if (m.rows () == 1) \ |
|
140 retval(0) = m.row_ ## FCN (index); \ |
|
141 else \ |
|
142 { \ |
|
143 if (m.rows () == 0 || m.columns () == 0) \ |
|
144 retval(0) = Matrix (); \ |
|
145 else \ |
|
146 retval(0) = m.column_ ## FCN (index); \ |
|
147 } \ |
|
148 } \ |
|
149 } \ |
|
150 else \ |
|
151 gripe_wrong_type_arg (#FCN, arg1); \ |
|
152 \ |
|
153 int len = index.length (); \ |
|
154 \ |
|
155 if (len > 0) \ |
|
156 { \ |
4102
|
157 double nan_val = lo_ieee_nan_value (); \ |
|
158 \ |
3747
|
159 RowVector idx (len); \ |
|
160 \ |
|
161 for (int i = 0; i < len; i++) \ |
|
162 { \ |
4153
|
163 OCTAVE_QUIT; \ |
3747
|
164 int tmp = index.elem (i) + 1; \ |
|
165 idx.elem (i) = (tmp <= 0) \ |
4102
|
166 ? nan_val : static_cast<double> (tmp); \ |
3747
|
167 } \ |
|
168 \ |
|
169 retval(1) = idx; \ |
|
170 } \ |
|
171 else \ |
|
172 retval(1) = Matrix (); \ |
|
173 } \ |
|
174 else if (nargin == 2) \ |
|
175 { \ |
|
176 int arg1_is_scalar = arg1.is_scalar_type (); \ |
|
177 int arg2_is_scalar = arg2.is_scalar_type (); \ |
|
178 \ |
|
179 int arg1_is_complex = arg1.is_complex_type (); \ |
|
180 int arg2_is_complex = arg2.is_complex_type (); \ |
|
181 \ |
|
182 if (arg1_is_scalar) \ |
|
183 { \ |
|
184 if (arg1_is_complex || arg2_is_complex) \ |
|
185 { \ |
|
186 Complex c1 = arg1.complex_value (); \ |
|
187 ComplexMatrix m2 = arg2.complex_matrix_value (); \ |
|
188 if (! error_state) \ |
|
189 { \ |
|
190 ComplexMatrix result = FCN (c1, m2); \ |
|
191 if (! error_state) \ |
|
192 retval(0) = result; \ |
|
193 } \ |
|
194 } \ |
|
195 else \ |
|
196 { \ |
|
197 double d1 = arg1.double_value (); \ |
|
198 Matrix m2 = arg2.matrix_value (); \ |
|
199 \ |
|
200 if (! error_state) \ |
|
201 { \ |
|
202 Matrix result = FCN (d1, m2); \ |
|
203 if (! error_state) \ |
|
204 retval(0) = result; \ |
|
205 } \ |
|
206 } \ |
|
207 } \ |
|
208 else if (arg2_is_scalar) \ |
|
209 { \ |
|
210 if (arg1_is_complex || arg2_is_complex) \ |
|
211 { \ |
|
212 ComplexMatrix m1 = arg1.complex_matrix_value (); \ |
|
213 \ |
|
214 if (! error_state) \ |
|
215 { \ |
|
216 Complex c2 = arg2.complex_value (); \ |
|
217 ComplexMatrix result = FCN (m1, c2); \ |
|
218 if (! error_state) \ |
|
219 retval(0) = result; \ |
|
220 } \ |
|
221 } \ |
|
222 else \ |
|
223 { \ |
|
224 Matrix m1 = arg1.matrix_value (); \ |
|
225 \ |
|
226 if (! error_state) \ |
|
227 { \ |
|
228 double d2 = arg2.double_value (); \ |
|
229 Matrix result = FCN (m1, d2); \ |
|
230 if (! error_state) \ |
|
231 retval(0) = result; \ |
|
232 } \ |
|
233 } \ |
|
234 } \ |
|
235 else \ |
|
236 { \ |
|
237 if (arg1_is_complex || arg2_is_complex) \ |
|
238 { \ |
|
239 ComplexMatrix m1 = arg1.complex_matrix_value (); \ |
|
240 \ |
|
241 if (! error_state) \ |
|
242 { \ |
|
243 ComplexMatrix m2 = arg2.complex_matrix_value (); \ |
|
244 \ |
|
245 if (! error_state) \ |
|
246 { \ |
|
247 ComplexMatrix result = FCN (m1, m2); \ |
|
248 if (! error_state) \ |
|
249 retval(0) = result; \ |
|
250 } \ |
|
251 } \ |
|
252 } \ |
|
253 else \ |
|
254 { \ |
|
255 Matrix m1 = arg1.matrix_value (); \ |
|
256 \ |
|
257 if (! error_state) \ |
|
258 { \ |
|
259 Matrix m2 = arg2.matrix_value (); \ |
|
260 \ |
|
261 if (! error_state) \ |
|
262 { \ |
|
263 Matrix result = FCN (m1, m2); \ |
|
264 if (! error_state) \ |
|
265 retval(0) = result; \ |
|
266 } \ |
|
267 } \ |
|
268 } \ |
|
269 } \ |
|
270 } \ |
|
271 else \ |
|
272 panic_impossible (); \ |
|
273 \ |
|
274 return retval |
|
275 |
2928
|
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\ |
3657
|
290 comparison.\n\ |
|
291 \n\ |
|
292 If called with two output arguments, also returns the index of the\n\ |
|
293 minimum value(s). Thus,\n\ |
3775
|
294 @example\n\ |
3657
|
295 [x, ix] = min ([1, 3, 0, 2, 5])\n\ |
|
296 @end example\n\ |
|
297 \n\ |
|
298 @noindent\n\ |
|
299 returns @var{x} = 0 and @var{ix} = 3.") |
2928
|
300 { |
3747
|
301 MINMAX_BODY (min); |
2928
|
302 } |
|
303 |
|
304 DEFUN_DLD (max, args, nargout, |
3443
|
305 "-*- texinfo -*-\n\ |
|
306 For a vector argument, return the maximum value. For a matrix\n\ |
|
307 argument, return the maximum value from each column, as a row\n\ |
|
308 vector. Thus,\n\ |
|
309 \n\ |
|
310 @example\n\ |
|
311 max (max (@var{x}))\n\ |
|
312 @end example\n\ |
|
313 \n\ |
|
314 @noindent\n\ |
|
315 returns the largest element of @var{x}.\n\ |
|
316 \n\ |
|
317 For complex arguments, the magnitude of the elements are used for\n\ |
3775
|
318 comparison.\n\ |
3657
|
319 \n\ |
|
320 If called with two output arguments, also returns the index of the\n\ |
|
321 maximum value(s). Thus,\n\ |
3775
|
322 @example\n\ |
3657
|
323 [x, ix] = max([1, 3, 5, 2, 5])\n\ |
|
324 @end example\n\ |
|
325 \n\ |
|
326 @noindent\n\ |
|
327 returns @var{x} = 5 and @var{ix} = 3.") |
2928
|
328 { |
3747
|
329 MINMAX_BODY (max); |
2928
|
330 } |
|
331 |
|
332 /* |
|
333 ;;; Local Variables: *** |
|
334 ;;; mode: C++ *** |
|
335 ;;; End: *** |
|
336 */ |