Mercurial > hg > octave-lyh
annotate src/DLD-FUNCTIONS/minmax.cc @ 7600:24abf5a702d9
Chop trailing singletons in min/max functions
author | David Bateman <dbateman@free.fr> |
---|---|
date | Tue, 18 Mar 2008 20:27:50 -0400 |
parents | f5005d9510f4 |
children |
rev | line source |
---|---|
2928 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
4 2005, 2006, 2007 John W. Eaton | |
2928 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2928 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2928 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
28 #include "lo-ieee.h" | |
3248 | 29 #include "lo-mappers.h" |
7231 | 30 #include "lo-math.h" |
4844 | 31 #include "dNDArray.h" |
32 #include "CNDArray.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 | |
4844 | 40 #include "ov-cx-mat.h" |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
41 #include "ov-re-sparse.h" |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
42 #include "ov-cx-sparse.h" |
4844 | 43 |
7189 | 44 #define MINMAX_DOUBLE_BODY(FCN) \ |
45 { \ | |
6711 | 46 bool single_arg = (nargin == 1) || (arg2.is_empty() && nargin == 3); \ |
4844 | 47 \ |
48 if (single_arg && (nargout == 1 || nargout == 0)) \ | |
3747 | 49 { \ |
50 if (arg1.is_real_type ()) \ | |
51 { \ | |
4844 | 52 NDArray m = arg1.array_value (); \ |
3747 | 53 \ |
54 if (! error_state) \ | |
55 { \ | |
4844 | 56 NDArray n = m. FCN (dim); \ |
57 retval(0) = n; \ | |
3747 | 58 } \ |
59 } \ | |
60 else if (arg1.is_complex_type ()) \ | |
61 { \ | |
4844 | 62 ComplexNDArray m = arg1.complex_array_value (); \ |
3747 | 63 \ |
64 if (! error_state) \ | |
65 { \ | |
4844 | 66 ComplexNDArray n = m. FCN (dim); \ |
67 retval(0) = n; \ | |
3747 | 68 } \ |
69 } \ | |
70 else \ | |
71 gripe_wrong_type_arg (#FCN, arg1); \ | |
72 } \ | |
4844 | 73 else if (single_arg && nargout == 2) \ |
3747 | 74 { \ |
5275 | 75 ArrayN<octave_idx_type> index; \ |
3747 | 76 \ |
77 if (arg1.is_real_type ()) \ | |
78 { \ | |
4844 | 79 NDArray m = arg1.array_value (); \ |
3747 | 80 \ |
81 if (! error_state) \ | |
82 { \ | |
4844 | 83 NDArray n = m. FCN (index, dim); \ |
84 retval(0) = n; \ | |
3747 | 85 } \ |
86 } \ | |
87 else if (arg1.is_complex_type ()) \ | |
88 { \ | |
4844 | 89 ComplexNDArray m = arg1.complex_array_value (); \ |
3747 | 90 \ |
91 if (! error_state) \ | |
92 { \ | |
4844 | 93 ComplexNDArray n = m. FCN (index, dim); \ |
94 retval(0) = n; \ | |
3747 | 95 } \ |
96 } \ | |
97 else \ | |
98 gripe_wrong_type_arg (#FCN, arg1); \ | |
99 \ | |
5275 | 100 octave_idx_type len = index.numel (); \ |
3747 | 101 \ |
102 if (len > 0) \ | |
103 { \ | |
4102 | 104 double nan_val = lo_ieee_nan_value (); \ |
105 \ | |
4844 | 106 NDArray idx (index.dims ()); \ |
3747 | 107 \ |
5275 | 108 for (octave_idx_type i = 0; i < len; i++) \ |
3747 | 109 { \ |
4153 | 110 OCTAVE_QUIT; \ |
7600
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
111 octave_idx_type tmp = index.elem (i) + 1; \ |
3747 | 112 idx.elem (i) = (tmp <= 0) \ |
4102 | 113 ? nan_val : static_cast<double> (tmp); \ |
3747 | 114 } \ |
115 \ | |
116 retval(1) = idx; \ | |
117 } \ | |
118 else \ | |
4844 | 119 retval(1) = NDArray (); \ |
3747 | 120 } \ |
4844 | 121 else \ |
3747 | 122 { \ |
123 int arg1_is_scalar = arg1.is_scalar_type (); \ | |
124 int arg2_is_scalar = arg2.is_scalar_type (); \ | |
125 \ | |
126 int arg1_is_complex = arg1.is_complex_type (); \ | |
127 int arg2_is_complex = arg2.is_complex_type (); \ | |
128 \ | |
129 if (arg1_is_scalar) \ | |
130 { \ | |
131 if (arg1_is_complex || arg2_is_complex) \ | |
132 { \ | |
133 Complex c1 = arg1.complex_value (); \ | |
4844 | 134 ComplexNDArray m2 = arg2.complex_array_value (); \ |
3747 | 135 if (! error_state) \ |
136 { \ | |
4844 | 137 ComplexNDArray result = FCN (c1, m2); \ |
3747 | 138 if (! error_state) \ |
139 retval(0) = result; \ | |
140 } \ | |
141 } \ | |
142 else \ | |
143 { \ | |
144 double d1 = arg1.double_value (); \ | |
4844 | 145 NDArray m2 = arg2.array_value (); \ |
3747 | 146 \ |
147 if (! error_state) \ | |
148 { \ | |
4844 | 149 NDArray result = FCN (d1, m2); \ |
3747 | 150 if (! error_state) \ |
151 retval(0) = result; \ | |
152 } \ | |
153 } \ | |
154 } \ | |
155 else if (arg2_is_scalar) \ | |
156 { \ | |
157 if (arg1_is_complex || arg2_is_complex) \ | |
158 { \ | |
4844 | 159 ComplexNDArray m1 = arg1.complex_array_value (); \ |
3747 | 160 \ |
161 if (! error_state) \ | |
162 { \ | |
163 Complex c2 = arg2.complex_value (); \ | |
4844 | 164 ComplexNDArray result = FCN (m1, c2); \ |
3747 | 165 if (! error_state) \ |
166 retval(0) = result; \ | |
167 } \ | |
168 } \ | |
169 else \ | |
170 { \ | |
4844 | 171 NDArray m1 = arg1.array_value (); \ |
3747 | 172 \ |
173 if (! error_state) \ | |
174 { \ | |
175 double d2 = arg2.double_value (); \ | |
4844 | 176 NDArray result = FCN (m1, d2); \ |
3747 | 177 if (! error_state) \ |
178 retval(0) = result; \ | |
179 } \ | |
180 } \ | |
181 } \ | |
182 else \ | |
183 { \ | |
184 if (arg1_is_complex || arg2_is_complex) \ | |
185 { \ | |
4844 | 186 ComplexNDArray m1 = arg1.complex_array_value (); \ |
3747 | 187 \ |
188 if (! error_state) \ | |
189 { \ | |
4844 | 190 ComplexNDArray m2 = arg2.complex_array_value (); \ |
3747 | 191 \ |
192 if (! error_state) \ | |
193 { \ | |
4844 | 194 ComplexNDArray result = FCN (m1, m2); \ |
3747 | 195 if (! error_state) \ |
196 retval(0) = result; \ | |
197 } \ | |
198 } \ | |
199 } \ | |
200 else \ | |
201 { \ | |
4844 | 202 NDArray m1 = arg1.array_value (); \ |
3747 | 203 \ |
204 if (! error_state) \ | |
205 { \ | |
4844 | 206 NDArray m2 = arg2.array_value (); \ |
3747 | 207 \ |
208 if (! error_state) \ | |
209 { \ | |
4844 | 210 NDArray result = FCN (m1, m2); \ |
3747 | 211 if (! error_state) \ |
212 retval(0) = result; \ | |
213 } \ | |
214 } \ | |
215 } \ | |
216 } \ | |
217 } \ | |
7189 | 218 } |
219 | |
220 #define MINMAX_INT_BODY(FCN, TYP) \ | |
221 { \ | |
222 bool single_arg = (nargin == 1) || (arg2.is_empty() && nargin == 3); \ | |
3747 | 223 \ |
7189 | 224 if (single_arg && (nargout == 1 || nargout == 0)) \ |
225 { \ | |
226 TYP ## NDArray m = arg1. TYP ## _array_value (); \ | |
227 \ | |
228 if (! error_state) \ | |
229 { \ | |
230 TYP ## NDArray n = m. FCN (dim); \ | |
231 retval(0) = n; \ | |
232 } \ | |
233 } \ | |
234 else if (single_arg && nargout == 2) \ | |
235 { \ | |
236 ArrayN<octave_idx_type> index; \ | |
237 \ | |
238 TYP ## NDArray m = arg1. TYP ## _array_value (); \ | |
239 \ | |
240 if (! error_state) \ | |
241 { \ | |
242 TYP ## NDArray n = m. FCN (index, dim); \ | |
243 retval(0) = n; \ | |
244 } \ | |
245 \ | |
246 octave_idx_type len = index.numel (); \ | |
247 \ | |
248 if (len > 0) \ | |
249 { \ | |
250 double nan_val = lo_ieee_nan_value (); \ | |
251 \ | |
252 NDArray idx (index.dims ()); \ | |
253 \ | |
254 for (octave_idx_type i = 0; i < len; i++) \ | |
255 { \ | |
256 OCTAVE_QUIT; \ | |
7600
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
257 octave_idx_type tmp = index.elem (i) + 1; \ |
7189 | 258 idx.elem (i) = (tmp <= 0) \ |
259 ? nan_val : static_cast<double> (tmp); \ | |
260 } \ | |
261 \ | |
262 retval(1) = idx; \ | |
263 } \ | |
264 else \ | |
265 retval(1) = NDArray (); \ | |
266 } \ | |
267 else \ | |
268 { \ | |
269 int arg1_is_scalar = arg1.is_scalar_type (); \ | |
270 int arg2_is_scalar = arg2.is_scalar_type (); \ | |
271 \ | |
272 if (arg1_is_scalar) \ | |
273 { \ | |
274 octave_ ## TYP d1 = arg1. TYP ## _scalar_value (); \ | |
275 TYP ## NDArray m2 = arg2. TYP ## _array_value (); \ | |
276 \ | |
277 if (! error_state) \ | |
278 { \ | |
279 TYP ## NDArray result = FCN (d1, m2); \ | |
280 if (! error_state) \ | |
281 retval(0) = result; \ | |
282 } \ | |
283 } \ | |
284 else if (arg2_is_scalar) \ | |
285 { \ | |
286 TYP ## NDArray m1 = arg1. TYP ## _array_value (); \ | |
287 \ | |
288 if (! error_state) \ | |
289 { \ | |
290 octave_ ## TYP d2 = arg2. TYP ## _scalar_value (); \ | |
291 TYP ## NDArray result = FCN (m1, d2); \ | |
292 if (! error_state) \ | |
293 retval(0) = result; \ | |
294 } \ | |
295 } \ | |
296 else \ | |
297 { \ | |
298 TYP ## NDArray m1 = arg1. TYP ## _array_value (); \ | |
299 \ | |
300 if (! error_state) \ | |
301 { \ | |
302 TYP ## NDArray m2 = arg2. TYP ## _array_value (); \ | |
303 \ | |
304 if (! error_state) \ | |
305 { \ | |
306 TYP ## NDArray result = FCN (m1, m2); \ | |
307 if (! error_state) \ | |
308 retval(0) = result; \ | |
309 } \ | |
310 } \ | |
311 } \ | |
312 } \ | |
313 } | |
314 | |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
315 #define MINMAX_SPARSE_BODY(FCN) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
316 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
317 bool single_arg = (nargin == 1) || arg2.is_empty(); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
318 \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
319 if (single_arg && (nargout == 1 || nargout == 0)) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
320 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
321 if (arg1.type_id () == octave_sparse_matrix::static_type_id ()) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
322 retval(0) = arg1.sparse_matrix_value () .FCN (dim); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
323 else if (arg1.type_id () == \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
324 octave_sparse_complex_matrix::static_type_id ()) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
325 retval(0) = arg1.sparse_complex_matrix_value () .FCN (dim); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
326 else \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
327 gripe_wrong_type_arg (#FCN, arg1); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
328 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
329 else if (single_arg && nargout == 2) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
330 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
331 Array2<octave_idx_type> index; \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
332 \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
333 if (arg1.type_id () == octave_sparse_matrix::static_type_id ()) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
334 retval(0) = arg1.sparse_matrix_value () .FCN (index, dim); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
335 else if (arg1.type_id () == \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
336 octave_sparse_complex_matrix::static_type_id ()) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
337 retval(0) = arg1.sparse_complex_matrix_value () .FCN (index, dim); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
338 else \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
339 gripe_wrong_type_arg (#FCN, arg1); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
340 \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
341 octave_idx_type len = index.numel (); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
342 \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
343 if (len > 0) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
344 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
345 double nan_val = lo_ieee_nan_value (); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
346 \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
347 NDArray idx (index.dims ()); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
348 \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
349 for (octave_idx_type i = 0; i < len; i++) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
350 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
351 OCTAVE_QUIT; \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
352 octave_idx_type tmp = index.elem (i) + 1; \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
353 idx.elem (i) = (tmp <= 0) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
354 ? nan_val : static_cast<double> (tmp); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
355 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
356 \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
357 retval(1) = idx; \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
358 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
359 else \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
360 retval(1) = NDArray (); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
361 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
362 else \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
363 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
364 int arg1_is_scalar = arg1.is_scalar_type (); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
365 int arg2_is_scalar = arg2.is_scalar_type (); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
366 \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
367 int arg1_is_complex = arg1.is_complex_type (); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
368 int arg2_is_complex = arg2.is_complex_type (); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
369 \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
370 if (arg1_is_scalar) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
371 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
372 if (arg1_is_complex || arg2_is_complex) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
373 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
374 Complex c1 = arg1.complex_value (); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
375 \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
376 SparseComplexMatrix m2 = arg2.sparse_complex_matrix_value (); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
377 \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
378 if (! error_state) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
379 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
380 SparseComplexMatrix result = FCN (c1, m2); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
381 if (! error_state) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
382 retval(0) = result; \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
383 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
384 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
385 else \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
386 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
387 double d1 = arg1.double_value (); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
388 SparseMatrix m2 = arg2.sparse_matrix_value (); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
389 \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
390 if (! error_state) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
391 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
392 SparseMatrix result = FCN (d1, m2); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
393 if (! error_state) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
394 retval(0) = result; \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
395 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
396 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
397 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
398 else if (arg2_is_scalar) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
399 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
400 if (arg1_is_complex || arg2_is_complex) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
401 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
402 SparseComplexMatrix m1 = arg1.sparse_complex_matrix_value (); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
403 \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
404 if (! error_state) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
405 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
406 Complex c2 = arg2.complex_value (); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
407 SparseComplexMatrix result = FCN (m1, c2); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
408 if (! error_state) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
409 retval(0) = result; \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
410 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
411 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
412 else \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
413 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
414 SparseMatrix m1 = arg1.sparse_matrix_value (); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
415 \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
416 if (! error_state) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
417 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
418 double d2 = arg2.double_value (); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
419 SparseMatrix result = FCN (m1, d2); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
420 if (! error_state) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
421 retval(0) = result; \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
422 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
423 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
424 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
425 else \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
426 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
427 if (arg1_is_complex || arg2_is_complex) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
428 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
429 SparseComplexMatrix m1 = arg1.sparse_complex_matrix_value (); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
430 \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
431 if (! error_state) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
432 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
433 SparseComplexMatrix m2 = arg2.sparse_complex_matrix_value (); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
434 \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
435 if (! error_state) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
436 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
437 SparseComplexMatrix result = FCN (m1, m2); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
438 if (! error_state) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
439 retval(0) = result; \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
440 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
441 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
442 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
443 else \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
444 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
445 SparseMatrix m1 = arg1.sparse_matrix_value (); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
446 \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
447 if (! error_state) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
448 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
449 SparseMatrix m2 = arg2.sparse_matrix_value (); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
450 \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
451 if (! error_state) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
452 { \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
453 SparseMatrix result = FCN (m1, m2); \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
454 if (! error_state) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
455 retval(0) = result; \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
456 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
457 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
458 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
459 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
460 } \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
461 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
462 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
463 |
7189 | 464 #define MINMAX_BODY(FCN) \ |
465 \ | |
466 octave_value_list retval; \ | |
467 \ | |
468 int nargin = args.length (); \ | |
469 \ | |
470 if (nargin < 1 || nargin > 3 || nargout > 2) \ | |
471 { \ | |
472 print_usage (); \ | |
473 return retval; \ | |
474 } \ | |
475 \ | |
476 octave_value arg1; \ | |
477 octave_value arg2; \ | |
478 octave_value arg3; \ | |
479 \ | |
480 switch (nargin) \ | |
481 { \ | |
482 case 3: \ | |
483 arg3 = args(2); \ | |
484 \ | |
485 case 2: \ | |
486 arg2 = args(1); \ | |
487 \ | |
488 case 1: \ | |
489 arg1 = args(0); \ | |
490 break; \ | |
491 \ | |
492 default: \ | |
493 panic_impossible (); \ | |
494 break; \ | |
495 } \ | |
496 \ | |
497 int dim; \ | |
498 dim_vector dv = arg1.dims (); \ | |
499 if (error_state) \ | |
500 { \ | |
501 gripe_wrong_type_arg (#FCN, arg1); \ | |
502 return retval; \ | |
503 } \ | |
504 \ | |
505 if (nargin == 3) \ | |
506 { \ | |
507 dim = arg3.nint_value () - 1; \ | |
508 if (dim < 0 || dim >= dv.length ()) \ | |
509 { \ | |
510 error ("%s: invalid dimension", #FCN); \ | |
511 return retval; \ | |
512 } \ | |
513 } \ | |
514 else \ | |
515 { \ | |
516 dim = 0; \ | |
517 while ((dim < dv.length ()) && (dv (dim) <= 1)) \ | |
518 dim++; \ | |
519 if (dim == dv.length ()) \ | |
520 dim = 0; \ | |
521 } \ | |
522 \ | |
523 if (arg1.is_integer_type ()) \ | |
524 { \ | |
525 if (arg1.is_uint8_type ()) \ | |
526 MINMAX_INT_BODY (FCN, uint8) \ | |
527 else if (arg1.is_uint16_type ()) \ | |
528 MINMAX_INT_BODY (FCN, uint16) \ | |
529 else if (arg1.is_uint32_type ()) \ | |
530 MINMAX_INT_BODY (FCN, uint32) \ | |
531 else if (arg1.is_uint64_type ()) \ | |
532 MINMAX_INT_BODY (FCN, uint64) \ | |
533 else if (arg1.is_int8_type ()) \ | |
534 MINMAX_INT_BODY (FCN, int8) \ | |
535 else if (arg1.is_int16_type ()) \ | |
536 MINMAX_INT_BODY (FCN, int16) \ | |
537 else if (arg1.is_int32_type ()) \ | |
538 MINMAX_INT_BODY (FCN, int32) \ | |
539 else if (arg1.is_int64_type ()) \ | |
540 MINMAX_INT_BODY (FCN, int64) \ | |
541 } \ | |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
542 else if (arg1.is_sparse_type ()) \ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
543 MINMAX_SPARSE_BODY (FCN) \ |
7189 | 544 else \ |
545 MINMAX_DOUBLE_BODY (FCN) \ | |
546 \ | |
547 return retval; | |
3747 | 548 |
2928 | 549 DEFUN_DLD (min, args, nargout, |
3443 | 550 "-*- texinfo -*-\n\ |
4844 | 551 @deftypefn {Mapping Function} {} min (@var{x}, @var{y}, @var{dim})\n\ |
4522 | 552 @deftypefnx {Mapping Function} {[@var{w}, @var{iw}] =} min (@var{x})\n\ |
553 @cindex Utility Functions\n\ | |
3443 | 554 For a vector argument, return the minimum value. For a matrix\n\ |
555 argument, return the minimum value from each column, as a row\n\ | |
4844 | 556 vector, or over the dimension @var{dim} if defined. For two matrices\n\ |
557 (or a matrix and scalar), return the pair-wise minimum.\n\ | |
4522 | 558 Thus,\n\ |
3443 | 559 \n\ |
560 @example\n\ | |
561 min (min (@var{x}))\n\ | |
562 @end example\n\ | |
563 \n\ | |
564 @noindent\n\ | |
4522 | 565 returns the smallest element of @var{x}, and\n\ |
566 \n\ | |
567 @example\n\ | |
568 @group\n\ | |
569 min (2:5, pi)\n\ | |
570 @result{} 2.0000 3.0000 3.1416 3.1416\n\ | |
571 @end group\n\ | |
572 @end example\n\ | |
573 @noindent\n\ | |
574 compares each element of the range @code{2:5} with @code{pi}, and\n\ | |
575 returns a row vector of the minimum values.\n\ | |
3443 | 576 \n\ |
577 For complex arguments, the magnitude of the elements are used for\n\ | |
3657 | 578 comparison.\n\ |
579 \n\ | |
4522 | 580 If called with one input and two output arguments,\n\ |
581 @code{min} also returns the first index of the\n\ | |
3657 | 582 minimum value(s). Thus,\n\ |
4522 | 583 \n\ |
3775 | 584 @example\n\ |
4522 | 585 @group\n\ |
3657 | 586 [x, ix] = min ([1, 3, 0, 2, 5])\n\ |
4522 | 587 @result{} x = 0\n\ |
588 ix = 3\n\ | |
589 @end group\n\ | |
3657 | 590 @end example\n\ |
4522 | 591 @end deftypefn") |
2928 | 592 { |
3747 | 593 MINMAX_BODY (min); |
2928 | 594 } |
595 | |
7600
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
596 /* |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
597 |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
598 %% test/octave.test/arith/min-1.m |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
599 %!assert (min ([1, 4, 2, 3]) == 1); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
600 %!assert (min ([1; -10; 5; -2]) == -10); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
601 |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
602 %% test/octave.test/arith/min-2.m |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
603 %!assert(all (min ([4, i; -2, 2]) == [-2, i])); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
604 |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
605 %% test/octave.test/arith/min-3.m |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
606 %!error <Invalid call to min.*> min (); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
607 |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
608 %% test/octave.test/arith/min-4.m |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
609 %!error <Invalid call to min.*> min (1, 2, 3, 4); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
610 |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
611 %!test |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
612 %! x = reshape (1:8,[2,2,2]); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
613 %! assert (max (x,[],1), reshape ([2, 4, 6, 8], [1,2,2])); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
614 %! assert (max (x,[],2), reshape ([3, 4, 7, 8], [2,1,2])); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
615 %! [y, i ] = max (x, [], 3); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
616 %! assert (y, [5, 7; 6, 8]); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
617 %! assert (ndims(y), 2); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
618 %! assert (i, [2, 2; 2, 2]); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
619 %! assert (ndims(i), 2); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
620 |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
621 */ |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
622 |
2928 | 623 DEFUN_DLD (max, args, nargout, |
3443 | 624 "-*- texinfo -*-\n\ |
4844 | 625 @deftypefn {Mapping Function} {} max (@var{x}, @var{y}, @var{dim})\n\ |
4522 | 626 @deftypefnx {Mapping Function} {[@var{w}, @var{iw}] =} max (@var{x})\n\ |
627 @cindex Utility Functions\n\ | |
3443 | 628 For a vector argument, return the maximum value. For a matrix\n\ |
629 argument, return the maximum value from each column, as a row\n\ | |
4844 | 630 vector, or over the dimension @var{dim} if defined. For two matrices\n\ |
631 (or a matrix and scalar), return the pair-wise maximum.\n\ | |
4522 | 632 Thus,\n\ |
3443 | 633 \n\ |
634 @example\n\ | |
635 max (max (@var{x}))\n\ | |
636 @end example\n\ | |
637 \n\ | |
638 @noindent\n\ | |
4522 | 639 returns the largest element of @var{x}, and\n\ |
640 \n\ | |
641 @example\n\ | |
642 @group\n\ | |
643 max (2:5, pi)\n\ | |
644 @result{} 3.1416 3.1416 4.0000 5.0000\n\ | |
645 @end group\n\ | |
646 @end example\n\ | |
647 @noindent\n\ | |
648 compares each element of the range @code{2:5} with @code{pi}, and\n\ | |
649 returns a row vector of the maximum values.\n\ | |
3443 | 650 \n\ |
651 For complex arguments, the magnitude of the elements are used for\n\ | |
3775 | 652 comparison.\n\ |
3657 | 653 \n\ |
4522 | 654 If called with one input and two output arguments,\n\ |
655 @code{max} also returns the first index of the\n\ | |
3657 | 656 maximum value(s). Thus,\n\ |
4522 | 657 \n\ |
3775 | 658 @example\n\ |
4522 | 659 @group\n\ |
660 [x, ix] = max ([1, 3, 5, 2, 5])\n\ | |
661 @result{} x = 5\n\ | |
662 ix = 3\n\ | |
663 @end group\n\ | |
3657 | 664 @end example\n\ |
4522 | 665 @end deftypefn") |
2928 | 666 { |
3747 | 667 MINMAX_BODY (max); |
2928 | 668 } |
669 | |
7600
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
670 /* |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
671 |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
672 %% test/octave.test/arith/max-1.m |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
673 %!assert (max ([1, 4, 2, 3]) == 4); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
674 %!assert (max ([1; -10; 5; -2]) == 5); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
675 |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
676 %% test/octave.test/arith/max-2.m |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
677 %!assert(all (max ([4, i 4.999; -2, 2, 3+4i]) == [4, 2, 3+4i])); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
678 |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
679 %% test/octave.test/arith/max-3.m |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
680 %!error <Invalid call to max.*> max (); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
681 |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
682 %% test/octave.test/arith/max-4.m |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
683 %!error <Invalid call to max.*> max (1, 2, 3, 4); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
684 |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
685 %!test |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
686 %! x = reshape (1:8,[2,2,2]); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
687 %! assert (min (x,[],1), reshape ([1, 3, 5, 7], [1,2,2])); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
688 %! assert (min (x,[],2), reshape ([1, 2, 5, 6], [2,1,2])); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
689 %! [y, i ] = min (x, [], 3); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
690 %! assert (y, [1, 3; 2, 4]); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
691 %! assert (ndims(y), 2); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
692 %! assert (i, [1, 1; 1, 1]); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
693 %! assert (ndims(i), 2); |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
694 |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
695 |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
696 */ |
24abf5a702d9
Chop trailing singletons in min/max functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
697 |
2928 | 698 /* |
699 ;;; Local Variables: *** | |
700 ;;; mode: C++ *** | |
701 ;;; End: *** | |
702 */ |