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