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 |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
2829
|
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 |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
2829
|
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 |
6708
|
28 #define BIN_OP_DECL(R, OP, X, Y, API) \ |
|
29 extern API R OP (const X&, const Y&) |
2829
|
30 |
2870
|
31 class boolMatrix; |
4543
|
32 class boolNDArray; |
2870
|
33 |
6708
|
34 #define CMP_OP_DECL(OP, X, Y, API) \ |
|
35 extern API boolMatrix OP (const X&, const Y&) |
2870
|
36 |
6708
|
37 #define NDCMP_OP_DECL(OP, X, Y, API) \ |
|
38 extern API boolNDArray OP (const X&, const Y&) |
4543
|
39 |
6708
|
40 #define BOOL_OP_DECL(OP, X, Y, API) \ |
|
41 extern API boolMatrix OP (const X&, const Y&) |
2870
|
42 |
6708
|
43 #define NDBOOL_OP_DECL(OP, X, Y, API) \ |
|
44 extern API boolNDArray OP (const X&, const Y&) |
4543
|
45 |
2870
|
46 // vector by scalar operations. |
|
47 |
6708
|
48 #define VS_BIN_OP_DECLS(R, V, S, API) \ |
|
49 BIN_OP_DECL (R, operator +, V, S, API); \ |
|
50 BIN_OP_DECL (R, operator -, V, S, API); \ |
|
51 BIN_OP_DECL (R, operator *, V, S, API); \ |
|
52 BIN_OP_DECL (R, operator /, V, S, API); |
2870
|
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 |
6708
|
74 #define VS_OP_DECLS(R, V, S, API) \ |
|
75 VS_BIN_OP_DECLS(R, V, S, API) |
3582
|
76 |
2870
|
77 // scalar by vector by operations. |
|
78 |
6708
|
79 #define SV_BIN_OP_DECLS(R, S, V, API) \ |
|
80 BIN_OP_DECL (R, operator +, S, V, API); \ |
|
81 BIN_OP_DECL (R, operator -, S, V, API); \ |
|
82 BIN_OP_DECL (R, operator *, S, V, API); \ |
|
83 BIN_OP_DECL (R, operator /, S, V, API); |
2870
|
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 |
6708
|
105 #define SV_OP_DECLS(R, S, V, API) \ |
|
106 SV_BIN_OP_DECLS(R, S, V, API) |
3582
|
107 |
2870
|
108 // vector by vector operations. |
|
109 |
6708
|
110 #define VV_BIN_OP_DECLS(R, V1, V2, API) \ |
|
111 BIN_OP_DECL (R, operator +, V1, V2, API); \ |
|
112 BIN_OP_DECL (R, operator -, V1, V2, API); \ |
|
113 BIN_OP_DECL (R, product, V1, V2, API); \ |
|
114 BIN_OP_DECL (R, quotient, V1, V2, API); |
2870
|
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 |
6708
|
144 #define VV_OP_DECLS(R, V1, V2, API) \ |
|
145 VV_BIN_OP_DECLS(R, V1, V2, API) |
2870
|
146 |
|
147 // matrix by scalar operations. |
|
148 |
6708
|
149 #define MS_BIN_OP_DECLS(R, M, S, API) \ |
|
150 BIN_OP_DECL (R, operator +, M, S, API); \ |
|
151 BIN_OP_DECL (R, operator -, M, S, API); \ |
|
152 BIN_OP_DECL (R, operator *, M, S, API); \ |
|
153 BIN_OP_DECL (R, operator /, M, S, API); |
2829
|
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 |
6708
|
176 #define MS_CMP_OP_DECLS(M, S, API) \ |
|
177 CMP_OP_DECL (mx_el_lt, M, S, API); \ |
|
178 CMP_OP_DECL (mx_el_le, M, S, API); \ |
|
179 CMP_OP_DECL (mx_el_ge, M, S, API); \ |
|
180 CMP_OP_DECL (mx_el_gt, M, S, API); \ |
|
181 CMP_OP_DECL (mx_el_eq, M, S, API); \ |
|
182 CMP_OP_DECL (mx_el_ne, M, S, API); |
2870
|
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 |
6708
|
213 #define MS_BOOL_OP_DECLS(M, S, API) \ |
|
214 BOOL_OP_DECL (mx_el_and, M, S, API); \ |
|
215 BOOL_OP_DECL (mx_el_or, M, S, API); \ |
2870
|
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 |
6708
|
245 #define MS_OP_DECLS(R, M, S, API) \ |
|
246 MS_BIN_OP_DECLS (R, M, S, API) \ |
|
247 MS_CMP_OP_DECLS (M, S, API) \ |
|
248 MS_BOOL_OP_DECLS (M, S, API) \ |
2870
|
249 |
|
250 // scalar by matrix operations. |
|
251 |
6708
|
252 #define SM_BIN_OP_DECLS(R, S, M, API) \ |
|
253 BIN_OP_DECL (R, operator +, S, M, API); \ |
|
254 BIN_OP_DECL (R, operator -, S, M, API); \ |
|
255 BIN_OP_DECL (R, operator *, S, M, API); \ |
|
256 BIN_OP_DECL (R, operator /, S, M, API); |
2870
|
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 |
6708
|
279 #define SM_CMP_OP_DECLS(S, M, API) \ |
|
280 CMP_OP_DECL (mx_el_lt, S, M, API); \ |
|
281 CMP_OP_DECL (mx_el_le, S, M, API); \ |
|
282 CMP_OP_DECL (mx_el_ge, S, M, API); \ |
|
283 CMP_OP_DECL (mx_el_gt, S, M, API); \ |
|
284 CMP_OP_DECL (mx_el_eq, S, M, API); \ |
|
285 CMP_OP_DECL (mx_el_ne, S, M, API); |
2870
|
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 |
6708
|
316 #define SM_BOOL_OP_DECLS(S, M, API) \ |
|
317 BOOL_OP_DECL (mx_el_and, S, M, API); \ |
|
318 BOOL_OP_DECL (mx_el_or, S, M, API); \ |
2870
|
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 |
6708
|
348 #define SM_OP_DECLS(R, S, M, API) \ |
|
349 SM_BIN_OP_DECLS (R, S, M, API) \ |
|
350 SM_CMP_OP_DECLS (S, M, API) \ |
|
351 SM_BOOL_OP_DECLS (S, M, API) \ |
2870
|
352 |
|
353 // matrix by matrix operations. |
|
354 |
6708
|
355 #define MM_BIN_OP_DECLS(R, M1, M2, API) \ |
|
356 BIN_OP_DECL (R, operator +, M1, M2, API); \ |
|
357 BIN_OP_DECL (R, operator -, M1, M2, API); \ |
|
358 BIN_OP_DECL (R, product, M1, M2, API); \ |
|
359 BIN_OP_DECL (R, quotient, M1, M2, API); |
2870
|
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 |
6708
|
392 #define MM_CMP_OP_DECLS(M1, M2, API) \ |
|
393 CMP_OP_DECL (mx_el_lt, M1, M2, API); \ |
|
394 CMP_OP_DECL (mx_el_le, M1, M2, API); \ |
|
395 CMP_OP_DECL (mx_el_ge, M1, M2, API); \ |
|
396 CMP_OP_DECL (mx_el_gt, M1, M2, API); \ |
|
397 CMP_OP_DECL (mx_el_eq, M1, M2, API); \ |
|
398 CMP_OP_DECL (mx_el_ne, M1, M2, API); |
2870
|
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 |
6708
|
434 #define MM_BOOL_OP_DECLS(M1, M2, API) \ |
|
435 BOOL_OP_DECL (mx_el_and, M1, M2, API); \ |
|
436 BOOL_OP_DECL (mx_el_or, M1, M2, API); |
2870
|
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 |
6708
|
478 #define MM_OP_DECLS(R, M1, M2, API) \ |
|
479 MM_BIN_OP_DECLS (R, M1, M2, API) \ |
|
480 MM_CMP_OP_DECLS (M1, M2, API) \ |
|
481 MM_BOOL_OP_DECLS (M1, M2, API) |
2870
|
482 |
4543
|
483 // N-d matrix by scalar operations. |
|
484 |
6708
|
485 #define NDS_BIN_OP_DECLS(R, ND, S, API) \ |
|
486 BIN_OP_DECL (R, operator +, ND, S, API); \ |
|
487 BIN_OP_DECL (R, operator -, ND, S, API); \ |
|
488 BIN_OP_DECL (R, operator *, ND, S, API); \ |
|
489 BIN_OP_DECL (R, operator /, ND, S, API); |
4543
|
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 |
6708
|
511 #define NDS_CMP_OP_DECLS(ND, S, API) \ |
|
512 NDCMP_OP_DECL (mx_el_lt, ND, S, API); \ |
|
513 NDCMP_OP_DECL (mx_el_le, ND, S, API); \ |
|
514 NDCMP_OP_DECL (mx_el_ge, ND, S, API); \ |
|
515 NDCMP_OP_DECL (mx_el_gt, ND, S, API); \ |
|
516 NDCMP_OP_DECL (mx_el_eq, ND, S, API); \ |
|
517 NDCMP_OP_DECL (mx_el_ne, ND, S, API); |
4543
|
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 |
6119
|
543 #define NDS_CMP_OP1(F, OP, ND, NDC, S, SC, SPEC) \ |
|
544 boolNDArray \ |
|
545 F (const ND& m, const S& s) \ |
|
546 { \ |
|
547 boolNDArray r; \ |
|
548 \ |
|
549 int len = m.length (); \ |
|
550 \ |
|
551 r.resize (m.dims ()); \ |
|
552 \ |
|
553 for (int i = 0; i < len; i++) \ |
|
554 r.elem(i) = operator OP <SPEC> (NDC (m.elem(i)), SC (s)); \ |
|
555 \ |
|
556 return r; \ |
|
557 } |
|
558 |
|
559 #define NDS_CMP_OPS1(ND, NDC, S, SC, SPEC) \ |
|
560 NDS_CMP_OP1 (mx_el_lt, <, ND, NDC, S, SC, SPEC) \ |
|
561 NDS_CMP_OP1 (mx_el_le, <=, ND, NDC, S, SC, SPEC) \ |
|
562 NDS_CMP_OP1 (mx_el_ge, >=, ND, NDC, S, SC, SPEC) \ |
|
563 NDS_CMP_OP1 (mx_el_gt, >, ND, NDC, S, SC, SPEC) \ |
|
564 NDS_CMP_OP1 (mx_el_eq, ==, ND, , S, , SPEC) \ |
|
565 NDS_CMP_OP1 (mx_el_ne, !=, ND, , S, , SPEC) |
|
566 |
|
567 #define NDS_CMP_OP2(F, OP, ND, NDC, S, SC, SPEC1, SPEC2) \ |
|
568 boolNDArray \ |
|
569 F (const ND& m, const S& s) \ |
|
570 { \ |
|
571 boolNDArray r; \ |
|
572 \ |
|
573 int len = m.length (); \ |
|
574 \ |
|
575 r.resize (m.dims ()); \ |
|
576 \ |
|
577 for (int i = 0; i < len; i++) \ |
|
578 r.elem(i) = operator OP <SPEC1,SPEC2> (NDC (m.elem(i)), SC (s)); \ |
|
579 \ |
|
580 return r; \ |
|
581 } |
|
582 |
|
583 #define NDS_CMP_OPS2(ND, NDC, S, SC, SPEC1, SPEC2) \ |
|
584 NDS_CMP_OP2 (mx_el_lt, <, ND, NDC, S, SC, SPEC1, SPEC2) \ |
|
585 NDS_CMP_OP2 (mx_el_le, <=, ND, NDC, S, SC, SPEC1, SPEC2) \ |
|
586 NDS_CMP_OP2 (mx_el_ge, >=, ND, NDC, S, SC, SPEC1, SPEC2) \ |
|
587 NDS_CMP_OP2 (mx_el_gt, >, ND, NDC, S, SC, SPEC1, SPEC2) \ |
|
588 NDS_CMP_OP2 (mx_el_eq, ==, ND, , S, , SPEC1, SPEC2) \ |
|
589 NDS_CMP_OP2 (mx_el_ne, !=, ND, , S, , SPEC1, SPEC2) |
|
590 |
6708
|
591 #define NDS_BOOL_OP_DECLS(ND, S, API) \ |
|
592 NDBOOL_OP_DECL (mx_el_and, ND, S, API); \ |
|
593 NDBOOL_OP_DECL (mx_el_or, ND, S, API); |
4543
|
594 |
5030
|
595 #define NDS_BOOL_OP(F, OP, ND, S, LHS_ZERO, RHS_ZERO) \ |
4543
|
596 boolNDArray \ |
|
597 F (const ND& m, const S& s) \ |
|
598 { \ |
|
599 boolNDArray r; \ |
|
600 \ |
|
601 int len = m.length (); \ |
|
602 \ |
|
603 if (len > 0) \ |
|
604 { \ |
|
605 r.resize (m.dims ()); \ |
|
606 \ |
|
607 for (int i = 0; i < len; i++) \ |
5030
|
608 r.elem(i) = (m.elem(i) != LHS_ZERO) OP (s != RHS_ZERO); \ |
4543
|
609 } \ |
|
610 \ |
|
611 return r; \ |
|
612 } |
|
613 |
5030
|
614 #define NDS_BOOL_OPS2(ND, S, LHS_ZERO, RHS_ZERO) \ |
|
615 NDS_BOOL_OP (mx_el_and, &&, ND, S, LHS_ZERO, RHS_ZERO) \ |
|
616 NDS_BOOL_OP (mx_el_or, ||, ND, S, LHS_ZERO, RHS_ZERO) |
|
617 |
4543
|
618 #define NDS_BOOL_OPS(ND, S, ZERO) \ |
5030
|
619 NDS_BOOL_OPS2(ND, S, ZERO, ZERO) |
4543
|
620 |
6708
|
621 #define NDS_OP_DECLS(R, ND, S, API) \ |
|
622 NDS_BIN_OP_DECLS (R, ND, S, API) \ |
|
623 NDS_CMP_OP_DECLS (ND, S, API) \ |
|
624 NDS_BOOL_OP_DECLS (ND, S, API) |
4543
|
625 |
|
626 // scalar by N-d matrix operations. |
|
627 |
6708
|
628 #define SND_BIN_OP_DECLS(R, S, ND, API) \ |
|
629 BIN_OP_DECL (R, operator +, S, ND, API); \ |
|
630 BIN_OP_DECL (R, operator -, S, ND, API); \ |
|
631 BIN_OP_DECL (R, operator *, S, ND, API); \ |
|
632 BIN_OP_DECL (R, operator /, S, ND, API); |
4543
|
633 |
|
634 #define SND_BIN_OP(R, OP, S, ND, F) \ |
|
635 R \ |
|
636 OP (const S& s, const ND& m) \ |
|
637 { \ |
|
638 R r (m.dims ()); \ |
|
639 \ |
|
640 int len = m.length (); \ |
|
641 \ |
|
642 if (len > 0) \ |
|
643 F ## _sv (r.fortran_vec (), s, m.data (), len); \ |
|
644 \ |
|
645 return r; \ |
|
646 } |
|
647 |
|
648 #define SND_BIN_OPS(R, S, ND) \ |
|
649 SND_BIN_OP (R, operator +, S, ND, mx_inline_add) \ |
|
650 SND_BIN_OP (R, operator -, S, ND, mx_inline_subtract) \ |
|
651 SND_BIN_OP (R, operator *, S, ND, mx_inline_multiply) \ |
|
652 SND_BIN_OP (R, operator /, S, ND, mx_inline_divide) |
|
653 |
6708
|
654 #define SND_CMP_OP_DECLS(S, ND, API) \ |
|
655 NDCMP_OP_DECL (mx_el_lt, S, ND, API); \ |
|
656 NDCMP_OP_DECL (mx_el_le, S, ND, API); \ |
|
657 NDCMP_OP_DECL (mx_el_ge, S, ND, API); \ |
|
658 NDCMP_OP_DECL (mx_el_gt, S, ND, API); \ |
|
659 NDCMP_OP_DECL (mx_el_eq, S, ND, API); \ |
|
660 NDCMP_OP_DECL (mx_el_ne, S, ND, API); |
4543
|
661 |
4826
|
662 #define SND_CMP_OP(F, OP, S, SC, ND, NDC) \ |
4543
|
663 boolNDArray \ |
|
664 F (const S& s, const ND& m) \ |
|
665 { \ |
|
666 boolNDArray r; \ |
|
667 \ |
|
668 int len = m.length (); \ |
|
669 \ |
4826
|
670 r.resize (m.dims ()); \ |
4543
|
671 \ |
4826
|
672 for (int i = 0; i < len; i++) \ |
|
673 r.elem(i) = SC (s) OP NDC (m.elem(i)); \ |
4543
|
674 \ |
|
675 return r; \ |
|
676 } |
|
677 |
|
678 #define SND_CMP_OPS(S, CS, ND, CND) \ |
4826
|
679 SND_CMP_OP (mx_el_lt, <, S, CS, ND, CND) \ |
|
680 SND_CMP_OP (mx_el_le, <=, S, CS, ND, CND) \ |
|
681 SND_CMP_OP (mx_el_ge, >=, S, CS, ND, CND) \ |
|
682 SND_CMP_OP (mx_el_gt, >, S, CS, ND, CND) \ |
|
683 SND_CMP_OP (mx_el_eq, ==, S, , ND, ) \ |
|
684 SND_CMP_OP (mx_el_ne, !=, S, , ND, ) |
4543
|
685 |
6119
|
686 #define SND_CMP_OP1(F, OP, S, SC, ND, NDC, SPEC) \ |
|
687 boolNDArray \ |
|
688 F (const S& s, const ND& m) \ |
|
689 { \ |
|
690 boolNDArray r; \ |
|
691 \ |
|
692 int len = m.length (); \ |
|
693 \ |
|
694 r.resize (m.dims ()); \ |
|
695 \ |
|
696 for (int i = 0; i < len; i++) \ |
|
697 r.elem(i) = operator OP <SPEC> (SC (s), NDC (m.elem(i))); \ |
|
698 \ |
|
699 return r; \ |
|
700 } |
|
701 |
|
702 #define SND_CMP_OPS1(S, CS, ND, CND, SPEC) \ |
|
703 SND_CMP_OP1 (mx_el_lt, <, S, CS, ND, CND, SPEC) \ |
|
704 SND_CMP_OP1 (mx_el_le, <=, S, CS, ND, CND, SPEC) \ |
|
705 SND_CMP_OP1 (mx_el_ge, >=, S, CS, ND, CND, SPEC) \ |
|
706 SND_CMP_OP1 (mx_el_gt, >, S, CS, ND, CND, SPEC) \ |
|
707 SND_CMP_OP1 (mx_el_eq, ==, S, , ND, , SPEC) \ |
|
708 SND_CMP_OP1 (mx_el_ne, !=, S, , ND, , SPEC) |
|
709 |
|
710 #define SND_CMP_OP2(F, OP, S, SC, ND, NDC, SPEC1, SPEC2) \ |
|
711 boolNDArray \ |
|
712 F (const S& s, const ND& m) \ |
|
713 { \ |
|
714 boolNDArray r; \ |
|
715 \ |
|
716 int len = m.length (); \ |
|
717 \ |
|
718 r.resize (m.dims ()); \ |
|
719 \ |
|
720 for (int i = 0; i < len; i++) \ |
|
721 r.elem(i) = operator OP <SPEC1, SPEC2> (SC (s), NDC (m.elem(i))); \ |
|
722 \ |
|
723 return r; \ |
|
724 } |
|
725 |
|
726 #define SND_CMP_OPS2(S, CS, ND, CND, SPEC1, SPEC2) \ |
|
727 SND_CMP_OP2 (mx_el_lt, <, S, CS, ND, CND, SPEC1, SPEC2) \ |
|
728 SND_CMP_OP2 (mx_el_le, <=, S, CS, ND, CND, SPEC1, SPEC2) \ |
|
729 SND_CMP_OP2 (mx_el_ge, >=, S, CS, ND, CND, SPEC1, SPEC2) \ |
|
730 SND_CMP_OP2 (mx_el_gt, >, S, CS, ND, CND, SPEC1, SPEC2) \ |
|
731 SND_CMP_OP2 (mx_el_eq, ==, S, , ND, , SPEC1, SPEC2) \ |
|
732 SND_CMP_OP2 (mx_el_ne, !=, S, , ND, , SPEC1, SPEC2) |
|
733 |
6708
|
734 #define SND_BOOL_OP_DECLS(S, ND, API) \ |
|
735 NDBOOL_OP_DECL (mx_el_and, S, ND, API); \ |
|
736 NDBOOL_OP_DECL (mx_el_or, S, ND, API); |
4543
|
737 |
5030
|
738 #define SND_BOOL_OP(F, OP, S, ND, LHS_ZERO, RHS_ZERO) \ |
4543
|
739 boolNDArray \ |
|
740 F (const S& s, const ND& m) \ |
|
741 { \ |
|
742 boolNDArray r; \ |
|
743 \ |
|
744 int len = m.length (); \ |
|
745 \ |
|
746 if (len > 0) \ |
|
747 { \ |
|
748 r.resize (m.dims ()); \ |
|
749 \ |
|
750 for (int i = 0; i < len; i++) \ |
5030
|
751 r.elem(i) = (s != LHS_ZERO) OP (m.elem(i) != RHS_ZERO); \ |
4543
|
752 } \ |
|
753 \ |
|
754 return r; \ |
|
755 } |
|
756 |
5030
|
757 #define SND_BOOL_OPS2(S, ND, LHS_ZERO, RHS_ZERO) \ |
|
758 SND_BOOL_OP (mx_el_and, &&, S, ND, LHS_ZERO, RHS_ZERO) \ |
|
759 SND_BOOL_OP (mx_el_or, ||, S, ND, LHS_ZERO, RHS_ZERO) |
|
760 |
4543
|
761 #define SND_BOOL_OPS(S, ND, ZERO) \ |
5030
|
762 SND_BOOL_OPS2(S, ND, ZERO, ZERO) |
4543
|
763 |
6708
|
764 #define SND_OP_DECLS(R, S, ND, API) \ |
|
765 SND_BIN_OP_DECLS (R, S, ND, API) \ |
|
766 SND_CMP_OP_DECLS (S, ND, API) \ |
|
767 SND_BOOL_OP_DECLS (S, ND, API) |
4543
|
768 |
|
769 // N-d matrix by N-d matrix operations. |
|
770 |
6708
|
771 #define NDND_BIN_OP_DECLS(R, ND1, ND2, API) \ |
|
772 BIN_OP_DECL (R, operator +, ND1, ND2, API); \ |
|
773 BIN_OP_DECL (R, operator -, ND1, ND2, API); \ |
|
774 BIN_OP_DECL (R, product, ND1, ND2, API); \ |
|
775 BIN_OP_DECL (R, quotient, ND1, ND2, API); |
4543
|
776 |
|
777 #define NDND_BIN_OP(R, OP, ND1, ND2, F) \ |
|
778 R \ |
|
779 OP (const ND1& m1, const ND2& m2) \ |
|
780 { \ |
|
781 R r; \ |
|
782 \ |
|
783 dim_vector m1_dims = m1.dims (); \ |
|
784 dim_vector m2_dims = m2.dims (); \ |
|
785 \ |
|
786 if (m1_dims != m2_dims) \ |
|
787 gripe_nonconformant (#OP, m1_dims, m2_dims); \ |
|
788 else \ |
|
789 { \ |
|
790 r.resize (m1_dims); \ |
|
791 \ |
|
792 int len = m1.length (); \ |
|
793 \ |
|
794 if (len > 0) \ |
|
795 F ## _vv (r.fortran_vec (), m1.data (), m2.data (), len); \ |
|
796 } \ |
|
797 \ |
|
798 return r; \ |
|
799 } |
|
800 |
|
801 #define NDND_BIN_OPS(R, ND1, ND2) \ |
|
802 NDND_BIN_OP (R, operator +, ND1, ND2, mx_inline_add) \ |
|
803 NDND_BIN_OP (R, operator -, ND1, ND2, mx_inline_subtract) \ |
|
804 NDND_BIN_OP (R, product, ND1, ND2, mx_inline_multiply) \ |
|
805 NDND_BIN_OP (R, quotient, ND1, ND2, mx_inline_divide) |
|
806 |
6708
|
807 #define NDND_CMP_OP_DECLS(ND1, ND2, API) \ |
|
808 NDCMP_OP_DECL (mx_el_lt, ND1, ND2, API); \ |
|
809 NDCMP_OP_DECL (mx_el_le, ND1, ND2, API); \ |
|
810 NDCMP_OP_DECL (mx_el_ge, ND1, ND2, API); \ |
|
811 NDCMP_OP_DECL (mx_el_gt, ND1, ND2, API); \ |
|
812 NDCMP_OP_DECL (mx_el_eq, ND1, ND2, API); \ |
|
813 NDCMP_OP_DECL (mx_el_ne, ND1, ND2, API); |
4543
|
814 |
4826
|
815 #define NDND_CMP_OP(F, OP, ND1, C1, ND2, C2) \ |
4543
|
816 boolNDArray \ |
|
817 F (const ND1& m1, const ND2& m2) \ |
|
818 { \ |
|
819 boolNDArray r; \ |
|
820 \ |
|
821 dim_vector m1_dims = m1.dims (); \ |
|
822 dim_vector m2_dims = m2.dims (); \ |
|
823 \ |
|
824 if (m1_dims == m2_dims) \ |
|
825 { \ |
4826
|
826 r.resize (m1_dims); \ |
4543
|
827 \ |
4826
|
828 for (int i = 0; i < m1.length (); i++) \ |
|
829 r.elem(i) = C1 (m1.elem(i)) OP C2 (m2.elem(i)); \ |
4543
|
830 } \ |
|
831 else \ |
4826
|
832 gripe_nonconformant (#F, m1_dims, m2_dims); \ |
4543
|
833 \ |
|
834 return r; \ |
|
835 } |
|
836 |
|
837 #define NDND_CMP_OPS(ND1, C1, ND2, C2) \ |
4826
|
838 NDND_CMP_OP (mx_el_lt, <, ND1, C1, ND2, C2) \ |
|
839 NDND_CMP_OP (mx_el_le, <=, ND1, C1, ND2, C2) \ |
|
840 NDND_CMP_OP (mx_el_ge, >=, ND1, C1, ND2, C2) \ |
|
841 NDND_CMP_OP (mx_el_gt, >, ND1, C1, ND2, C2) \ |
|
842 NDND_CMP_OP (mx_el_eq, ==, ND1, , ND2, ) \ |
|
843 NDND_CMP_OP (mx_el_ne, !=, ND1, , ND2, ) |
4543
|
844 |
6708
|
845 #define NDND_BOOL_OP_DECLS(ND1, ND2, API) \ |
|
846 NDBOOL_OP_DECL (mx_el_and, ND1, ND2, API); \ |
|
847 NDBOOL_OP_DECL (mx_el_or, ND1, ND2, API); |
4543
|
848 |
5030
|
849 #define NDND_BOOL_OP(F, OP, ND1, ND2, LHS_ZERO, RHS_ZERO) \ |
4543
|
850 boolNDArray \ |
|
851 F (const ND1& m1, const ND2& m2) \ |
|
852 { \ |
|
853 boolNDArray r; \ |
|
854 \ |
|
855 dim_vector m1_dims = m1.dims (); \ |
|
856 dim_vector m2_dims = m2.dims (); \ |
|
857 \ |
|
858 if (m1_dims == m2_dims) \ |
|
859 { \ |
|
860 if (! m1_dims.all_zero ()) \ |
|
861 { \ |
|
862 r.resize (m1_dims); \ |
|
863 \ |
|
864 for (int i = 0; i < m1.length (); i++) \ |
5030
|
865 r.elem(i) = (m1.elem(i) != LHS_ZERO) OP (m2.elem(i) != RHS_ZERO); \ |
4543
|
866 } \ |
|
867 } \ |
|
868 else \ |
|
869 gripe_nonconformant (#F, m1_dims, m2_dims); \ |
|
870 \ |
|
871 return r; \ |
|
872 } |
|
873 |
5030
|
874 #define NDND_BOOL_OPS2(ND1, ND2, LHS_ZERO, RHS_ZERO) \ |
|
875 NDND_BOOL_OP (mx_el_and, &&, ND1, ND2, LHS_ZERO, RHS_ZERO) \ |
|
876 NDND_BOOL_OP (mx_el_or, ||, ND1, ND2, LHS_ZERO, RHS_ZERO) |
|
877 |
4543
|
878 #define NDND_BOOL_OPS(ND1, ND2, ZERO) \ |
5030
|
879 NDND_BOOL_OPS2(ND1, ND2, ZERO, ZERO) |
4543
|
880 |
6708
|
881 #define NDND_OP_DECLS(R, ND1, ND2, API) \ |
|
882 NDND_BIN_OP_DECLS (R, ND1, ND2, API) \ |
|
883 NDND_CMP_OP_DECLS (ND1, ND2, API) \ |
|
884 NDND_BOOL_OP_DECLS (ND1, ND2, API) |
4543
|
885 |
2870
|
886 // scalar by diagonal matrix operations. |
|
887 |
6708
|
888 #define SDM_BIN_OP_DECLS(R, S, DM, API) \ |
|
889 BIN_OP_DECL (R, operator +, S, DM, API); \ |
|
890 BIN_OP_DECL (R, operator -, S, DM, API); |
2870
|
891 |
|
892 #define SDM_BIN_OP(R, OP, S, DM, OPEQ) \ |
2829
|
893 R \ |
|
894 OP (const S& s, const DM& dm) \ |
|
895 { \ |
|
896 int nr = dm.rows (); \ |
|
897 int nc = dm.cols (); \ |
|
898 \ |
|
899 R r (nr, nc, s); \ |
|
900 \ |
4543
|
901 for (int i = 0; i < dm.length (); i++) \ |
2870
|
902 r.elem(i, i) OPEQ dm.elem(i, i); \ |
2829
|
903 \ |
|
904 return r; \ |
|
905 } |
|
906 |
2870
|
907 #define SDM_BIN_OPS(R, S, DM) \ |
|
908 SDM_BIN_OP (R, operator +, S, DM, +=) \ |
|
909 SDM_BIN_OP (R, operator -, S, DM, -=) |
|
910 |
6708
|
911 #define SDM_OP_DECLS(R, S, DM, API) \ |
|
912 SDM_BIN_OP_DECLS(R, S, DM, API) |
2829
|
913 |
2870
|
914 // diagonal matrix by scalar operations. |
|
915 |
6708
|
916 #define DMS_BIN_OP_DECLS(R, DM, S, API) \ |
|
917 BIN_OP_DECL (R, operator +, DM, S, API); \ |
|
918 BIN_OP_DECL (R, operator -, DM, S, API); |
2870
|
919 |
|
920 #define DMS_BIN_OP(R, OP, DM, S, SGN) \ |
2829
|
921 R \ |
|
922 OP (const DM& dm, const S& s) \ |
|
923 { \ |
|
924 int nr = dm.rows (); \ |
|
925 int nc = dm.cols (); \ |
|
926 \ |
|
927 R r (nr, nc, SGN s); \ |
|
928 \ |
4543
|
929 for (int i = 0; i < dm.length (); i++) \ |
2870
|
930 r.elem(i, i) += dm.elem(i, i); \ |
2829
|
931 \ |
|
932 return r; \ |
|
933 } |
|
934 |
2870
|
935 #define DMS_BIN_OPS(R, DM, S) \ |
|
936 DMS_BIN_OP (R, operator +, DM, S, ) \ |
|
937 DMS_BIN_OP (R, operator -, DM, S, -) |
|
938 |
6708
|
939 #define DMS_OP_DECLS(R, DM, S, API) \ |
|
940 DMS_BIN_OP_DECLS(R, DM, S, API) |
2829
|
941 |
2870
|
942 // matrix by diagonal matrix operations. |
|
943 |
6708
|
944 #define MDM_BIN_OP_DECLS(R, M, DM, API) \ |
|
945 BIN_OP_DECL (R, operator +, M, DM, API); \ |
|
946 BIN_OP_DECL (R, operator -, M, DM, API); \ |
|
947 BIN_OP_DECL (R, operator *, M, DM, API); |
2870
|
948 |
|
949 #define MDM_BIN_OP(R, OP, M, DM, OPEQ) \ |
2829
|
950 R \ |
|
951 OP (const M& m, const DM& dm) \ |
|
952 { \ |
|
953 R r; \ |
|
954 \ |
|
955 int m_nr = m.rows (); \ |
|
956 int m_nc = m.cols (); \ |
|
957 \ |
|
958 int dm_nr = dm.rows (); \ |
|
959 int dm_nc = dm.cols (); \ |
|
960 \ |
|
961 if (m_nr != dm_nr || m_nc != dm_nc) \ |
|
962 gripe_nonconformant (#OP, m_nr, m_nc, dm_nr, dm_nc); \ |
|
963 else \ |
|
964 { \ |
|
965 r.resize (m_nr, m_nc); \ |
|
966 \ |
|
967 if (m_nr > 0 && m_nc > 0) \ |
|
968 { \ |
3585
|
969 r = R (m); \ |
2829
|
970 \ |
|
971 int len = dm.length (); \ |
|
972 \ |
|
973 for (int i = 0; i < len; i++) \ |
2870
|
974 r.elem(i, i) OPEQ dm.elem(i, i); \ |
2829
|
975 } \ |
|
976 } \ |
|
977 \ |
|
978 return r; \ |
|
979 } |
|
980 |
5030
|
981 #define MDM_MULTIPLY_OP(R, M, DM, R_ZERO) \ |
2829
|
982 R \ |
|
983 operator * (const M& m, const DM& dm) \ |
|
984 { \ |
|
985 R r; \ |
|
986 \ |
|
987 int m_nr = m.rows (); \ |
|
988 int m_nc = m.cols (); \ |
|
989 \ |
|
990 int dm_nr = dm.rows (); \ |
|
991 int dm_nc = dm.cols (); \ |
|
992 \ |
|
993 if (m_nc != dm_nr) \ |
|
994 gripe_nonconformant ("operator *", m_nr, m_nc, dm_nr, dm_nc); \ |
|
995 else \ |
|
996 { \ |
5030
|
997 r.resize (m_nr, dm_nc, R_ZERO); \ |
2829
|
998 \ |
3176
|
999 if (m_nr > 0 && m_nc > 0 && dm_nc > 0) \ |
2829
|
1000 { \ |
4543
|
1001 int len = dm.length (); \ |
|
1002 \ |
|
1003 for (int j = 0; j < len; j++) \ |
2829
|
1004 { \ |
2870
|
1005 if (dm.elem(j, j) == 1.0) \ |
2829
|
1006 { \ |
|
1007 for (int i = 0; i < m_nr; i++) \ |
2870
|
1008 r.elem(i, j) = m.elem(i, j); \ |
2829
|
1009 } \ |
5030
|
1010 else \ |
2829
|
1011 { \ |
|
1012 for (int i = 0; i < m_nr; i++) \ |
2870
|
1013 r.elem(i, j) = dm.elem(j, j) * m.elem(i, j); \ |
2829
|
1014 } \ |
|
1015 } \ |
|
1016 } \ |
|
1017 } \ |
|
1018 \ |
|
1019 return r; \ |
|
1020 } |
|
1021 |
5030
|
1022 #define MDM_BIN_OPS(R, M, DM, R_ZERO) \ |
2870
|
1023 MDM_BIN_OP (R, operator +, M, DM, +=) \ |
|
1024 MDM_BIN_OP (R, operator -, M, DM, -=) \ |
5030
|
1025 MDM_MULTIPLY_OP (R, M, DM, R_ZERO) |
2829
|
1026 |
6708
|
1027 #define MDM_OP_DECLS(R, M, DM, API) \ |
|
1028 MDM_BIN_OP_DECLS(R, M, DM, API) |
2870
|
1029 |
|
1030 // diagonal matrix by matrix operations. |
|
1031 |
6708
|
1032 #define DMM_BIN_OP_DECLS(R, DM, M, API) \ |
|
1033 BIN_OP_DECL (R, operator +, DM, M, API); \ |
|
1034 BIN_OP_DECL (R, operator -, DM, M, API); \ |
|
1035 BIN_OP_DECL (R, operator *, DM, M, API); |
2870
|
1036 |
3585
|
1037 #define DMM_BIN_OP(R, OP, DM, M, OPEQ, PREOP) \ |
2829
|
1038 R \ |
|
1039 OP (const DM& dm, const M& m) \ |
|
1040 { \ |
|
1041 R r; \ |
|
1042 \ |
|
1043 int dm_nr = dm.rows (); \ |
|
1044 int dm_nc = dm.cols (); \ |
|
1045 \ |
|
1046 int m_nr = m.rows (); \ |
|
1047 int m_nc = m.cols (); \ |
|
1048 \ |
|
1049 if (dm_nr != m_nr || dm_nc != m_nc) \ |
|
1050 gripe_nonconformant (#OP, dm_nr, dm_nc, m_nr, m_nc); \ |
|
1051 else \ |
|
1052 { \ |
|
1053 if (m_nr > 0 && m_nc > 0) \ |
|
1054 { \ |
3585
|
1055 r = R (PREOP m); \ |
2829
|
1056 \ |
|
1057 int len = dm.length (); \ |
|
1058 \ |
|
1059 for (int i = 0; i < len; i++) \ |
2870
|
1060 r.elem(i, i) OPEQ dm.elem(i, i); \ |
2829
|
1061 } \ |
|
1062 else \ |
|
1063 r.resize (m_nr, m_nc); \ |
|
1064 } \ |
|
1065 \ |
|
1066 return r; \ |
|
1067 } |
|
1068 |
5030
|
1069 #define DMM_MULTIPLY_OP(R, DM, M, R_ZERO) \ |
2829
|
1070 R \ |
|
1071 operator * (const DM& dm, const M& m) \ |
|
1072 { \ |
|
1073 R r; \ |
|
1074 \ |
|
1075 int dm_nr = dm.rows (); \ |
|
1076 int dm_nc = dm.cols (); \ |
|
1077 \ |
|
1078 int m_nr = m.rows (); \ |
|
1079 int m_nc = m.cols (); \ |
|
1080 \ |
|
1081 if (dm_nc != m_nr) \ |
|
1082 gripe_nonconformant ("operator *", dm_nr, dm_nc, m_nr, m_nc); \ |
|
1083 else \ |
|
1084 { \ |
5030
|
1085 r.resize (dm_nr, m_nc, R_ZERO); \ |
2829
|
1086 \ |
|
1087 if (dm_nr > 0 && dm_nc > 0 && m_nc > 0) \ |
|
1088 { \ |
4543
|
1089 int len = dm.length (); \ |
|
1090 \ |
|
1091 for (int i = 0; i < len; i++) \ |
2829
|
1092 { \ |
2870
|
1093 if (dm.elem(i, i) == 1.0) \ |
2829
|
1094 { \ |
|
1095 for (int j = 0; j < m_nc; j++) \ |
2870
|
1096 r.elem(i, j) = m.elem(i, j); \ |
2829
|
1097 } \ |
5030
|
1098 else \ |
2829
|
1099 { \ |
|
1100 for (int j = 0; j < m_nc; j++) \ |
2870
|
1101 r.elem(i, j) = dm.elem(i, i) * m.elem(i, j); \ |
2829
|
1102 } \ |
|
1103 } \ |
|
1104 } \ |
|
1105 } \ |
|
1106 \ |
|
1107 return r; \ |
|
1108 } |
|
1109 |
5030
|
1110 #define DMM_BIN_OPS(R, DM, M, R_ZERO) \ |
3585
|
1111 DMM_BIN_OP (R, operator +, DM, M, +=, ) \ |
|
1112 DMM_BIN_OP (R, operator -, DM, M, +=, -) \ |
5030
|
1113 DMM_MULTIPLY_OP (R, DM, M, R_ZERO) |
2829
|
1114 |
6708
|
1115 #define DMM_OP_DECLS(R, DM, M, API) \ |
|
1116 DMM_BIN_OP_DECLS(R, DM, M, API) |
2870
|
1117 |
|
1118 // diagonal matrix by diagonal matrix operations. |
2829
|
1119 |
6708
|
1120 #define DMDM_BIN_OP_DECLS(R, DM1, DM2, API) \ |
|
1121 BIN_OP_DECL (R, operator +, DM1, DM2, API); \ |
|
1122 BIN_OP_DECL (R, operator -, DM1, DM2, API); \ |
|
1123 BIN_OP_DECL (R, product, DM1, DM2, API); |
2870
|
1124 |
|
1125 #define DMDM_BIN_OP(R, OP, DM1, DM2, F) \ |
2829
|
1126 R \ |
|
1127 OP (const DM1& dm1, const DM2& dm2) \ |
|
1128 { \ |
|
1129 R r; \ |
|
1130 \ |
|
1131 int dm1_nr = dm1.rows (); \ |
|
1132 int dm1_nc = dm1.cols (); \ |
|
1133 \ |
|
1134 int dm2_nr = dm2.rows (); \ |
|
1135 int dm2_nc = dm2.cols (); \ |
|
1136 \ |
|
1137 if (dm1_nr != dm2_nr || dm1_nc != dm2_nc) \ |
|
1138 gripe_nonconformant (#OP, dm1_nr, dm1_nc, dm2_nr, dm2_nc); \ |
|
1139 else \ |
|
1140 { \ |
|
1141 r.resize (dm1_nr, dm1_nc); \ |
|
1142 \ |
|
1143 if (dm1_nr > 0 && dm1_nc > 0) \ |
|
1144 F ## _vv (r.fortran_vec (), dm1.data (), dm2.data (), \ |
|
1145 dm1_nr * dm2_nc); \ |
|
1146 } \ |
|
1147 \ |
|
1148 return r; \ |
|
1149 } |
|
1150 |
2870
|
1151 #define DMDM_BIN_OPS(R, DM1, DM2) \ |
3769
|
1152 DMDM_BIN_OP (R, operator +, DM1, DM2, mx_inline_add) \ |
|
1153 DMDM_BIN_OP (R, operator -, DM1, DM2, mx_inline_subtract) \ |
|
1154 DMDM_BIN_OP (R, product, DM1, DM2, mx_inline_multiply) |
2870
|
1155 |
6708
|
1156 #define DMDM_OP_DECLS(R, DM1, DM2, API) \ |
|
1157 DMDM_BIN_OP_DECLS (R, DM1, DM2, API) |
2829
|
1158 |
3582
|
1159 #endif |
|
1160 |
2829
|
1161 /* |
|
1162 ;;; Local Variables: *** |
|
1163 ;;; mode: C++ *** |
|
1164 ;;; End: *** |
|
1165 */ |