5205
|
1 /* |
|
2 |
|
3 Copyright (C) 2005 Mohamed Kamoun |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
5205
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <string> |
|
29 |
|
30 #include "lo-mappers.h" |
|
31 |
|
32 #include "Cell.h" |
|
33 #include "defun-dld.h" |
|
34 #include "parse.h" |
|
35 #include "variables.h" |
5451
|
36 #include "ov-colon.h" |
5205
|
37 |
|
38 DEFUN_DLD (cellfun, args, , |
|
39 " -*- texinfo -*-\n\ |
|
40 @deftypefn {Lodable Function} {} cellfun (@var{name}, @var{c})\n\ |
5543
|
41 @deftypefnx {Lodable Function} {} cellfun (\"size\", @var{c}, @var{k})\n\ |
5205
|
42 @deftypefnx {Lodable Function} {} cellfun (\"isclass\", @var{c}, @var{class})\n\ |
|
43 @deftypefnx {Lodable Function} {} cellfun (@var{func}, @var{c})\n\ |
|
44 \n\ |
|
45 Evaluate the function named @var{name} on the elements of the cell array\n\ |
5543
|
46 @var{c}. Elements in @var{c} are passed on to the named function\n\ |
5205
|
47 individually. The function @var{name} can be one of the functions\n\ |
|
48 \n\ |
|
49 @table @code\n\ |
|
50 @item isempty\n\ |
5543
|
51 Return 1 for empty elements.\n\ |
5205
|
52 @item islogical\n\ |
|
53 Return 1 for logical elements.\n\ |
|
54 @item isreal\n\ |
|
55 Return 1 for real elements.\n\ |
|
56 @item length\n\ |
|
57 Return a vector of the lengths of cell elements.\n\ |
5543
|
58 @item ndims\n\ |
5205
|
59 Return the number of dimensions of each element.\n\ |
|
60 @item prodofsize\n\ |
|
61 Return the product of dimensions of each element.\n\ |
|
62 @item size\n\ |
|
63 Return the size along the @var{k}-th dimension.\n\ |
|
64 @item isclass\n\ |
|
65 Return 1 for elements of @var{class}.\n\ |
|
66 @end table\n\ |
|
67 \n\ |
|
68 Additionally, @code{cellfun} accepts an arbitrary function @var{func}\n\ |
|
69 in the form of an inline function, function handle, or the name of a\n\ |
|
70 function (in a character string). The function should take a single\n\ |
|
71 argument and return a single value, and in the case of a character string\n\ |
|
72 argument, the argument must be named @var{x}. For example\n\ |
|
73 \n\ |
|
74 @example\n\ |
|
75 @group\n\ |
|
76 cellfun (\"tolower(x)\", @{\"Foo\", \"Bar\", \"FooBar\"@})\n\ |
|
77 @result{} ans = @{\"foo\", \"bar\", \"foobar\"@}\n\ |
|
78 @end group\n\ |
|
79 @end example\n\ |
5642
|
80 @seealso{isempty, islogical, isreal, length, ndims, numel, size, isclass}\n\ |
|
81 @end deftypefn") |
5205
|
82 { |
|
83 octave_value retval; |
|
84 |
|
85 std::string name = "function"; |
|
86 |
|
87 octave_function *func = 0; |
|
88 |
|
89 int nargin = args.length (); |
|
90 |
|
91 if (nargin < 2) |
|
92 { |
|
93 error ("cellfun: you must supply at least 2 arguments"); |
|
94 print_usage ("cellfun"); |
|
95 return retval; |
|
96 } |
|
97 |
|
98 if (args(0).is_function_handle () || args(0).is_inline_function ()) |
|
99 { |
|
100 func = args(0).function_value (); |
|
101 |
|
102 if (error_state) |
|
103 return retval; |
|
104 } |
|
105 else if (args(0).is_string ()) |
|
106 name = args(0).string_value (); |
|
107 else |
|
108 { |
5543
|
109 error ("cellfun: first argument must be a string or function handle"); |
5205
|
110 return retval; |
|
111 } |
|
112 |
|
113 if (! args(1).is_cell ()) |
|
114 { |
5543
|
115 error ("cellfun: second argument must be a cell array"); |
5205
|
116 |
|
117 return retval; |
|
118 } |
|
119 |
|
120 Cell f_args = args(1).cell_value (); |
|
121 |
|
122 int k = f_args.numel (); |
|
123 |
|
124 if (name == "isempty") |
|
125 { |
|
126 boolNDArray result (f_args.dims ()); |
|
127 for (int count = 0; count < k ; count++) |
5543
|
128 result(count) = f_args.elem(count).is_empty (); |
5205
|
129 retval = result; |
|
130 } |
|
131 else if (name == "islogical") |
|
132 { |
|
133 boolNDArray result (f_args.dims ()); |
|
134 for (int count= 0; count < k ; count++) |
|
135 result(count) = f_args.elem(count).is_bool_type (); |
|
136 retval = result; |
|
137 } |
|
138 else if (name == "isreal") |
|
139 { |
|
140 boolNDArray result (f_args.dims ()); |
|
141 for (int count= 0; count < k ; count++) |
|
142 result(count) = f_args.elem(count).is_real_type (); |
|
143 retval = result; |
|
144 } |
|
145 else if (name == "length") |
|
146 { |
|
147 NDArray result (f_args.dims ()); |
|
148 for (int count= 0; count < k ; count++) |
5544
|
149 result(count) = static_cast<double> (f_args.elem(count).length ()); |
5205
|
150 retval = result; |
|
151 } |
|
152 else if (name == "ndims") |
|
153 { |
|
154 NDArray result (f_args.dims ()); |
|
155 for (int count = 0; count < k ; count++) |
5544
|
156 result(count) = static_cast<double> (f_args.elem(count).ndims ()); |
5205
|
157 retval = result; |
|
158 } |
|
159 else if (name == "prodofsize") |
|
160 { |
|
161 NDArray result (f_args.dims ()); |
|
162 for (int count = 0; count < k ; count++) |
5544
|
163 result(count) = static_cast<double> (f_args.elem(count).numel ()); |
5205
|
164 retval = result; |
|
165 } |
|
166 else if (name == "size") |
|
167 { |
|
168 if (nargin == 3) |
|
169 { |
|
170 int d = args(2).nint_value () - 1; |
|
171 |
|
172 if (d < 0) |
|
173 error ("cellfun: third argument must be a postive integer"); |
|
174 |
|
175 if (!error_state) |
|
176 { |
|
177 NDArray result (f_args.dims ()); |
|
178 for (int count = 0; count < k ; count++) |
|
179 { |
|
180 dim_vector dv = f_args.elem(count).dims (); |
|
181 if (d < dv.length ()) |
5544
|
182 result(count) = static_cast<double> (dv(d)); |
5205
|
183 else |
|
184 result(count) = 1.0; |
|
185 } |
|
186 retval = result; |
|
187 } |
|
188 } |
|
189 else |
5543
|
190 error ("not enough arguments for `size'"); |
5205
|
191 } |
|
192 else if (name == "isclass") |
|
193 { |
|
194 if (nargin == 3) |
|
195 { |
|
196 std::string class_name = args(2).string_value(); |
|
197 boolNDArray result (f_args.dims ()); |
|
198 for (int count = 0; count < k ; count++) |
|
199 result(count) = (f_args.elem(count).class_name() == class_name); |
|
200 |
|
201 retval = result; |
|
202 } |
|
203 else |
5543
|
204 error ("not enough arguments for `isclass'"); |
5205
|
205 } |
|
206 else |
|
207 { |
|
208 std::string fcn_name; |
|
209 |
|
210 if (! func) |
|
211 { |
|
212 fcn_name = unique_symbol_name ("__cellfun_fcn_"); |
|
213 std::string fname = "function y = "; |
|
214 fname.append (fcn_name); |
|
215 fname.append ("(x) y = "); |
|
216 func = extract_function (args(0), "cellfun", fcn_name, fname, |
|
217 "; endfunction"); |
|
218 } |
|
219 |
|
220 if (! func) |
|
221 error ("unknown function"); |
|
222 else |
|
223 { |
|
224 Cell result (f_args.dims ()); |
|
225 |
|
226 for (int count = 0; count < k ; count++) |
|
227 { |
|
228 octave_value_list tmp |
|
229 = func->do_multi_index_op (1, f_args.elem (count)); |
|
230 result(count) = tmp(0); |
|
231 |
|
232 if (error_state) |
|
233 break; |
|
234 } |
|
235 |
|
236 if (! error_state) |
|
237 retval = result; |
|
238 |
|
239 if (! fcn_name.empty ()) |
|
240 clear_function (fcn_name); |
|
241 } |
|
242 } |
|
243 |
|
244 return retval; |
|
245 } |
|
246 |
5451
|
247 DEFUN_DLD (num2cell, args, , |
|
248 "-*- texinfo -*-\n\ |
|
249 @deftypefn {Loadable Function} {@var{c} =} num2cell (@var{m})\n\ |
|
250 @deftypefnx {Loadable Function} {@var{c} =} num2cell (@var{m}, @var{d})\n\ |
|
251 Convert to matrix @var{m} into a cell array. If @var{d} is defined the\n\ |
|
252 value @var{c} is of dimension 1 in this dimension and the elements of\n\ |
|
253 @var{m} are placed in slices in @var{c}.\n\ |
5642
|
254 @seealso{mat2cell}\n\ |
|
255 @end deftypefn") |
5451
|
256 { |
|
257 int nargin = args.length(); |
|
258 octave_value retval; |
|
259 |
|
260 if (nargin < 1 || nargin > 2) |
|
261 print_usage ("num2cell"); |
|
262 else |
|
263 { |
|
264 dim_vector dv = args(0).dims (); |
|
265 Array<int> sings; |
|
266 |
|
267 if (nargin == 2) |
|
268 { |
|
269 ColumnVector dsings = ColumnVector (args(1).vector_value |
|
270 (false, true)); |
|
271 sings.resize (dsings.length()); |
|
272 |
|
273 if (!error_state) |
|
274 for (int i = 0; i < dsings.length(); i++) |
|
275 if (dsings(i) > dv.length() || dsings(i) < 1 || |
|
276 D_NINT(dsings(i)) != dsings(i)) |
|
277 { |
|
278 error ("invalid dimension specified"); |
|
279 break; |
|
280 } |
|
281 else |
|
282 sings(i) = NINT(dsings(i)) - 1; |
|
283 } |
|
284 |
|
285 if (! error_state) |
|
286 { |
|
287 Array<bool> idx_colon (dv.length()); |
|
288 dim_vector new_dv (dv); |
|
289 octave_value_list lst (new_dv.length(), octave_value()); |
|
290 |
|
291 for (int i = 0; i < dv.length(); i++) |
|
292 { |
|
293 idx_colon(i) = false; |
|
294 for (int j = 0; j < sings.length(); j++) |
|
295 { |
|
296 if (sings(j) == i) |
|
297 { |
|
298 new_dv(i) = 1; |
|
299 idx_colon(i) = true; |
|
300 lst(i) = octave_value (octave_value::magic_colon_t); |
|
301 break; |
|
302 } |
|
303 } |
|
304 } |
|
305 |
|
306 Cell ret (new_dv); |
|
307 octave_idx_type nel = new_dv.numel(); |
|
308 octave_idx_type ntot = 1; |
|
309 |
|
310 for (int j = 0; j < new_dv.length()-1; j++) |
|
311 ntot *= new_dv(j); |
|
312 |
|
313 for (octave_idx_type i = 0; i < nel; i++) |
|
314 { |
|
315 octave_idx_type n = ntot; |
|
316 octave_idx_type ii = i; |
|
317 for (int j = new_dv.length() - 1; j >= 0 ; j--) |
|
318 { |
|
319 if (! idx_colon(j)) |
|
320 lst (j) = ii/n + 1; |
|
321 ii = ii % n; |
|
322 if (j != 0) |
|
323 n /= new_dv(j-1); |
|
324 } |
|
325 ret(i) = args(0).do_index_op(lst, 0); |
|
326 } |
|
327 |
|
328 retval = ret; |
|
329 } |
|
330 } |
|
331 |
|
332 return retval; |
|
333 } |
|
334 |
5205
|
335 /* |
|
336 ;;; Local Variables: *** |
|
337 ;;; mode: C++ *** |
|
338 ;;; End: *** |
|
339 */ |