Mercurial > hg > octave-lyh
comparison liboctave/mx-op-defs.h @ 2870:3241d0057e78
[project @ 1997-04-19 01:21:29 by jwe]
author | jwe |
---|---|
date | Sat, 19 Apr 1997 01:23:06 +0000 |
parents | 8b262e771614 |
children | fccab8e7d35f |
comparison
equal
deleted
inserted
replaced
2869:0da4980855bc | 2870:3241d0057e78 |
---|---|
26 #include "mx-inlines.cc" | 26 #include "mx-inlines.cc" |
27 | 27 |
28 #define BIN_OP_DECL(R, OP, X, Y) \ | 28 #define BIN_OP_DECL(R, OP, X, Y) \ |
29 extern R OP (const X&, const Y&) | 29 extern R OP (const X&, const Y&) |
30 | 30 |
31 #define MS_OP_DECLS(R, M, S) \ | 31 class boolMatrix; |
32 | |
33 #define CMP_OP_DECL(OP, X, Y) \ | |
34 extern boolMatrix OP (const X&, const Y&) | |
35 | |
36 #define BOOL_OP_DECL(OP, X, Y) \ | |
37 extern boolMatrix OP (const X&, const Y&) | |
38 | |
39 #define TBM boolMatrix (1, 1, true) | |
40 #define FBM boolMatrix (1, 1, false) | |
41 #define NBM boolMatrix () | |
42 | |
43 #if 0 | |
44 | |
45 // vector by scalar operations. | |
46 | |
47 #define VS_BIN_OP_DECLS(R, V, S) \ | |
48 BIN_OP_DECL (R, operator +, V, S); \ | |
49 BIN_OP_DECL (R, operator -, V, S); \ | |
50 BIN_OP_DECL (R, operator *, V, S); \ | |
51 BIN_OP_DECL (R, operator /, V, S); | |
52 | |
53 #define VS_BIN_OP(R, F, OP, V, S) \ | |
54 R \ | |
55 F (const V& v, const S& s) \ | |
56 { \ | |
57 int len = v.length (); \ | |
58 \ | |
59 R r (len); \ | |
60 \ | |
61 for (size_t i = 0; i < len; i++) \ | |
62 r.elem(i) = v.elem(i) OP s; \ | |
63 \ | |
64 return r; \ | |
65 } | |
66 | |
67 #define VS_BIN_OPS(R, V, S) \ | |
68 VS_BIN_OP (R, operator +, +, V, S) \ | |
69 VS_BIN_OP (R, operator -, -, V, S) \ | |
70 VS_BIN_OP (R, operator *, *, V, S) \ | |
71 VS_BIN_OP (R, operator /, /, V, S) | |
72 | |
73 // scalar by vector by operations. | |
74 | |
75 #define SV_BIN_OP_DECLS(R, S, V) \ | |
76 BIN_OP_DECL (R, operator +, S, V); \ | |
77 BIN_OP_DECL (R, operator -, S, V); \ | |
78 BIN_OP_DECL (R, operator *, S, V); \ | |
79 BIN_OP_DECL (R, operator /, S, V); | |
80 | |
81 #define SV_BIN_OP(R, F, OP, S, V) \ | |
82 R \ | |
83 F (const S& s, const V& v) \ | |
84 { \ | |
85 int len = v.length (); \ | |
86 \ | |
87 R r (len); \ | |
88 \ | |
89 for (size_t i = 0; i < len; i++) \ | |
90 r.elem(i) = s OP v.elem(i); \ | |
91 \ | |
92 return r; \ | |
93 } | |
94 | |
95 #define SV_BIN_OPS(R, S, V) \ | |
96 SV_BIN_OP (R, operator +, +, S, V) \ | |
97 SV_BIN_OP (R, operator -, -, S, V) \ | |
98 SV_BIN_OP (R, operator *, *, S, V) \ | |
99 SV_BIN_OP (R, operator /, /, S, V) | |
100 | |
101 // vector by vector operations. | |
102 | |
103 #define VV_BIN_OP_DECLS(R, V1, V2) \ | |
104 BIN_OP_DECL (R, operator +, V1, V2); \ | |
105 BIN_OP_DECL (R, operator -, V1, V2); \ | |
106 BIN_OP_DECL (R, product, V1, V2); \ | |
107 BIN_OP_DECL (R, quotient, V1, V2); | |
108 | |
109 #define VV_BIN_OP(R, F, OP, V1, V2) \ | |
110 R \ | |
111 F (const V1& v1, const V2& v2) \ | |
112 { \ | |
113 R r; \ | |
114 \ | |
115 int v1_len = v1.length (); \ | |
116 int v2_len = v2.length (); \ | |
117 \ | |
118 if (v1_len != v2_len) \ | |
119 gripe_nonconformant (#OP, v1_len, v2_len); \ | |
120 else \ | |
121 { \ | |
122 r.resize (v1_len); \ | |
123 \ | |
124 for (size_t i = 0; i < v1_len; i++) \ | |
125 r.elem(i) = v1.elem(i) OP v2.elem(i); \ | |
126 } \ | |
127 \ | |
128 return r; \ | |
129 } | |
130 | |
131 #define VV_BIN_OPS(R, V1, V2) \ | |
132 VV_BIN_OP (R, operator +, +, V1, V2) \ | |
133 VV_BIN_OP (R, operator -, -, V1, V2) \ | |
134 VV_BIN_OP (R, product, *, V1, V2) \ | |
135 VV_BIN_OP (R, quotient, /, V1, V2) | |
136 | |
137 #endif | |
138 | |
139 // matrix by scalar operations. | |
140 | |
141 #define MS_BIN_OP_DECLS(R, M, S) \ | |
32 BIN_OP_DECL (R, operator +, M, S); \ | 142 BIN_OP_DECL (R, operator +, M, S); \ |
33 BIN_OP_DECL (R, operator -, M, S); \ | 143 BIN_OP_DECL (R, operator -, M, S); \ |
34 BIN_OP_DECL (R, operator *, M, S); \ | 144 BIN_OP_DECL (R, operator *, M, S); \ |
35 BIN_OP_DECL (R, operator /, M, S); | 145 BIN_OP_DECL (R, operator /, M, S); |
36 | 146 |
37 #define SM_OP_DECLS(R, S, M) \ | 147 #define MS_BIN_OP(R, OP, M, S, F) \ |
148 R \ | |
149 OP (const M& m, const S& s) \ | |
150 { \ | |
151 int nr = m.rows (); \ | |
152 int nc = m.cols (); \ | |
153 \ | |
154 R r (nr, nc); \ | |
155 \ | |
156 if (nr > 0 && nc > 0) \ | |
157 F ## _vs (r.fortran_vec (), m.data (), nr * nc, s); \ | |
158 \ | |
159 return r; \ | |
160 } | |
161 | |
162 #define MS_BIN_OPS(R, M, S) \ | |
163 MS_BIN_OP (R, operator +, M, S, add) \ | |
164 MS_BIN_OP (R, operator -, M, S, subtract) \ | |
165 MS_BIN_OP (R, operator *, M, S, multiply) \ | |
166 MS_BIN_OP (R, operator /, M, S, divide) | |
167 | |
168 #define MS_CMP_OP_DECLS(M, S) \ | |
169 CMP_OP_DECL (mx_el_lt, M, S); \ | |
170 CMP_OP_DECL (mx_el_le, M, S); \ | |
171 CMP_OP_DECL (mx_el_ge, M, S); \ | |
172 CMP_OP_DECL (mx_el_gt, M, S); \ | |
173 CMP_OP_DECL (mx_el_eq, M, S); \ | |
174 CMP_OP_DECL (mx_el_ne, M, S); | |
175 | |
176 #define MS_CMP_OP(F, OP, M, MC, S, SC, EMPTY_RESULT) \ | |
177 boolMatrix \ | |
178 F (const M& m, const S& s) \ | |
179 { \ | |
180 boolMatrix r; \ | |
181 \ | |
182 int nr = m.rows (); \ | |
183 int nc = m.cols (); \ | |
184 \ | |
185 if (nr == 0 || nc == 0) \ | |
186 r = EMPTY_RESULT; \ | |
187 else \ | |
188 { \ | |
189 r.resize (nr, nc); \ | |
190 \ | |
191 for (int j = 0; j < nc; j++) \ | |
192 for (int i = 0; i < nr; i++) \ | |
193 r.elem(i, j) = MC (m.elem(i, j)) OP SC (s); \ | |
194 } \ | |
195 \ | |
196 return r; \ | |
197 } | |
198 | |
199 #define MS_CMP_OPS(M, CM, S, CS) \ | |
200 MS_CMP_OP (mx_el_lt, <, M, CM, S, CS, NBM) \ | |
201 MS_CMP_OP (mx_el_le, <=, M, CM, S, CS, NBM) \ | |
202 MS_CMP_OP (mx_el_ge, >=, M, CM, S, CS, NBM) \ | |
203 MS_CMP_OP (mx_el_gt, >, M, CM, S, CS, NBM) \ | |
204 MS_CMP_OP (mx_el_eq, ==, M, , S, , FBM) \ | |
205 MS_CMP_OP (mx_el_ne, !=, M, , S, , TBM) | |
206 | |
207 #define MS_BOOL_OP_DECLS(M, S) \ | |
208 BOOL_OP_DECL (mx_el_and, M, S); \ | |
209 BOOL_OP_DECL (mx_el_or, M, S); \ | |
210 | |
211 #define MS_BOOL_OP(F, OP, M, S) \ | |
212 boolMatrix \ | |
213 F (const M& m, const S& s) \ | |
214 { \ | |
215 boolMatrix r; \ | |
216 \ | |
217 int nr = m.rows (); \ | |
218 int nc = m.cols (); \ | |
219 \ | |
220 if (nr != 0 && nc != 0) \ | |
221 { \ | |
222 r.resize (nr, nc); \ | |
223 \ | |
224 for (int j = 0; j < nc; j++) \ | |
225 for (int i = 0; i < nr; i++) \ | |
226 r.elem(i, j) = (m.elem(i, j) != 0) OP (s != 0); \ | |
227 } \ | |
228 \ | |
229 return r; \ | |
230 } | |
231 | |
232 #define MS_BOOL_OPS(M, S) \ | |
233 MS_BOOL_OP (mx_el_and, &&, M, S) \ | |
234 MS_BOOL_OP (mx_el_or, ||, M, S) | |
235 | |
236 #define MS_OP_DECLS(R, M, S) \ | |
237 MS_BIN_OP_DECLS (R, M, S) \ | |
238 MS_CMP_OP_DECLS (M, S) \ | |
239 MS_BOOL_OP_DECLS (M, S) \ | |
240 | |
241 // scalar by matrix operations. | |
242 | |
243 #define SM_BIN_OP_DECLS(R, S, M) \ | |
38 BIN_OP_DECL (R, operator +, S, M); \ | 244 BIN_OP_DECL (R, operator +, S, M); \ |
39 BIN_OP_DECL (R, operator -, S, M); \ | 245 BIN_OP_DECL (R, operator -, S, M); \ |
40 BIN_OP_DECL (R, operator *, S, M); \ | 246 BIN_OP_DECL (R, operator *, S, M); \ |
41 BIN_OP_DECL (R, operator /, S, M); | 247 BIN_OP_DECL (R, operator /, S, M); |
42 | 248 |
43 #define MM_OP_DECLS(R, M1, M2) \ | 249 #define SM_BIN_OP(R, OP, S, M, F) \ |
250 R \ | |
251 OP (const S& s, const M& m) \ | |
252 { \ | |
253 int nr = m.rows (); \ | |
254 int nc = m.cols (); \ | |
255 \ | |
256 R r (nr, nc); \ | |
257 \ | |
258 if (nr > 0 && nc > 0) \ | |
259 F ## _sv (r.fortran_vec (), s, m.data (), nr * nc); \ | |
260 \ | |
261 return r; \ | |
262 } | |
263 | |
264 #define SM_BIN_OPS(R, S, M) \ | |
265 SM_BIN_OP (R, operator +, S, M, add) \ | |
266 SM_BIN_OP (R, operator -, S, M, subtract) \ | |
267 SM_BIN_OP (R, operator *, S, M, multiply) \ | |
268 SM_BIN_OP (R, operator /, S, M, divide) | |
269 | |
270 #define SM_CMP_OP_DECLS(S, M) \ | |
271 CMP_OP_DECL (mx_el_lt, S, M); \ | |
272 CMP_OP_DECL (mx_el_le, S, M); \ | |
273 CMP_OP_DECL (mx_el_ge, S, M); \ | |
274 CMP_OP_DECL (mx_el_gt, S, M); \ | |
275 CMP_OP_DECL (mx_el_eq, S, M); \ | |
276 CMP_OP_DECL (mx_el_ne, S, M); | |
277 | |
278 #define SM_CMP_OP(F, OP, S, SC, M, MC, EMPTY_RESULT) \ | |
279 boolMatrix \ | |
280 F (const S& s, const M& m) \ | |
281 { \ | |
282 boolMatrix r; \ | |
283 \ | |
284 int nr = m.rows (); \ | |
285 int nc = m.cols (); \ | |
286 \ | |
287 if (nr == 0 || nc == 0) \ | |
288 r = EMPTY_RESULT; \ | |
289 else \ | |
290 { \ | |
291 r.resize (nr, nc); \ | |
292 \ | |
293 for (int j = 0; j < nc; j++) \ | |
294 for (int i = 0; i < nr; i++) \ | |
295 r.elem(i, j) = SC (s) OP MC (m.elem(i, j)); \ | |
296 } \ | |
297 \ | |
298 return r; \ | |
299 } | |
300 | |
301 #define SM_CMP_OPS(S, CS, M, CM) \ | |
302 SM_CMP_OP (mx_el_lt, <, S, CS, M, CM, NBM) \ | |
303 SM_CMP_OP (mx_el_le, <=, S, CS, M, CM, NBM) \ | |
304 SM_CMP_OP (mx_el_ge, >=, S, CS, M, CM, NBM) \ | |
305 SM_CMP_OP (mx_el_gt, >, S, CS, M, CM, NBM) \ | |
306 SM_CMP_OP (mx_el_eq, ==, S, , M, , FBM) \ | |
307 SM_CMP_OP (mx_el_ne, !=, S, , M, , TBM) | |
308 | |
309 #define SM_BOOL_OP_DECLS(S, M) \ | |
310 BOOL_OP_DECL (mx_el_and, S, M); \ | |
311 BOOL_OP_DECL (mx_el_or, S, M); \ | |
312 | |
313 #define SM_BOOL_OP(F, OP, S, M) \ | |
314 boolMatrix \ | |
315 F (const S& s, const M& m) \ | |
316 { \ | |
317 boolMatrix r; \ | |
318 \ | |
319 int nr = m.rows (); \ | |
320 int nc = m.cols (); \ | |
321 \ | |
322 if (nr != 0 && nc != 0) \ | |
323 { \ | |
324 r.resize (nr, nc); \ | |
325 \ | |
326 for (int j = 0; j < nc; j++) \ | |
327 for (int i = 0; i < nr; i++) \ | |
328 r.elem(i, j) = (s != 0) OP (m.elem(i, j) != 0); \ | |
329 } \ | |
330 \ | |
331 return r; \ | |
332 } | |
333 | |
334 #define SM_BOOL_OPS(S, M) \ | |
335 SM_BOOL_OP (mx_el_and, &&, S, M) \ | |
336 SM_BOOL_OP (mx_el_or, ||, S, M) | |
337 | |
338 #define SM_OP_DECLS(R, S, M) \ | |
339 SM_BIN_OP_DECLS (R, S, M) \ | |
340 SM_CMP_OP_DECLS (S, M) \ | |
341 SM_BOOL_OP_DECLS (S, M) \ | |
342 | |
343 // matrix by matrix operations. | |
344 | |
345 #define MM_BIN_OP_DECLS(R, M1, M2) \ | |
44 BIN_OP_DECL (R, operator +, M1, M2); \ | 346 BIN_OP_DECL (R, operator +, M1, M2); \ |
45 BIN_OP_DECL (R, operator -, M1, M2); \ | 347 BIN_OP_DECL (R, operator -, M1, M2); \ |
46 BIN_OP_DECL (R, product, M1, M2); \ | 348 BIN_OP_DECL (R, product, M1, M2); \ |
47 BIN_OP_DECL (R, quotient, M1, M2); | 349 BIN_OP_DECL (R, quotient, M1, M2); |
48 | 350 |
49 #define SDM_OP_DECLS(R, S, DM) \ | 351 #define MM_BIN_OP(R, OP, M1, M2, F) \ |
352 R \ | |
353 OP (const M1& m1, const M2& m2) \ | |
354 { \ | |
355 R r; \ | |
356 \ | |
357 int m1_nr = m1.rows (); \ | |
358 int m1_nc = m1.cols (); \ | |
359 \ | |
360 int m2_nr = m2.rows (); \ | |
361 int m2_nc = m2.cols (); \ | |
362 \ | |
363 if (m1_nr != m2_nr || m1_nc != m2_nc) \ | |
364 gripe_nonconformant (#OP, m1_nr, m1_nc, m2_nr, m2_nc); \ | |
365 else \ | |
366 { \ | |
367 r.resize (m1_nr, m1_nc); \ | |
368 \ | |
369 if (m1_nr > 0 && m1_nc > 0) \ | |
370 F ## _vv (r.fortran_vec (), m1.data (), m2.data (), m1_nr * m1_nc); \ | |
371 } \ | |
372 \ | |
373 return r; \ | |
374 } | |
375 | |
376 #define MM_BIN_OPS(R, M1, M2) \ | |
377 MM_BIN_OP (R, operator +, M1, M2, add) \ | |
378 MM_BIN_OP (R, operator -, M1, M2, subtract) \ | |
379 MM_BIN_OP (R, product, M1, M2, multiply) \ | |
380 MM_BIN_OP (R, quotient, M1, M2, divide) | |
381 | |
382 #define MM_CMP_OP_DECLS(M1, M2) \ | |
383 CMP_OP_DECL (mx_el_lt, M1, M2); \ | |
384 CMP_OP_DECL (mx_el_le, M1, M2); \ | |
385 CMP_OP_DECL (mx_el_ge, M1, M2); \ | |
386 CMP_OP_DECL (mx_el_gt, M1, M2); \ | |
387 CMP_OP_DECL (mx_el_eq, M1, M2); \ | |
388 CMP_OP_DECL (mx_el_ne, M1, M2); | |
389 | |
390 #define MM_CMP_OP(F, OP, M1, C1, M2, C2, ONE_MT_RESULT, TWO_MT_RESULT) \ | |
391 boolMatrix \ | |
392 F (const M1& m1, const M2& m2) \ | |
393 { \ | |
394 boolMatrix r; \ | |
395 \ | |
396 int m1_nr = m1.rows (); \ | |
397 int m1_nc = m1.cols (); \ | |
398 \ | |
399 int m2_nr = m2.rows (); \ | |
400 int m2_nc = m2.cols (); \ | |
401 \ | |
402 if (m1_nr == m2_nr && m1_nc == m2_nc) \ | |
403 { \ | |
404 if (m1_nr == 0 && m1_nc == 0) \ | |
405 r = TWO_MT_RESULT; \ | |
406 else \ | |
407 { \ | |
408 r.resize (m1_nr, m1_nc); \ | |
409 \ | |
410 for (int j = 0; j < m1_nc; j++) \ | |
411 for (int i = 0; i < m1_nr; i++) \ | |
412 r.elem(i, j) = C1 (m1.elem(i, j)) OP C2 (m2.elem(i, j)); \ | |
413 } \ | |
414 } \ | |
415 else \ | |
416 { \ | |
417 if ((m1_nr == 0 && m1_nc == 0) || (m2_nr == 0 && m2_nc == 0)) \ | |
418 r = ONE_MT_RESULT; \ | |
419 else \ | |
420 gripe_nonconformant (#F, m1_nr, m1_nc, m2_nr, m2_nc); \ | |
421 } \ | |
422 \ | |
423 return r; \ | |
424 } | |
425 | |
426 #define MM_CMP_OPS(M1, C1, M2, C2) \ | |
427 MM_CMP_OP (mx_el_lt, <, M1, C1, M2, C2, NBM, NBM) \ | |
428 MM_CMP_OP (mx_el_le, <=, M1, C1, M2, C2, NBM, NBM) \ | |
429 MM_CMP_OP (mx_el_ge, >=, M1, C1, M2, C2, NBM, NBM) \ | |
430 MM_CMP_OP (mx_el_gt, >, M1, C1, M2, C2, NBM, NBM) \ | |
431 MM_CMP_OP (mx_el_eq, ==, M1, , M2, , FBM, TBM) \ | |
432 MM_CMP_OP (mx_el_ne, !=, M1, , M2, , TBM, FBM) | |
433 | |
434 #define MM_BOOL_OP_DECLS(M1, M2) \ | |
435 BOOL_OP_DECL (mx_el_and, M1, M2); \ | |
436 BOOL_OP_DECL (mx_el_or, M1, M2); | |
437 | |
438 #define MM_BOOL_OP(F, OP, M1, M2) \ | |
439 boolMatrix \ | |
440 F (const M1& m1, const M2& m2) \ | |
441 { \ | |
442 boolMatrix r; \ | |
443 \ | |
444 int m1_nr = m1.rows (); \ | |
445 int m1_nc = m1.cols (); \ | |
446 \ | |
447 int m2_nr = m2.rows (); \ | |
448 int m2_nc = m2.cols (); \ | |
449 \ | |
450 if (m1_nr == m2_nr && m1_nc == m2_nc) \ | |
451 { \ | |
452 if (m1_nr != 0 || m1_nc != 0) \ | |
453 { \ | |
454 r.resize (m1_nr, m1_nc); \ | |
455 \ | |
456 for (int j = 0; j < m1_nc; j++) \ | |
457 for (int i = 0; i < m1_nr; i++) \ | |
458 r.elem(i, j) = (m1.elem(i, j) != 0) OP (m2.elem(i, j) != 0); \ | |
459 } \ | |
460 } \ | |
461 else \ | |
462 { \ | |
463 if ((m1_nr != 0 || m1_nc != 0) && (m2_nr != 0 || m2_nc != 0)) \ | |
464 gripe_nonconformant (#F, m1_nr, m1_nc, m2_nr, m2_nc); \ | |
465 } \ | |
466 \ | |
467 return r; \ | |
468 } | |
469 | |
470 #define MM_BOOL_OPS(M1, M2) \ | |
471 MM_BOOL_OP (mx_el_and, &&, M1, M2) \ | |
472 MM_BOOL_OP (mx_el_or, ||, M1, M2) | |
473 | |
474 #define MM_OP_DECLS(R, M1, M2) \ | |
475 MM_BIN_OP_DECLS (R, M1, M2) \ | |
476 MM_CMP_OP_DECLS (M1, M2) \ | |
477 MM_BOOL_OP_DECLS (M1, M2) | |
478 | |
479 // scalar by diagonal matrix operations. | |
480 | |
481 #define SDM_BIN_OP_DECLS(R, S, DM) \ | |
50 BIN_OP_DECL (R, operator +, S, DM); \ | 482 BIN_OP_DECL (R, operator +, S, DM); \ |
51 BIN_OP_DECL (R, operator -, S, DM); | 483 BIN_OP_DECL (R, operator -, S, DM); |
52 | 484 |
53 #define DMS_OP_DECLS(R, DM, S) \ | 485 #define SDM_BIN_OP(R, OP, S, DM, OPEQ) \ |
486 R \ | |
487 OP (const S& s, const DM& dm) \ | |
488 { \ | |
489 int nr = dm.rows (); \ | |
490 int nc = dm.cols (); \ | |
491 \ | |
492 R r (nr, nc, s); \ | |
493 \ | |
494 int len = dm.length (); \ | |
495 \ | |
496 for (int i = 0; i < len; i++) \ | |
497 r.elem(i, i) OPEQ dm.elem(i, i); \ | |
498 \ | |
499 return r; \ | |
500 } | |
501 | |
502 #define SDM_BIN_OPS(R, S, DM) \ | |
503 SDM_BIN_OP (R, operator +, S, DM, +=) \ | |
504 SDM_BIN_OP (R, operator -, S, DM, -=) | |
505 | |
506 #define SDM_OP_DECLS(R, S, DM) \ | |
507 SDM_BIN_OP_DECLS(R, S, DM) | |
508 | |
509 // diagonal matrix by scalar operations. | |
510 | |
511 #define DMS_BIN_OP_DECLS(R, DM, S) \ | |
54 BIN_OP_DECL (R, operator +, DM, S); \ | 512 BIN_OP_DECL (R, operator +, DM, S); \ |
55 BIN_OP_DECL (R, operator -, DM, S); | 513 BIN_OP_DECL (R, operator -, DM, S); |
56 | 514 |
57 #define MDM_OP_DECLS(R, M, DM) \ | 515 #define DMS_BIN_OP(R, OP, DM, S, SGN) \ |
516 R \ | |
517 OP (const DM& dm, const S& s) \ | |
518 { \ | |
519 int nr = dm.rows (); \ | |
520 int nc = dm.cols (); \ | |
521 \ | |
522 R r (nr, nc, SGN s); \ | |
523 \ | |
524 int len = dm.length (); \ | |
525 \ | |
526 for (int i = 0; i < len; i++) \ | |
527 r.elem(i, i) += dm.elem(i, i); \ | |
528 \ | |
529 return r; \ | |
530 } | |
531 | |
532 #define DMS_BIN_OPS(R, DM, S) \ | |
533 DMS_BIN_OP (R, operator +, DM, S, ) \ | |
534 DMS_BIN_OP (R, operator -, DM, S, -) | |
535 | |
536 #define DMS_OP_DECLS(R, DM, S) \ | |
537 DMS_BIN_OP_DECLS(R, DM, S) | |
538 | |
539 // matrix by diagonal matrix operations. | |
540 | |
541 #define MDM_BIN_OP_DECLS(R, M, DM) \ | |
58 BIN_OP_DECL (R, operator +, M, DM); \ | 542 BIN_OP_DECL (R, operator +, M, DM); \ |
59 BIN_OP_DECL (R, operator -, M, DM); \ | 543 BIN_OP_DECL (R, operator -, M, DM); \ |
60 BIN_OP_DECL (R, operator *, M, DM); | 544 BIN_OP_DECL (R, operator *, M, DM); |
61 | 545 |
62 #define DMM_OP_DECLS(R, DM, M) \ | 546 #define MDM_BIN_OP(R, OP, M, DM, OPEQ) \ |
63 BIN_OP_DECL (R, operator +, DM, M); \ | |
64 BIN_OP_DECL (R, operator -, DM, M); \ | |
65 BIN_OP_DECL (R, operator *, DM, M); | |
66 | |
67 #define DMDM_OP_DECLS(R, DM1, DM2) \ | |
68 BIN_OP_DECL (R, operator +, DM1, DM2); \ | |
69 BIN_OP_DECL (R, operator -, DM1, DM2); \ | |
70 BIN_OP_DECL (R, product, DM1, DM2); | |
71 | |
72 #define MS_OP(R, OP, M, S, F) \ | |
73 R \ | |
74 OP (const M& m, const S& s) \ | |
75 { \ | |
76 int nr = m.rows (); \ | |
77 int nc = m.cols (); \ | |
78 \ | |
79 R r (nr, nc); \ | |
80 \ | |
81 if (nr > 0 && nc > 0) \ | |
82 F ## _vs (r.fortran_vec (), m.data (), nr * nc, s); \ | |
83 \ | |
84 return r; \ | |
85 } | |
86 | |
87 #define MS_OPS(R, M, S) \ | |
88 MS_OP (R, operator +, M, S, add) \ | |
89 MS_OP (R, operator -, M, S, subtract) \ | |
90 MS_OP (R, operator *, M, S, multiply) \ | |
91 MS_OP (R, operator /, M, S, divide) | |
92 | |
93 #define SM_OP(R, OP, S, M, F) \ | |
94 R \ | |
95 OP (const S& s, const M& m) \ | |
96 { \ | |
97 int nr = m.rows (); \ | |
98 int nc = m.cols (); \ | |
99 \ | |
100 R r (nr, nc); \ | |
101 \ | |
102 if (nr > 0 && nc > 0) \ | |
103 F ## _sv (r.fortran_vec (), s, m.data (), nr * nc); \ | |
104 \ | |
105 return r; \ | |
106 } | |
107 | |
108 #define SM_OPS(R, S, M) \ | |
109 SM_OP (R, operator +, S, M, add) \ | |
110 SM_OP (R, operator -, S, M, subtract) \ | |
111 SM_OP (R, operator *, S, M, multiply) \ | |
112 SM_OP (R, operator /, S, M, divide) | |
113 | |
114 #define MM_OP(R, OP, M1, M2, F) \ | |
115 R \ | |
116 OP (const M1& m1, const M2& m2) \ | |
117 { \ | |
118 R r; \ | |
119 \ | |
120 int m1_nr = m1.rows (); \ | |
121 int m1_nc = m1.cols (); \ | |
122 \ | |
123 int m2_nr = m2.rows (); \ | |
124 int m2_nc = m2.cols (); \ | |
125 \ | |
126 if (m1_nr != m2_nr || m1_nc != m2_nc) \ | |
127 gripe_nonconformant (#OP, m1_nr, m1_nc, m2_nr, m2_nc); \ | |
128 else \ | |
129 { \ | |
130 r.resize (m1_nr, m1_nc); \ | |
131 \ | |
132 if (m1_nr > 0 && m1_nc > 0) \ | |
133 F ## _vv (r.fortran_vec (), m1.data (), m2.data (), m1_nr * m1_nc); \ | |
134 } \ | |
135 \ | |
136 return r; \ | |
137 } | |
138 | |
139 #define MM_OPS(R, M1, M2) \ | |
140 MM_OP (R, operator +, M1, M2, add) \ | |
141 MM_OP (R, operator -, M1, M2, subtract) \ | |
142 MM_OP (R, product, M1, M2, multiply) \ | |
143 MM_OP (R, quotient, M1, M2, divide) | |
144 | |
145 #define SDM_OP(R, OP, S, DM, OPEQ) \ | |
146 R \ | |
147 OP (const S& s, const DM& dm) \ | |
148 { \ | |
149 int nr = dm.rows (); \ | |
150 int nc = dm.cols (); \ | |
151 \ | |
152 R r (nr, nc, s); \ | |
153 \ | |
154 int len = dm.length (); \ | |
155 \ | |
156 for (int i = 0; i < len; i++) \ | |
157 r.elem (i, i) OPEQ dm.elem (i, i); \ | |
158 \ | |
159 return r; \ | |
160 } | |
161 | |
162 #define SDM_OPS(R, S, DM) \ | |
163 SDM_OP (R, operator +, S, DM, +=) \ | |
164 SDM_OP (R, operator -, S, DM, -=) | |
165 | |
166 #define DMS_OP(R, OP, DM, S, SGN) \ | |
167 R \ | |
168 OP (const DM& dm, const S& s) \ | |
169 { \ | |
170 int nr = dm.rows (); \ | |
171 int nc = dm.cols (); \ | |
172 \ | |
173 R r (nr, nc, SGN s); \ | |
174 \ | |
175 int len = dm.length (); \ | |
176 \ | |
177 for (int i = 0; i < len; i++) \ | |
178 r.elem (i, i) += dm.elem (i, i); \ | |
179 \ | |
180 return r; \ | |
181 } | |
182 | |
183 #define DMS_OPS(R, DM, S) \ | |
184 DMS_OP (R, operator +, DM, S, ) \ | |
185 DMS_OP (R, operator -, DM, S, -) | |
186 | |
187 #define MDM_OP(R, OP, M, DM, OPEQ) \ | |
188 R \ | 547 R \ |
189 OP (const M& m, const DM& dm) \ | 548 OP (const M& m, const DM& dm) \ |
190 { \ | 549 { \ |
191 R r; \ | 550 R r; \ |
192 \ | 551 \ |
207 r = m; \ | 566 r = m; \ |
208 \ | 567 \ |
209 int len = dm.length (); \ | 568 int len = dm.length (); \ |
210 \ | 569 \ |
211 for (int i = 0; i < len; i++) \ | 570 for (int i = 0; i < len; i++) \ |
212 r.elem (i, i) OPEQ dm.elem (i, i); \ | 571 r.elem(i, i) OPEQ dm.elem(i, i); \ |
213 } \ | 572 } \ |
214 } \ | 573 } \ |
215 \ | 574 \ |
216 return r; \ | 575 return r; \ |
217 } | 576 } |
236 \ | 595 \ |
237 if (m_nr > 0 && m_nc > 0 && dm_nc == 0) \ | 596 if (m_nr > 0 && m_nc > 0 && dm_nc == 0) \ |
238 { \ | 597 { \ |
239 for (int j = 0; j < dm.length (); j++) \ | 598 for (int j = 0; j < dm.length (); j++) \ |
240 { \ | 599 { \ |
241 if (dm.elem (j, j) == 1.0) \ | 600 if (dm.elem(j, j) == 1.0) \ |
242 { \ | 601 { \ |
243 for (int i = 0; i < m_nr; i++) \ | 602 for (int i = 0; i < m_nr; i++) \ |
244 r.elem (i, j) = m.elem (i, j); \ | 603 r.elem(i, j) = m.elem(i, j); \ |
245 } \ | 604 } \ |
246 else if (dm.elem (j, j) != 0.0) \ | 605 else if (dm.elem(j, j) != 0.0) \ |
247 { \ | 606 { \ |
248 for (int i = 0; i < m_nr; i++) \ | 607 for (int i = 0; i < m_nr; i++) \ |
249 r.elem (i, j) = dm.elem (j, j) * m.elem (i, j); \ | 608 r.elem(i, j) = dm.elem(j, j) * m.elem(i, j); \ |
250 } \ | 609 } \ |
251 } \ | 610 } \ |
252 } \ | 611 } \ |
253 } \ | 612 } \ |
254 \ | 613 \ |
255 return r; \ | 614 return r; \ |
256 } | 615 } |
257 | 616 |
258 #define MDM_OPS(R, M, DM) \ | 617 #define MDM_BIN_OPS(R, M, DM) \ |
259 MDM_OP (R, operator +, M, DM, +=) \ | 618 MDM_BIN_OP (R, operator +, M, DM, +=) \ |
260 MDM_OP (R, operator -, M, DM, -=) \ | 619 MDM_BIN_OP (R, operator -, M, DM, -=) \ |
261 MDM_MULTIPLY_OP (R, M, DM) | 620 MDM_MULTIPLY_OP (R, M, DM) |
262 | 621 |
622 #define MDM_OP_DECLS(R, M, DM) \ | |
623 MDM_BIN_OP_DECLS(R, M, DM) | |
624 | |
625 // diagonal matrix by matrix operations. | |
626 | |
263 // XXX FIXME XXX -- DM - M will not give the correct result. | 627 // XXX FIXME XXX -- DM - M will not give the correct result. |
264 | 628 |
265 #define DMM_OP(R, OP, DM, M, OPEQ) \ | 629 #define DMM_BIN_OP_DECLS(R, DM, M) \ |
630 BIN_OP_DECL (R, operator +, DM, M); \ | |
631 BIN_OP_DECL (R, operator -, DM, M); \ | |
632 BIN_OP_DECL (R, operator *, DM, M); | |
633 | |
634 #define DMM_BIN_OP(R, OP, DM, M, OPEQ) \ | |
266 R \ | 635 R \ |
267 OP (const DM& dm, const M& m) \ | 636 OP (const DM& dm, const M& m) \ |
268 { \ | 637 { \ |
269 R r; \ | 638 R r; \ |
270 \ | 639 \ |
283 r = m; \ | 652 r = m; \ |
284 \ | 653 \ |
285 int len = dm.length (); \ | 654 int len = dm.length (); \ |
286 \ | 655 \ |
287 for (int i = 0; i < len; i++) \ | 656 for (int i = 0; i < len; i++) \ |
288 r.elem (i, i) OPEQ dm.elem (i, i); \ | 657 r.elem(i, i) OPEQ dm.elem(i, i); \ |
289 } \ | 658 } \ |
290 else \ | 659 else \ |
291 r.resize (m_nr, m_nc); \ | 660 r.resize (m_nr, m_nc); \ |
292 } \ | 661 } \ |
293 \ | 662 \ |
314 \ | 683 \ |
315 if (dm_nr > 0 && dm_nc > 0 && m_nc > 0) \ | 684 if (dm_nr > 0 && dm_nc > 0 && m_nc > 0) \ |
316 { \ | 685 { \ |
317 for (int i = 0; i < dm.length (); i++) \ | 686 for (int i = 0; i < dm.length (); i++) \ |
318 { \ | 687 { \ |
319 if (dm.elem (i, i) == 1.0) \ | 688 if (dm.elem(i, i) == 1.0) \ |
320 { \ | 689 { \ |
321 for (int j = 0; j < m_nc; j++) \ | 690 for (int j = 0; j < m_nc; j++) \ |
322 r.elem (i, j) = m.elem (i, j); \ | 691 r.elem(i, j) = m.elem(i, j); \ |
323 } \ | 692 } \ |
324 else if (dm.elem (i, i) != 0.0) \ | 693 else if (dm.elem(i, i) != 0.0) \ |
325 { \ | 694 { \ |
326 for (int j = 0; j < m_nc; j++) \ | 695 for (int j = 0; j < m_nc; j++) \ |
327 r.elem (i, j) = dm.elem (i, i) * m.elem (i, j); \ | 696 r.elem(i, j) = dm.elem(i, i) * m.elem(i, j); \ |
328 } \ | 697 } \ |
329 } \ | 698 } \ |
330 } \ | 699 } \ |
331 } \ | 700 } \ |
332 \ | 701 \ |
333 return r; \ | 702 return r; \ |
334 } | 703 } |
335 | 704 |
336 #define DMM_OPS(R, DM, M) \ | 705 #define DMM_BIN_OPS(R, DM, M) \ |
337 DMM_OP (R, operator +, DM, M, +=) \ | 706 DMM_BIN_OP (R, operator +, DM, M, +=) \ |
338 DMM_OP (R, operator -, DM, M, -=) \ | 707 DMM_BIN_OP (R, operator -, DM, M, -=) \ |
339 DMM_MULTIPLY_OP(R, DM, M) | 708 DMM_MULTIPLY_OP(R, DM, M) |
340 | 709 |
341 #define MM_OP(R, OP, M1, M2, F) \ | 710 #define DMM_OP_DECLS(R, DM, M) \ |
342 R \ | 711 DMM_BIN_OP_DECLS(R, DM, M) |
343 OP (const M1& m1, const M2& m2) \ | 712 |
344 { \ | 713 // diagonal matrix by diagonal matrix operations. |
345 R r; \ | 714 |
346 \ | 715 #define DMDM_BIN_OP_DECLS(R, DM1, DM2) \ |
347 int m1_nr = m1.rows (); \ | 716 BIN_OP_DECL (R, operator +, DM1, DM2); \ |
348 int m1_nc = m1.cols (); \ | 717 BIN_OP_DECL (R, operator -, DM1, DM2); \ |
349 \ | 718 BIN_OP_DECL (R, product, DM1, DM2); |
350 int m2_nr = m2.rows (); \ | 719 |
351 int m2_nc = m2.cols (); \ | 720 #define DMDM_BIN_OP(R, OP, DM1, DM2, F) \ |
352 \ | |
353 if (m1_nr != m2_nr || m1_nc != m2_nc) \ | |
354 gripe_nonconformant (#OP, m1_nr, m1_nc, m2_nr, m2_nc); \ | |
355 else \ | |
356 { \ | |
357 r.resize (m1_nr, m1_nc); \ | |
358 \ | |
359 if (m1_nr > 0 && m1_nc > 0) \ | |
360 F ## _vv (r.fortran_vec (), m1.data (), m2.data (), m1_nr * m1_nc); \ | |
361 } \ | |
362 \ | |
363 return r; \ | |
364 } | |
365 | |
366 #define DMDM_OP(R, OP, DM1, DM2, F) \ | |
367 R \ | 721 R \ |
368 OP (const DM1& dm1, const DM2& dm2) \ | 722 OP (const DM1& dm1, const DM2& dm2) \ |
369 { \ | 723 { \ |
370 R r; \ | 724 R r; \ |
371 \ | 725 \ |
387 } \ | 741 } \ |
388 \ | 742 \ |
389 return r; \ | 743 return r; \ |
390 } | 744 } |
391 | 745 |
392 #define DMDM_OPS(R, DM1, DM2) \ | 746 #define DMDM_BIN_OPS(R, DM1, DM2) \ |
393 DMDM_OP (R, operator +, DM1, DM2, add) \ | 747 DMDM_BIN_OP (R, operator +, DM1, DM2, add) \ |
394 DMDM_OP (R, operator -, DM1, DM2, subtract) \ | 748 DMDM_BIN_OP (R, operator -, DM1, DM2, subtract) \ |
395 DMDM_OP (R, product, DM1, DM2, multiply) | 749 DMDM_BIN_OP (R, product, DM1, DM2, multiply) |
750 | |
751 #define DMDM_OP_DECLS(R, DM1, DM2) \ | |
752 DMDM_BIN_OP_DECLS (R, DM1, DM2) | |
396 | 753 |
397 #endif | 754 #endif |
398 | 755 |
399 /* | 756 /* |
400 ;;; Local Variables: *** | 757 ;;; Local Variables: *** |