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