Mercurial > octave
annotate src/DLD-FUNCTIONS/cellfun.cc @ 9459:cb0b21f34abc
avoid double error messages in cellfun
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 24 Jul 2009 11:41:41 +0200 |
parents | cf714e75c656 |
children | 40de4692c860 |
rev | line source |
---|---|
5205 | 1 /* |
2 | |
8920 | 3 Copyright (C) 2005, 2006, 2007, 2008, 2009 Mohamed Kamoun |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
4 Copyright (C) 2009 Jaroslav Hajek |
5205 | 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. | |
5205 | 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/>. | |
5205 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
28 #include <string> | |
5988 | 29 #include <vector> |
30 #include <list> | |
8994 | 31 #include <memory> |
5205 | 32 |
33 #include "lo-mappers.h" | |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8373
diff
changeset
|
34 #include "oct-locbuf.h" |
5205 | 35 |
36 #include "Cell.h" | |
5988 | 37 #include "oct-map.h" |
5205 | 38 #include "defun-dld.h" |
39 #include "parse.h" | |
40 #include "variables.h" | |
5451 | 41 #include "ov-colon.h" |
5988 | 42 #include "unwind-prot.h" |
5205 | 43 |
8994 | 44 // Rationale: |
45 // The octave_base_value::subsasgn method carries too much overhead for | |
46 // per-element assignment strategy. | |
47 // This class will optimize the most optimistic and most likely case | |
48 // when the output really is scalar by defining a hierarchy of virtual | |
49 // collectors specialized for some scalar types. | |
50 | |
51 class scalar_col_helper | |
52 { | |
53 public: | |
54 virtual bool collect (octave_idx_type i, const octave_value& val) = 0; | |
55 virtual octave_value result (void) = 0; | |
56 virtual ~scalar_col_helper (void) { } | |
57 }; | |
58 | |
59 // The default collector represents what was previously done in the main loop. | |
60 // This reuses the existing assignment machinery via octave_value::subsasgn, | |
61 // which can perform all sorts of conversions, but is relatively slow. | |
62 | |
63 class scalar_col_helper_def : public scalar_col_helper | |
64 { | |
65 std::list<octave_value_list> idx_list; | |
66 octave_value resval; | |
67 public: | |
68 scalar_col_helper_def (const octave_value& val, const dim_vector& dims) | |
69 : idx_list (1), resval (val) | |
70 { | |
71 idx_list.front ().resize (1); | |
72 if (resval.dims () != dims) | |
73 resval.resize (dims); | |
74 } | |
75 ~scalar_col_helper_def (void) { } | |
76 | |
77 bool collect (octave_idx_type i, const octave_value& val) | |
78 { | |
79 if (val.numel () == 1) | |
80 { | |
81 idx_list.front ()(0) = static_cast<double> (i + 1); | |
82 resval = resval.subsasgn ("(", idx_list, val); | |
83 } | |
84 else | |
85 error ("cellfun: expecting all values to be scalars for UniformOutput = true"); | |
86 | |
87 return true; | |
88 } | |
89 octave_value result (void) | |
90 { | |
91 return resval; | |
92 } | |
93 }; | |
94 | |
95 template <class T> | |
96 struct scalar_query_helper { }; | |
97 | |
98 #define DEF_QUERY_HELPER(T, TEST, QUERY) \ | |
99 template <> \ | |
100 struct scalar_query_helper<T> \ | |
101 { \ | |
102 static bool has_value (const octave_value& val) \ | |
103 { return TEST; } \ | |
104 static T get_value (const octave_value& val) \ | |
105 { return QUERY; } \ | |
106 } | |
107 | |
108 DEF_QUERY_HELPER (double, val.is_real_scalar (), val.scalar_value ()); | |
109 DEF_QUERY_HELPER (Complex, val.is_complex_scalar (), val.complex_value ()); | |
110 DEF_QUERY_HELPER (float, val.is_single_type () && val.is_real_scalar (), | |
111 val.float_scalar_value ()); | |
112 DEF_QUERY_HELPER (FloatComplex, val.is_single_type () && val.is_complex_scalar (), | |
113 val.float_complex_value ()); | |
114 DEF_QUERY_HELPER (bool, val.is_bool_scalar (), val.bool_value ()); | |
115 // FIXME: More? | |
116 | |
117 // This specializes for collecting elements of a single type, by accessing | |
118 // an array directly. If the scalar is not valid, it returns false. | |
119 | |
120 template <class NDA> | |
121 class scalar_col_helper_nda : public scalar_col_helper | |
122 { | |
123 NDA arrayval; | |
124 typedef typename NDA::element_type T; | |
125 public: | |
126 scalar_col_helper_nda (const octave_value& val, const dim_vector& dims) | |
127 : arrayval (dims) | |
128 { | |
129 arrayval(0) = scalar_query_helper<T>::get_value (val); | |
130 } | |
131 ~scalar_col_helper_nda (void) { } | |
132 | |
133 bool collect (octave_idx_type i, const octave_value& val) | |
134 { | |
135 bool retval = scalar_query_helper<T>::has_value (val); | |
136 if (retval) | |
137 arrayval(i) = scalar_query_helper<T>::get_value (val); | |
138 return retval; | |
139 } | |
140 octave_value result (void) | |
141 { | |
142 return arrayval; | |
143 } | |
144 }; | |
145 | |
146 template class scalar_col_helper_nda<NDArray>; | |
147 template class scalar_col_helper_nda<FloatNDArray>; | |
148 template class scalar_col_helper_nda<ComplexNDArray>; | |
149 template class scalar_col_helper_nda<FloatComplexNDArray>; | |
150 template class scalar_col_helper_nda<boolNDArray>; | |
151 | |
152 // the virtual constructor. | |
153 scalar_col_helper * | |
154 make_col_helper (const octave_value& val, const dim_vector& dims) | |
155 { | |
156 scalar_col_helper *retval; | |
157 | |
158 if (val.is_bool_scalar ()) | |
159 retval = new scalar_col_helper_nda<boolNDArray> (val, dims); | |
160 else if (val.is_complex_scalar ()) | |
161 { | |
162 if (val.is_single_type ()) | |
163 retval = new scalar_col_helper_nda<FloatComplexNDArray> (val, dims); | |
164 else | |
165 retval = new scalar_col_helper_nda<ComplexNDArray> (val, dims); | |
166 } | |
167 else if (val.is_real_scalar ()) | |
168 { | |
169 if (val.is_single_type ()) | |
170 retval = new scalar_col_helper_nda<FloatNDArray> (val, dims); | |
171 else | |
172 retval = new scalar_col_helper_nda<NDArray> (val, dims); | |
173 } | |
174 else | |
175 retval = new scalar_col_helper_def (val, dims); | |
176 | |
177 return retval; | |
178 } | |
179 | |
5988 | 180 DEFUN_DLD (cellfun, args, nargout, |
8715 | 181 "-*- texinfo -*-\n\ |
6854 | 182 @deftypefn {Loadable Function} {} cellfun (@var{name}, @var{c})\n\ |
183 @deftypefnx {Loadable Function} {} cellfun (\"size\", @var{c}, @var{k})\n\ | |
184 @deftypefnx {Loadable Function} {} cellfun (\"isclass\", @var{c}, @var{class})\n\ | |
185 @deftypefnx {Loadable Function} {} cellfun (@var{func}, @var{c})\n\ | |
186 @deftypefnx {Loadable Function} {} cellfun (@var{func}, @var{c}, @var{d})\n\ | |
187 @deftypefnx {Loadable Function} {[@var{a}, @var{b}] =} cellfun (@dots{})\n\ | |
188 @deftypefnx {Loadable Function} {} cellfun (@dots{}, 'ErrorHandler', @var{errfunc})\n\ | |
189 @deftypefnx {Loadable Function} {} cellfun (@dots{}, 'UniformOutput', @var{val})\n\ | |
5205 | 190 \n\ |
191 Evaluate the function named @var{name} on the elements of the cell array\n\ | |
5543 | 192 @var{c}. Elements in @var{c} are passed on to the named function\n\ |
5205 | 193 individually. The function @var{name} can be one of the functions\n\ |
194 \n\ | |
195 @table @code\n\ | |
196 @item isempty\n\ | |
5543 | 197 Return 1 for empty elements.\n\ |
5205 | 198 @item islogical\n\ |
199 Return 1 for logical elements.\n\ | |
200 @item isreal\n\ | |
201 Return 1 for real elements.\n\ | |
202 @item length\n\ | |
203 Return a vector of the lengths of cell elements.\n\ | |
5543 | 204 @item ndims\n\ |
5205 | 205 Return the number of dimensions of each element.\n\ |
206 @item prodofsize\n\ | |
207 Return the product of dimensions of each element.\n\ | |
208 @item size\n\ | |
209 Return the size along the @var{k}-th dimension.\n\ | |
210 @item isclass\n\ | |
211 Return 1 for elements of @var{class}.\n\ | |
212 @end table\n\ | |
213 \n\ | |
214 Additionally, @code{cellfun} accepts an arbitrary function @var{func}\n\ | |
215 in the form of an inline function, function handle, or the name of a\n\ | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
216 function (in a character string). In the case of a character string\n\ |
5988 | 217 argument, the function must accept a single argument named @var{x}, and\n\ |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
218 it must return a string value. The function can take one or more arguments,\n\ |
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
219 with the inputs args given by @var{c}, @var{d}, etc. Equally the function\n\ |
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
220 can return one or more output arguments. For example\n\ |
5205 | 221 \n\ |
222 @example\n\ | |
223 @group\n\ | |
5988 | 224 cellfun (@@atan2, @{1, 0@}, @{0, 1@})\n\ |
225 @result{}ans = [1.57080 0.00000]\n\ | |
226 @end group\n\ | |
227 @end example\n\ | |
228 \n\ | |
229 Note that the default output argument is an array of the same size as the\n\ | |
230 input arguments.\n\ | |
231 \n\ | |
8828 | 232 If the parameter 'UniformOutput' is set to true (the default), then the function\n\ |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
8286
diff
changeset
|
233 must return a single element which will be concatenated into the\n\ |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
234 return value. If 'UniformOutput' is false, the outputs are concatenated in\n\ |
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
235 a cell array. For example\n\ |
5988 | 236 \n\ |
237 @example\n\ | |
238 @group\n\ | |
6623 | 239 cellfun (\"tolower(x)\", @{\"Foo\", \"Bar\", \"FooBar\"@},\n\ |
240 \"UniformOutput\",false)\n\ | |
5205 | 241 @result{} ans = @{\"foo\", \"bar\", \"foobar\"@}\n\ |
242 @end group\n\ | |
243 @end example\n\ | |
5988 | 244 \n\ |
245 Given the parameter 'ErrorHandler', then @var{errfunc} defines a function to\n\ | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
246 call in case @var{func} generates an error. The form of the function is\n\ |
5988 | 247 \n\ |
248 @example\n\ | |
249 function [@dots{}] = errfunc (@var{s}, @dots{})\n\ | |
250 @end example\n\ | |
251 \n\ | |
252 where there is an additional input argument to @var{errfunc} relative to\n\ | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
253 @var{func}, given by @var{s}. This is a structure with the elements\n\ |
5988 | 254 'identifier', 'message' and 'index', giving respectively the error\n\ |
255 identifier, the error message, and the index into the input arguments\n\ | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
256 of the element that caused the error. For example\n\ |
5988 | 257 \n\ |
258 @example\n\ | |
259 @group\n\ | |
260 function y = foo (s, x), y = NaN; endfunction\n\ | |
261 cellfun (@@factorial, @{-1,2@},'ErrorHandler',@@foo)\n\ | |
262 @result{} ans = [NaN 2]\n\ | |
263 @end group\n\ | |
264 @end example\n\ | |
265 \n\ | |
8286
6f2d95255911
fix @seealso references to point to existing anchors
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8096
diff
changeset
|
266 @seealso{isempty, islogical, isreal, length, ndims, numel, size}\n\ |
5642 | 267 @end deftypefn") |
5205 | 268 { |
5988 | 269 octave_value_list retval; |
5205 | 270 int nargin = args.length (); |
5988 | 271 nargout = (nargout < 1 ? 1 : nargout); |
5205 | 272 |
273 if (nargin < 2) | |
274 { | |
275 error ("cellfun: you must supply at least 2 arguments"); | |
5823 | 276 print_usage (); |
5205 | 277 return retval; |
278 } | |
279 | |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
280 octave_value func = args(0); |
5205 | 281 |
282 if (! args(1).is_cell ()) | |
283 { | |
5543 | 284 error ("cellfun: second argument must be a cell array"); |
5205 | 285 |
286 return retval; | |
287 } | |
288 | |
8994 | 289 const Cell f_args = args(1).cell_value (); |
5205 | 290 |
5988 | 291 octave_idx_type k = f_args.numel (); |
5205 | 292 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
293 if (func.is_string ()) |
5205 | 294 { |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
295 std::string name = func.string_value (); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
296 if (name.find_first_of ("(x)") != std::string::npos) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
297 warning ("cellfun: passing function body as string is no longer supported." |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
298 " Use @ or `inline'."); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
299 |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
300 if (name == "isempty") |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
301 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
302 boolNDArray result (f_args.dims ()); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
303 for (octave_idx_type count = 0; count < k ; count++) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
304 result(count) = f_args.elem(count).is_empty (); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
305 retval(0) = result; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
306 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
307 else if (name == "islogical") |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
308 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
309 boolNDArray result (f_args.dims ()); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
310 for (octave_idx_type count= 0; count < k ; count++) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
311 result(count) = f_args.elem(count).is_bool_type (); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
312 retval(0) = result; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
313 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
314 else if (name == "isreal") |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
315 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
316 boolNDArray result (f_args.dims ()); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
317 for (octave_idx_type count= 0; count < k ; count++) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
318 result(count) = f_args.elem(count).is_real_type (); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
319 retval(0) = result; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
320 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
321 else if (name == "length") |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
322 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
323 NDArray result (f_args.dims ()); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
324 for (octave_idx_type count= 0; count < k ; count++) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
325 result(count) = static_cast<double> (f_args.elem(count).length ()); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
326 retval(0) = result; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
327 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
328 else if (name == "ndims") |
5205 | 329 { |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
330 NDArray result (f_args.dims ()); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
331 for (octave_idx_type count = 0; count < k ; count++) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
332 result(count) = static_cast<double> (f_args.elem(count).ndims ()); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
333 retval(0) = result; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
334 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
335 else if (name == "prodofsize" || name == "numel") |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
336 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
337 NDArray result (f_args.dims ()); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
338 for (octave_idx_type count = 0; count < k ; count++) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
339 result(count) = static_cast<double> (f_args.elem(count).numel ()); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
340 retval(0) = result; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
341 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
342 else if (name == "size") |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
343 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
344 if (nargin == 3) |
5205 | 345 { |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
346 int d = args(2).nint_value () - 1; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
347 |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
348 if (d < 0) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
349 error ("cellfun: third argument must be a positive integer"); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
350 |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
351 if (! error_state) |
5205 | 352 { |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
353 NDArray result (f_args.dims ()); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
354 for (octave_idx_type count = 0; count < k ; count++) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
355 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
356 dim_vector dv = f_args.elem(count).dims (); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
357 if (d < dv.length ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
358 result(count) = static_cast<double> (dv(d)); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
359 else |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
360 result(count) = 1.0; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
361 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
362 retval(0) = result; |
5205 | 363 } |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
364 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
365 else |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
366 error ("not enough arguments for `size'"); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
367 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
368 else if (name == "isclass") |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
369 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
370 if (nargin == 3) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
371 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
372 std::string class_name = args(2).string_value(); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
373 boolNDArray result (f_args.dims ()); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
374 for (octave_idx_type count = 0; count < k ; count++) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
375 result(count) = (f_args.elem(count).class_name() == class_name); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
376 |
5988 | 377 retval(0) = result; |
5205 | 378 } |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
379 else |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
380 error ("not enough arguments for `isclass'"); |
5205 | 381 } |
382 else | |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
383 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
384 func = symbol_table::find_function (name); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
385 if (func.is_undefined ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
386 error ("cellfun: invalid function name: %s", name.c_str ()); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
387 } |
5205 | 388 } |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
389 |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
390 if (error_state || ! retval.empty ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
391 return retval; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
392 |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
393 if (func.is_function_handle () || func.is_inline_function () |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
394 || func.is_function ()) |
5205 | 395 { |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
396 unwind_protect::frame_id_t uwp_frame = unwind_protect::begin_frame (); |
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
397 unwind_protect::protect_var (buffer_error_messages); |
5988 | 398 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
399 octave_value_list inputlist; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
400 bool uniform_output = true; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
401 octave_value error_handler; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
402 int offset = 1; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
403 int i = 1; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
404 OCTAVE_LOCAL_BUFFER (Cell, inputs, nargin); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
405 // This is to prevent copy-on-write. |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
406 const Cell *cinputs = inputs; |
5205 | 407 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
408 while (i < nargin) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
409 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
410 if (args(i).is_string()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
411 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
412 std::string arg = args(i++).string_value(); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
413 if (i == nargin) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
414 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
415 error ("cellfun: parameter value is missing"); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
416 goto cellfun_err; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
417 } |
5205 | 418 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
419 std::transform (arg.begin (), arg.end (), |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
420 arg.begin (), tolower); |
5205 | 421 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
422 if (arg == "uniformoutput") |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
423 uniform_output = args(i++).bool_value(); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
424 else if (arg == "errorhandler") |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
425 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
426 if (args(i).is_function_handle () || |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
427 args(i).is_inline_function ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
428 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
429 error_handler = args(i++); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
430 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
431 else if (args(i).is_string ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
432 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
433 std::string err_name = args(i++).string_value (); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
434 error_handler = symbol_table::find_function (err_name); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
435 if (error_handler.is_undefined ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
436 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
437 error ("cellfun: invalid function name: %s", err_name.c_str ()); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
438 goto cellfun_err; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
439 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
440 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
441 else |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
442 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
443 error ("invalid errorhandler value"); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
444 goto cellfun_err; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
445 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
446 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
447 else |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
448 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
449 error ("cellfun: unrecognized parameter %s", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
450 arg.c_str()); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
451 goto cellfun_err; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
452 } |
5988 | 453 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
454 offset += 2; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
455 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
456 else |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
457 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
458 inputs[i-offset] = args(i).cell_value (); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
459 if (f_args.dims() != inputs[i-offset].dims()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
460 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
461 error ("cellfun: Dimension mismatch"); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
462 goto cellfun_err; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
463 |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
464 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
465 i++; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
466 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
467 } |
5988 | 468 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
469 nargin -= offset; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
470 inputlist.resize(nargin); |
5205 | 471 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
472 if (error_handler.is_defined ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
473 buffer_error_messages++; |
5988 | 474 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
475 if (uniform_output) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
476 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
477 OCTAVE_LOCAL_BUFFER (std::auto_ptr<scalar_col_helper>, retptr, nargout); |
5988 | 478 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
479 for (octave_idx_type count = 0; count < k ; count++) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
480 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
481 for (int j = 0; j < nargin; j++) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
482 inputlist(j) = cinputs[j](count); |
5988 | 483 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
484 octave_value_list tmp = func.do_multi_index_op (nargout, inputlist); |
5988 | 485 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
486 if (error_state && error_handler.is_defined ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
487 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
488 Octave_map msg; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
489 msg.assign ("identifier", last_error_id ()); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
490 msg.assign ("message", last_error_message ()); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
491 msg.assign ("index", octave_value(double (count + static_cast<octave_idx_type>(1)))); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
492 octave_value_list errlist = inputlist; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
493 errlist.prepend (msg); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
494 buffer_error_messages--; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
495 error_state = 0; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
496 tmp = error_handler.do_multi_index_op (nargout, errlist); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
497 buffer_error_messages++; |
5988 | 498 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
499 if (error_state) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
500 goto cellfun_err; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
501 } |
5988 | 502 |
9459
cb0b21f34abc
avoid double error messages in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
503 if (error_state) |
cb0b21f34abc
avoid double error messages in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
504 goto cellfun_err; |
cb0b21f34abc
avoid double error messages in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
505 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
506 if (tmp.length() < nargout) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
507 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
508 error ("cellfun: too many output arguments"); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
509 goto cellfun_err; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
510 } |
5988 | 511 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
512 if (count == 0) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
513 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
514 for (int j = 0; j < nargout; j++) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
515 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
516 octave_value val = tmp(j); |
5988 | 517 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
518 if (val.numel () == 1) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
519 retptr[j].reset (make_col_helper (val, f_args.dims ())); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
520 else |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
521 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
522 error ("cellfun: expecting all values to be scalars for UniformOutput = true"); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
523 break; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
524 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
525 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
526 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
527 else |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
528 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
529 for (int j = 0; j < nargout; j++) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
530 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
531 octave_value val = tmp(j); |
5989 | 532 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
533 if (! retptr[j]->collect (count, val)) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
534 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
535 // FIXME: A more elaborate structure would allow again a virtual |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
536 // constructor here. |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
537 retptr[j].reset (new scalar_col_helper_def (retptr[j]->result (), |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
538 f_args.dims ())); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
539 retptr[j]->collect (count, val); |
8994 | 540 } |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
541 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
542 } |
5988 | 543 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
544 if (error_state) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
545 break; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
546 } |
8994 | 547 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
548 retval.resize (nargout); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
549 for (int j = 0; j < nargout; j++) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
550 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
551 if (retptr[j].get ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
552 retval(j) = retptr[j]->result (); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
553 else |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
554 retval(j) = Matrix (); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
555 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
556 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
557 else |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
558 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
559 OCTAVE_LOCAL_BUFFER (Cell, results, nargout); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
560 for (int j = 0; j < nargout; j++) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
561 results[j].resize(f_args.dims()); |
5988 | 562 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
563 for (octave_idx_type count = 0; count < k ; count++) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
564 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
565 for (int j = 0; j < nargin; j++) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
566 inputlist(j) = cinputs[j](count); |
5988 | 567 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
568 octave_value_list tmp = func.do_multi_index_op (nargout, inputlist); |
5988 | 569 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
570 if (error_state && error_handler.is_defined ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
571 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
572 Octave_map msg; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
573 msg.assign ("identifier", last_error_id ()); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
574 msg.assign ("message", last_error_message ()); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
575 msg.assign ("index", octave_value(double (count + static_cast<octave_idx_type>(1)))); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
576 octave_value_list errlist = inputlist; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
577 errlist.prepend (msg); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
578 buffer_error_messages--; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
579 error_state = 0; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
580 tmp = error_handler.do_multi_index_op (nargout, errlist); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
581 buffer_error_messages++; |
5988 | 582 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
583 if (error_state) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
584 goto cellfun_err; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
585 } |
5988 | 586 |
9459
cb0b21f34abc
avoid double error messages in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
587 if (error_state) |
cb0b21f34abc
avoid double error messages in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
588 goto cellfun_err; |
cb0b21f34abc
avoid double error messages in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
589 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
590 if (tmp.length() < nargout) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
591 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
592 error ("cellfun: too many output arguments"); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
593 goto cellfun_err; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
594 } |
5988 | 595 |
596 | |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
597 for (int j = 0; j < nargout; j++) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
598 results[j](count) = tmp(j); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
599 } |
5988 | 600 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
601 retval.resize(nargout); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
602 for (int j = 0; j < nargout; j++) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
603 retval(j) = results[j]; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
604 } |
5205 | 605 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
606 cellfun_err: |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
607 if (error_state) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
608 retval = octave_value_list(); |
5988 | 609 |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9307
diff
changeset
|
610 unwind_protect::run_frame (uwp_frame); |
5205 | 611 } |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
612 else |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
613 error ("cellfun: first argument must be a string or function handle"); |
5205 | 614 |
615 return retval; | |
616 } | |
617 | |
5988 | 618 /* |
619 | |
8373
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
620 %% Test function to check the "Errorhandler" option |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
621 %!function [z] = cellfunerror (S, varargin) |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
622 %! z = S; |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
623 %! endfunction |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
624 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
625 %% First input argument can be a string, an inline function, |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
626 %% a function_handle or an anonymous function |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
627 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
628 %! A = cellfun ("islogical", {true, 0.1, false, i*2}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
629 %! assert (A, [true, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
630 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
631 %! A = cellfun (inline ("islogical (x)", "x"), {true, 0.1, false, i*2}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
632 %! assert (A, [true, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
633 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
634 %! A = cellfun (@islogical, {true, 0.1, false, i*2}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
635 %! assert (A, [true, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
636 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
637 %! A = cellfun (@(x) islogical(x), {true, 0.1, false, i*2}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
638 %! assert (A, [true, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
639 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
640 %% First input argument can be the special string "isreal", |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
641 %% "isempty", "islogical", "length", "ndims" or "prodofsize" |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
642 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
643 %! A = cellfun ("isreal", {true, 0.1, false, i*2, [], "abc"}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
644 %! assert (A, [true, true, true, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
645 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
646 %! A = cellfun ("isempty", {true, 0.1, false, i*2, [], "abc"}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
647 %! assert (A, [false, false, false, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
648 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
649 %! A = cellfun ("islogical", {true, 0.1, false, i*2, [], "abc"}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
650 %! assert (A, [true, false, true, false, false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
651 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
652 %! A = cellfun ("length", {true, 0.1, false, i*2, [], "abc"}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
653 %! assert (A, [1, 1, 1, 1, 0, 3]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
654 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
655 %! A = cellfun ("ndims", {[1, 2; 3, 4]; (cell (1,2,3,4))}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
656 %! assert (A, [2; 4]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
657 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
658 %! A = cellfun ("prodofsize", {[1, 2; 3, 4], (cell (1,2,3,4))}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
659 %! assert (A, [4, 24]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
660 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
661 %% Number of input and output arguments may not be limited to one |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
662 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
663 %! A = cellfun (@(x,y,z) x + y + z, {1, 1, 1}, {2, 2, 2}, {3, 4, 5}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
664 %! assert (A, [6, 7, 8]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
665 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
666 %! A = cellfun (@(x,y,z) x + y + z, {1, 1, 1}, {2, 2, 2}, {3, 4, 5}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
667 %! "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
668 %! assert (A, {6, 7, 8}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
669 %!test %% Two input arguments of different types |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
670 %! A = cellfun (@(x,y) islogical (x) && ischar (y), {false, true}, {"a", 3}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
671 %! assert (A, [true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
672 %!test %% Pass another variable to the anonymous function |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
673 %! y = true; A = cellfun (@(x) islogical (x) && y, {false, 0.3}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
674 %! assert (A, [true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
675 %!test %% Three ouptut arguments of different type |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
676 %! [A, B, C] = cellfun (@find, {10, 11; 0, 12}, "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
677 %! assert (isequal (A, {true, true; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
678 %! assert (isequal (B, {true, true; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
679 %! assert (isequal (C, {10, 11; [], 12})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
680 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
681 %% Input arguments can be of type cell array of logical |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
682 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
683 %! A = cellfun (@(x,y) x == y, {false, true}, {true, true}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
684 %! assert (A, [false, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
685 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
686 %! A = cellfun (@(x,y) x == y, {false; true}, {true; true}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
687 %! "UniformOutput", true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
688 %! assert (A, [false; true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
689 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
690 %! A = cellfun (@(x) x, {false, true; false, true}, "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
691 %! assert (A, {false, true; false, true}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
692 %!test %% Three ouptut arguments of same type |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
693 %! [A, B, C] = cellfun (@find, {true, false; false, true}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
694 %! "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
695 %! assert (isequal (A, {true, []; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
696 %! assert (isequal (B, {true, []; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
697 %! assert (isequal (C, {true, []; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
698 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
699 %! A = cellfun (@(x,y) cell2str (x,y), {true}, {true}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
700 %! "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
701 %! assert (isfield (A, "identifier"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
702 %! assert (isfield (A, "message"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
703 %! assert (isfield (A, "index"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
704 %! assert (isempty (A.message), false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
705 %! assert (A.index, 1); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
706 %!test %% Overwriting setting of "UniformOutput" true |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
707 %! A = cellfun (@(x,y) cell2str (x,y), {true}, {true}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
708 %! "UniformOutput", true, "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
709 %! assert (isfield (A, "identifier"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
710 %! assert (isfield (A, "message"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
711 %! assert (isfield (A, "index"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
712 %! assert (isempty (A.message), false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
713 %! assert (A.index, 1); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
714 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
715 %% Input arguments can be of type cell array of numeric |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
716 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
717 %! A = cellfun (@(x,y) x>y, {1.1, 4.2}, {3.1, 2+6*i}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
718 %! assert (A, [false, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
719 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
720 %! A = cellfun (@(x,y) x>y, {1.1, 4.2; 2, 4}, {3.1, 2; 2, 4+2*i}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
721 %! "UniformOutput", true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
722 %! assert (A, [false, true; false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
723 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
724 %! A = cellfun (@(x,y) x:y, {1.1, 4}, {3.1, 6}, "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
725 %! assert (isequal (A{1}, [1.1, 2.1, 3.1])); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
726 %! assert (isequal (A{2}, [4, 5, 6])); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
727 %!test %% Three ouptut arguments of different type |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
728 %! [A, B, C] = cellfun (@find, {10, 11; 0, 12}, "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
729 %! assert (isequal (A, {true, true; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
730 %! assert (isequal (B, {true, true; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
731 %! assert (isequal (C, {10, 11; [], 12})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
732 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
733 %! A = cellfun (@(x,y) cell2str(x,y), {1.1, 4}, {3.1, 6}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
734 %! "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
735 %! B = isfield (A(1), "message") && isfield (A(1), "index"); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
736 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
737 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
738 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
739 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
740 %! assert ([A(1).index, A(2).index], [1, 2]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
741 %!test %% Overwriting setting of "UniformOutput" true |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
742 %! A = cellfun (@(x,y) cell2str(x,y), {1.1, 4}, {3.1, 6}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
743 %! "UniformOutput", true, "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
744 %! B = isfield (A(1), "message") && isfield (A(1), "index"); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
745 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
746 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
747 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
748 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
749 %! assert ([A(1).index, A(2).index], [1, 2]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
750 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
751 %% Input arguments can be of type cell arrays of character or strings |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
752 %!error %% "UniformOutput" false should be used |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
753 %! A = cellfun (@(x,y) x>y, {"ad", "c", "ghi"}, {"cc", "d", "fgh"}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
754 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
755 %! A = cellfun (@(x,y) x>y, {"a"; "f"}, {"c"; "d"}, "UniformOutput", true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
756 %! assert (A, [false; true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
757 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
758 %! A = cellfun (@(x,y) x:y, {"a", "d"}, {"c", "f"}, "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
759 %! assert (A, {"abc", "def"}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
760 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
761 %! A = cellfun (@(x,y) cell2str(x,y), {"a", "d"}, {"c", "f"}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
762 %! "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
763 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
764 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
765 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
766 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
767 %! assert ([A(1).index, A(2).index], [1, 2]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
768 %!test %% Overwriting setting of "UniformOutput" true |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
769 %! A = cellfun (@(x,y) cell2str(x,y), {"a", "d"}, {"c", "f"}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
770 %! "UniformOutput", true, "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
771 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
772 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
773 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
774 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
775 %! assert ([A(1).index, A(2).index], [1, 2]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
776 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
777 %% Structures cannot be handled by cellfun |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
778 %!error |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
779 %! vst1.a = 1.1; vst1.b = 4.2; vst2.a = 3.1; vst2.b = 2; |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
780 %! A = cellfun (@(x,y) (x.a < y.a) && (x.b > y.b), vst1, vst2); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
781 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
782 %% Input arguments can be of type cell array of cell arrays |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
783 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
784 %! A = cellfun (@(x,y) x{1} < y{1}, {{1.1}, {4.2}}, {{3.1}, {2}}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
785 %! assert (A, [1, 0], 1e-16); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
786 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
787 %! A = cellfun (@(x,y) x{1} < y{1}, {{1.1}; {4.2}}, {{3.1}; {2}}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
788 %! "UniformOutput", true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
789 %! assert (A, [1; 0], 1e-16); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
790 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
791 %! A = cellfun (@(x,y) x{1} < y{1}, {{1.1}, {4.2}}, {{3.1}, {2}}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
792 %! "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
793 %! assert (A, {true, false}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
794 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
795 %! A = cellfun (@(x,y) mat2str(x,y), {{1.1}, {4.2}}, {{3.1}, {2}}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
796 %! "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
797 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
798 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
799 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
800 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
801 %! assert ([A(1).index, A(2).index], [1, 2]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
802 %!test %% Overwriting setting of "UniformOutput" true |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
803 %! A = cellfun (@(x,y) mat2str(x,y), {{1.1}, {4.2}}, {{3.1}, {2}}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
804 %! "UniformOutput", true, "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
805 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
806 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
807 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
808 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
809 %! assert ([A(1).index, A(2).index], [1, 2]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
810 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
811 %% Input arguments can be of type cell array of structure arrays |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
812 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
813 %! a = struct ("a", 1, "b", 2); b = struct ("a", 1, "b", 3); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
814 %! A = cellfun (@(x,y) (x.a == y.a) && (x.b < y.b), {a}, {b}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
815 %! assert (A, true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
816 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
817 %! a = struct ("a", 1, "b", 2); b = struct ("a", 1, "b", 3); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
818 %! A = cellfun (@(x,y) (x.a == y.a) && (x.b < y.b) , {a}, {b}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
819 %! "UniformOutput", true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
820 %! assert (A, true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
821 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
822 %! a = struct ("a", 1, "b", 2); b = struct ("a", 1, "b", 3); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
823 %! A = cellfun (@(x,y) (x.a == y.a) && (x.b < y.b) , {a}, {b}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
824 %! "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
825 %! assert (A, {true}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
826 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
827 %! a = struct ("a", 1, "b", 2); b = struct ("a", 1, "b", 3); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
828 %! A = cellfun (@(x,y) cell2str (x.a, y.a), {a}, {b}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
829 %! "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
830 %! assert (isfield (A, "identifier"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
831 %! assert (isfield (A, "message"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
832 %! assert (isfield (A, "index"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
833 %! assert (isempty (A.message), false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
834 %! assert (A.index, 1); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
835 %!test %% Overwriting setting of "UniformOutput" true |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
836 %! a = struct ("a", 1, "b", 2); b = struct ("a", 1, "b", 3); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
837 %! A = cellfun (@(x,y) cell2str (x.a, y.a), {a}, {b}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
838 %! "UniformOutput", true, "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
839 %! assert (isfield (A, "identifier"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
840 %! assert (isfield (A, "message"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
841 %! assert (isfield (A, "index"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
842 %! assert (isempty (A.message), false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
843 %! assert (A.index, 1); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
844 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
845 %% A lot of other tests |
5988 | 846 %!error(cellfun(1)) |
847 %!error(cellfun('isclass',1)) | |
848 %!error(cellfun('size',1)) | |
849 %!error(cellfun(@sin,{[]},'BadParam',false)) | |
850 %!error(cellfun(@sin,{[]},'UniformOuput')) | |
851 %!error(cellfun(@sin,{[]},'ErrorHandler')) | |
852 %!assert(cellfun(@sin,{0,1}),sin([0,1])) | |
853 %!assert(cellfun(inline('sin(x)'),{0,1}),sin([0,1])) | |
854 %!assert(cellfun('sin',{0,1}),sin([0,1])) | |
855 %!assert(cellfun('isempty',{1,[]}),[false,true]) | |
856 %!assert(cellfun('islogical',{false,pi}),[true,false]) | |
857 %!assert(cellfun('isreal',{1i,1}),[false,true]) | |
858 %!assert(cellfun('length',{zeros(2,2),1}),[2,1]) | |
859 %!assert(cellfun('prodofsize',{zeros(2,2),1}),[4,1]) | |
860 %!assert(cellfun('ndims',{zeros([2,2,2]),1}),[3,2]) | |
861 %!assert(cellfun('isclass',{zeros([2,2,2]),'test'},'double'),[true,false]) | |
862 %!assert(cellfun('size',{zeros([1,2,3]),1},1),[1,1]) | |
863 %!assert(cellfun('size',{zeros([1,2,3]),1},2),[2,1]) | |
864 %!assert(cellfun('size',{zeros([1,2,3]),1},3),[3,1]) | |
865 %!assert(cellfun(@atan2,{1,1},{1,2}),[atan2(1,1),atan2(1,2)]) | |
866 %!assert(cellfun(@atan2,{1,1},{1,2},'UniformOutput',false),{atan2(1,1),atan2(1,2)}) | |
6422 | 867 %!assert(cellfun(@sin,{1,2;3,4}),sin([1,2;3,4])) |
868 %!assert(cellfun(@atan2,{1,1;1,1},{1,2;1,2}),atan2([1,1;1,1],[1,2;1,2])) | |
5988 | 869 %!error(cellfun(@factorial,{-1,3})) |
870 %!assert(cellfun(@factorial,{-1,3},'ErrorHandler',@(x,y) NaN),[NaN,6]) | |
871 %!test | |
6704 | 872 %! [a,b,c]=cellfun(@fileparts,{fullfile("a","b","c.d"),fullfile("e","f","g.h")},'UniformOutput',false); |
873 %! assert(a,{fullfile("a","b"),fullfile("e","f")}) | |
5988 | 874 %! assert(b,{'c','g'}) |
875 %! assert(c,{'.d','.h'}) | |
876 | |
877 */ | |
878 | |
5451 | 879 DEFUN_DLD (num2cell, args, , |
880 "-*- texinfo -*-\n\ | |
9307
c2923c27c877
Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
881 @deftypefn {Loadable Function} {@var{c} =} num2cell (@var{m})\n\ |
8095
d94a597c94a2
Typo in num2cell's description
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7977
diff
changeset
|
882 @deftypefnx {Loadable Function} {@var{c} =} num2cell (@var{m}, @var{dim})\n\ |
9307
c2923c27c877
Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
883 Convert the matrix @var{m} to a cell array. If @var{dim} is defined, the\n\ |
5451 | 884 value @var{c} is of dimension 1 in this dimension and the elements of\n\ |
885 @var{m} are placed in slices in @var{c}.\n\ | |
5642 | 886 @seealso{mat2cell}\n\ |
887 @end deftypefn") | |
5451 | 888 { |
889 int nargin = args.length(); | |
890 octave_value retval; | |
891 | |
892 if (nargin < 1 || nargin > 2) | |
5823 | 893 print_usage (); |
5451 | 894 else |
895 { | |
896 dim_vector dv = args(0).dims (); | |
897 Array<int> sings; | |
898 | |
899 if (nargin == 2) | |
900 { | |
901 ColumnVector dsings = ColumnVector (args(1).vector_value | |
902 (false, true)); | |
903 sings.resize (dsings.length()); | |
904 | |
905 if (!error_state) | |
5988 | 906 for (octave_idx_type i = 0; i < dsings.length(); i++) |
5451 | 907 if (dsings(i) > dv.length() || dsings(i) < 1 || |
908 D_NINT(dsings(i)) != dsings(i)) | |
909 { | |
910 error ("invalid dimension specified"); | |
911 break; | |
912 } | |
913 else | |
914 sings(i) = NINT(dsings(i)) - 1; | |
915 } | |
916 | |
917 if (! error_state) | |
918 { | |
919 Array<bool> idx_colon (dv.length()); | |
920 dim_vector new_dv (dv); | |
921 octave_value_list lst (new_dv.length(), octave_value()); | |
922 | |
923 for (int i = 0; i < dv.length(); i++) | |
924 { | |
925 idx_colon(i) = false; | |
926 for (int j = 0; j < sings.length(); j++) | |
927 { | |
928 if (sings(j) == i) | |
929 { | |
930 new_dv(i) = 1; | |
931 idx_colon(i) = true; | |
932 lst(i) = octave_value (octave_value::magic_colon_t); | |
933 break; | |
934 } | |
935 } | |
936 } | |
937 | |
938 Cell ret (new_dv); | |
939 octave_idx_type nel = new_dv.numel(); | |
940 octave_idx_type ntot = 1; | |
941 | |
942 for (int j = 0; j < new_dv.length()-1; j++) | |
943 ntot *= new_dv(j); | |
944 | |
945 for (octave_idx_type i = 0; i < nel; i++) | |
946 { | |
947 octave_idx_type n = ntot; | |
948 octave_idx_type ii = i; | |
949 for (int j = new_dv.length() - 1; j >= 0 ; j--) | |
950 { | |
951 if (! idx_colon(j)) | |
952 lst (j) = ii/n + 1; | |
953 ii = ii % n; | |
954 if (j != 0) | |
955 n /= new_dv(j-1); | |
956 } | |
957 ret(i) = args(0).do_index_op(lst, 0); | |
958 } | |
959 | |
960 retval = ret; | |
961 } | |
962 } | |
963 | |
964 return retval; | |
965 } | |
966 | |
5988 | 967 /* |
968 | |
969 %!assert(num2cell([1,2;3,4]),{1,2;3,4}) | |
970 %!assert(num2cell([1,2;3,4],1),{[1;3],[2;4]}) | |
971 %!assert(num2cell([1,2;3,4],2),{[1,2];[3,4]}) | |
972 | |
973 */ | |
974 | |
5784 | 975 DEFUN_DLD (mat2cell, args, , |
976 "-*- texinfo -*-\n\ | |
977 @deftypefn {Loadable Function} {@var{b} =} mat2cell (@var{a}, @var{m}, @var{n})\n\ | |
978 @deftypefnx {Loadable Function} {@var{b} =} mat2cell (@var{a}, @var{d1}, @var{d2}, @dots{})\n\ | |
979 @deftypefnx {Loadable Function} {@var{b} =} mat2cell (@var{a}, @var{r})\n\ | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
980 Convert the matrix @var{a} to a cell array. If @var{a} is 2-D, then\n\ |
5784 | 981 it is required that @code{sum (@var{m}) == size (@var{a}, 1)} and\n\ |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
982 @code{sum (@var{n}) == size (@var{a}, 2)}. Similarly, if @var{a} is\n\ |
5784 | 983 a multi-dimensional and the number of dimensional arguments is equal\n\ |
984 to the dimensions of @var{a}, then it is required that @code{sum (@var{di})\n\ | |
985 == size (@var{a}, i)}.\n\ | |
986 \n\ | |
987 Given a single dimensional argument @var{r}, the other dimensional\n\ | |
988 arguments are assumed to equal @code{size (@var{a},@var{i})}.\n\ | |
989 \n\ | |
990 An example of the use of mat2cell is\n\ | |
991 \n\ | |
992 @example\n\ | |
993 mat2cell (reshape(1:16,4,4),[3,1],[3,1])\n\ | |
994 @result{} @{\n\ | |
995 [1,1] =\n\ | |
996 \n\ | |
997 1 5 9\n\ | |
998 2 6 10\n\ | |
999 3 7 11\n\ | |
1000 \n\ | |
1001 [2,1] =\n\ | |
1002 \n\ | |
1003 4 8 12\n\ | |
1004 \n\ | |
1005 [1,2] =\n\ | |
1006 \n\ | |
1007 13\n\ | |
1008 14\n\ | |
1009 15\n\ | |
1010 \n\ | |
1011 [2,2] = 16\n\ | |
1012 @}\n\ | |
1013 @end example\n\ | |
5829 | 1014 @seealso{num2cell, cell2mat}\n\ |
5784 | 1015 @end deftypefn") |
1016 { | |
1017 int nargin = args.length(); | |
1018 octave_value retval; | |
1019 | |
1020 if (nargin < 2) | |
7038 | 1021 print_usage (); |
5784 | 1022 else |
1023 { | |
1024 dim_vector dv = args(0).dims(); | |
1025 dim_vector new_dv; | |
1026 new_dv.resize(dv.length()); | |
1027 | |
1028 if (nargin > 2) | |
1029 { | |
1030 octave_idx_type nmax = -1; | |
1031 | |
1032 if (nargin - 1 != dv.length()) | |
1033 error ("mat2cell: Incorrect number of dimensions"); | |
1034 else | |
1035 { | |
1036 for (octave_idx_type j = 0; j < dv.length(); j++) | |
1037 { | |
1038 ColumnVector d = ColumnVector (args(j+1).vector_value | |
1039 (false, true)); | |
1040 | |
1041 if (d.length() < 1) | |
1042 { | |
1043 error ("mat2cell: dimension can not be empty"); | |
1044 break; | |
1045 } | |
1046 else | |
1047 { | |
1048 if (nmax < d.length()) | |
1049 nmax = d.length(); | |
1050 | |
1051 for (octave_idx_type i = 1; i < d.length(); i++) | |
1052 { | |
1053 OCTAVE_QUIT; | |
1054 | |
1055 if (d(i) >= 0) | |
1056 d(i) += d(i-1); | |
1057 else | |
1058 { | |
1059 error ("mat2cell: invalid dimensional argument"); | |
1060 break; | |
1061 } | |
1062 } | |
1063 | |
1064 if (d(0) < 0) | |
1065 error ("mat2cell: invalid dimensional argument"); | |
1066 | |
1067 if (d(d.length() - 1) != dv(j)) | |
1068 error ("mat2cell: inconsistent dimensions"); | |
1069 | |
1070 if (error_state) | |
1071 break; | |
1072 | |
1073 new_dv(j) = d.length(); | |
1074 } | |
1075 } | |
1076 } | |
1077 | |
1078 if (! error_state) | |
1079 { | |
1080 // Construct a matrix with the index values | |
1081 Matrix dimargs(nmax, new_dv.length()); | |
1082 for (octave_idx_type j = 0; j < new_dv.length(); j++) | |
1083 { | |
1084 OCTAVE_QUIT; | |
1085 | |
1086 ColumnVector d = ColumnVector (args(j+1).vector_value | |
1087 (false, true)); | |
1088 | |
1089 dimargs(0,j) = d(0); | |
1090 for (octave_idx_type i = 1; i < d.length(); i++) | |
1091 dimargs(i,j) = dimargs(i-1,j) + d(i); | |
1092 } | |
1093 | |
1094 | |
1095 octave_value_list lst (new_dv.length(), octave_value()); | |
1096 Cell ret (new_dv); | |
1097 octave_idx_type nel = new_dv.numel(); | |
1098 octave_idx_type ntot = 1; | |
1099 | |
1100 for (int j = 0; j < new_dv.length()-1; j++) | |
1101 ntot *= new_dv(j); | |
1102 | |
1103 for (octave_idx_type i = 0; i < nel; i++) | |
1104 { | |
1105 octave_idx_type n = ntot; | |
1106 octave_idx_type ii = i; | |
1107 for (octave_idx_type j = new_dv.length() - 1; j >= 0; j--) | |
1108 { | |
1109 OCTAVE_QUIT; | |
1110 | |
1111 octave_idx_type idx = ii / n; | |
1112 lst (j) = Range((idx == 0 ? 1. : dimargs(idx-1,j)+1.), | |
1113 dimargs(idx,j)); | |
1114 ii = ii % n; | |
1115 if (j != 0) | |
1116 n /= new_dv(j-1); | |
1117 } | |
1118 ret(i) = args(0).do_index_op(lst, 0); | |
1119 if (error_state) | |
1120 break; | |
1121 } | |
1122 | |
1123 if (!error_state) | |
1124 retval = ret; | |
1125 } | |
1126 } | |
1127 else | |
1128 { | |
1129 ColumnVector d = ColumnVector (args(1).vector_value | |
1130 (false, true)); | |
1131 | |
1132 double sumd = 0.; | |
1133 for (octave_idx_type i = 0; i < d.length(); i++) | |
1134 { | |
1135 OCTAVE_QUIT; | |
1136 | |
1137 if (d(i) >= 0) | |
1138 sumd += d(i); | |
1139 else | |
1140 { | |
1141 error ("mat2cell: invalid dimensional argument"); | |
1142 break; | |
1143 } | |
1144 } | |
1145 | |
1146 if (sumd != dv(0)) | |
1147 error ("mat2cell: inconsistent dimensions"); | |
1148 | |
1149 new_dv(0) = d.length(); | |
1150 for (octave_idx_type i = 1; i < dv.length(); i++) | |
1151 new_dv(i) = 1; | |
1152 | |
1153 if (! error_state) | |
1154 { | |
1155 octave_value_list lst (new_dv.length(), octave_value()); | |
1156 Cell ret (new_dv); | |
1157 | |
1158 for (octave_idx_type i = 1; i < new_dv.length(); i++) | |
1159 lst (i) = Range (1., static_cast<double>(dv(i))); | |
1160 | |
1161 double idx = 0.; | |
1162 for (octave_idx_type i = 0; i < new_dv(0); i++) | |
1163 { | |
1164 OCTAVE_QUIT; | |
1165 | |
1166 lst(0) = Range(idx + 1., idx + d(i)); | |
1167 ret(i) = args(0).do_index_op(lst, 0); | |
1168 idx += d(i); | |
1169 if (error_state) | |
1170 break; | |
1171 } | |
1172 | |
1173 if (!error_state) | |
1174 retval = ret; | |
1175 } | |
1176 } | |
1177 } | |
1178 | |
1179 return retval; | |
1180 } | |
1181 | |
1182 /* | |
1183 | |
1184 %!test | |
1185 %! x = reshape(1:20,5,4); | |
1186 %! c = mat2cell(x,[3,2],[3,1]); | |
1187 %! assert(c,{[1,6,11;2,7,12;3,8,13],[16;17;18];[4,9,14;5,10,15],[19;20]}) | |
1188 | |
1189 %!test | |
1190 %! x = 'abcdefghij'; | |
1191 %! c = mat2cell(x,1,[0,4,2,0,4,0]); | |
1192 %! empty1by0str = resize('',1,0); | |
1193 %! assert(c,{empty1by0str,'abcd','ef',empty1by0str,'ghij',empty1by0str}) | |
1194 | |
1195 */ | |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1196 |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1197 template <class NDA> |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1198 Cell |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1199 do_cellslices_nda (const NDA& array, const idx_vector& lb, const idx_vector& ub) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1200 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1201 octave_idx_type n = lb.length (0); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1202 Cell retval (1, n); |
9057
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1203 if (array.is_vector ()) |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1204 { |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1205 for (octave_idx_type i = 0; i < n && ! error_state; i++) |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1206 retval(i) = array.index (idx_vector (lb(i), ub(i) + 1)); |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1207 } |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1208 else |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1209 { |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1210 dim_vector dv = array.dims (); |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1211 octave_idx_type nl = 1; |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1212 for (int i = 0; i < dv.length () - 1; i++) nl *= dv(i); |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1213 for (octave_idx_type i = 0; i < n && ! error_state; i++) |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1214 { |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1215 // Do it with a single index to speed things up. |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1216 dv(dv.length () - 1) = ub(i) + 1 - lb(i); |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1217 retval(i) = array.index (idx_vector (nl*lb(i), nl*(ub(i) + 1))).reshape (dv); |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1218 } |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1219 } |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1220 |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1221 return retval; |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1222 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1223 |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1224 DEFUN_DLD (cellslices, args, , |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1225 "-*- texinfo -*-\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1226 @deftypefn {Loadable Function} {@var{sl} =} cellslices (@var{x}, @var{lb}, @var{ub})\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1227 Given a vector @var{x}, this function produces a cell array of slices from the vector\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1228 determined by the index vectors @var{lb}, @var{ub}, for lower and upper bounds, respectively.\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1229 In other words, it is equivalent to the following code:\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1230 \n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1231 @example\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9057
diff
changeset
|
1232 @group\n\ |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1233 n = length (lb);\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1234 sl = cell (1, n);\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1235 for i = 1:length (lb)\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1236 sl@{i@} = x(lb(i):ub(i));\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1237 endfor\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9057
diff
changeset
|
1238 @end group\n\ |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1239 @end example\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1240 \n\ |
9057
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1241 If @var{X} is a matrix or array, indexing is done along the last dimension.\n\ |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1242 @seealso{mat2cell}\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1243 @end deftypefn") |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1244 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1245 octave_value retval; |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1246 int nargin = args.length (); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1247 if (nargin == 3) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1248 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1249 octave_value x = args(0); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1250 idx_vector lb = args(1).index_vector (), ub = args(2).index_vector (); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1251 if (! error_state) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1252 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1253 if (lb.is_colon () || ub.is_colon ()) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1254 error ("cellslices: invalid use of colon"); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1255 else if (lb.length (0) != ub.length (0)) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1256 error ("cellslices: the lengths of lb and ub must match"); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1257 else |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1258 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1259 Cell retcell; |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1260 if (! x.is_sparse_type () && x.is_matrix_type ()) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1261 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1262 // specialize for some dense arrays. |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1263 if (x.is_bool_type ()) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1264 retcell = do_cellslices_nda (x.bool_array_value (), lb, ub); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1265 else if (x.is_char_matrix ()) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1266 retcell = do_cellslices_nda (x.char_array_value (), lb, ub); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1267 else if (x.is_complex_type ()) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1268 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1269 if (x.is_single_type ()) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1270 retcell = do_cellslices_nda (x.float_complex_array_value (), lb, ub); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1271 else |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1272 retcell = do_cellslices_nda (x.complex_array_value (), lb, ub); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1273 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1274 else |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1275 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1276 if (x.is_single_type ()) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1277 retcell = do_cellslices_nda (x.float_array_value (), lb, ub); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1278 else |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1279 retcell = do_cellslices_nda (x.array_value (), lb, ub); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1280 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1281 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1282 else |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1283 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1284 // generic code. |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1285 octave_idx_type n = lb.length (0); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1286 retcell = Cell (1, n); |
9057
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1287 octave_idx_type nind = x.dims ().is_vector () ? 1 : x.ndims (); |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1288 octave_value_list idx (nind, octave_value::magic_colon_t); |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1289 for (octave_idx_type i = 0; i < n && ! error_state; i++) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1290 { |
9057
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1291 idx(nind-1) = Range (static_cast<double> (lb(i)) + 1, |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
1292 static_cast<double> (ub(i)) + 1); |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1293 retcell(i) = x.do_index_op (idx); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1294 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1295 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1296 if (! error_state) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1297 retval = retcell; |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1298 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1299 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1300 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1301 else |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1302 print_usage (); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1303 |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1304 return retval; |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1305 } |
5784 | 1306 |
5205 | 1307 /* |
1308 ;;; Local Variables: *** | |
1309 ;;; mode: C++ *** | |
1310 ;;; End: *** | |
1311 */ |