2974
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
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 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include "error.h" |
|
32 #include "gripes.h" |
|
33 #include "oct-obj.h" |
|
34 #include "ov-mapper.h" |
|
35 #include "ov.h" |
|
36 |
3219
|
37 DEFINE_OCTAVE_ALLOCATOR (octave_mapper); |
2974
|
38 |
3219
|
39 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_mapper, |
|
40 "built-in mapper function"); |
2974
|
41 |
|
42 static bool |
|
43 any_element_less_than (const Matrix& a, double val) |
|
44 { |
|
45 int nr = a.rows (); |
|
46 int nc = a.columns (); |
|
47 |
|
48 for (int j = 0; j < nc; j++) |
|
49 for (int i = 0; i < nr; i++) |
|
50 if (a (i, j) < val) |
|
51 return true; |
|
52 |
|
53 return false; |
|
54 } |
|
55 |
|
56 static bool |
|
57 any_element_greater_than (const Matrix& a, double val) |
|
58 { |
|
59 int nr = a.rows (); |
|
60 int nc = a.columns (); |
|
61 |
|
62 for (int j = 0; j < nc; j++) |
|
63 for (int i = 0; i < nr; i++) |
|
64 if (a (i, j) > val) |
|
65 return true; |
|
66 |
|
67 return false; |
|
68 } |
|
69 |
3962
|
70 // In most cases, we could use the map member function from the Matrix |
|
71 // classes, but as currently implemented, they don't allow us to |
|
72 // detect errors and abort properly. So use these macros to do the |
|
73 // looping here instead. |
|
74 |
|
75 #define MAPPER_LOOP_2(T, F, M, CONV, R) \ |
|
76 do \ |
|
77 { \ |
|
78 int nr = M.rows (); \ |
|
79 int nc = M.cols (); \ |
|
80 \ |
|
81 T result (nr, nc); \ |
|
82 \ |
|
83 for (int j = 0; j < nc; j++) \ |
|
84 { \ |
|
85 for (int i = 0; i < nr; i++) \ |
|
86 { \ |
|
87 result (i, j) = CONV (F (M (i, j))); \ |
|
88 \ |
|
89 if (error_state) \ |
|
90 return retval; \ |
|
91 } \ |
|
92 } \ |
|
93 retval = R; \ |
|
94 } \ |
|
95 while (0) |
|
96 |
|
97 #define MAPPER_LOOP_1(T, F, M, CONV) \ |
|
98 MAPPER_LOOP_2 (T, F, M, CONV, result) |
|
99 |
|
100 #define MAPPER_LOOP(T, F, M) \ |
|
101 MAPPER_LOOP_1 (T, F, M, ) |
|
102 |
2974
|
103 octave_value |
|
104 octave_mapper::apply (const octave_value& arg) const |
|
105 { |
|
106 octave_value retval; |
|
107 |
|
108 if (ch_map_fcn) |
|
109 { |
|
110 // XXX FIXME XXX -- this could be done in a better way... |
|
111 |
|
112 octave_value tmp = arg.convert_to_str (); |
|
113 |
|
114 if (! error_state) |
|
115 { |
|
116 charMatrix chm = tmp.char_matrix_value (); |
|
117 |
|
118 if (! error_state) |
|
119 { |
|
120 switch (flag) |
|
121 { |
|
122 case 0: |
3962
|
123 MAPPER_LOOP_1 (boolMatrix, ch_map_fcn, chm, bool); |
2974
|
124 break; |
|
125 |
|
126 case 1: |
3962
|
127 MAPPER_LOOP (Matrix, ch_map_fcn, chm); |
2974
|
128 break; |
|
129 |
|
130 case 2: |
3962
|
131 MAPPER_LOOP_2 (charMatrix, ch_map_fcn, chm, , |
|
132 octave_value (result, true)); |
2974
|
133 break; |
|
134 |
|
135 default: |
|
136 panic_impossible (); |
|
137 break; |
|
138 } |
|
139 } |
|
140 } |
|
141 } |
|
142 else |
|
143 { |
|
144 if (arg.is_real_type ()) |
|
145 { |
|
146 if (arg.is_scalar_type ()) |
|
147 { |
|
148 double d = arg.double_value (); |
|
149 |
|
150 if (flag && (d < lower_limit || d > upper_limit)) |
|
151 { |
|
152 if (c_c_map_fcn) |
|
153 retval = c_c_map_fcn (Complex (d)); |
|
154 else |
|
155 error ("%s: unable to handle real arguments", |
|
156 name().c_str ()); |
|
157 } |
|
158 else if (d_d_map_fcn) |
|
159 retval = d_d_map_fcn (d); |
3249
|
160 else if (d_b_map_fcn) |
|
161 retval = d_b_map_fcn (d); |
2974
|
162 else |
|
163 error ("%s: unable to handle real arguments", |
|
164 name().c_str ()); |
|
165 } |
|
166 else |
|
167 { |
|
168 Matrix m = arg.matrix_value (); |
|
169 |
|
170 if (error_state) |
|
171 return retval; |
|
172 |
|
173 if (flag |
|
174 && (any_element_less_than (m, lower_limit) |
|
175 || any_element_greater_than (m, upper_limit))) |
|
176 { |
|
177 if (c_c_map_fcn) |
3962
|
178 MAPPER_LOOP (ComplexMatrix, c_c_map_fcn, m); |
2974
|
179 else |
|
180 error ("%s: unable to handle real arguments", |
|
181 name().c_str ()); |
|
182 } |
|
183 else if (d_d_map_fcn) |
3962
|
184 MAPPER_LOOP (Matrix, d_d_map_fcn, m); |
3249
|
185 else if (d_b_map_fcn) |
3962
|
186 MAPPER_LOOP (boolMatrix, d_b_map_fcn, m); |
2974
|
187 else |
|
188 error ("%s: unable to handle real arguments", |
|
189 name().c_str ()); |
|
190 } |
|
191 } |
|
192 else if (arg.is_complex_type ()) |
|
193 { |
|
194 if (arg.is_scalar_type ()) |
|
195 { |
|
196 Complex c = arg.complex_value (); |
|
197 |
|
198 if (d_c_map_fcn) |
|
199 retval = d_c_map_fcn (c); |
|
200 else if (c_c_map_fcn) |
|
201 retval = c_c_map_fcn (c); |
3249
|
202 else if (c_b_map_fcn) |
|
203 retval = c_b_map_fcn (c); |
2974
|
204 else |
|
205 error ("%s: unable to handle complex arguments", |
|
206 name().c_str ()); |
|
207 } |
|
208 else |
|
209 { |
|
210 ComplexMatrix cm = arg.complex_matrix_value (); |
|
211 |
|
212 if (error_state) |
|
213 return retval; |
|
214 |
|
215 if (d_c_map_fcn) |
3962
|
216 MAPPER_LOOP (Matrix, d_c_map_fcn, cm); |
2974
|
217 else if (c_c_map_fcn) |
3962
|
218 MAPPER_LOOP (ComplexMatrix, c_c_map_fcn, cm); |
3249
|
219 else if (c_b_map_fcn) |
3962
|
220 MAPPER_LOOP (boolMatrix, c_b_map_fcn, cm); |
2974
|
221 else |
|
222 error ("%s: unable to handle complex arguments", |
|
223 name().c_str ()); |
|
224 } |
|
225 } |
|
226 else |
|
227 gripe_wrong_type_arg ("mapper", arg); |
|
228 } |
|
229 |
|
230 return retval; |
|
231 } |
|
232 |
|
233 octave_value_list |
3933
|
234 octave_mapper::subsref (const std::string type, |
|
235 const SLList<octave_value_list>& idx, |
|
236 int nargout) |
|
237 { |
|
238 octave_value_list retval; |
|
239 |
|
240 switch (type[0]) |
|
241 { |
|
242 case '(': |
|
243 retval = do_multi_index_op (nargout, idx.front ()); |
|
244 break; |
|
245 |
|
246 case '{': |
|
247 case '.': |
|
248 { |
|
249 std::string nm = type_name (); |
|
250 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
251 } |
|
252 break; |
|
253 |
|
254 default: |
|
255 panic_impossible (); |
|
256 } |
|
257 |
|
258 return retval; |
|
259 |
|
260 // XXX FIXME XXX |
|
261 // return retval.next_subsref (type, idx); |
|
262 } |
|
263 |
|
264 octave_value_list |
3544
|
265 octave_mapper::do_multi_index_op (int, const octave_value_list& args) |
2974
|
266 { |
|
267 octave_value retval; |
|
268 |
|
269 if (error_state) |
|
270 return retval; |
|
271 |
|
272 int nargin = args.length (); |
|
273 |
|
274 if (nargin > 1) |
|
275 ::error ("%s: too many arguments", name().c_str ()); |
|
276 else if (nargin < 1) |
|
277 ::error ("%s: too few arguments", name().c_str ()); |
|
278 else |
|
279 { |
|
280 if (args(0).is_defined ()) |
|
281 retval = apply (args(0)); |
|
282 else |
|
283 ::error ("%s: argument undefined", name().c_str ()); |
|
284 } |
|
285 |
|
286 return retval; |
|
287 } |
|
288 |
|
289 /* |
|
290 ;;; Local Variables: *** |
|
291 ;;; mode: C++ *** |
|
292 ;;; End: *** |
|
293 */ |