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 |
|
70 octave_value |
|
71 octave_mapper::apply (const octave_value& arg) const |
|
72 { |
|
73 octave_value retval; |
|
74 |
|
75 if (ch_map_fcn) |
|
76 { |
|
77 // XXX FIXME XXX -- this could be done in a better way... |
|
78 |
|
79 octave_value tmp = arg.convert_to_str (); |
|
80 |
|
81 if (! error_state) |
|
82 { |
|
83 charMatrix chm = tmp.char_matrix_value (); |
|
84 |
|
85 if (! error_state) |
|
86 { |
|
87 int nr = chm.rows (); |
|
88 int nc = chm.cols (); |
|
89 |
|
90 switch (flag) |
|
91 { |
|
92 case 0: |
|
93 { |
|
94 Matrix result (nr, nc); |
|
95 |
|
96 // islapha and friends can return any nonzero value |
|
97 // to mean true, but we want to return 1 or 0 only. |
|
98 |
|
99 for (int j = 0; j < nc; j++) |
|
100 for (int i = 0; i < nr; i++) |
|
101 result (i, j) = ch_map_fcn (chm (i, j)) ? 1 : 0; |
|
102 |
|
103 retval = result; |
|
104 } |
|
105 break; |
|
106 |
|
107 case 1: |
|
108 { |
|
109 Matrix result (nr, nc); |
|
110 |
|
111 for (int j = 0; j < nc; j++) |
|
112 for (int i = 0; i < nr; i++) |
|
113 result (i, j) = ch_map_fcn (chm (i, j)); |
|
114 |
|
115 retval = result; |
|
116 } |
|
117 break; |
|
118 |
|
119 case 2: |
|
120 { |
|
121 charMatrix result (nr, nc); |
|
122 |
|
123 for (int j = 0; j < nc; j++) |
|
124 for (int i = 0; i < nr; i++) |
|
125 result (i, j) = ch_map_fcn (chm (i, j)); |
|
126 |
|
127 retval = octave_value (result, true); |
|
128 } |
|
129 break; |
|
130 |
|
131 default: |
|
132 panic_impossible (); |
|
133 break; |
|
134 } |
|
135 } |
|
136 } |
|
137 } |
|
138 else |
|
139 { |
|
140 if (arg.is_real_type ()) |
|
141 { |
|
142 if (arg.is_scalar_type ()) |
|
143 { |
|
144 double d = arg.double_value (); |
|
145 |
|
146 if (flag && (d < lower_limit || d > upper_limit)) |
|
147 { |
|
148 if (c_c_map_fcn) |
|
149 retval = c_c_map_fcn (Complex (d)); |
|
150 else |
|
151 error ("%s: unable to handle real arguments", |
|
152 name().c_str ()); |
|
153 } |
|
154 else if (d_d_map_fcn) |
|
155 retval = d_d_map_fcn (d); |
|
156 else |
|
157 error ("%s: unable to handle real arguments", |
|
158 name().c_str ()); |
|
159 } |
|
160 else |
|
161 { |
|
162 Matrix m = arg.matrix_value (); |
|
163 |
|
164 if (error_state) |
|
165 return retval; |
|
166 |
|
167 if (flag |
|
168 && (any_element_less_than (m, lower_limit) |
|
169 || any_element_greater_than (m, upper_limit))) |
|
170 { |
|
171 if (c_c_map_fcn) |
|
172 { |
|
173 ComplexMatrix cm (m); |
|
174 retval = cm.map (c_c_map_fcn); |
|
175 } |
|
176 else |
|
177 error ("%s: unable to handle real arguments", |
|
178 name().c_str ()); |
|
179 } |
|
180 else if (d_d_map_fcn) |
|
181 retval = m.map (d_d_map_fcn); |
|
182 else |
|
183 error ("%s: unable to handle real arguments", |
|
184 name().c_str ()); |
|
185 } |
|
186 } |
|
187 else if (arg.is_complex_type ()) |
|
188 { |
|
189 if (arg.is_scalar_type ()) |
|
190 { |
|
191 Complex c = arg.complex_value (); |
|
192 |
|
193 if (d_c_map_fcn) |
|
194 retval = d_c_map_fcn (c); |
|
195 else if (c_c_map_fcn) |
|
196 retval = c_c_map_fcn (c); |
|
197 else |
|
198 error ("%s: unable to handle complex arguments", |
|
199 name().c_str ()); |
|
200 } |
|
201 else |
|
202 { |
|
203 ComplexMatrix cm = arg.complex_matrix_value (); |
|
204 |
|
205 if (error_state) |
|
206 return retval; |
|
207 |
|
208 if (d_c_map_fcn) |
|
209 retval = cm.map (d_c_map_fcn); |
|
210 else if (c_c_map_fcn) |
|
211 retval = cm.map (c_c_map_fcn); |
|
212 else |
|
213 error ("%s: unable to handle complex arguments", |
|
214 name().c_str ()); |
|
215 } |
|
216 } |
|
217 else |
|
218 gripe_wrong_type_arg ("mapper", arg); |
|
219 } |
|
220 |
|
221 return retval; |
|
222 } |
|
223 |
|
224 octave_value_list |
|
225 octave_mapper::do_index_op (int, const octave_value_list& args) |
|
226 { |
|
227 octave_value retval; |
|
228 |
|
229 if (error_state) |
|
230 return retval; |
|
231 |
|
232 int nargin = args.length (); |
|
233 |
|
234 if (nargin > 1) |
|
235 ::error ("%s: too many arguments", name().c_str ()); |
|
236 else if (nargin < 1) |
|
237 ::error ("%s: too few arguments", name().c_str ()); |
|
238 else |
|
239 { |
|
240 if (args(0).is_defined ()) |
|
241 retval = apply (args(0)); |
|
242 else |
|
243 ::error ("%s: argument undefined", name().c_str ()); |
|
244 } |
|
245 |
|
246 return retval; |
|
247 } |
|
248 |
|
249 /* |
|
250 ;;; Local Variables: *** |
|
251 ;;; mode: C++ *** |
|
252 ;;; End: *** |
|
253 */ |