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