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