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 |
|
37 octave_allocator |
|
38 octave_mapper::allocator (sizeof (octave_mapper)); |
|
39 |
|
40 int |
|
41 octave_mapper::t_id (-1); |
|
42 |
|
43 const string |
|
44 octave_mapper::t_name ("built-in mapper function"); |
|
45 |
|
46 static bool |
|
47 any_element_less_than (const Matrix& a, double val) |
|
48 { |
|
49 int nr = a.rows (); |
|
50 int nc = a.columns (); |
|
51 |
|
52 for (int j = 0; j < nc; j++) |
|
53 for (int i = 0; i < nr; i++) |
|
54 if (a (i, j) < val) |
|
55 return true; |
|
56 |
|
57 return false; |
|
58 } |
|
59 |
|
60 static bool |
|
61 any_element_greater_than (const Matrix& a, double val) |
|
62 { |
|
63 int nr = a.rows (); |
|
64 int nc = a.columns (); |
|
65 |
|
66 for (int j = 0; j < nc; j++) |
|
67 for (int i = 0; i < nr; i++) |
|
68 if (a (i, j) > val) |
|
69 return true; |
|
70 |
|
71 return false; |
|
72 } |
|
73 |
|
74 octave_value |
|
75 octave_mapper::apply (const octave_value& arg) const |
|
76 { |
|
77 octave_value retval; |
|
78 |
|
79 if (ch_map_fcn) |
|
80 { |
|
81 // XXX FIXME XXX -- this could be done in a better way... |
|
82 |
|
83 octave_value tmp = arg.convert_to_str (); |
|
84 |
|
85 if (! error_state) |
|
86 { |
|
87 charMatrix chm = tmp.char_matrix_value (); |
|
88 |
|
89 if (! error_state) |
|
90 { |
|
91 int nr = chm.rows (); |
|
92 int nc = chm.cols (); |
|
93 |
|
94 switch (flag) |
|
95 { |
|
96 case 0: |
|
97 { |
|
98 Matrix result (nr, nc); |
|
99 |
|
100 // islapha and friends can return any nonzero value |
|
101 // to mean true, but we want to return 1 or 0 only. |
|
102 |
|
103 for (int j = 0; j < nc; j++) |
|
104 for (int i = 0; i < nr; i++) |
|
105 result (i, j) = ch_map_fcn (chm (i, j)) ? 1 : 0; |
|
106 |
|
107 retval = result; |
|
108 } |
|
109 break; |
|
110 |
|
111 case 1: |
|
112 { |
|
113 Matrix result (nr, nc); |
|
114 |
|
115 for (int j = 0; j < nc; j++) |
|
116 for (int i = 0; i < nr; i++) |
|
117 result (i, j) = ch_map_fcn (chm (i, j)); |
|
118 |
|
119 retval = result; |
|
120 } |
|
121 break; |
|
122 |
|
123 case 2: |
|
124 { |
|
125 charMatrix result (nr, nc); |
|
126 |
|
127 for (int j = 0; j < nc; j++) |
|
128 for (int i = 0; i < nr; i++) |
|
129 result (i, j) = ch_map_fcn (chm (i, j)); |
|
130 |
|
131 retval = octave_value (result, true); |
|
132 } |
|
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); |
|
160 else |
|
161 error ("%s: unable to handle real arguments", |
|
162 name().c_str ()); |
|
163 } |
|
164 else |
|
165 { |
|
166 Matrix m = arg.matrix_value (); |
|
167 |
|
168 if (error_state) |
|
169 return retval; |
|
170 |
|
171 if (flag |
|
172 && (any_element_less_than (m, lower_limit) |
|
173 || any_element_greater_than (m, upper_limit))) |
|
174 { |
|
175 if (c_c_map_fcn) |
|
176 { |
|
177 ComplexMatrix cm (m); |
|
178 retval = cm.map (c_c_map_fcn); |
|
179 } |
|
180 else |
|
181 error ("%s: unable to handle real arguments", |
|
182 name().c_str ()); |
|
183 } |
|
184 else if (d_d_map_fcn) |
|
185 retval = m.map (d_d_map_fcn); |
|
186 else |
|
187 error ("%s: unable to handle real arguments", |
|
188 name().c_str ()); |
|
189 } |
|
190 } |
|
191 else if (arg.is_complex_type ()) |
|
192 { |
|
193 if (arg.is_scalar_type ()) |
|
194 { |
|
195 Complex c = arg.complex_value (); |
|
196 |
|
197 if (d_c_map_fcn) |
|
198 retval = d_c_map_fcn (c); |
|
199 else if (c_c_map_fcn) |
|
200 retval = c_c_map_fcn (c); |
|
201 else |
|
202 error ("%s: unable to handle complex arguments", |
|
203 name().c_str ()); |
|
204 } |
|
205 else |
|
206 { |
|
207 ComplexMatrix cm = arg.complex_matrix_value (); |
|
208 |
|
209 if (error_state) |
|
210 return retval; |
|
211 |
|
212 if (d_c_map_fcn) |
|
213 retval = cm.map (d_c_map_fcn); |
|
214 else if (c_c_map_fcn) |
|
215 retval = cm.map (c_c_map_fcn); |
|
216 else |
|
217 error ("%s: unable to handle complex arguments", |
|
218 name().c_str ()); |
|
219 } |
|
220 } |
|
221 else |
|
222 gripe_wrong_type_arg ("mapper", arg); |
|
223 } |
|
224 |
|
225 return retval; |
|
226 } |
|
227 |
|
228 octave_value_list |
|
229 octave_mapper::do_index_op (int, const octave_value_list& args) |
|
230 { |
|
231 octave_value retval; |
|
232 |
|
233 if (error_state) |
|
234 return retval; |
|
235 |
|
236 int nargin = args.length (); |
|
237 |
|
238 if (nargin > 1) |
|
239 ::error ("%s: too many arguments", name().c_str ()); |
|
240 else if (nargin < 1) |
|
241 ::error ("%s: too few arguments", name().c_str ()); |
|
242 else |
|
243 { |
|
244 if (args(0).is_defined ()) |
|
245 retval = apply (args(0)); |
|
246 else |
|
247 ::error ("%s: argument undefined", name().c_str ()); |
|
248 } |
|
249 |
|
250 return retval; |
|
251 } |
|
252 |
|
253 /* |
|
254 ;;; Local Variables: *** |
|
255 ;;; mode: C++ *** |
|
256 ;;; End: *** |
|
257 */ |