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