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; |
4543
|
32 class boolNDArray; |
2870
|
33 |
|
34 #define CMP_OP_DECL(OP, X, Y) \ |
|
35 extern boolMatrix OP (const X&, const Y&) |
|
36 |
4543
|
37 #define NDCMP_OP_DECL(OP, X, Y) \ |
|
38 extern boolNDArray OP (const X&, const Y&) |
|
39 |
2870
|
40 #define BOOL_OP_DECL(OP, X, Y) \ |
|
41 extern boolMatrix OP (const X&, const Y&) |
|
42 |
4543
|
43 #define NDBOOL_OP_DECL(OP, X, Y) \ |
|
44 extern boolNDArray OP (const X&, const Y&) |
|
45 |
2870
|
46 #define TBM boolMatrix (1, 1, true) |
|
47 #define FBM boolMatrix (1, 1, false) |
|
48 #define NBM boolMatrix () |
|
49 |
|
50 // vector by scalar operations. |
|
51 |
|
52 #define VS_BIN_OP_DECLS(R, V, S) \ |
|
53 BIN_OP_DECL (R, operator +, V, S); \ |
|
54 BIN_OP_DECL (R, operator -, V, S); \ |
|
55 BIN_OP_DECL (R, operator *, V, S); \ |
|
56 BIN_OP_DECL (R, operator /, V, S); |
|
57 |
|
58 #define VS_BIN_OP(R, F, OP, V, S) \ |
|
59 R \ |
|
60 F (const V& v, const S& s) \ |
|
61 { \ |
|
62 int len = v.length (); \ |
|
63 \ |
|
64 R r (len); \ |
|
65 \ |
3582
|
66 for (int i = 0; i < len; i++) \ |
2870
|
67 r.elem(i) = v.elem(i) OP s; \ |
|
68 \ |
|
69 return r; \ |
|
70 } |
|
71 |
|
72 #define VS_BIN_OPS(R, V, S) \ |
|
73 VS_BIN_OP (R, operator +, +, V, S) \ |
|
74 VS_BIN_OP (R, operator -, -, V, S) \ |
|
75 VS_BIN_OP (R, operator *, *, V, S) \ |
|
76 VS_BIN_OP (R, operator /, /, V, S) |
|
77 |
3582
|
78 #define VS_OP_DECLS(R, V, S) \ |
|
79 VS_BIN_OP_DECLS(R, V, S) |
|
80 |
2870
|
81 // scalar by vector by operations. |
|
82 |
|
83 #define SV_BIN_OP_DECLS(R, S, V) \ |
|
84 BIN_OP_DECL (R, operator +, S, V); \ |
|
85 BIN_OP_DECL (R, operator -, S, V); \ |
|
86 BIN_OP_DECL (R, operator *, S, V); \ |
|
87 BIN_OP_DECL (R, operator /, S, V); |
|
88 |
|
89 #define SV_BIN_OP(R, F, OP, S, V) \ |
|
90 R \ |
|
91 F (const S& s, const V& v) \ |
|
92 { \ |
|
93 int len = v.length (); \ |
|
94 \ |
|
95 R r (len); \ |
|
96 \ |
3582
|
97 for (int i = 0; i < len; i++) \ |
2870
|
98 r.elem(i) = s OP v.elem(i); \ |
|
99 \ |
|
100 return r; \ |
|
101 } |
|
102 |
|
103 #define SV_BIN_OPS(R, S, V) \ |
|
104 SV_BIN_OP (R, operator +, +, S, V) \ |
|
105 SV_BIN_OP (R, operator -, -, S, V) \ |
|
106 SV_BIN_OP (R, operator *, *, S, V) \ |
|
107 SV_BIN_OP (R, operator /, /, S, V) |
|
108 |
3582
|
109 #define SV_OP_DECLS(R, S, V) \ |
|
110 SV_BIN_OP_DECLS(R, S, V) |
|
111 |
2870
|
112 // vector by vector operations. |
|
113 |
|
114 #define VV_BIN_OP_DECLS(R, V1, V2) \ |
|
115 BIN_OP_DECL (R, operator +, V1, V2); \ |
|
116 BIN_OP_DECL (R, operator -, V1, V2); \ |
|
117 BIN_OP_DECL (R, product, V1, V2); \ |
|
118 BIN_OP_DECL (R, quotient, V1, V2); |
|
119 |
|
120 #define VV_BIN_OP(R, F, OP, V1, V2) \ |
|
121 R \ |
|
122 F (const V1& v1, const V2& v2) \ |
|
123 { \ |
|
124 R r; \ |
|
125 \ |
|
126 int v1_len = v1.length (); \ |
|
127 int v2_len = v2.length (); \ |
|
128 \ |
|
129 if (v1_len != v2_len) \ |
|
130 gripe_nonconformant (#OP, v1_len, v2_len); \ |
|
131 else \ |
|
132 { \ |
|
133 r.resize (v1_len); \ |
|
134 \ |
3582
|
135 for (int i = 0; i < v1_len; i++) \ |
2870
|
136 r.elem(i) = v1.elem(i) OP v2.elem(i); \ |
|
137 } \ |
|
138 \ |
|
139 return r; \ |
|
140 } |
|
141 |
|
142 #define VV_BIN_OPS(R, V1, V2) \ |
|
143 VV_BIN_OP (R, operator +, +, V1, V2) \ |
|
144 VV_BIN_OP (R, operator -, -, V1, V2) \ |
|
145 VV_BIN_OP (R, product, *, V1, V2) \ |
|
146 VV_BIN_OP (R, quotient, /, V1, V2) |
|
147 |
3582
|
148 #define VV_OP_DECLS(R, V1, V2) \ |
|
149 VV_BIN_OP_DECLS(R, V1, V2) |
2870
|
150 |
|
151 // matrix by scalar operations. |
|
152 |
|
153 #define MS_BIN_OP_DECLS(R, M, S) \ |
2829
|
154 BIN_OP_DECL (R, operator +, M, S); \ |
|
155 BIN_OP_DECL (R, operator -, M, S); \ |
|
156 BIN_OP_DECL (R, operator *, M, S); \ |
|
157 BIN_OP_DECL (R, operator /, M, S); |
|
158 |
2870
|
159 #define MS_BIN_OP(R, OP, M, S, F) \ |
2829
|
160 R \ |
|
161 OP (const M& m, const S& s) \ |
|
162 { \ |
|
163 int nr = m.rows (); \ |
|
164 int nc = m.cols (); \ |
|
165 \ |
|
166 R r (nr, nc); \ |
|
167 \ |
|
168 if (nr > 0 && nc > 0) \ |
|
169 F ## _vs (r.fortran_vec (), m.data (), nr * nc, s); \ |
|
170 \ |
|
171 return r; \ |
|
172 } |
|
173 |
2870
|
174 #define MS_BIN_OPS(R, M, S) \ |
3769
|
175 MS_BIN_OP (R, operator +, M, S, mx_inline_add) \ |
|
176 MS_BIN_OP (R, operator -, M, S, mx_inline_subtract) \ |
|
177 MS_BIN_OP (R, operator *, M, S, mx_inline_multiply) \ |
|
178 MS_BIN_OP (R, operator /, M, S, mx_inline_divide) |
2870
|
179 |
|
180 #define MS_CMP_OP_DECLS(M, S) \ |
|
181 CMP_OP_DECL (mx_el_lt, M, S); \ |
|
182 CMP_OP_DECL (mx_el_le, M, S); \ |
|
183 CMP_OP_DECL (mx_el_ge, M, S); \ |
|
184 CMP_OP_DECL (mx_el_gt, M, S); \ |
|
185 CMP_OP_DECL (mx_el_eq, M, S); \ |
|
186 CMP_OP_DECL (mx_el_ne, M, S); |
|
187 |
|
188 #define MS_CMP_OP(F, OP, M, MC, S, SC, EMPTY_RESULT) \ |
|
189 boolMatrix \ |
|
190 F (const M& m, const S& s) \ |
|
191 { \ |
|
192 boolMatrix r; \ |
|
193 \ |
|
194 int nr = m.rows (); \ |
|
195 int nc = m.cols (); \ |
|
196 \ |
|
197 if (nr == 0 || nc == 0) \ |
|
198 r = EMPTY_RESULT; \ |
|
199 else \ |
|
200 { \ |
|
201 r.resize (nr, nc); \ |
|
202 \ |
|
203 for (int j = 0; j < nc; j++) \ |
|
204 for (int i = 0; i < nr; i++) \ |
|
205 r.elem(i, j) = MC (m.elem(i, j)) OP SC (s); \ |
|
206 } \ |
|
207 \ |
|
208 return r; \ |
|
209 } |
2829
|
210 |
2870
|
211 #define MS_CMP_OPS(M, CM, S, CS) \ |
|
212 MS_CMP_OP (mx_el_lt, <, M, CM, S, CS, NBM) \ |
|
213 MS_CMP_OP (mx_el_le, <=, M, CM, S, CS, NBM) \ |
|
214 MS_CMP_OP (mx_el_ge, >=, M, CM, S, CS, NBM) \ |
|
215 MS_CMP_OP (mx_el_gt, >, M, CM, S, CS, NBM) \ |
|
216 MS_CMP_OP (mx_el_eq, ==, M, , S, , FBM) \ |
|
217 MS_CMP_OP (mx_el_ne, !=, M, , S, , TBM) |
|
218 |
|
219 #define MS_BOOL_OP_DECLS(M, S) \ |
|
220 BOOL_OP_DECL (mx_el_and, M, S); \ |
|
221 BOOL_OP_DECL (mx_el_or, M, S); \ |
|
222 |
3504
|
223 #define MS_BOOL_OP(F, OP, M, S, ZERO) \ |
2870
|
224 boolMatrix \ |
|
225 F (const M& m, const S& s) \ |
|
226 { \ |
|
227 boolMatrix r; \ |
|
228 \ |
|
229 int nr = m.rows (); \ |
|
230 int nc = m.cols (); \ |
|
231 \ |
|
232 if (nr != 0 && nc != 0) \ |
|
233 { \ |
|
234 r.resize (nr, nc); \ |
|
235 \ |
|
236 for (int j = 0; j < nc; j++) \ |
|
237 for (int i = 0; i < nr; i++) \ |
3504
|
238 r.elem(i, j) = (m.elem(i, j) != ZERO) OP (s != ZERO); \ |
2870
|
239 } \ |
|
240 \ |
|
241 return r; \ |
|
242 } |
|
243 |
3504
|
244 #define MS_BOOL_OPS(M, S, ZERO) \ |
|
245 MS_BOOL_OP (mx_el_and, &&, M, S, ZERO) \ |
|
246 MS_BOOL_OP (mx_el_or, ||, M, S, ZERO) |
2870
|
247 |
|
248 #define MS_OP_DECLS(R, M, S) \ |
|
249 MS_BIN_OP_DECLS (R, M, S) \ |
|
250 MS_CMP_OP_DECLS (M, S) \ |
|
251 MS_BOOL_OP_DECLS (M, S) \ |
|
252 |
|
253 // scalar by matrix operations. |
|
254 |
|
255 #define SM_BIN_OP_DECLS(R, S, M) \ |
|
256 BIN_OP_DECL (R, operator +, S, M); \ |
|
257 BIN_OP_DECL (R, operator -, S, M); \ |
|
258 BIN_OP_DECL (R, operator *, S, M); \ |
|
259 BIN_OP_DECL (R, operator /, S, M); |
|
260 |
|
261 #define SM_BIN_OP(R, OP, S, M, F) \ |
2829
|
262 R \ |
|
263 OP (const S& s, const M& m) \ |
|
264 { \ |
|
265 int nr = m.rows (); \ |
|
266 int nc = m.cols (); \ |
|
267 \ |
|
268 R r (nr, nc); \ |
|
269 \ |
|
270 if (nr > 0 && nc > 0) \ |
|
271 F ## _sv (r.fortran_vec (), s, m.data (), nr * nc); \ |
|
272 \ |
|
273 return r; \ |
|
274 } |
|
275 |
2870
|
276 #define SM_BIN_OPS(R, S, M) \ |
3769
|
277 SM_BIN_OP (R, operator +, S, M, mx_inline_add) \ |
|
278 SM_BIN_OP (R, operator -, S, M, mx_inline_subtract) \ |
|
279 SM_BIN_OP (R, operator *, S, M, mx_inline_multiply) \ |
|
280 SM_BIN_OP (R, operator /, S, M, mx_inline_divide) |
2870
|
281 |
|
282 #define SM_CMP_OP_DECLS(S, M) \ |
|
283 CMP_OP_DECL (mx_el_lt, S, M); \ |
|
284 CMP_OP_DECL (mx_el_le, S, M); \ |
|
285 CMP_OP_DECL (mx_el_ge, S, M); \ |
|
286 CMP_OP_DECL (mx_el_gt, S, M); \ |
|
287 CMP_OP_DECL (mx_el_eq, S, M); \ |
|
288 CMP_OP_DECL (mx_el_ne, S, M); |
|
289 |
|
290 #define SM_CMP_OP(F, OP, S, SC, M, MC, EMPTY_RESULT) \ |
|
291 boolMatrix \ |
|
292 F (const S& s, const M& m) \ |
|
293 { \ |
|
294 boolMatrix r; \ |
|
295 \ |
|
296 int nr = m.rows (); \ |
|
297 int nc = m.cols (); \ |
|
298 \ |
|
299 if (nr == 0 || nc == 0) \ |
|
300 r = EMPTY_RESULT; \ |
|
301 else \ |
|
302 { \ |
|
303 r.resize (nr, nc); \ |
|
304 \ |
|
305 for (int j = 0; j < nc; j++) \ |
|
306 for (int i = 0; i < nr; i++) \ |
|
307 r.elem(i, j) = SC (s) OP MC (m.elem(i, j)); \ |
|
308 } \ |
|
309 \ |
|
310 return r; \ |
|
311 } |
2829
|
312 |
2870
|
313 #define SM_CMP_OPS(S, CS, M, CM) \ |
|
314 SM_CMP_OP (mx_el_lt, <, S, CS, M, CM, NBM) \ |
|
315 SM_CMP_OP (mx_el_le, <=, S, CS, M, CM, NBM) \ |
|
316 SM_CMP_OP (mx_el_ge, >=, S, CS, M, CM, NBM) \ |
|
317 SM_CMP_OP (mx_el_gt, >, S, CS, M, CM, NBM) \ |
|
318 SM_CMP_OP (mx_el_eq, ==, S, , M, , FBM) \ |
|
319 SM_CMP_OP (mx_el_ne, !=, S, , M, , TBM) |
|
320 |
|
321 #define SM_BOOL_OP_DECLS(S, M) \ |
|
322 BOOL_OP_DECL (mx_el_and, S, M); \ |
|
323 BOOL_OP_DECL (mx_el_or, S, M); \ |
|
324 |
3504
|
325 #define SM_BOOL_OP(F, OP, S, M, ZERO) \ |
2870
|
326 boolMatrix \ |
|
327 F (const S& s, const M& m) \ |
|
328 { \ |
|
329 boolMatrix r; \ |
|
330 \ |
|
331 int nr = m.rows (); \ |
|
332 int nc = m.cols (); \ |
|
333 \ |
|
334 if (nr != 0 && nc != 0) \ |
|
335 { \ |
|
336 r.resize (nr, nc); \ |
|
337 \ |
|
338 for (int j = 0; j < nc; j++) \ |
|
339 for (int i = 0; i < nr; i++) \ |
3504
|
340 r.elem(i, j) = (s != ZERO) OP (m.elem(i, j) != ZERO); \ |
2870
|
341 } \ |
|
342 \ |
|
343 return r; \ |
|
344 } |
|
345 |
3504
|
346 #define SM_BOOL_OPS(S, M, ZERO) \ |
|
347 SM_BOOL_OP (mx_el_and, &&, S, M, ZERO) \ |
|
348 SM_BOOL_OP (mx_el_or, ||, S, M, ZERO) |
2870
|
349 |
|
350 #define SM_OP_DECLS(R, S, M) \ |
|
351 SM_BIN_OP_DECLS (R, S, M) \ |
|
352 SM_CMP_OP_DECLS (S, M) \ |
|
353 SM_BOOL_OP_DECLS (S, M) \ |
|
354 |
|
355 // matrix by matrix operations. |
|
356 |
|
357 #define MM_BIN_OP_DECLS(R, M1, M2) \ |
|
358 BIN_OP_DECL (R, operator +, M1, M2); \ |
|
359 BIN_OP_DECL (R, operator -, M1, M2); \ |
|
360 BIN_OP_DECL (R, product, M1, M2); \ |
|
361 BIN_OP_DECL (R, quotient, M1, M2); |
|
362 |
|
363 #define MM_BIN_OP(R, OP, M1, M2, F) \ |
2829
|
364 R \ |
|
365 OP (const M1& m1, const M2& m2) \ |
|
366 { \ |
|
367 R r; \ |
|
368 \ |
|
369 int m1_nr = m1.rows (); \ |
|
370 int m1_nc = m1.cols (); \ |
|
371 \ |
|
372 int m2_nr = m2.rows (); \ |
|
373 int m2_nc = m2.cols (); \ |
|
374 \ |
|
375 if (m1_nr != m2_nr || m1_nc != m2_nc) \ |
|
376 gripe_nonconformant (#OP, m1_nr, m1_nc, m2_nr, m2_nc); \ |
|
377 else \ |
|
378 { \ |
|
379 r.resize (m1_nr, m1_nc); \ |
|
380 \ |
|
381 if (m1_nr > 0 && m1_nc > 0) \ |
|
382 F ## _vv (r.fortran_vec (), m1.data (), m2.data (), m1_nr * m1_nc); \ |
|
383 } \ |
|
384 \ |
|
385 return r; \ |
|
386 } |
|
387 |
2870
|
388 #define MM_BIN_OPS(R, M1, M2) \ |
3769
|
389 MM_BIN_OP (R, operator +, M1, M2, mx_inline_add) \ |
|
390 MM_BIN_OP (R, operator -, M1, M2, mx_inline_subtract) \ |
|
391 MM_BIN_OP (R, product, M1, M2, mx_inline_multiply) \ |
|
392 MM_BIN_OP (R, quotient, M1, M2, mx_inline_divide) |
2870
|
393 |
|
394 #define MM_CMP_OP_DECLS(M1, M2) \ |
|
395 CMP_OP_DECL (mx_el_lt, M1, M2); \ |
|
396 CMP_OP_DECL (mx_el_le, M1, M2); \ |
|
397 CMP_OP_DECL (mx_el_ge, M1, M2); \ |
|
398 CMP_OP_DECL (mx_el_gt, M1, M2); \ |
|
399 CMP_OP_DECL (mx_el_eq, M1, M2); \ |
|
400 CMP_OP_DECL (mx_el_ne, M1, M2); |
|
401 |
|
402 #define MM_CMP_OP(F, OP, M1, C1, M2, C2, ONE_MT_RESULT, TWO_MT_RESULT) \ |
|
403 boolMatrix \ |
|
404 F (const M1& m1, const M2& m2) \ |
|
405 { \ |
|
406 boolMatrix r; \ |
|
407 \ |
|
408 int m1_nr = m1.rows (); \ |
|
409 int m1_nc = m1.cols (); \ |
|
410 \ |
|
411 int m2_nr = m2.rows (); \ |
|
412 int m2_nc = m2.cols (); \ |
|
413 \ |
|
414 if (m1_nr == m2_nr && m1_nc == m2_nc) \ |
|
415 { \ |
|
416 if (m1_nr == 0 && m1_nc == 0) \ |
|
417 r = TWO_MT_RESULT; \ |
|
418 else \ |
|
419 { \ |
|
420 r.resize (m1_nr, m1_nc); \ |
|
421 \ |
|
422 for (int j = 0; j < m1_nc; j++) \ |
|
423 for (int i = 0; i < m1_nr; i++) \ |
|
424 r.elem(i, j) = C1 (m1.elem(i, j)) OP C2 (m2.elem(i, j)); \ |
|
425 } \ |
|
426 } \ |
|
427 else \ |
|
428 { \ |
|
429 if ((m1_nr == 0 && m1_nc == 0) || (m2_nr == 0 && m2_nc == 0)) \ |
|
430 r = ONE_MT_RESULT; \ |
|
431 else \ |
|
432 gripe_nonconformant (#F, m1_nr, m1_nc, m2_nr, m2_nc); \ |
|
433 } \ |
|
434 \ |
|
435 return r; \ |
|
436 } |
2829
|
437 |
2870
|
438 #define MM_CMP_OPS(M1, C1, M2, C2) \ |
|
439 MM_CMP_OP (mx_el_lt, <, M1, C1, M2, C2, NBM, NBM) \ |
|
440 MM_CMP_OP (mx_el_le, <=, M1, C1, M2, C2, NBM, NBM) \ |
|
441 MM_CMP_OP (mx_el_ge, >=, M1, C1, M2, C2, NBM, NBM) \ |
|
442 MM_CMP_OP (mx_el_gt, >, M1, C1, M2, C2, NBM, NBM) \ |
|
443 MM_CMP_OP (mx_el_eq, ==, M1, , M2, , FBM, TBM) \ |
|
444 MM_CMP_OP (mx_el_ne, !=, M1, , M2, , TBM, FBM) |
|
445 |
|
446 #define MM_BOOL_OP_DECLS(M1, M2) \ |
|
447 BOOL_OP_DECL (mx_el_and, M1, M2); \ |
|
448 BOOL_OP_DECL (mx_el_or, M1, M2); |
|
449 |
3504
|
450 #define MM_BOOL_OP(F, OP, M1, M2, ZERO) \ |
2870
|
451 boolMatrix \ |
|
452 F (const M1& m1, const M2& m2) \ |
|
453 { \ |
|
454 boolMatrix r; \ |
|
455 \ |
|
456 int m1_nr = m1.rows (); \ |
|
457 int m1_nc = m1.cols (); \ |
|
458 \ |
|
459 int m2_nr = m2.rows (); \ |
|
460 int m2_nc = m2.cols (); \ |
|
461 \ |
|
462 if (m1_nr == m2_nr && m1_nc == m2_nc) \ |
|
463 { \ |
|
464 if (m1_nr != 0 || m1_nc != 0) \ |
|
465 { \ |
|
466 r.resize (m1_nr, m1_nc); \ |
|
467 \ |
|
468 for (int j = 0; j < m1_nc; j++) \ |
|
469 for (int i = 0; i < m1_nr; i++) \ |
3504
|
470 r.elem(i, j) = (m1.elem(i, j) != ZERO) \ |
|
471 OP (m2.elem(i, j) != ZERO); \ |
2870
|
472 } \ |
|
473 } \ |
|
474 else \ |
|
475 { \ |
|
476 if ((m1_nr != 0 || m1_nc != 0) && (m2_nr != 0 || m2_nc != 0)) \ |
|
477 gripe_nonconformant (#F, m1_nr, m1_nc, m2_nr, m2_nc); \ |
|
478 } \ |
|
479 \ |
|
480 return r; \ |
|
481 } |
|
482 |
3504
|
483 #define MM_BOOL_OPS(M1, M2, ZERO) \ |
|
484 MM_BOOL_OP (mx_el_and, &&, M1, M2, ZERO) \ |
|
485 MM_BOOL_OP (mx_el_or, ||, M1, M2, ZERO) |
2870
|
486 |
|
487 #define MM_OP_DECLS(R, M1, M2) \ |
|
488 MM_BIN_OP_DECLS (R, M1, M2) \ |
|
489 MM_CMP_OP_DECLS (M1, M2) \ |
|
490 MM_BOOL_OP_DECLS (M1, M2) |
|
491 |
4543
|
492 // N-d matrix by scalar operations. |
|
493 |
|
494 #define NDS_BIN_OP_DECLS(R, ND, S) \ |
|
495 BIN_OP_DECL (R, operator +, ND, S); \ |
|
496 BIN_OP_DECL (R, operator -, ND, S); \ |
|
497 BIN_OP_DECL (R, operator *, ND, S); \ |
|
498 BIN_OP_DECL (R, operator /, ND, S); |
|
499 |
|
500 #define NDS_BIN_OP(R, OP, ND, S, F) \ |
|
501 R \ |
|
502 OP (const ND& m, const S& s) \ |
|
503 { \ |
|
504 R r (m.dims ()); \ |
|
505 \ |
|
506 int len = m.length (); \ |
|
507 \ |
|
508 if (len > 0) \ |
|
509 F ## _vs (r.fortran_vec (), m.data (), len, s); \ |
|
510 \ |
|
511 return r; \ |
|
512 } |
|
513 |
|
514 #define NDS_BIN_OPS(R, ND, S) \ |
|
515 NDS_BIN_OP (R, operator +, ND, S, mx_inline_add) \ |
|
516 NDS_BIN_OP (R, operator -, ND, S, mx_inline_subtract) \ |
|
517 NDS_BIN_OP (R, operator *, ND, S, mx_inline_multiply) \ |
|
518 NDS_BIN_OP (R, operator /, ND, S, mx_inline_divide) |
|
519 |
|
520 #define NDS_CMP_OP_DECLS(ND, S) \ |
|
521 NDCMP_OP_DECL (mx_el_lt, ND, S); \ |
|
522 NDCMP_OP_DECL (mx_el_le, ND, S); \ |
|
523 NDCMP_OP_DECL (mx_el_ge, ND, S); \ |
|
524 NDCMP_OP_DECL (mx_el_gt, ND, S); \ |
|
525 NDCMP_OP_DECL (mx_el_eq, ND, S); \ |
|
526 NDCMP_OP_DECL (mx_el_ne, ND, S); |
|
527 |
|
528 #define NDS_CMP_OP(F, OP, ND, NDC, S, SC, EMPTY_RESULT) \ |
|
529 boolNDArray \ |
|
530 F (const ND& m, const S& s) \ |
|
531 { \ |
|
532 boolNDArray r; \ |
|
533 \ |
|
534 int len = m.length (); \ |
|
535 \ |
|
536 if (len == 0) \ |
|
537 r = EMPTY_RESULT; \ |
|
538 else \ |
|
539 { \ |
|
540 r.resize (m.dims ()); \ |
|
541 \ |
|
542 for (int i = 0; i < len; i++) \ |
|
543 r.elem(i) = NDC (m.elem(i)) OP SC (s); \ |
|
544 } \ |
|
545 \ |
|
546 return r; \ |
|
547 } |
|
548 |
|
549 #define NDS_CMP_OPS(ND, NDC, S, SC) \ |
|
550 NDS_CMP_OP (mx_el_lt, <, ND, NDC, S, SC, NBM) \ |
|
551 NDS_CMP_OP (mx_el_le, <=, ND, NDC, S, SC, NBM) \ |
|
552 NDS_CMP_OP (mx_el_ge, >=, ND, NDC, S, SC, NBM) \ |
|
553 NDS_CMP_OP (mx_el_gt, >, ND, NDC, S, SC, NBM) \ |
|
554 NDS_CMP_OP (mx_el_eq, ==, ND, , S, , FBM) \ |
|
555 NDS_CMP_OP (mx_el_ne, !=, ND, , S, , TBM) |
|
556 |
|
557 #define NDS_BOOL_OP_DECLS(ND, S) \ |
|
558 NDBOOL_OP_DECL (mx_el_and, ND, S); \ |
|
559 NDBOOL_OP_DECL (mx_el_or, ND, S); |
|
560 |
|
561 #define NDS_BOOL_OP(F, OP, ND, S, ZERO) \ |
|
562 boolNDArray \ |
|
563 F (const ND& m, const S& s) \ |
|
564 { \ |
|
565 boolNDArray r; \ |
|
566 \ |
|
567 int len = m.length (); \ |
|
568 \ |
|
569 if (len > 0) \ |
|
570 { \ |
|
571 r.resize (m.dims ()); \ |
|
572 \ |
|
573 for (int i = 0; i < len; i++) \ |
|
574 r.elem(i) = (m.elem(i) != ZERO) OP (s != ZERO); \ |
|
575 } \ |
|
576 \ |
|
577 return r; \ |
|
578 } |
|
579 |
|
580 #define NDS_BOOL_OPS(ND, S, ZERO) \ |
|
581 NDS_BOOL_OP (mx_el_and, &&, ND, S, ZERO) \ |
|
582 NDS_BOOL_OP (mx_el_or, ||, ND, S, ZERO) |
|
583 |
|
584 #define NDS_OP_DECLS(R, ND, S) \ |
|
585 NDS_BIN_OP_DECLS (R, ND, S) \ |
|
586 NDS_CMP_OP_DECLS (ND, S) \ |
|
587 NDS_BOOL_OP_DECLS (ND, S) |
|
588 |
|
589 // scalar by N-d matrix operations. |
|
590 |
|
591 #define SND_BIN_OP_DECLS(R, S, ND) \ |
|
592 BIN_OP_DECL (R, operator +, S, ND); \ |
|
593 BIN_OP_DECL (R, operator -, S, ND); \ |
|
594 BIN_OP_DECL (R, operator *, S, ND); \ |
|
595 BIN_OP_DECL (R, operator /, S, ND); |
|
596 |
|
597 #define SND_BIN_OP(R, OP, S, ND, F) \ |
|
598 R \ |
|
599 OP (const S& s, const ND& m) \ |
|
600 { \ |
|
601 R r (m.dims ()); \ |
|
602 \ |
|
603 int len = m.length (); \ |
|
604 \ |
|
605 if (len > 0) \ |
|
606 F ## _sv (r.fortran_vec (), s, m.data (), len); \ |
|
607 \ |
|
608 return r; \ |
|
609 } |
|
610 |
|
611 #define SND_BIN_OPS(R, S, ND) \ |
|
612 SND_BIN_OP (R, operator +, S, ND, mx_inline_add) \ |
|
613 SND_BIN_OP (R, operator -, S, ND, mx_inline_subtract) \ |
|
614 SND_BIN_OP (R, operator *, S, ND, mx_inline_multiply) \ |
|
615 SND_BIN_OP (R, operator /, S, ND, mx_inline_divide) |
|
616 |
|
617 #define SND_CMP_OP_DECLS(S, ND) \ |
|
618 NDCMP_OP_DECL (mx_el_lt, S, ND); \ |
|
619 NDCMP_OP_DECL (mx_el_le, S, ND); \ |
|
620 NDCMP_OP_DECL (mx_el_ge, S, ND); \ |
|
621 NDCMP_OP_DECL (mx_el_gt, S, ND); \ |
|
622 NDCMP_OP_DECL (mx_el_eq, S, ND); \ |
|
623 NDCMP_OP_DECL (mx_el_ne, S, ND); |
|
624 |
|
625 #define SND_CMP_OP(F, OP, S, SC, ND, NDC, EMPTY_RESULT) \ |
|
626 boolNDArray \ |
|
627 F (const S& s, const ND& m) \ |
|
628 { \ |
|
629 boolNDArray r; \ |
|
630 \ |
|
631 int len = m.length (); \ |
|
632 \ |
|
633 if (len == 0) \ |
|
634 r = EMPTY_RESULT; \ |
|
635 else \ |
|
636 { \ |
|
637 r.resize (m.dims ()); \ |
|
638 \ |
|
639 for (int i = 0; i < len; i++) \ |
|
640 r.elem(i) = SC (s) OP NDC (m.elem(i)); \ |
|
641 } \ |
|
642 \ |
|
643 return r; \ |
|
644 } |
|
645 |
|
646 #define SND_CMP_OPS(S, CS, ND, CND) \ |
|
647 SND_CMP_OP (mx_el_lt, <, S, CS, ND, CND, NBM) \ |
|
648 SND_CMP_OP (mx_el_le, <=, S, CS, ND, CND, NBM) \ |
|
649 SND_CMP_OP (mx_el_ge, >=, S, CS, ND, CND, NBM) \ |
|
650 SND_CMP_OP (mx_el_gt, >, S, CS, ND, CND, NBM) \ |
|
651 SND_CMP_OP (mx_el_eq, ==, S, , ND, , FBM) \ |
|
652 SND_CMP_OP (mx_el_ne, !=, S, , ND, , TBM) |
|
653 |
|
654 #define SND_BOOL_OP_DECLS(S, ND) \ |
|
655 NDBOOL_OP_DECL (mx_el_and, S, ND); \ |
|
656 NDBOOL_OP_DECL (mx_el_or, S, ND); |
|
657 |
|
658 #define SND_BOOL_OP(F, OP, S, ND, ZERO) \ |
|
659 boolNDArray \ |
|
660 F (const S& s, const ND& m) \ |
|
661 { \ |
|
662 boolNDArray r; \ |
|
663 \ |
|
664 int len = m.length (); \ |
|
665 \ |
|
666 if (len > 0) \ |
|
667 { \ |
|
668 r.resize (m.dims ()); \ |
|
669 \ |
|
670 for (int i = 0; i < len; i++) \ |
|
671 r.elem(i) = (s != ZERO) OP (m.elem(i) != ZERO); \ |
|
672 } \ |
|
673 \ |
|
674 return r; \ |
|
675 } |
|
676 |
|
677 #define SND_BOOL_OPS(S, ND, ZERO) \ |
|
678 SND_BOOL_OP (mx_el_and, &&, S, ND, ZERO) \ |
|
679 SND_BOOL_OP (mx_el_or, ||, S, ND, ZERO) |
|
680 |
|
681 #define SND_OP_DECLS(R, S, ND) \ |
|
682 SND_BIN_OP_DECLS (R, S, ND) \ |
|
683 SND_CMP_OP_DECLS (S, ND) \ |
|
684 SND_BOOL_OP_DECLS (S, ND) |
|
685 |
|
686 // N-d matrix by N-d matrix operations. |
|
687 |
|
688 #define NDND_BIN_OP_DECLS(R, ND1, ND2) \ |
|
689 BIN_OP_DECL (R, operator +, ND1, ND2); \ |
|
690 BIN_OP_DECL (R, operator -, ND1, ND2); \ |
|
691 BIN_OP_DECL (R, product, ND1, ND2); \ |
|
692 BIN_OP_DECL (R, quotient, ND1, ND2); |
|
693 |
|
694 #define NDND_BIN_OP(R, OP, ND1, ND2, F) \ |
|
695 R \ |
|
696 OP (const ND1& m1, const ND2& m2) \ |
|
697 { \ |
|
698 R r; \ |
|
699 \ |
|
700 dim_vector m1_dims = m1.dims (); \ |
|
701 dim_vector m2_dims = m2.dims (); \ |
|
702 \ |
|
703 if (m1_dims != m2_dims) \ |
|
704 gripe_nonconformant (#OP, m1_dims, m2_dims); \ |
|
705 else \ |
|
706 { \ |
|
707 r.resize (m1_dims); \ |
|
708 \ |
|
709 int len = m1.length (); \ |
|
710 \ |
|
711 if (len > 0) \ |
|
712 F ## _vv (r.fortran_vec (), m1.data (), m2.data (), len); \ |
|
713 } \ |
|
714 \ |
|
715 return r; \ |
|
716 } |
|
717 |
|
718 #define NDND_BIN_OPS(R, ND1, ND2) \ |
|
719 NDND_BIN_OP (R, operator +, ND1, ND2, mx_inline_add) \ |
|
720 NDND_BIN_OP (R, operator -, ND1, ND2, mx_inline_subtract) \ |
|
721 NDND_BIN_OP (R, product, ND1, ND2, mx_inline_multiply) \ |
|
722 NDND_BIN_OP (R, quotient, ND1, ND2, mx_inline_divide) |
|
723 |
|
724 #define NDND_CMP_OP_DECLS(ND1, ND2) \ |
|
725 NDCMP_OP_DECL (mx_el_lt, ND1, ND2); \ |
|
726 NDCMP_OP_DECL (mx_el_le, ND1, ND2); \ |
|
727 NDCMP_OP_DECL (mx_el_ge, ND1, ND2); \ |
|
728 NDCMP_OP_DECL (mx_el_gt, ND1, ND2); \ |
|
729 NDCMP_OP_DECL (mx_el_eq, ND1, ND2); \ |
|
730 NDCMP_OP_DECL (mx_el_ne, ND1, ND2); |
|
731 |
|
732 #define NDND_CMP_OP(F, OP, ND1, C1, ND2, C2, ONE_MT_RESULT, TWO_MT_RESULT) \ |
|
733 boolNDArray \ |
|
734 F (const ND1& m1, const ND2& m2) \ |
|
735 { \ |
|
736 boolNDArray r; \ |
|
737 \ |
|
738 dim_vector m1_dims = m1.dims (); \ |
|
739 dim_vector m2_dims = m2.dims (); \ |
|
740 \ |
|
741 if (m1_dims == m2_dims) \ |
|
742 { \ |
|
743 if (m1_dims.all_zero ()) \ |
|
744 r = TWO_MT_RESULT; \ |
|
745 else \ |
|
746 { \ |
|
747 r.resize (m1_dims); \ |
|
748 \ |
|
749 for (int i = 0; i < m1.length (); i++) \ |
|
750 r.elem(i) = C1 (m1.elem(i)) OP C2 (m2.elem(i)); \ |
|
751 } \ |
|
752 } \ |
|
753 else \ |
|
754 { \ |
|
755 if (m1_dims.all_zero () || m2_dims.all_zero ()) \ |
|
756 r = ONE_MT_RESULT; \ |
|
757 else \ |
|
758 gripe_nonconformant (#F, m1_dims, m2_dims); \ |
|
759 } \ |
|
760 \ |
|
761 return r; \ |
|
762 } |
|
763 |
|
764 #define NDND_CMP_OPS(ND1, C1, ND2, C2) \ |
|
765 NDND_CMP_OP (mx_el_lt, <, ND1, C1, ND2, C2, NBM, NBM) \ |
|
766 NDND_CMP_OP (mx_el_le, <=, ND1, C1, ND2, C2, NBM, NBM) \ |
|
767 NDND_CMP_OP (mx_el_ge, >=, ND1, C1, ND2, C2, NBM, NBM) \ |
|
768 NDND_CMP_OP (mx_el_gt, >, ND1, C1, ND2, C2, NBM, NBM) \ |
|
769 NDND_CMP_OP (mx_el_eq, ==, ND1, , ND2, , FBM, TBM) \ |
|
770 NDND_CMP_OP (mx_el_ne, !=, ND1, , ND2, , TBM, FBM) |
|
771 |
|
772 #define NDND_BOOL_OP_DECLS(ND1, ND2) \ |
|
773 NDBOOL_OP_DECL (mx_el_and, ND1, ND2); \ |
|
774 NDBOOL_OP_DECL (mx_el_or, ND1, ND2); |
|
775 |
|
776 #define NDND_BOOL_OP(F, OP, ND1, ND2, ZERO) \ |
|
777 boolNDArray \ |
|
778 F (const ND1& m1, const ND2& m2) \ |
|
779 { \ |
|
780 boolNDArray r; \ |
|
781 \ |
|
782 dim_vector m1_dims = m1.dims (); \ |
|
783 dim_vector m2_dims = m2.dims (); \ |
|
784 \ |
|
785 if (m1_dims == m2_dims) \ |
|
786 { \ |
|
787 if (! m1_dims.all_zero ()) \ |
|
788 { \ |
|
789 r.resize (m1_dims); \ |
|
790 \ |
|
791 for (int i = 0; i < m1.length (); i++) \ |
|
792 r.elem(i) = (m1.elem(i) != ZERO) OP (m2.elem(i) != ZERO); \ |
|
793 } \ |
|
794 } \ |
|
795 else \ |
|
796 gripe_nonconformant (#F, m1_dims, m2_dims); \ |
|
797 \ |
|
798 return r; \ |
|
799 } |
|
800 |
|
801 #define NDND_BOOL_OPS(ND1, ND2, ZERO) \ |
|
802 NDND_BOOL_OP (mx_el_and, &&, ND1, ND2, ZERO) \ |
|
803 NDND_BOOL_OP (mx_el_or, ||, ND1, ND2, ZERO) |
|
804 |
|
805 #define NDND_OP_DECLS(R, ND1, ND2) \ |
|
806 NDND_BIN_OP_DECLS (R, ND1, ND2) \ |
|
807 NDND_CMP_OP_DECLS (ND1, ND2) \ |
|
808 NDND_BOOL_OP_DECLS (ND1, ND2) |
|
809 |
2870
|
810 // scalar by diagonal matrix operations. |
|
811 |
|
812 #define SDM_BIN_OP_DECLS(R, S, DM) \ |
|
813 BIN_OP_DECL (R, operator +, S, DM); \ |
|
814 BIN_OP_DECL (R, operator -, S, DM); |
|
815 |
|
816 #define SDM_BIN_OP(R, OP, S, DM, OPEQ) \ |
2829
|
817 R \ |
|
818 OP (const S& s, const DM& dm) \ |
|
819 { \ |
|
820 int nr = dm.rows (); \ |
|
821 int nc = dm.cols (); \ |
|
822 \ |
|
823 R r (nr, nc, s); \ |
|
824 \ |
4543
|
825 for (int i = 0; i < dm.length (); i++) \ |
2870
|
826 r.elem(i, i) OPEQ dm.elem(i, i); \ |
2829
|
827 \ |
|
828 return r; \ |
|
829 } |
|
830 |
2870
|
831 #define SDM_BIN_OPS(R, S, DM) \ |
|
832 SDM_BIN_OP (R, operator +, S, DM, +=) \ |
|
833 SDM_BIN_OP (R, operator -, S, DM, -=) |
|
834 |
|
835 #define SDM_OP_DECLS(R, S, DM) \ |
|
836 SDM_BIN_OP_DECLS(R, S, DM) |
2829
|
837 |
2870
|
838 // diagonal matrix by scalar operations. |
|
839 |
|
840 #define DMS_BIN_OP_DECLS(R, DM, S) \ |
|
841 BIN_OP_DECL (R, operator +, DM, S); \ |
|
842 BIN_OP_DECL (R, operator -, DM, S); |
|
843 |
|
844 #define DMS_BIN_OP(R, OP, DM, S, SGN) \ |
2829
|
845 R \ |
|
846 OP (const DM& dm, const S& s) \ |
|
847 { \ |
|
848 int nr = dm.rows (); \ |
|
849 int nc = dm.cols (); \ |
|
850 \ |
|
851 R r (nr, nc, SGN s); \ |
|
852 \ |
4543
|
853 for (int i = 0; i < dm.length (); i++) \ |
2870
|
854 r.elem(i, i) += dm.elem(i, i); \ |
2829
|
855 \ |
|
856 return r; \ |
|
857 } |
|
858 |
2870
|
859 #define DMS_BIN_OPS(R, DM, S) \ |
|
860 DMS_BIN_OP (R, operator +, DM, S, ) \ |
|
861 DMS_BIN_OP (R, operator -, DM, S, -) |
|
862 |
|
863 #define DMS_OP_DECLS(R, DM, S) \ |
|
864 DMS_BIN_OP_DECLS(R, DM, S) |
2829
|
865 |
2870
|
866 // matrix by diagonal matrix operations. |
|
867 |
|
868 #define MDM_BIN_OP_DECLS(R, M, DM) \ |
|
869 BIN_OP_DECL (R, operator +, M, DM); \ |
|
870 BIN_OP_DECL (R, operator -, M, DM); \ |
|
871 BIN_OP_DECL (R, operator *, M, DM); |
|
872 |
|
873 #define MDM_BIN_OP(R, OP, M, DM, OPEQ) \ |
2829
|
874 R \ |
|
875 OP (const M& m, const DM& dm) \ |
|
876 { \ |
|
877 R r; \ |
|
878 \ |
|
879 int m_nr = m.rows (); \ |
|
880 int m_nc = m.cols (); \ |
|
881 \ |
|
882 int dm_nr = dm.rows (); \ |
|
883 int dm_nc = dm.cols (); \ |
|
884 \ |
|
885 if (m_nr != dm_nr || m_nc != dm_nc) \ |
|
886 gripe_nonconformant (#OP, m_nr, m_nc, dm_nr, dm_nc); \ |
|
887 else \ |
|
888 { \ |
|
889 r.resize (m_nr, m_nc); \ |
|
890 \ |
|
891 if (m_nr > 0 && m_nc > 0) \ |
|
892 { \ |
3585
|
893 r = R (m); \ |
2829
|
894 \ |
|
895 int len = dm.length (); \ |
|
896 \ |
|
897 for (int i = 0; i < len; i++) \ |
2870
|
898 r.elem(i, i) OPEQ dm.elem(i, i); \ |
2829
|
899 } \ |
|
900 } \ |
|
901 \ |
|
902 return r; \ |
|
903 } |
|
904 |
3504
|
905 #define MDM_MULTIPLY_OP(R, M, DM, ZERO) \ |
2829
|
906 R \ |
|
907 operator * (const M& m, const DM& dm) \ |
|
908 { \ |
|
909 R r; \ |
|
910 \ |
|
911 int m_nr = m.rows (); \ |
|
912 int m_nc = m.cols (); \ |
|
913 \ |
|
914 int dm_nr = dm.rows (); \ |
|
915 int dm_nc = dm.cols (); \ |
|
916 \ |
|
917 if (m_nc != dm_nr) \ |
|
918 gripe_nonconformant ("operator *", m_nr, m_nc, dm_nr, dm_nc); \ |
|
919 else \ |
|
920 { \ |
3504
|
921 r.resize (m_nr, dm_nc, ZERO); \ |
2829
|
922 \ |
3176
|
923 if (m_nr > 0 && m_nc > 0 && dm_nc > 0) \ |
2829
|
924 { \ |
4543
|
925 int len = dm.length (); \ |
|
926 \ |
|
927 for (int j = 0; j < len; j++) \ |
2829
|
928 { \ |
2870
|
929 if (dm.elem(j, j) == 1.0) \ |
2829
|
930 { \ |
|
931 for (int i = 0; i < m_nr; i++) \ |
2870
|
932 r.elem(i, j) = m.elem(i, j); \ |
2829
|
933 } \ |
3504
|
934 else if (dm.elem(j, j) != ZERO) \ |
2829
|
935 { \ |
|
936 for (int i = 0; i < m_nr; i++) \ |
2870
|
937 r.elem(i, j) = dm.elem(j, j) * m.elem(i, j); \ |
2829
|
938 } \ |
|
939 } \ |
|
940 } \ |
|
941 } \ |
|
942 \ |
|
943 return r; \ |
|
944 } |
|
945 |
3504
|
946 #define MDM_BIN_OPS(R, M, DM, ZERO) \ |
2870
|
947 MDM_BIN_OP (R, operator +, M, DM, +=) \ |
|
948 MDM_BIN_OP (R, operator -, M, DM, -=) \ |
3504
|
949 MDM_MULTIPLY_OP (R, M, DM, ZERO) |
2829
|
950 |
2870
|
951 #define MDM_OP_DECLS(R, M, DM) \ |
|
952 MDM_BIN_OP_DECLS(R, M, DM) |
|
953 |
|
954 // diagonal matrix by matrix operations. |
|
955 |
|
956 #define DMM_BIN_OP_DECLS(R, DM, M) \ |
|
957 BIN_OP_DECL (R, operator +, DM, M); \ |
|
958 BIN_OP_DECL (R, operator -, DM, M); \ |
|
959 BIN_OP_DECL (R, operator *, DM, M); |
|
960 |
3585
|
961 #define DMM_BIN_OP(R, OP, DM, M, OPEQ, PREOP) \ |
2829
|
962 R \ |
|
963 OP (const DM& dm, const M& m) \ |
|
964 { \ |
|
965 R r; \ |
|
966 \ |
|
967 int dm_nr = dm.rows (); \ |
|
968 int dm_nc = dm.cols (); \ |
|
969 \ |
|
970 int m_nr = m.rows (); \ |
|
971 int m_nc = m.cols (); \ |
|
972 \ |
|
973 if (dm_nr != m_nr || dm_nc != m_nc) \ |
|
974 gripe_nonconformant (#OP, dm_nr, dm_nc, m_nr, m_nc); \ |
|
975 else \ |
|
976 { \ |
|
977 if (m_nr > 0 && m_nc > 0) \ |
|
978 { \ |
3585
|
979 r = R (PREOP m); \ |
2829
|
980 \ |
|
981 int len = dm.length (); \ |
|
982 \ |
|
983 for (int i = 0; i < len; i++) \ |
2870
|
984 r.elem(i, i) OPEQ dm.elem(i, i); \ |
2829
|
985 } \ |
|
986 else \ |
|
987 r.resize (m_nr, m_nc); \ |
|
988 } \ |
|
989 \ |
|
990 return r; \ |
|
991 } |
|
992 |
3504
|
993 #define DMM_MULTIPLY_OP(R, DM, M, ZERO) \ |
2829
|
994 R \ |
|
995 operator * (const DM& dm, const M& m) \ |
|
996 { \ |
|
997 R r; \ |
|
998 \ |
|
999 int dm_nr = dm.rows (); \ |
|
1000 int dm_nc = dm.cols (); \ |
|
1001 \ |
|
1002 int m_nr = m.rows (); \ |
|
1003 int m_nc = m.cols (); \ |
|
1004 \ |
|
1005 if (dm_nc != m_nr) \ |
|
1006 gripe_nonconformant ("operator *", dm_nr, dm_nc, m_nr, m_nc); \ |
|
1007 else \ |
|
1008 { \ |
3504
|
1009 r.resize (dm_nr, m_nc, ZERO); \ |
2829
|
1010 \ |
|
1011 if (dm_nr > 0 && dm_nc > 0 && m_nc > 0) \ |
|
1012 { \ |
4543
|
1013 int len = dm.length (); \ |
|
1014 \ |
|
1015 for (int i = 0; i < len; i++) \ |
2829
|
1016 { \ |
2870
|
1017 if (dm.elem(i, i) == 1.0) \ |
2829
|
1018 { \ |
|
1019 for (int j = 0; j < m_nc; j++) \ |
2870
|
1020 r.elem(i, j) = m.elem(i, j); \ |
2829
|
1021 } \ |
3504
|
1022 else if (dm.elem(i, i) != ZERO) \ |
2829
|
1023 { \ |
|
1024 for (int j = 0; j < m_nc; j++) \ |
2870
|
1025 r.elem(i, j) = dm.elem(i, i) * m.elem(i, j); \ |
2829
|
1026 } \ |
|
1027 } \ |
|
1028 } \ |
|
1029 } \ |
|
1030 \ |
|
1031 return r; \ |
|
1032 } |
|
1033 |
3504
|
1034 #define DMM_BIN_OPS(R, DM, M, ZERO) \ |
3585
|
1035 DMM_BIN_OP (R, operator +, DM, M, +=, ) \ |
|
1036 DMM_BIN_OP (R, operator -, DM, M, +=, -) \ |
|
1037 DMM_MULTIPLY_OP (R, DM, M, ZERO) |
2829
|
1038 |
2870
|
1039 #define DMM_OP_DECLS(R, DM, M) \ |
|
1040 DMM_BIN_OP_DECLS(R, DM, M) |
|
1041 |
|
1042 // diagonal matrix by diagonal matrix operations. |
2829
|
1043 |
2870
|
1044 #define DMDM_BIN_OP_DECLS(R, DM1, DM2) \ |
|
1045 BIN_OP_DECL (R, operator +, DM1, DM2); \ |
|
1046 BIN_OP_DECL (R, operator -, DM1, DM2); \ |
|
1047 BIN_OP_DECL (R, product, DM1, DM2); |
|
1048 |
|
1049 #define DMDM_BIN_OP(R, OP, DM1, DM2, F) \ |
2829
|
1050 R \ |
|
1051 OP (const DM1& dm1, const DM2& dm2) \ |
|
1052 { \ |
|
1053 R r; \ |
|
1054 \ |
|
1055 int dm1_nr = dm1.rows (); \ |
|
1056 int dm1_nc = dm1.cols (); \ |
|
1057 \ |
|
1058 int dm2_nr = dm2.rows (); \ |
|
1059 int dm2_nc = dm2.cols (); \ |
|
1060 \ |
|
1061 if (dm1_nr != dm2_nr || dm1_nc != dm2_nc) \ |
|
1062 gripe_nonconformant (#OP, dm1_nr, dm1_nc, dm2_nr, dm2_nc); \ |
|
1063 else \ |
|
1064 { \ |
|
1065 r.resize (dm1_nr, dm1_nc); \ |
|
1066 \ |
|
1067 if (dm1_nr > 0 && dm1_nc > 0) \ |
|
1068 F ## _vv (r.fortran_vec (), dm1.data (), dm2.data (), \ |
|
1069 dm1_nr * dm2_nc); \ |
|
1070 } \ |
|
1071 \ |
|
1072 return r; \ |
|
1073 } |
|
1074 |
2870
|
1075 #define DMDM_BIN_OPS(R, DM1, DM2) \ |
3769
|
1076 DMDM_BIN_OP (R, operator +, DM1, DM2, mx_inline_add) \ |
|
1077 DMDM_BIN_OP (R, operator -, DM1, DM2, mx_inline_subtract) \ |
|
1078 DMDM_BIN_OP (R, product, DM1, DM2, mx_inline_multiply) |
2870
|
1079 |
|
1080 #define DMDM_OP_DECLS(R, DM1, DM2) \ |
|
1081 DMDM_BIN_OP_DECLS (R, DM1, DM2) |
2829
|
1082 |
3582
|
1083 #endif |
|
1084 |
2829
|
1085 /* |
|
1086 ;;; Local Variables: *** |
|
1087 ;;; mode: C++ *** |
|
1088 ;;; End: *** |
|
1089 */ |