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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <string> |
|
28 |
|
29 #include "lo-mappers.h" |
|
30 |
|
31 #include "Cell.h" |
|
32 #include "defun-dld.h" |
|
33 #include "parse.h" |
|
34 #include "variables.h" |
|
35 |
|
36 DEFUN_DLD (cellfun, args, , |
|
37 " -*- texinfo -*-\n\ |
|
38 @deftypefn {Lodable Function} {} cellfun (@var{name}, @var{c})\n\ |
|
39 @deftypefnx {Lodable Function} {} cellfun (\"isclass\", @var{c}, @var{class})\n\ |
|
40 @deftypefnx {Lodable Function} {} cellfun (\"size\", @var{c}, @var{k})\n\ |
|
41 @deftypefnx {Lodable Function} {} cellfun (@var{func}, @var{c})\n\ |
|
42 \n\ |
|
43 Evaluate the function named @var{name} on the elements of the cell array\n\ |
|
44 @var{c}. Elements in cell_array are passed on to the named function\n\ |
|
45 individually. The function @var{name} can be one of the functions\n\ |
|
46 \n\ |
|
47 @table @code\n\ |
|
48 @item isempty\n\ |
|
49 Return 1 when for non empty elements and 0 for others.\n\ |
|
50 @item islogical\n\ |
|
51 Return 1 for logical elements.\n\ |
|
52 @item isreal\n\ |
|
53 Return 1 for real elements.\n\ |
|
54 @item length\n\ |
|
55 Return a vector of the lengths of cell elements.\n\ |
|
56 @item dims\n\ |
|
57 Return the number of dimensions of each element.\n\ |
|
58 @item prodofsize\n\ |
|
59 Return the product of dimensions of each element.\n\ |
|
60 @item size\n\ |
|
61 Return the size along the @var{k}-th dimension.\n\ |
|
62 @item isclass\n\ |
|
63 Return 1 for elements of @var{class}.\n\ |
|
64 @end table\n\ |
|
65 \n\ |
|
66 Additionally, @code{cellfun} accepts an arbitrary function @var{func}\n\ |
|
67 in the form of an inline function, function handle, or the name of a\n\ |
|
68 function (in a character string). The function should take a single\n\ |
|
69 argument and return a single value, and in the case of a character string\n\ |
|
70 argument, the argument must be named @var{x}. For example\n\ |
|
71 \n\ |
|
72 @example\n\ |
|
73 @group\n\ |
|
74 cellfun (\"tolower(x)\", @{\"Foo\", \"Bar\", \"FooBar\"@})\n\ |
|
75 @result{} ans = @{\"foo\", \"bar\", \"foobar\"@}\n\ |
|
76 @end group\n\ |
|
77 @end example\n\ |
|
78 \n\ |
|
79 @end deftypefn") |
|
80 { |
|
81 octave_value retval; |
|
82 |
|
83 std::string name = "function"; |
|
84 |
|
85 octave_function *func = 0; |
|
86 |
|
87 int nargin = args.length (); |
|
88 |
|
89 if (nargin < 2) |
|
90 { |
|
91 error ("cellfun: you must supply at least 2 arguments"); |
|
92 print_usage ("cellfun"); |
|
93 return retval; |
|
94 } |
|
95 |
|
96 if (args(0).is_function_handle () || args(0).is_inline_function ()) |
|
97 { |
|
98 func = args(0).function_value (); |
|
99 |
|
100 if (error_state) |
|
101 return retval; |
|
102 } |
|
103 else if (args(0).is_string ()) |
|
104 name = args(0).string_value (); |
|
105 else |
|
106 { |
|
107 error ("cellfun: first argument must be a string"); |
|
108 return retval; |
|
109 } |
|
110 |
|
111 if (! args(1).is_cell ()) |
|
112 { |
|
113 error ("cellfun: second argument must be a cell"); |
|
114 |
|
115 return retval; |
|
116 } |
|
117 |
|
118 Cell f_args = args(1).cell_value (); |
|
119 |
|
120 int k = f_args.numel (); |
|
121 |
|
122 if (name == "isempty") |
|
123 { |
|
124 boolNDArray result (f_args.dims ()); |
|
125 for (int count = 0; count < k ; count++) |
|
126 result(count) = f_args.elem(count).is_empty(); |
|
127 retval = result; |
|
128 } |
|
129 else if (name == "islogical") |
|
130 { |
|
131 boolNDArray result (f_args.dims ()); |
|
132 for (int count= 0; count < k ; count++) |
|
133 result(count) = f_args.elem(count).is_bool_type (); |
|
134 retval = result; |
|
135 } |
|
136 else if (name == "isreal") |
|
137 { |
|
138 boolNDArray result (f_args.dims ()); |
|
139 for (int count= 0; count < k ; count++) |
|
140 result(count) = f_args.elem(count).is_real_type (); |
|
141 retval = result; |
|
142 } |
|
143 else if (name == "length") |
|
144 { |
|
145 NDArray result (f_args.dims ()); |
|
146 for (int count= 0; count < k ; count++) |
|
147 result(count) = double (f_args.elem(count).numel ()); |
|
148 retval = result; |
|
149 } |
|
150 else if (name == "ndims") |
|
151 { |
|
152 NDArray result (f_args.dims ()); |
|
153 for (int count = 0; count < k ; count++) |
|
154 result(count) = double ((f_args.elem(count).dims ()).numel ()); |
|
155 retval = result; |
|
156 } |
|
157 else if (name == "prodofsize") |
|
158 { |
|
159 NDArray result (f_args.dims ()); |
|
160 for (int count = 0; count < k ; count++) |
|
161 result(count) = double ((f_args.elem(count).dims ()).numel ()); |
|
162 retval = result; |
|
163 } |
|
164 else if (name == "size") |
|
165 { |
|
166 if (nargin == 3) |
|
167 { |
|
168 int d = args(2).nint_value () - 1; |
|
169 |
|
170 if (d < 0) |
|
171 error ("cellfun: third argument must be a postive integer"); |
|
172 |
|
173 if (!error_state) |
|
174 { |
|
175 NDArray result (f_args.dims ()); |
|
176 for (int count = 0; count < k ; count++) |
|
177 { |
|
178 dim_vector dv = f_args.elem(count).dims (); |
|
179 if (d < dv.length ()) |
|
180 result(count) = double (dv(d)); |
|
181 else |
|
182 result(count) = 1.0; |
|
183 } |
|
184 retval = result; |
|
185 } |
|
186 } |
|
187 else |
|
188 error ("Not enough argument for size"); |
|
189 } |
|
190 else if (name == "isclass") |
|
191 { |
|
192 if (nargin == 3) |
|
193 { |
|
194 std::string class_name = args(2).string_value(); |
|
195 boolNDArray result (f_args.dims ()); |
|
196 for (int count = 0; count < k ; count++) |
|
197 result(count) = (f_args.elem(count).class_name() == class_name); |
|
198 |
|
199 retval = result; |
|
200 } |
|
201 else |
|
202 error ("Not enough argument for isclass"); |
|
203 } |
|
204 else |
|
205 { |
|
206 std::string fcn_name; |
|
207 |
|
208 if (! func) |
|
209 { |
|
210 fcn_name = unique_symbol_name ("__cellfun_fcn_"); |
|
211 std::string fname = "function y = "; |
|
212 fname.append (fcn_name); |
|
213 fname.append ("(x) y = "); |
|
214 func = extract_function (args(0), "cellfun", fcn_name, fname, |
|
215 "; endfunction"); |
|
216 } |
|
217 |
|
218 if (! func) |
|
219 error ("unknown function"); |
|
220 else |
|
221 { |
|
222 Cell result (f_args.dims ()); |
|
223 |
|
224 for (int count = 0; count < k ; count++) |
|
225 { |
|
226 octave_value_list tmp |
|
227 = func->do_multi_index_op (1, f_args.elem (count)); |
|
228 result(count) = tmp(0); |
|
229 |
|
230 if (error_state) |
|
231 break; |
|
232 } |
|
233 |
|
234 if (! error_state) |
|
235 retval = result; |
|
236 |
|
237 if (! fcn_name.empty ()) |
|
238 clear_function (fcn_name); |
|
239 } |
|
240 } |
|
241 |
|
242 return retval; |
|
243 } |
|
244 |
|
245 /* |
|
246 ;;; Local Variables: *** |
|
247 ;;; mode: C++ *** |
|
248 ;;; End: *** |
|
249 */ |