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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
237
|
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 |
1360
|
79 // Like type operations for vectors. |
237
|
80 |
|
81 // Element by element vector by scalar ops. |
|
82 |
1209
|
83 #define KL_VS_OP(OP) \ |
|
84 KL_VEC_TYPE \ |
|
85 operator OP (const KL_VEC_TYPE& a, const TYPE& s) \ |
|
86 { \ |
|
87 DO_VS_OP (OP); \ |
|
88 return KL_VEC_TYPE (result, l); \ |
|
89 } |
237
|
90 |
1209
|
91 KL_VS_OP (+) |
|
92 KL_VS_OP (-) |
|
93 KL_VS_OP (*) |
|
94 KL_VS_OP (/) |
237
|
95 |
|
96 // Element by element scalar by vector ops. |
|
97 |
1209
|
98 #define KL_SV_OP(OP) \ |
|
99 KL_VEC_TYPE \ |
|
100 operator OP (const TYPE& s, const KL_VEC_TYPE& a) \ |
|
101 { \ |
|
102 DO_SV_OP (OP); \ |
|
103 return KL_VEC_TYPE (result, l); \ |
|
104 } |
237
|
105 |
1209
|
106 KL_SV_OP (+) |
|
107 KL_SV_OP (-) |
|
108 KL_SV_OP (*) |
|
109 KL_SV_OP (/) |
237
|
110 |
|
111 // Element by element vector by vector ops. |
|
112 |
1209
|
113 #define KL_VV_OP(FCN, OP, OP_STR) \ |
|
114 KL_VEC_TYPE \ |
|
115 FCN (const KL_VEC_TYPE& a, const KL_VEC_TYPE& b) \ |
|
116 { \ |
|
117 int l = a.length (); \ |
|
118 if (l != b.length ()) \ |
|
119 { \ |
|
120 (*current_liboctave_error_handler) \ |
|
121 ("nonconformant array " OP_STR " attempted"); \ |
|
122 return KL_VEC_TYPE (); \ |
|
123 } \ |
|
124 if (l == 0) \ |
|
125 return KL_VEC_TYPE (); \ |
|
126 DO_VV_OP (OP); \ |
|
127 return KL_VEC_TYPE (result, l); \ |
|
128 } |
237
|
129 |
1209
|
130 KL_VV_OP(operator +, +, "addition") |
|
131 KL_VV_OP(operator -, -, "subtraction") |
|
132 KL_VV_OP(product, *, "product") |
|
133 KL_VV_OP(quotient, /, "quotient") |
237
|
134 |
|
135 // Unary MArray ops. |
|
136 |
|
137 KL_VEC_TYPE |
|
138 operator - (const KL_VEC_TYPE& a) |
|
139 { |
|
140 NEG_V; |
|
141 return KL_VEC_TYPE (result, l); |
|
142 } |
|
143 |
|
144 #endif |
|
145 |
|
146 #ifdef KLUDGE_MATRICES |
|
147 |
1360
|
148 // Like type operations for matrices. |
237
|
149 |
|
150 // Element by element matrix by scalar ops. |
|
151 |
1209
|
152 #define KL_MS_OP(OP) \ |
|
153 KL_MAT_TYPE \ |
|
154 operator OP (const KL_MAT_TYPE& a, const TYPE& s) \ |
|
155 { \ |
|
156 DO_VS_OP (OP); \ |
|
157 return KL_MAT_TYPE (result, a.rows (), a.cols ()); \ |
|
158 } |
237
|
159 |
1209
|
160 KL_MS_OP(+) |
|
161 KL_MS_OP(-) |
|
162 KL_MS_OP(*) |
|
163 KL_MS_OP(/) |
237
|
164 |
|
165 // Element by element scalar by matrix ops. |
|
166 |
1209
|
167 #define KL_SM_OP(OP) \ |
|
168 KL_MAT_TYPE \ |
|
169 operator OP (const TYPE& s, const KL_MAT_TYPE& a) \ |
|
170 { \ |
|
171 DO_SV_OP (OP); \ |
|
172 return KL_MAT_TYPE (result, a.rows (), a.cols ()); \ |
|
173 } |
237
|
174 |
1209
|
175 KL_SM_OP(+) |
|
176 KL_SM_OP(-) |
|
177 KL_SM_OP(*) |
|
178 KL_SM_OP(/) |
237
|
179 |
|
180 // Element by element matrix by matrix ops. |
|
181 |
1209
|
182 #define KL_MM_OP(FCN, OP, OP_STR) \ |
|
183 KL_MAT_TYPE \ |
|
184 FCN (const KL_MAT_TYPE& a, const KL_MAT_TYPE& b) \ |
|
185 { \ |
|
186 int r = a.rows (); \ |
|
187 int c = a.cols (); \ |
|
188 if (r != b.rows () || c != b.cols ()) \ |
|
189 { \ |
|
190 (*current_liboctave_error_handler) \ |
|
191 ("nonconformant array " OP_STR " attempted"); \ |
|
192 return KL_MAT_TYPE (); \ |
|
193 } \ |
|
194 if (r == 0 || c == 0) \ |
|
195 return KL_MAT_TYPE (r, c); \ |
|
196 int l = a.length (); \ |
|
197 DO_VV_OP (+); \ |
|
198 return KL_MAT_TYPE (result, r, c); \ |
|
199 } |
237
|
200 |
1209
|
201 KL_MM_OP (operator +, +, "addition") |
|
202 KL_MM_OP (operator -, -, "subtraction") |
|
203 KL_MM_OP (product, *, "product") |
|
204 KL_MM_OP (quotient, /, "quotient") |
237
|
205 |
|
206 // Unary matrix ops. |
|
207 |
|
208 KL_MAT_TYPE |
|
209 operator - (const KL_MAT_TYPE& a) |
|
210 { |
|
211 NEG_V; |
|
212 return KL_MAT_TYPE (result, a.rows (), a.cols ()); |
|
213 } |
|
214 |
|
215 #endif |
|
216 |
|
217 #ifdef KLUDGE_DIAG_MATRICES |
|
218 |
1360
|
219 // Like type operations for diagonal matrices. |
237
|
220 |
|
221 // Element by element MDiagArray by scalar ops. |
|
222 |
1209
|
223 #define KL_DMS_OP(OP) \ |
|
224 KL_DMAT_TYPE \ |
|
225 operator OP (const KL_DMAT_TYPE& a, const TYPE& s) \ |
|
226 { \ |
|
227 DO_VS_OP (OP); \ |
|
228 return KL_DMAT_TYPE (result, a.rows (), a.cols ()); \ |
|
229 } |
237
|
230 |
1209
|
231 KL_DMS_OP (*) |
|
232 KL_DMS_OP (/) |
237
|
233 |
|
234 // Element by element scalar by MDiagArray ops. |
|
235 |
1209
|
236 #define KL_SDM_OP(OP) \ |
|
237 KL_DMAT_TYPE \ |
|
238 operator OP (const TYPE& s, const KL_DMAT_TYPE& a) \ |
|
239 { \ |
|
240 DO_SV_OP (OP); \ |
|
241 return KL_DMAT_TYPE (result, a.rows (), a.cols ()); \ |
|
242 } |
|
243 |
|
244 KL_SDM_OP (*) |
237
|
245 |
|
246 // Element by element MDiagArray by MDiagArray ops. |
|
247 |
1209
|
248 #define KL_DMDM_OP(FCN, OP, OP_STR) \ |
|
249 KL_DMAT_TYPE \ |
|
250 FCN (const KL_DMAT_TYPE& a, const KL_DMAT_TYPE& b) \ |
|
251 { \ |
|
252 int r = a.rows (); \ |
|
253 int c = a.cols (); \ |
|
254 if (r != b.rows () || c != b.cols ()) \ |
|
255 { \ |
|
256 (*current_liboctave_error_handler) \ |
|
257 ("nonconformant diagonal array " OP_STR " attempted"); \ |
|
258 return KL_DMAT_TYPE (); \ |
|
259 } \ |
|
260 if (c == 0 || r == 0) \ |
|
261 return KL_DMAT_TYPE (); \ |
|
262 int l = a.length (); \ |
|
263 DO_VV_OP (OP); \ |
|
264 return KL_DMAT_TYPE (result, r, c); \ |
|
265 } |
237
|
266 |
1209
|
267 KL_DMDM_OP (operator +, +, "addition") |
|
268 KL_DMDM_OP (operator -, -, "subtraction") |
|
269 KL_DMDM_OP (product, *, "product") |
237
|
270 |
|
271 // Unary MDiagArray ops. |
|
272 |
|
273 KL_DMAT_TYPE |
|
274 operator - (const KL_DMAT_TYPE& a) |
|
275 { |
|
276 NEG_V; |
|
277 return KL_DMAT_TYPE (result, a.rows (), a.cols ()); |
|
278 } |
|
279 |
|
280 #endif |
|
281 |
1210
|
282 #undef DO_VS_OP |
237
|
283 #undef DO_SV_OP |
|
284 #undef DO_VV_OP |
|
285 #undef NEG_V |
1210
|
286 #undef KL_VS_OP |
|
287 #undef KL_SV_OP |
|
288 #undef KL_VV_OP |
|
289 #undef KL_MS_OP |
|
290 #undef KL_SM_OP |
|
291 #undef KL_MM_OP |
|
292 #undef KL_DMS_OP |
|
293 #undef KL_SDM_OP |
|
294 #undef KL_DMDM_OP |
237
|
295 |
|
296 /* |
|
297 ;;; Local Variables: *** |
|
298 ;;; mode: C++ *** |
|
299 ;;; page-delimiter: "^/\\*" *** |
|
300 ;;; End: *** |
|
301 */ |