2974
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, |
7344
|
4 2006, 2007, 2008 John W. Eaton |
2974
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
2974
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
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; \ |
7343
|
162 /* Use data instead of elem for better performance. */ \ |
|
163 result.data (M.ridx (i) + j * nr) = CONV (F (M.data(i))); \ |
5282
|
164 \ |
|
165 if (error_state) \ |
|
166 return retval; \ |
|
167 } \ |
|
168 \ |
|
169 result.maybe_compress (true); \ |
|
170 retval = R; \ |
|
171 } \ |
|
172 else \ |
|
173 { \ |
5648
|
174 octave_idx_type nz = M.nnz (); \ |
5282
|
175 octave_idx_type nr = M.rows (); \ |
|
176 octave_idx_type nc = M.cols (); \ |
|
177 \ |
5648
|
178 T result (nr, nc, nz); \ |
5282
|
179 ET zero = ET (0.); \ |
|
180 octave_idx_type ii = 0; \ |
|
181 result.cidx (ii) = 0; \ |
|
182 \ |
|
183 for (octave_idx_type j = 0; j < nc; j++) \ |
|
184 { \ |
|
185 for (octave_idx_type i = M.cidx(j); i < M.cidx (j+1); i++) \ |
|
186 { \ |
|
187 ET val = CONV (F (M.data (i))); \ |
|
188 if (val != zero) \ |
|
189 { \ |
|
190 result.data (ii) = val; \ |
|
191 result.ridx (ii++) = M.ridx (i); \ |
|
192 } \ |
|
193 OCTAVE_QUIT; \ |
|
194 \ |
|
195 if (error_state) \ |
|
196 return retval; \ |
|
197 } \ |
|
198 result.cidx (j+1) = ii; \ |
|
199 } \ |
|
200 \ |
|
201 result.maybe_compress (false); \ |
|
202 retval = R; \ |
|
203 } \ |
|
204 } \ |
|
205 while (0) |
|
206 |
|
207 #define SPARSE_MAPPER_LOOP_1(T, ET, F, M, CONV) \ |
|
208 SPARSE_MAPPER_LOOP_2 (T, ET, F, M, CONV, result) |
|
209 |
|
210 #define SPARSE_MAPPER_LOOP(T, ET, F, M) \ |
|
211 SPARSE_MAPPER_LOOP_1 (T, ET, F, M, ) |
|
212 |
2974
|
213 octave_value |
|
214 octave_mapper::apply (const octave_value& arg) const |
|
215 { |
|
216 octave_value retval; |
|
217 |
5775
|
218 // FIXME -- is_real_type can return true. Should it really |
4452
|
219 // work that way? |
4123
|
220 |
5124
|
221 if (arg.is_real_type () |
|
222 && (c_c_map_fcn || d_d_map_fcn || d_b_map_fcn) |
|
223 && ! (arg.is_string () && ch_map_fcn)) |
4104
|
224 { |
|
225 if (arg.is_scalar_type ()) |
|
226 { |
|
227 double d = arg.double_value (); |
|
228 |
|
229 if (can_ret_cmplx_for_real && (d < lower_limit || d > upper_limit)) |
|
230 { |
|
231 if (c_c_map_fcn) |
|
232 retval = c_c_map_fcn (Complex (d)); |
|
233 else |
|
234 error ("%s: unable to handle real arguments", |
|
235 name().c_str ()); |
|
236 } |
|
237 else if (d_d_map_fcn) |
|
238 retval = d_d_map_fcn (d); |
|
239 else if (d_b_map_fcn) |
|
240 retval = d_b_map_fcn (d); |
|
241 else |
|
242 error ("%s: unable to handle real arguments", |
|
243 name().c_str ()); |
|
244 } |
6823
|
245 else if (arg.is_sparse_type ()) |
5282
|
246 { |
|
247 const SparseMatrix m = arg.sparse_matrix_value (); |
|
248 |
|
249 if (error_state) |
|
250 return retval; |
|
251 |
|
252 if (can_ret_cmplx_for_real |
|
253 && (any_element_less_than (m, lower_limit) |
|
254 || any_element_greater_than (m, upper_limit))) |
|
255 { |
|
256 if (c_c_map_fcn) |
|
257 SPARSE_MAPPER_LOOP (SparseComplexMatrix, Complex, |
|
258 c_c_map_fcn, m); |
|
259 else |
|
260 error ("%s: unable to handle real arguments", |
|
261 name().c_str ()); |
|
262 } |
|
263 else if (d_d_map_fcn) |
|
264 SPARSE_MAPPER_LOOP (SparseMatrix, double, d_d_map_fcn, m); |
|
265 else if (d_b_map_fcn) |
|
266 SPARSE_MAPPER_LOOP (SparseBoolMatrix, bool, d_b_map_fcn, m); |
|
267 else |
|
268 error ("%s: unable to handle real arguments", |
|
269 name().c_str ()); |
|
270 } |
4104
|
271 else |
|
272 { |
4643
|
273 NDArray m = arg.array_value (); |
4104
|
274 |
|
275 if (error_state) |
|
276 return retval; |
|
277 |
|
278 if (can_ret_cmplx_for_real |
|
279 && (any_element_less_than (m, lower_limit) |
|
280 || any_element_greater_than (m, upper_limit))) |
|
281 { |
|
282 if (c_c_map_fcn) |
4643
|
283 MAPPER_LOOP (ComplexNDArray, c_c_map_fcn, m); |
4104
|
284 else |
|
285 error ("%s: unable to handle real arguments", |
|
286 name().c_str ()); |
|
287 } |
|
288 else if (d_d_map_fcn) |
4643
|
289 MAPPER_LOOP (NDArray, d_d_map_fcn, m); |
4104
|
290 else if (d_b_map_fcn) |
4643
|
291 MAPPER_LOOP (boolNDArray, d_b_map_fcn, m); |
4104
|
292 else |
|
293 error ("%s: unable to handle real arguments", |
|
294 name().c_str ()); |
|
295 } |
|
296 } |
|
297 else if (arg.is_complex_type ()) |
|
298 { |
6989
|
299 // In the following, we use d_d_map_fcn to handle the case of |
|
300 // imag (z) == 0. This can happen when a complex value is not |
|
301 // narrowed to a real value automatically, possibly due to some |
|
302 // imaginary parts being -0. |
|
303 |
4104
|
304 if (arg.is_scalar_type ()) |
|
305 { |
|
306 Complex c = arg.complex_value (); |
|
307 |
6989
|
308 if (c_d_map_fcn) |
|
309 retval = c_d_map_fcn (c); |
4104
|
310 else if (c_c_map_fcn) |
|
311 retval = c_c_map_fcn (c); |
|
312 else if (c_b_map_fcn) |
|
313 retval = c_b_map_fcn (c); |
6989
|
314 else if (d_d_map_fcn && imag (c) == 0) |
|
315 retval = d_d_map_fcn (real (c)); |
4104
|
316 else |
|
317 error ("%s: unable to handle complex arguments", |
|
318 name().c_str ()); |
|
319 } |
6823
|
320 else if (arg.is_sparse_type ()) |
5282
|
321 { |
|
322 SparseComplexMatrix cm = arg.sparse_complex_matrix_value (); |
|
323 |
|
324 if (error_state) |
|
325 return retval; |
|
326 |
6989
|
327 if (c_d_map_fcn) |
|
328 SPARSE_MAPPER_LOOP (SparseMatrix, double, c_d_map_fcn, cm); |
5282
|
329 else if (c_c_map_fcn) |
|
330 SPARSE_MAPPER_LOOP (SparseComplexMatrix, Complex, |
|
331 c_c_map_fcn, cm); |
|
332 else if (c_b_map_fcn) |
|
333 SPARSE_MAPPER_LOOP (SparseBoolMatrix, bool, |
|
334 c_b_map_fcn, cm); |
|
335 else |
6989
|
336 { |
|
337 SparseMatrix im = imag (cm); |
|
338 |
|
339 if (d_d_map_fcn && im.all_elements_are_zero ()) |
|
340 SPARSE_MAPPER_LOOP (SparseMatrix, double, d_d_map_fcn, real (cm)); |
|
341 else |
|
342 error ("%s: unable to handle complex arguments", |
|
343 name().c_str ()); |
|
344 } |
5282
|
345 } |
4104
|
346 else |
|
347 { |
4643
|
348 ComplexNDArray cm = arg.complex_array_value (); |
4104
|
349 |
|
350 if (error_state) |
|
351 return retval; |
|
352 |
6989
|
353 if (c_d_map_fcn) |
|
354 MAPPER_LOOP (NDArray, c_d_map_fcn, cm); |
4104
|
355 else if (c_c_map_fcn) |
4643
|
356 MAPPER_LOOP (ComplexNDArray, c_c_map_fcn, cm); |
4104
|
357 else if (c_b_map_fcn) |
4643
|
358 MAPPER_LOOP (boolNDArray, c_b_map_fcn, cm); |
4104
|
359 else |
6989
|
360 { |
|
361 NDArray im = imag (cm); |
|
362 |
|
363 if (d_d_map_fcn && im.all_elements_are_zero ()) |
|
364 MAPPER_LOOP (NDArray, d_d_map_fcn, real (cm)); |
|
365 else |
|
366 error ("%s: unable to handle complex arguments", |
|
367 name().c_str ()); |
|
368 } |
4104
|
369 } |
|
370 } |
|
371 else if (ch_map_fcn) |
2974
|
372 { |
5775
|
373 // FIXME -- this could be done in a better way... |
2974
|
374 |
|
375 octave_value tmp = arg.convert_to_str (); |
|
376 |
|
377 if (! error_state) |
|
378 { |
4643
|
379 charNDArray chm = tmp.char_array_value (); |
2974
|
380 |
|
381 if (! error_state) |
|
382 { |
4100
|
383 switch (ch_map_flag) |
2974
|
384 { |
|
385 case 0: |
4643
|
386 MAPPER_LOOP_1 (boolNDArray, ch_map_fcn, chm, bool); |
2974
|
387 break; |
|
388 |
|
389 case 1: |
4643
|
390 MAPPER_LOOP (NDArray, ch_map_fcn, chm); |
2974
|
391 break; |
|
392 |
|
393 case 2: |
4643
|
394 MAPPER_LOOP_2 (charNDArray, ch_map_fcn, chm, , |
3962
|
395 octave_value (result, true)); |
2974
|
396 break; |
|
397 |
|
398 default: |
|
399 panic_impossible (); |
|
400 break; |
|
401 } |
|
402 } |
|
403 } |
|
404 } |
|
405 else |
4104
|
406 gripe_wrong_type_arg ("mapper", arg); |
2974
|
407 |
|
408 return retval; |
|
409 } |
|
410 |
|
411 octave_value_list |
4247
|
412 octave_mapper::subsref (const std::string& type, |
4219
|
413 const std::list<octave_value_list>& idx, |
3933
|
414 int nargout) |
|
415 { |
|
416 octave_value_list retval; |
|
417 |
|
418 switch (type[0]) |
|
419 { |
|
420 case '(': |
5156
|
421 { |
|
422 int tmp_nargout = (type.length () > 1 && nargout == 0) ? 1 : nargout; |
|
423 |
|
424 retval = do_multi_index_op (tmp_nargout, idx.front ()); |
|
425 } |
3933
|
426 break; |
|
427 |
|
428 case '{': |
|
429 case '.': |
|
430 { |
|
431 std::string nm = type_name (); |
|
432 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
433 } |
|
434 break; |
|
435 |
|
436 default: |
|
437 panic_impossible (); |
|
438 } |
|
439 |
5775
|
440 // FIXME -- perhaps there should be an |
4994
|
441 // octave_value_list::next_subsref member function? See also |
|
442 // and octave_builtin::subsref. |
|
443 |
|
444 if (idx.size () > 1) |
|
445 retval = retval(0).next_subsref (nargout, type, idx); |
5156
|
446 |
|
447 return retval; |
3933
|
448 } |
|
449 |
|
450 octave_value_list |
3544
|
451 octave_mapper::do_multi_index_op (int, const octave_value_list& args) |
2974
|
452 { |
|
453 octave_value retval; |
|
454 |
|
455 if (error_state) |
|
456 return retval; |
|
457 |
|
458 int nargin = args.length (); |
|
459 |
|
460 if (nargin > 1) |
|
461 ::error ("%s: too many arguments", name().c_str ()); |
|
462 else if (nargin < 1) |
|
463 ::error ("%s: too few arguments", name().c_str ()); |
|
464 else |
|
465 { |
|
466 if (args(0).is_defined ()) |
4748
|
467 { |
4987
|
468 unwind_protect::begin_frame ("mapper_func_eval"); |
|
469 |
5743
|
470 octave_call_stack::push (this); |
4975
|
471 |
5743
|
472 unwind_protect::add (octave_call_stack::unwind_pop, 0); |
4748
|
473 |
|
474 retval = apply (args(0)); |
|
475 |
4987
|
476 unwind_protect::run_frame ("mapper_func_eval"); |
4748
|
477 } |
2974
|
478 else |
|
479 ::error ("%s: argument undefined", name().c_str ()); |
|
480 } |
|
481 |
|
482 return retval; |
|
483 } |
|
484 |
|
485 /* |
|
486 ;;; Local Variables: *** |
|
487 ;;; mode: C++ *** |
|
488 ;;; End: *** |
|
489 */ |