237
|
1 // kludge.cc -*- C++ -*- |
|
2 /* |
|
3 |
1011
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
237
|
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 |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
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 |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 // Nothing like a little CPP abuse to brighten everyone's day. Would |
|
25 // have been nice to do this with template functions but as of 2.5.x, |
|
26 // g++ seems to fail in various ways, either not resolving general |
459
|
27 // template functions, or not instantiating non-member template |
237
|
28 // functions. |
|
29 // |
|
30 // When templates work more reliably in g++, this will be replaced by |
|
31 // the MArray class. |
|
32 |
|
33 #define DO_VS_OP(OP) \ |
|
34 int l = a.length (); \ |
|
35 TYPE *result = 0; \ |
|
36 if (l > 0) \ |
|
37 { \ |
|
38 result = new TYPE [l]; \ |
|
39 const TYPE *x = a.data (); \ |
|
40 for (int i = 0; i < l; i++) \ |
|
41 result[i] = x[i] OP s; \ |
|
42 } |
|
43 |
|
44 #define DO_SV_OP(OP) \ |
|
45 int l = a.length (); \ |
|
46 TYPE *result = 0; \ |
|
47 if (l > 0) \ |
|
48 { \ |
|
49 result = new TYPE [l]; \ |
|
50 const TYPE *x = a.data (); \ |
|
51 for (int i = 0; i < l; i++) \ |
|
52 result[i] = s OP x[i]; \ |
|
53 } |
|
54 |
|
55 #define DO_VV_OP(OP) \ |
|
56 TYPE *result = 0; \ |
|
57 if (l > 0) \ |
|
58 { \ |
|
59 result = new TYPE [l]; \ |
|
60 const TYPE *x = a.data (); \ |
|
61 const TYPE *y = b.data (); \ |
|
62 for (int i = 0; i < l; i++) \ |
|
63 result[i] = x[i] OP y[i]; \ |
|
64 } |
|
65 |
|
66 #define NEG_V \ |
|
67 int l = a.length (); \ |
|
68 TYPE *result = 0; \ |
|
69 if (l > 0) \ |
|
70 { \ |
|
71 result = new TYPE [l]; \ |
|
72 const TYPE *x = a.data (); \ |
|
73 for (int i = 0; i < l; i++) \ |
|
74 result[i] = -x[i]; \ |
|
75 } |
|
76 |
|
77 #ifdef KLUDGE_VECTORS |
|
78 |
|
79 /* |
|
80 * Like type operations for vectors. |
|
81 */ |
|
82 |
|
83 // Element by element vector by scalar ops. |
|
84 |
1209
|
85 #define KL_VS_OP(OP) \ |
|
86 KL_VEC_TYPE \ |
|
87 operator OP (const KL_VEC_TYPE& a, const TYPE& s) \ |
|
88 { \ |
|
89 DO_VS_OP (OP); \ |
|
90 return KL_VEC_TYPE (result, l); \ |
|
91 } |
237
|
92 |
1209
|
93 KL_VS_OP (+) |
|
94 KL_VS_OP (-) |
|
95 KL_VS_OP (*) |
|
96 KL_VS_OP (/) |
237
|
97 |
|
98 // Element by element scalar by vector ops. |
|
99 |
1209
|
100 #define KL_SV_OP(OP) \ |
|
101 KL_VEC_TYPE \ |
|
102 operator OP (const TYPE& s, const KL_VEC_TYPE& a) \ |
|
103 { \ |
|
104 DO_SV_OP (OP); \ |
|
105 return KL_VEC_TYPE (result, l); \ |
|
106 } |
237
|
107 |
1209
|
108 KL_SV_OP (+) |
|
109 KL_SV_OP (-) |
|
110 KL_SV_OP (*) |
|
111 KL_SV_OP (/) |
237
|
112 |
|
113 // Element by element vector by vector ops. |
|
114 |
1209
|
115 #define KL_VV_OP(FCN, OP, OP_STR) \ |
|
116 KL_VEC_TYPE \ |
|
117 FCN (const KL_VEC_TYPE& a, const KL_VEC_TYPE& b) \ |
|
118 { \ |
|
119 int l = a.length (); \ |
|
120 if (l != b.length ()) \ |
|
121 { \ |
|
122 (*current_liboctave_error_handler) \ |
|
123 ("nonconformant array " OP_STR " attempted"); \ |
|
124 return KL_VEC_TYPE (); \ |
|
125 } \ |
|
126 if (l == 0) \ |
|
127 return KL_VEC_TYPE (); \ |
|
128 DO_VV_OP (OP); \ |
|
129 return KL_VEC_TYPE (result, l); \ |
|
130 } |
237
|
131 |
1209
|
132 KL_VV_OP(operator +, +, "addition") |
|
133 KL_VV_OP(operator -, -, "subtraction") |
|
134 KL_VV_OP(product, *, "product") |
|
135 KL_VV_OP(quotient, /, "quotient") |
237
|
136 |
|
137 // Unary MArray ops. |
|
138 |
|
139 KL_VEC_TYPE |
|
140 operator - (const KL_VEC_TYPE& a) |
|
141 { |
|
142 NEG_V; |
|
143 return KL_VEC_TYPE (result, l); |
|
144 } |
|
145 |
|
146 #endif |
|
147 |
|
148 #ifdef KLUDGE_MATRICES |
|
149 |
|
150 /* |
|
151 * Like type operations for matrices |
|
152 */ |
|
153 |
|
154 // Element by element matrix by scalar ops. |
|
155 |
1209
|
156 #define KL_MS_OP(OP) \ |
|
157 KL_MAT_TYPE \ |
|
158 operator OP (const KL_MAT_TYPE& a, const TYPE& s) \ |
|
159 { \ |
|
160 DO_VS_OP (OP); \ |
|
161 return KL_MAT_TYPE (result, a.rows (), a.cols ()); \ |
|
162 } |
237
|
163 |
1209
|
164 KL_MS_OP(+) |
|
165 KL_MS_OP(-) |
|
166 KL_MS_OP(*) |
|
167 KL_MS_OP(/) |
237
|
168 |
|
169 // Element by element scalar by matrix ops. |
|
170 |
1209
|
171 #define KL_SM_OP(OP) \ |
|
172 KL_MAT_TYPE \ |
|
173 operator OP (const TYPE& s, const KL_MAT_TYPE& a) \ |
|
174 { \ |
|
175 DO_SV_OP (OP); \ |
|
176 return KL_MAT_TYPE (result, a.rows (), a.cols ()); \ |
|
177 } |
237
|
178 |
1209
|
179 KL_SM_OP(+) |
|
180 KL_SM_OP(-) |
|
181 KL_SM_OP(*) |
|
182 KL_SM_OP(/) |
237
|
183 |
|
184 // Element by element matrix by matrix ops. |
|
185 |
1209
|
186 #define KL_MM_OP(FCN, OP, OP_STR) \ |
|
187 KL_MAT_TYPE \ |
|
188 FCN (const KL_MAT_TYPE& a, const KL_MAT_TYPE& b) \ |
|
189 { \ |
|
190 int r = a.rows (); \ |
|
191 int c = a.cols (); \ |
|
192 if (r != b.rows () || c != b.cols ()) \ |
|
193 { \ |
|
194 (*current_liboctave_error_handler) \ |
|
195 ("nonconformant array " OP_STR " attempted"); \ |
|
196 return KL_MAT_TYPE (); \ |
|
197 } \ |
|
198 if (r == 0 || c == 0) \ |
|
199 return KL_MAT_TYPE (r, c); \ |
|
200 int l = a.length (); \ |
|
201 DO_VV_OP (+); \ |
|
202 return KL_MAT_TYPE (result, r, c); \ |
|
203 } |
237
|
204 |
1209
|
205 KL_MM_OP (operator +, +, "addition") |
|
206 KL_MM_OP (operator -, -, "subtraction") |
|
207 KL_MM_OP (product, *, "product") |
|
208 KL_MM_OP (quotient, /, "quotient") |
237
|
209 |
|
210 // Unary matrix ops. |
|
211 |
|
212 KL_MAT_TYPE |
|
213 operator - (const KL_MAT_TYPE& a) |
|
214 { |
|
215 NEG_V; |
|
216 return KL_MAT_TYPE (result, a.rows (), a.cols ()); |
|
217 } |
|
218 |
|
219 #endif |
|
220 |
|
221 #ifdef KLUDGE_DIAG_MATRICES |
|
222 |
|
223 /* |
|
224 * Like type operations for diagonal matrices. |
|
225 */ |
|
226 |
|
227 // Element by element MDiagArray by scalar ops. |
|
228 |
1209
|
229 #define KL_DMS_OP(OP) \ |
|
230 KL_DMAT_TYPE \ |
|
231 operator OP (const KL_DMAT_TYPE& a, const TYPE& s) \ |
|
232 { \ |
|
233 DO_VS_OP (OP); \ |
|
234 return KL_DMAT_TYPE (result, a.rows (), a.cols ()); \ |
|
235 } |
237
|
236 |
1209
|
237 KL_DMS_OP (*) |
|
238 KL_DMS_OP (/) |
237
|
239 |
|
240 // Element by element scalar by MDiagArray ops. |
|
241 |
1209
|
242 #define KL_SDM_OP(OP) \ |
|
243 KL_DMAT_TYPE \ |
|
244 operator OP (const TYPE& s, const KL_DMAT_TYPE& a) \ |
|
245 { \ |
|
246 DO_SV_OP (OP); \ |
|
247 return KL_DMAT_TYPE (result, a.rows (), a.cols ()); \ |
|
248 } |
|
249 |
|
250 KL_SDM_OP (*) |
237
|
251 |
|
252 // Element by element MDiagArray by MDiagArray ops. |
|
253 |
1209
|
254 #define KL_DMDM_OP(FCN, OP, OP_STR) \ |
|
255 KL_DMAT_TYPE \ |
|
256 FCN (const KL_DMAT_TYPE& a, const KL_DMAT_TYPE& b) \ |
|
257 { \ |
|
258 int r = a.rows (); \ |
|
259 int c = a.cols (); \ |
|
260 if (r != b.rows () || c != b.cols ()) \ |
|
261 { \ |
|
262 (*current_liboctave_error_handler) \ |
|
263 ("nonconformant diagonal array " OP_STR " attempted"); \ |
|
264 return KL_DMAT_TYPE (); \ |
|
265 } \ |
|
266 if (c == 0 || r == 0) \ |
|
267 return KL_DMAT_TYPE (); \ |
|
268 int l = a.length (); \ |
|
269 DO_VV_OP (OP); \ |
|
270 return KL_DMAT_TYPE (result, r, c); \ |
|
271 } |
237
|
272 |
1209
|
273 KL_DMDM_OP (operator +, +, "addition") |
|
274 KL_DMDM_OP (operator -, -, "subtraction") |
|
275 KL_DMDM_OP (product, *, "product") |
237
|
276 |
|
277 // Unary MDiagArray ops. |
|
278 |
|
279 KL_DMAT_TYPE |
|
280 operator - (const KL_DMAT_TYPE& a) |
|
281 { |
|
282 NEG_V; |
|
283 return KL_DMAT_TYPE (result, a.rows (), a.cols ()); |
|
284 } |
|
285 |
|
286 #endif |
|
287 |
1210
|
288 #undef DO_VS_OP |
237
|
289 #undef DO_SV_OP |
|
290 #undef DO_VV_OP |
|
291 #undef NEG_V |
1210
|
292 #undef KL_VS_OP |
|
293 #undef KL_SV_OP |
|
294 #undef KL_VV_OP |
|
295 #undef KL_MS_OP |
|
296 #undef KL_SM_OP |
|
297 #undef KL_MM_OP |
|
298 #undef KL_DMS_OP |
|
299 #undef KL_SDM_OP |
|
300 #undef KL_DMDM_OP |
237
|
301 |
|
302 /* |
|
303 ;;; Local Variables: *** |
|
304 ;;; mode: C++ *** |
|
305 ;;; page-delimiter: "^/\\*" *** |
|
306 ;;; End: *** |
|
307 */ |