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