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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2974
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
4153
|
28 #include "quit.h" |
|
29 |
2974
|
30 #include "error.h" |
|
31 #include "gripes.h" |
|
32 #include "oct-obj.h" |
|
33 #include "ov-mapper.h" |
|
34 #include "ov.h" |
4748
|
35 #include "toplev.h" |
|
36 #include "unwind-prot.h" |
2974
|
37 |
3219
|
38 DEFINE_OCTAVE_ALLOCATOR (octave_mapper); |
2974
|
39 |
3219
|
40 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_mapper, |
4612
|
41 "built-in mapper function", |
3219
|
42 "built-in mapper function"); |
2974
|
43 |
|
44 static bool |
4643
|
45 any_element_less_than (const NDArray& a, double val) |
2974
|
46 { |
5275
|
47 octave_idx_type len = a.length (); |
2974
|
48 |
5275
|
49 for (octave_idx_type i = 0; i < len; i++) |
4643
|
50 { |
|
51 OCTAVE_QUIT; |
4153
|
52 |
4643
|
53 if (a(i) < val) |
|
54 return true; |
|
55 } |
2974
|
56 |
|
57 return false; |
|
58 } |
|
59 |
|
60 static bool |
5282
|
61 any_element_less_than (const SparseMatrix& a, double val) |
|
62 { |
5604
|
63 octave_idx_type len = a.nnz (); |
5282
|
64 |
|
65 if (val > 0. && len != a.numel ()) |
|
66 return true; |
|
67 |
|
68 for (octave_idx_type i = 0; i < len; i++) |
|
69 { |
|
70 OCTAVE_QUIT; |
|
71 |
|
72 if (a.data(i) < val) |
|
73 return true; |
|
74 } |
|
75 |
|
76 return false; |
|
77 } |
|
78 |
|
79 static bool |
4643
|
80 any_element_greater_than (const NDArray& a, double val) |
2974
|
81 { |
5275
|
82 octave_idx_type len = a.length (); |
2974
|
83 |
5275
|
84 for (octave_idx_type i = 0; i < len; i++) |
4643
|
85 { |
|
86 OCTAVE_QUIT; |
4153
|
87 |
4643
|
88 if (a(i) > val) |
|
89 return true; |
|
90 } |
2974
|
91 |
|
92 return false; |
|
93 } |
|
94 |
5282
|
95 static bool |
|
96 any_element_greater_than (const SparseMatrix& a, double val) |
|
97 { |
5604
|
98 octave_idx_type len = a.nnz (); |
5282
|
99 |
|
100 if (val < 0. && len != a.numel ()) |
|
101 return true; |
|
102 |
|
103 for (octave_idx_type i = 0; i < len; i++) |
|
104 { |
|
105 OCTAVE_QUIT; |
|
106 |
|
107 if (a.data(i) > val) |
|
108 return true; |
|
109 } |
|
110 |
|
111 return false; |
|
112 } |
|
113 |
4643
|
114 // In most cases, we could use the map member function from the NDArray |
3962
|
115 // classes, but as currently implemented, they don't allow us to |
|
116 // detect errors and abort properly. So use these macros to do the |
|
117 // looping here instead. |
|
118 |
|
119 #define MAPPER_LOOP_2(T, F, M, CONV, R) \ |
|
120 do \ |
|
121 { \ |
5275
|
122 octave_idx_type len = M.length (); \ |
3962
|
123 \ |
4643
|
124 T result (M.dims ()); \ |
3962
|
125 \ |
5275
|
126 for (octave_idx_type i = 0; i < len; i++) \ |
3962
|
127 { \ |
4643
|
128 OCTAVE_QUIT; \ |
|
129 \ |
|
130 result(i) = CONV (F (M(i))); \ |
4153
|
131 \ |
4643
|
132 if (error_state) \ |
|
133 return retval; \ |
|
134 } \ |
3962
|
135 \ |
|
136 retval = R; \ |
|
137 } \ |
|
138 while (0) |
|
139 |
|
140 #define MAPPER_LOOP_1(T, F, M, CONV) \ |
|
141 MAPPER_LOOP_2 (T, F, M, CONV, result) |
|
142 |
|
143 #define MAPPER_LOOP(T, F, M) \ |
|
144 MAPPER_LOOP_1 (T, F, M, ) |
|
145 |
5282
|
146 #define SPARSE_MAPPER_LOOP_2(T, ET, F, M, CONV, R) \ |
|
147 do \ |
|
148 { \ |
|
149 ET f_zero = CONV (F (0.)); \ |
|
150 \ |
|
151 if (f_zero != 0.) \ |
|
152 { \ |
|
153 octave_idx_type nr = M.rows (); \ |
|
154 octave_idx_type nc = M.cols (); \ |
|
155 \ |
|
156 T result (nr, nc, f_zero); \ |
|
157 \ |
|
158 for (octave_idx_type j = 0; j < nc; j++) \ |
|
159 for (octave_idx_type i = M.cidx(j); i < M.cidx (j+1); i++) \ |
|
160 { \ |
|
161 OCTAVE_QUIT; \ |
|
162 result.elem (M.ridx (i), j) = CONV (F (M.data(i))); \ |
|
163 \ |
|
164 if (error_state) \ |
|
165 return retval; \ |
|
166 } \ |
|
167 \ |
|
168 result.maybe_compress (true); \ |
|
169 retval = R; \ |
|
170 } \ |
|
171 else \ |
|
172 { \ |
5604
|
173 octave_idx_type nnz = M.nnz (); \ |
5282
|
174 octave_idx_type nr = M.rows (); \ |
|
175 octave_idx_type nc = M.cols (); \ |
|
176 \ |
|
177 T result (nr, nc, nnz); \ |
|
178 ET zero = ET (0.); \ |
|
179 octave_idx_type ii = 0; \ |
|
180 result.cidx (ii) = 0; \ |
|
181 \ |
|
182 for (octave_idx_type j = 0; j < nc; j++) \ |
|
183 { \ |
|
184 for (octave_idx_type i = M.cidx(j); i < M.cidx (j+1); i++) \ |
|
185 { \ |
|
186 ET val = CONV (F (M.data (i))); \ |
|
187 if (val != zero) \ |
|
188 { \ |
|
189 result.data (ii) = val; \ |
|
190 result.ridx (ii++) = M.ridx (i); \ |
|
191 } \ |
|
192 OCTAVE_QUIT; \ |
|
193 \ |
|
194 if (error_state) \ |
|
195 return retval; \ |
|
196 } \ |
|
197 result.cidx (j+1) = ii; \ |
|
198 } \ |
|
199 \ |
|
200 result.maybe_compress (false); \ |
|
201 retval = R; \ |
|
202 } \ |
|
203 } \ |
|
204 while (0) |
|
205 |
|
206 #define SPARSE_MAPPER_LOOP_1(T, ET, F, M, CONV) \ |
|
207 SPARSE_MAPPER_LOOP_2 (T, ET, F, M, CONV, result) |
|
208 |
|
209 #define SPARSE_MAPPER_LOOP(T, ET, F, M) \ |
|
210 SPARSE_MAPPER_LOOP_1 (T, ET, F, M, ) |
|
211 |
2974
|
212 octave_value |
|
213 octave_mapper::apply (const octave_value& arg) const |
|
214 { |
|
215 octave_value retval; |
|
216 |
4452
|
217 // XXX FIXME XXX -- is_real_type can return true. Should it really |
|
218 // work that way? |
4123
|
219 |
5124
|
220 if (arg.is_real_type () |
|
221 && (c_c_map_fcn || d_d_map_fcn || d_b_map_fcn) |
|
222 && ! (arg.is_string () && ch_map_fcn)) |
4104
|
223 { |
|
224 if (arg.is_scalar_type ()) |
|
225 { |
|
226 double d = arg.double_value (); |
|
227 |
|
228 if (can_ret_cmplx_for_real && (d < lower_limit || d > upper_limit)) |
|
229 { |
|
230 if (c_c_map_fcn) |
|
231 retval = c_c_map_fcn (Complex (d)); |
|
232 else |
|
233 error ("%s: unable to handle real arguments", |
|
234 name().c_str ()); |
|
235 } |
|
236 else if (d_d_map_fcn) |
|
237 retval = d_d_map_fcn (d); |
|
238 else if (d_b_map_fcn) |
|
239 retval = d_b_map_fcn (d); |
|
240 else |
|
241 error ("%s: unable to handle real arguments", |
|
242 name().c_str ()); |
|
243 } |
5282
|
244 else if (arg.class_name () == "sparse") |
|
245 { |
|
246 const SparseMatrix m = arg.sparse_matrix_value (); |
|
247 |
|
248 if (error_state) |
|
249 return retval; |
|
250 |
|
251 if (can_ret_cmplx_for_real |
|
252 && (any_element_less_than (m, lower_limit) |
|
253 || any_element_greater_than (m, upper_limit))) |
|
254 { |
|
255 if (c_c_map_fcn) |
|
256 SPARSE_MAPPER_LOOP (SparseComplexMatrix, Complex, |
|
257 c_c_map_fcn, m); |
|
258 else |
|
259 error ("%s: unable to handle real arguments", |
|
260 name().c_str ()); |
|
261 } |
|
262 else if (d_d_map_fcn) |
|
263 SPARSE_MAPPER_LOOP (SparseMatrix, double, d_d_map_fcn, m); |
|
264 else if (d_b_map_fcn) |
|
265 SPARSE_MAPPER_LOOP (SparseBoolMatrix, bool, d_b_map_fcn, m); |
|
266 else |
|
267 error ("%s: unable to handle real arguments", |
|
268 name().c_str ()); |
|
269 } |
4104
|
270 else |
|
271 { |
4643
|
272 NDArray m = arg.array_value (); |
4104
|
273 |
|
274 if (error_state) |
|
275 return retval; |
|
276 |
|
277 if (can_ret_cmplx_for_real |
|
278 && (any_element_less_than (m, lower_limit) |
|
279 || any_element_greater_than (m, upper_limit))) |
|
280 { |
|
281 if (c_c_map_fcn) |
4643
|
282 MAPPER_LOOP (ComplexNDArray, c_c_map_fcn, m); |
4104
|
283 else |
|
284 error ("%s: unable to handle real arguments", |
|
285 name().c_str ()); |
|
286 } |
|
287 else if (d_d_map_fcn) |
4643
|
288 MAPPER_LOOP (NDArray, d_d_map_fcn, m); |
4104
|
289 else if (d_b_map_fcn) |
4643
|
290 MAPPER_LOOP (boolNDArray, d_b_map_fcn, m); |
4104
|
291 else |
|
292 error ("%s: unable to handle real arguments", |
|
293 name().c_str ()); |
|
294 } |
|
295 } |
|
296 else if (arg.is_complex_type ()) |
|
297 { |
|
298 if (arg.is_scalar_type ()) |
|
299 { |
|
300 Complex c = arg.complex_value (); |
|
301 |
|
302 if (d_c_map_fcn) |
|
303 retval = d_c_map_fcn (c); |
|
304 else if (c_c_map_fcn) |
|
305 retval = c_c_map_fcn (c); |
|
306 else if (c_b_map_fcn) |
|
307 retval = c_b_map_fcn (c); |
|
308 else |
|
309 error ("%s: unable to handle complex arguments", |
|
310 name().c_str ()); |
|
311 } |
5282
|
312 else if (arg.class_name () == "sparse") |
|
313 { |
|
314 SparseComplexMatrix cm = arg.sparse_complex_matrix_value (); |
|
315 |
|
316 if (error_state) |
|
317 return retval; |
|
318 |
|
319 if (d_c_map_fcn) |
|
320 SPARSE_MAPPER_LOOP (SparseMatrix, double, d_c_map_fcn, cm); |
|
321 else if (c_c_map_fcn) |
|
322 SPARSE_MAPPER_LOOP (SparseComplexMatrix, Complex, |
|
323 c_c_map_fcn, cm); |
|
324 else if (c_b_map_fcn) |
|
325 SPARSE_MAPPER_LOOP (SparseBoolMatrix, bool, |
|
326 c_b_map_fcn, cm); |
|
327 else |
|
328 error ("%s: unable to handle complex arguments", |
|
329 name().c_str ()); |
|
330 } |
4104
|
331 else |
|
332 { |
4643
|
333 ComplexNDArray cm = arg.complex_array_value (); |
4104
|
334 |
|
335 if (error_state) |
|
336 return retval; |
|
337 |
|
338 if (d_c_map_fcn) |
4643
|
339 MAPPER_LOOP (NDArray, d_c_map_fcn, cm); |
4104
|
340 else if (c_c_map_fcn) |
4643
|
341 MAPPER_LOOP (ComplexNDArray, c_c_map_fcn, cm); |
4104
|
342 else if (c_b_map_fcn) |
4643
|
343 MAPPER_LOOP (boolNDArray, c_b_map_fcn, cm); |
4104
|
344 else |
|
345 error ("%s: unable to handle complex arguments", |
|
346 name().c_str ()); |
|
347 } |
|
348 } |
|
349 else if (ch_map_fcn) |
2974
|
350 { |
|
351 // XXX FIXME XXX -- this could be done in a better way... |
|
352 |
|
353 octave_value tmp = arg.convert_to_str (); |
|
354 |
|
355 if (! error_state) |
|
356 { |
4643
|
357 charNDArray chm = tmp.char_array_value (); |
2974
|
358 |
|
359 if (! error_state) |
|
360 { |
4100
|
361 switch (ch_map_flag) |
2974
|
362 { |
|
363 case 0: |
4643
|
364 MAPPER_LOOP_1 (boolNDArray, ch_map_fcn, chm, bool); |
2974
|
365 break; |
|
366 |
|
367 case 1: |
4643
|
368 MAPPER_LOOP (NDArray, ch_map_fcn, chm); |
2974
|
369 break; |
|
370 |
|
371 case 2: |
4643
|
372 MAPPER_LOOP_2 (charNDArray, ch_map_fcn, chm, , |
3962
|
373 octave_value (result, true)); |
2974
|
374 break; |
|
375 |
|
376 default: |
|
377 panic_impossible (); |
|
378 break; |
|
379 } |
|
380 } |
|
381 } |
|
382 } |
|
383 else |
4104
|
384 gripe_wrong_type_arg ("mapper", arg); |
2974
|
385 |
|
386 return retval; |
|
387 } |
|
388 |
|
389 octave_value_list |
4247
|
390 octave_mapper::subsref (const std::string& type, |
4219
|
391 const std::list<octave_value_list>& idx, |
3933
|
392 int nargout) |
|
393 { |
|
394 octave_value_list retval; |
|
395 |
|
396 switch (type[0]) |
|
397 { |
|
398 case '(': |
5156
|
399 { |
|
400 int tmp_nargout = (type.length () > 1 && nargout == 0) ? 1 : nargout; |
|
401 |
|
402 retval = do_multi_index_op (tmp_nargout, idx.front ()); |
|
403 } |
3933
|
404 break; |
|
405 |
|
406 case '{': |
|
407 case '.': |
|
408 { |
|
409 std::string nm = type_name (); |
|
410 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
411 } |
|
412 break; |
|
413 |
|
414 default: |
|
415 panic_impossible (); |
|
416 } |
|
417 |
4994
|
418 // XXX FIXME XXX -- perhaps there should be an |
|
419 // octave_value_list::next_subsref member function? See also |
|
420 // and octave_builtin::subsref. |
|
421 |
|
422 if (idx.size () > 1) |
|
423 retval = retval(0).next_subsref (nargout, type, idx); |
5156
|
424 |
|
425 return retval; |
3933
|
426 } |
|
427 |
|
428 octave_value_list |
3544
|
429 octave_mapper::do_multi_index_op (int, const octave_value_list& args) |
2974
|
430 { |
|
431 octave_value retval; |
|
432 |
|
433 if (error_state) |
|
434 return retval; |
|
435 |
|
436 int nargin = args.length (); |
|
437 |
|
438 if (nargin > 1) |
|
439 ::error ("%s: too many arguments", name().c_str ()); |
|
440 else if (nargin < 1) |
|
441 ::error ("%s: too few arguments", name().c_str ()); |
|
442 else |
|
443 { |
|
444 if (args(0).is_defined ()) |
4748
|
445 { |
4987
|
446 unwind_protect::begin_frame ("mapper_func_eval"); |
|
447 |
4748
|
448 unwind_protect_ptr (curr_function); |
4975
|
449 unwind_protect_ptr (curr_caller_function); |
|
450 |
|
451 curr_caller_function = curr_function; |
4748
|
452 curr_function = this; |
|
453 |
|
454 retval = apply (args(0)); |
|
455 |
4987
|
456 unwind_protect::run_frame ("mapper_func_eval"); |
4748
|
457 } |
2974
|
458 else |
|
459 ::error ("%s: argument undefined", name().c_str ()); |
|
460 } |
|
461 |
|
462 return retval; |
|
463 } |
|
464 |
|
465 /* |
|
466 ;;; Local Variables: *** |
|
467 ;;; mode: C++ *** |
|
468 ;;; End: *** |
|
469 */ |