Mercurial > hg > octave-lyh
annotate liboctave/mx-op-defs.h @ 9207:25f50d2d76b3
improve TR updating strategy for fminunc and fsolve
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Sun, 17 May 2009 17:54:51 +0200 |
parents | a48fba01e4ac |
children | 3d6a9aea2aea |
rev | line source |
---|---|
2829 | 1 /* |
2 | |
8920 | 3 Copyright (C) 2008, 2009 Jaroslav Hajek |
7017 | 4 Copyright (C) 1996, 1997, 1998, 2000, 2001, 2003, 2004, 2005, 2006, |
5 2007 John W. Eaton | |
2829 | 6 |
7 This file is part of Octave. | |
8 | |
9 Octave is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
7016 | 11 Free Software Foundation; either version 3 of the License, or (at your |
12 option) any later version. | |
2829 | 13 |
14 Octave is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
7016 | 20 along with Octave; see the file COPYING. If not, see |
21 <http://www.gnu.org/licenses/>. | |
2829 | 22 |
23 */ | |
24 | |
25 #if !defined (octave_mx_op_defs_h) | |
26 #define octave_mx_op_defs_h 1 | |
27 | |
8774
b756ce0002db
split implementation and interface in mx-op-defs and MArray-defs
Jaroslav Hajek <highegg@gmail.com>
parents:
8397
diff
changeset
|
28 #include "mx-op-decl.h" |
2829 | 29 #include "mx-inlines.cc" |
30 | |
2870 | 31 // vector by scalar operations. |
32 | |
33 #define VS_BIN_OP(R, F, OP, V, S) \ | |
34 R \ | |
35 F (const V& v, const S& s) \ | |
36 { \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
37 octave_idx_type len = v.length (); \ |
2870 | 38 \ |
39 R r (len); \ | |
40 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
41 for (octave_idx_type i = 0; i < len; i++) \ |
2870 | 42 r.elem(i) = v.elem(i) OP s; \ |
43 \ | |
44 return r; \ | |
45 } | |
46 | |
47 #define VS_BIN_OPS(R, V, S) \ | |
48 VS_BIN_OP (R, operator +, +, V, S) \ | |
49 VS_BIN_OP (R, operator -, -, V, S) \ | |
50 VS_BIN_OP (R, operator *, *, V, S) \ | |
51 VS_BIN_OP (R, operator /, /, V, S) | |
52 | |
53 // scalar by vector by operations. | |
54 | |
55 #define SV_BIN_OP(R, F, OP, S, V) \ | |
56 R \ | |
57 F (const S& s, const V& v) \ | |
58 { \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
59 octave_idx_type len = v.length (); \ |
2870 | 60 \ |
61 R r (len); \ | |
62 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
63 for (octave_idx_type i = 0; i < len; i++) \ |
2870 | 64 r.elem(i) = s OP v.elem(i); \ |
65 \ | |
66 return r; \ | |
67 } | |
68 | |
69 #define SV_BIN_OPS(R, S, V) \ | |
70 SV_BIN_OP (R, operator +, +, S, V) \ | |
71 SV_BIN_OP (R, operator -, -, S, V) \ | |
72 SV_BIN_OP (R, operator *, *, S, V) \ | |
73 SV_BIN_OP (R, operator /, /, S, V) | |
74 | |
75 // vector by vector operations. | |
76 | |
77 #define VV_BIN_OP(R, F, OP, V1, V2) \ | |
78 R \ | |
79 F (const V1& v1, const V2& v2) \ | |
80 { \ | |
81 R r; \ | |
82 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
83 octave_idx_type v1_len = v1.length (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
84 octave_idx_type v2_len = v2.length (); \ |
2870 | 85 \ |
86 if (v1_len != v2_len) \ | |
87 gripe_nonconformant (#OP, v1_len, v2_len); \ | |
88 else \ | |
89 { \ | |
90 r.resize (v1_len); \ | |
91 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
92 for (octave_idx_type i = 0; i < v1_len; i++) \ |
2870 | 93 r.elem(i) = v1.elem(i) OP v2.elem(i); \ |
94 } \ | |
95 \ | |
96 return r; \ | |
97 } | |
98 | |
99 #define VV_BIN_OPS(R, V1, V2) \ | |
100 VV_BIN_OP (R, operator +, +, V1, V2) \ | |
101 VV_BIN_OP (R, operator -, -, V1, V2) \ | |
102 VV_BIN_OP (R, product, *, V1, V2) \ | |
103 VV_BIN_OP (R, quotient, /, V1, V2) | |
104 | |
105 // matrix by scalar operations. | |
106 | |
107 #define MS_BIN_OP(R, OP, M, S, F) \ | |
2829 | 108 R \ |
109 OP (const M& m, const S& s) \ | |
110 { \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
111 octave_idx_type nr = m.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
112 octave_idx_type nc = m.cols (); \ |
2829 | 113 \ |
114 R r (nr, nc); \ | |
115 \ | |
116 if (nr > 0 && nc > 0) \ | |
117 F ## _vs (r.fortran_vec (), m.data (), nr * nc, s); \ | |
118 \ | |
119 return r; \ | |
120 } | |
121 | |
2870 | 122 #define MS_BIN_OPS(R, M, S) \ |
3769 | 123 MS_BIN_OP (R, operator +, M, S, mx_inline_add) \ |
124 MS_BIN_OP (R, operator -, M, S, mx_inline_subtract) \ | |
125 MS_BIN_OP (R, operator *, M, S, mx_inline_multiply) \ | |
126 MS_BIN_OP (R, operator /, M, S, mx_inline_divide) | |
2870 | 127 |
4826 | 128 #define MS_CMP_OP(F, OP, M, MC, S, SC) \ |
2870 | 129 boolMatrix \ |
130 F (const M& m, const S& s) \ | |
131 { \ | |
132 boolMatrix r; \ | |
133 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
134 octave_idx_type nr = m.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
135 octave_idx_type nc = m.cols (); \ |
2870 | 136 \ |
4826 | 137 r.resize (nr, nc); \ |
138 \ | |
139 if (nr > 0 && nc > 0) \ | |
2870 | 140 { \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
141 for (octave_idx_type j = 0; j < nc; j++) \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
142 for (octave_idx_type i = 0; i < nr; i++) \ |
2870 | 143 r.elem(i, j) = MC (m.elem(i, j)) OP SC (s); \ |
144 } \ | |
145 \ | |
146 return r; \ | |
147 } | |
2829 | 148 |
2870 | 149 #define MS_CMP_OPS(M, CM, S, CS) \ |
4826 | 150 MS_CMP_OP (mx_el_lt, <, M, CM, S, CS) \ |
151 MS_CMP_OP (mx_el_le, <=, M, CM, S, CS) \ | |
152 MS_CMP_OP (mx_el_ge, >=, M, CM, S, CS) \ | |
153 MS_CMP_OP (mx_el_gt, >, M, CM, S, CS) \ | |
154 MS_CMP_OP (mx_el_eq, ==, M, , S, ) \ | |
155 MS_CMP_OP (mx_el_ne, !=, M, , S, ) | |
2870 | 156 |
5030 | 157 #define MS_BOOL_OP(F, OP, M, S, LHS_ZERO, RHS_ZERO) \ |
2870 | 158 boolMatrix \ |
159 F (const M& m, const S& s) \ | |
160 { \ | |
161 boolMatrix r; \ | |
162 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
163 octave_idx_type nr = m.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
164 octave_idx_type nc = m.cols (); \ |
2870 | 165 \ |
166 if (nr != 0 && nc != 0) \ | |
167 { \ | |
168 r.resize (nr, nc); \ | |
169 \ | |
7922
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
170 if (xisnan (s)) \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
171 gripe_nan_to_logical_conversion (); \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
172 else \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
173 { \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
174 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
175 for (octave_idx_type j = 0; j < nc; j++) \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
176 for (octave_idx_type i = 0; i < nr; i++) \ |
7922
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
177 if (xisnan (m.elem(i, j))) \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
178 { \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
179 gripe_nan_to_logical_conversion (); \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
180 return r; \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
181 } \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
182 else \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
183 r.elem(i, j) = (m.elem(i, j) != LHS_ZERO) OP (s != RHS_ZERO); \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
184 } \ |
2870 | 185 } \ |
186 \ | |
187 return r; \ | |
188 } | |
189 | |
5030 | 190 #define MS_BOOL_OPS2(M, S, LHS_ZERO, RHS_ZERO) \ |
191 MS_BOOL_OP (mx_el_and, &&, M, S, LHS_ZERO, RHS_ZERO) \ | |
192 MS_BOOL_OP (mx_el_or, ||, M, S, LHS_ZERO, RHS_ZERO) | |
193 | |
3504 | 194 #define MS_BOOL_OPS(M, S, ZERO) \ |
5030 | 195 MS_BOOL_OPS2(M, S, ZERO, ZERO) |
2870 | 196 |
197 // scalar by matrix operations. | |
198 | |
199 #define SM_BIN_OP(R, OP, S, M, F) \ | |
2829 | 200 R \ |
201 OP (const S& s, const M& m) \ | |
202 { \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
203 octave_idx_type nr = m.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
204 octave_idx_type nc = m.cols (); \ |
2829 | 205 \ |
206 R r (nr, nc); \ | |
207 \ | |
208 if (nr > 0 && nc > 0) \ | |
209 F ## _sv (r.fortran_vec (), s, m.data (), nr * nc); \ | |
210 \ | |
211 return r; \ | |
212 } | |
213 | |
2870 | 214 #define SM_BIN_OPS(R, S, M) \ |
3769 | 215 SM_BIN_OP (R, operator +, S, M, mx_inline_add) \ |
216 SM_BIN_OP (R, operator -, S, M, mx_inline_subtract) \ | |
217 SM_BIN_OP (R, operator *, S, M, mx_inline_multiply) \ | |
218 SM_BIN_OP (R, operator /, S, M, mx_inline_divide) | |
2870 | 219 |
4826 | 220 #define SM_CMP_OP(F, OP, S, SC, M, MC) \ |
2870 | 221 boolMatrix \ |
222 F (const S& s, const M& m) \ | |
223 { \ | |
224 boolMatrix r; \ | |
225 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
226 octave_idx_type nr = m.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
227 octave_idx_type nc = m.cols (); \ |
2870 | 228 \ |
4826 | 229 r.resize (nr, nc); \ |
230 \ | |
231 if (nr > 0 && nc > 0) \ | |
2870 | 232 { \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
233 for (octave_idx_type j = 0; j < nc; j++) \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
234 for (octave_idx_type i = 0; i < nr; i++) \ |
2870 | 235 r.elem(i, j) = SC (s) OP MC (m.elem(i, j)); \ |
236 } \ | |
237 \ | |
238 return r; \ | |
239 } | |
2829 | 240 |
2870 | 241 #define SM_CMP_OPS(S, CS, M, CM) \ |
4826 | 242 SM_CMP_OP (mx_el_lt, <, S, CS, M, CM) \ |
243 SM_CMP_OP (mx_el_le, <=, S, CS, M, CM) \ | |
244 SM_CMP_OP (mx_el_ge, >=, S, CS, M, CM) \ | |
245 SM_CMP_OP (mx_el_gt, >, S, CS, M, CM) \ | |
246 SM_CMP_OP (mx_el_eq, ==, S, , M, ) \ | |
247 SM_CMP_OP (mx_el_ne, !=, S, , M, ) | |
2870 | 248 |
5030 | 249 #define SM_BOOL_OP(F, OP, S, M, LHS_ZERO, RHS_ZERO) \ |
2870 | 250 boolMatrix \ |
251 F (const S& s, const M& m) \ | |
252 { \ | |
253 boolMatrix r; \ | |
254 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
255 octave_idx_type nr = m.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
256 octave_idx_type nc = m.cols (); \ |
2870 | 257 \ |
258 if (nr != 0 && nc != 0) \ | |
259 { \ | |
260 r.resize (nr, nc); \ | |
261 \ | |
7922
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
262 if (xisnan (s)) \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
263 gripe_nan_to_logical_conversion (); \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
264 else \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
265 { \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
266 for (octave_idx_type j = 0; j < nc; j++) \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
267 for (octave_idx_type i = 0; i < nr; i++) \ |
7922
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
268 if (xisnan (m.elem(i, j))) \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
269 { \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
270 gripe_nan_to_logical_conversion (); \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
271 return r; \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
272 } \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
273 else \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
274 r.elem(i, j) = (s != LHS_ZERO) OP (m.elem(i, j) != RHS_ZERO); \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
275 } \ |
2870 | 276 } \ |
277 \ | |
278 return r; \ | |
279 } | |
280 | |
5030 | 281 #define SM_BOOL_OPS2(S, M, LHS_ZERO, RHS_ZERO) \ |
282 SM_BOOL_OP (mx_el_and, &&, S, M, LHS_ZERO, RHS_ZERO) \ | |
283 SM_BOOL_OP (mx_el_or, ||, S, M, LHS_ZERO, RHS_ZERO) | |
284 | |
3504 | 285 #define SM_BOOL_OPS(S, M, ZERO) \ |
5030 | 286 SM_BOOL_OPS2(S, M, ZERO, ZERO) |
2870 | 287 |
288 // matrix by matrix operations. | |
289 | |
290 #define MM_BIN_OP(R, OP, M1, M2, F) \ | |
2829 | 291 R \ |
292 OP (const M1& m1, const M2& m2) \ | |
293 { \ | |
294 R r; \ | |
295 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
296 octave_idx_type m1_nr = m1.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
297 octave_idx_type m1_nc = m1.cols (); \ |
2829 | 298 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
299 octave_idx_type m2_nr = m2.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
300 octave_idx_type m2_nc = m2.cols (); \ |
2829 | 301 \ |
302 if (m1_nr != m2_nr || m1_nc != m2_nc) \ | |
303 gripe_nonconformant (#OP, m1_nr, m1_nc, m2_nr, m2_nc); \ | |
304 else \ | |
305 { \ | |
306 r.resize (m1_nr, m1_nc); \ | |
307 \ | |
308 if (m1_nr > 0 && m1_nc > 0) \ | |
309 F ## _vv (r.fortran_vec (), m1.data (), m2.data (), m1_nr * m1_nc); \ | |
310 } \ | |
311 \ | |
312 return r; \ | |
313 } | |
314 | |
2870 | 315 #define MM_BIN_OPS(R, M1, M2) \ |
3769 | 316 MM_BIN_OP (R, operator +, M1, M2, mx_inline_add) \ |
317 MM_BIN_OP (R, operator -, M1, M2, mx_inline_subtract) \ | |
318 MM_BIN_OP (R, product, M1, M2, mx_inline_multiply) \ | |
319 MM_BIN_OP (R, quotient, M1, M2, mx_inline_divide) | |
2870 | 320 |
4826 | 321 #define MM_CMP_OP(F, OP, M1, C1, M2, C2) \ |
2870 | 322 boolMatrix \ |
323 F (const M1& m1, const M2& m2) \ | |
324 { \ | |
325 boolMatrix r; \ | |
326 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
327 octave_idx_type m1_nr = m1.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
328 octave_idx_type m1_nc = m1.cols (); \ |
2870 | 329 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
330 octave_idx_type m2_nr = m2.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
331 octave_idx_type m2_nc = m2.cols (); \ |
2870 | 332 \ |
333 if (m1_nr == m2_nr && m1_nc == m2_nc) \ | |
334 { \ | |
4826 | 335 r.resize (m1_nr, m1_nc); \ |
2870 | 336 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
337 for (octave_idx_type j = 0; j < m1_nc; j++) \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
338 for (octave_idx_type i = 0; i < m1_nr; i++) \ |
4826 | 339 r.elem(i, j) = C1 (m1.elem(i, j)) OP C2 (m2.elem(i, j)); \ |
2870 | 340 } \ |
341 else \ | |
4826 | 342 gripe_nonconformant (#F, m1_nr, m1_nc, m2_nr, m2_nc); \ |
2870 | 343 \ |
344 return r; \ | |
345 } | |
2829 | 346 |
2870 | 347 #define MM_CMP_OPS(M1, C1, M2, C2) \ |
4826 | 348 MM_CMP_OP (mx_el_lt, <, M1, C1, M2, C2) \ |
349 MM_CMP_OP (mx_el_le, <=, M1, C1, M2, C2) \ | |
350 MM_CMP_OP (mx_el_ge, >=, M1, C1, M2, C2) \ | |
351 MM_CMP_OP (mx_el_gt, >, M1, C1, M2, C2) \ | |
352 MM_CMP_OP (mx_el_eq, ==, M1, , M2, ) \ | |
353 MM_CMP_OP (mx_el_ne, !=, M1, , M2, ) | |
2870 | 354 |
5030 | 355 #define MM_BOOL_OP(F, OP, M1, M2, LHS_ZERO, RHS_ZERO) \ |
2870 | 356 boolMatrix \ |
357 F (const M1& m1, const M2& m2) \ | |
358 { \ | |
359 boolMatrix r; \ | |
360 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
361 octave_idx_type m1_nr = m1.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
362 octave_idx_type m1_nc = m1.cols (); \ |
2870 | 363 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
364 octave_idx_type m2_nr = m2.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
365 octave_idx_type m2_nc = m2.cols (); \ |
2870 | 366 \ |
367 if (m1_nr == m2_nr && m1_nc == m2_nc) \ | |
368 { \ | |
369 if (m1_nr != 0 || m1_nc != 0) \ | |
370 { \ | |
371 r.resize (m1_nr, m1_nc); \ | |
372 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
373 for (octave_idx_type j = 0; j < m1_nc; j++) \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
374 for (octave_idx_type i = 0; i < m1_nr; i++) \ |
7922
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
375 if (xisnan (m1.elem(i, j)) || xisnan (m2.elem(i, j))) \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
376 { \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
377 gripe_nan_to_logical_conversion (); \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
378 return r; \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
379 } \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
380 else \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
381 r.elem(i, j) = (m1.elem(i, j) != LHS_ZERO) \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
382 OP (m2.elem(i, j) != RHS_ZERO); \ |
2870 | 383 } \ |
384 } \ | |
385 else \ | |
386 { \ | |
387 if ((m1_nr != 0 || m1_nc != 0) && (m2_nr != 0 || m2_nc != 0)) \ | |
388 gripe_nonconformant (#F, m1_nr, m1_nc, m2_nr, m2_nc); \ | |
389 } \ | |
390 \ | |
391 return r; \ | |
392 } | |
393 | |
5030 | 394 #define MM_BOOL_OPS2(M1, M2, LHS_ZERO, RHS_ZERO) \ |
395 MM_BOOL_OP (mx_el_and, &&, M1, M2, LHS_ZERO, RHS_ZERO) \ | |
396 MM_BOOL_OP (mx_el_or, ||, M1, M2, LHS_ZERO, RHS_ZERO) | |
397 | |
3504 | 398 #define MM_BOOL_OPS(M1, M2, ZERO) \ |
5030 | 399 MM_BOOL_OPS2(M1, M2, ZERO, ZERO) |
2870 | 400 |
4543 | 401 // N-d matrix by scalar operations. |
402 | |
403 #define NDS_BIN_OP(R, OP, ND, S, F) \ | |
404 R \ | |
405 OP (const ND& m, const S& s) \ | |
406 { \ | |
407 R r (m.dims ()); \ | |
408 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
409 octave_idx_type len = m.length (); \ |
4543 | 410 \ |
411 if (len > 0) \ | |
412 F ## _vs (r.fortran_vec (), m.data (), len, s); \ | |
413 \ | |
414 return r; \ | |
415 } | |
416 | |
417 #define NDS_BIN_OPS(R, ND, S) \ | |
418 NDS_BIN_OP (R, operator +, ND, S, mx_inline_add) \ | |
419 NDS_BIN_OP (R, operator -, ND, S, mx_inline_subtract) \ | |
420 NDS_BIN_OP (R, operator *, ND, S, mx_inline_multiply) \ | |
421 NDS_BIN_OP (R, operator /, ND, S, mx_inline_divide) | |
422 | |
4826 | 423 #define NDS_CMP_OP(F, OP, ND, NDC, S, SC) \ |
4543 | 424 boolNDArray \ |
425 F (const ND& m, const S& s) \ | |
426 { \ | |
8983
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
427 boolNDArray r (m.dims ()); \ |
4543 | 428 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
429 octave_idx_type len = m.length (); \ |
4543 | 430 \ |
8983
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
431 if (s == S ()) \ |
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
432 { \ |
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
433 for (octave_idx_type i = 0; i < len; i++) \ |
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
434 r.xelem(i) = NDC (m.elem(i)) OP SC (S ()); \ |
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
435 } \ |
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
436 else \ |
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
437 { \ |
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
438 for (octave_idx_type i = 0; i < len; i++) \ |
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
439 r.xelem(i) = NDC (m.elem(i)) OP SC (s); \ |
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
440 } \ |
4543 | 441 \ |
442 return r; \ | |
443 } | |
444 | |
445 #define NDS_CMP_OPS(ND, NDC, S, SC) \ | |
4826 | 446 NDS_CMP_OP (mx_el_lt, <, ND, NDC, S, SC) \ |
447 NDS_CMP_OP (mx_el_le, <=, ND, NDC, S, SC) \ | |
448 NDS_CMP_OP (mx_el_ge, >=, ND, NDC, S, SC) \ | |
449 NDS_CMP_OP (mx_el_gt, >, ND, NDC, S, SC) \ | |
450 NDS_CMP_OP (mx_el_eq, ==, ND, , S, ) \ | |
451 NDS_CMP_OP (mx_el_ne, !=, ND, , S, ) | |
4543 | 452 |
6119 | 453 #define NDS_CMP_OP1(F, OP, ND, NDC, S, SC, SPEC) \ |
454 boolNDArray \ | |
455 F (const ND& m, const S& s) \ | |
456 { \ | |
8983
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
457 boolNDArray r (m.dims ()); \ |
6119 | 458 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
459 octave_idx_type len = m.length (); \ |
6119 | 460 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
461 for (octave_idx_type i = 0; i < len; i++) \ |
6119 | 462 r.elem(i) = operator OP <SPEC> (NDC (m.elem(i)), SC (s)); \ |
463 \ | |
464 return r; \ | |
465 } | |
466 | |
467 #define NDS_CMP_OPS1(ND, NDC, S, SC, SPEC) \ | |
468 NDS_CMP_OP1 (mx_el_lt, <, ND, NDC, S, SC, SPEC) \ | |
469 NDS_CMP_OP1 (mx_el_le, <=, ND, NDC, S, SC, SPEC) \ | |
470 NDS_CMP_OP1 (mx_el_ge, >=, ND, NDC, S, SC, SPEC) \ | |
471 NDS_CMP_OP1 (mx_el_gt, >, ND, NDC, S, SC, SPEC) \ | |
472 NDS_CMP_OP1 (mx_el_eq, ==, ND, , S, , SPEC) \ | |
473 NDS_CMP_OP1 (mx_el_ne, !=, ND, , S, , SPEC) | |
474 | |
475 #define NDS_CMP_OP2(F, OP, ND, NDC, S, SC, SPEC1, SPEC2) \ | |
476 boolNDArray \ | |
477 F (const ND& m, const S& s) \ | |
478 { \ | |
479 boolNDArray r; \ | |
480 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
481 octave_idx_type len = m.length (); \ |
6119 | 482 \ |
483 r.resize (m.dims ()); \ | |
484 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
485 for (octave_idx_type i = 0; i < len; i++) \ |
6119 | 486 r.elem(i) = operator OP <SPEC1,SPEC2> (NDC (m.elem(i)), SC (s)); \ |
487 \ | |
488 return r; \ | |
489 } | |
490 | |
491 #define NDS_CMP_OPS2(ND, NDC, S, SC, SPEC1, SPEC2) \ | |
492 NDS_CMP_OP2 (mx_el_lt, <, ND, NDC, S, SC, SPEC1, SPEC2) \ | |
493 NDS_CMP_OP2 (mx_el_le, <=, ND, NDC, S, SC, SPEC1, SPEC2) \ | |
494 NDS_CMP_OP2 (mx_el_ge, >=, ND, NDC, S, SC, SPEC1, SPEC2) \ | |
495 NDS_CMP_OP2 (mx_el_gt, >, ND, NDC, S, SC, SPEC1, SPEC2) \ | |
496 NDS_CMP_OP2 (mx_el_eq, ==, ND, , S, , SPEC1, SPEC2) \ | |
497 NDS_CMP_OP2 (mx_el_ne, !=, ND, , S, , SPEC1, SPEC2) | |
498 | |
8982
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
499 #define NDS_BOOL_OP(F, EQ, OP, ND, S, LHS_ZERO, RHS_ZERO) \ |
4543 | 500 boolNDArray \ |
501 F (const ND& m, const S& s) \ | |
502 { \ | |
8982
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
503 boolNDArray r (m.dims ()); \ |
4543 | 504 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
505 octave_idx_type len = m.length (); \ |
4543 | 506 \ |
507 if (len > 0) \ | |
508 { \ | |
7922
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
509 if (xisnan (s)) \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
510 gripe_nan_to_logical_conversion (); \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
511 else \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
512 { \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
513 for (octave_idx_type i = 0; i < len; i++) \ |
7922
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
514 if (xisnan (m.elem(i))) \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
515 { \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
516 gripe_nan_to_logical_conversion (); \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
517 return r; \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
518 } \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
519 else \ |
8982
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
520 r.xelem(i) = (m.elem(i) EQ LHS_ZERO) OP (s != RHS_ZERO); \ |
7922
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
521 } \ |
4543 | 522 } \ |
523 \ | |
524 return r; \ | |
525 } | |
526 | |
5030 | 527 #define NDS_BOOL_OPS2(ND, S, LHS_ZERO, RHS_ZERO) \ |
8982
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
528 NDS_BOOL_OP (mx_el_and, !=, &&, ND, S, LHS_ZERO, RHS_ZERO) \ |
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
529 NDS_BOOL_OP (mx_el_or, !=, ||, ND, S, LHS_ZERO, RHS_ZERO) \ |
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
530 NDS_BOOL_OP (mx_el_not_and, ==, &&, ND, S, LHS_ZERO, RHS_ZERO) \ |
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
531 NDS_BOOL_OP (mx_el_not_or, ==, ||, ND, S, LHS_ZERO, RHS_ZERO) |
5030 | 532 |
4543 | 533 #define NDS_BOOL_OPS(ND, S, ZERO) \ |
5030 | 534 NDS_BOOL_OPS2(ND, S, ZERO, ZERO) |
4543 | 535 |
536 // scalar by N-d matrix operations. | |
537 | |
538 #define SND_BIN_OP(R, OP, S, ND, F) \ | |
539 R \ | |
540 OP (const S& s, const ND& m) \ | |
541 { \ | |
542 R r (m.dims ()); \ | |
543 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
544 octave_idx_type len = m.length (); \ |
4543 | 545 \ |
546 if (len > 0) \ | |
547 F ## _sv (r.fortran_vec (), s, m.data (), len); \ | |
548 \ | |
549 return r; \ | |
550 } | |
551 | |
552 #define SND_BIN_OPS(R, S, ND) \ | |
553 SND_BIN_OP (R, operator +, S, ND, mx_inline_add) \ | |
554 SND_BIN_OP (R, operator -, S, ND, mx_inline_subtract) \ | |
555 SND_BIN_OP (R, operator *, S, ND, mx_inline_multiply) \ | |
556 SND_BIN_OP (R, operator /, S, ND, mx_inline_divide) | |
557 | |
4826 | 558 #define SND_CMP_OP(F, OP, S, SC, ND, NDC) \ |
4543 | 559 boolNDArray \ |
560 F (const S& s, const ND& m) \ | |
561 { \ | |
8983
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
562 boolNDArray r (m.dims ()); \ |
4543 | 563 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
564 octave_idx_type len = m.length (); \ |
4543 | 565 \ |
8983
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
566 if (s == S ()) \ |
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
567 { \ |
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
568 for (octave_idx_type i = 0; i < len; i++) \ |
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
569 r.xelem(i) = SC (S ()) OP NDC (m.elem(i)); \ |
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
570 } \ |
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
571 else \ |
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
572 { \ |
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
573 for (octave_idx_type i = 0; i < len; i++) \ |
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
574 r.xelem(i) = SC (s) OP NDC (m.elem(i)); \ |
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
575 } \ |
4543 | 576 \ |
577 return r; \ | |
578 } | |
579 | |
580 #define SND_CMP_OPS(S, CS, ND, CND) \ | |
4826 | 581 SND_CMP_OP (mx_el_lt, <, S, CS, ND, CND) \ |
582 SND_CMP_OP (mx_el_le, <=, S, CS, ND, CND) \ | |
583 SND_CMP_OP (mx_el_ge, >=, S, CS, ND, CND) \ | |
584 SND_CMP_OP (mx_el_gt, >, S, CS, ND, CND) \ | |
585 SND_CMP_OP (mx_el_eq, ==, S, , ND, ) \ | |
586 SND_CMP_OP (mx_el_ne, !=, S, , ND, ) | |
4543 | 587 |
6119 | 588 #define SND_CMP_OP1(F, OP, S, SC, ND, NDC, SPEC) \ |
589 boolNDArray \ | |
590 F (const S& s, const ND& m) \ | |
591 { \ | |
8983
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
592 boolNDArray r (m.dims ()); \ |
6119 | 593 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
594 octave_idx_type len = m.length (); \ |
6119 | 595 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
596 for (octave_idx_type i = 0; i < len; i++) \ |
6119 | 597 r.elem(i) = operator OP <SPEC> (SC (s), NDC (m.elem(i))); \ |
598 \ | |
599 return r; \ | |
600 } | |
601 | |
602 #define SND_CMP_OPS1(S, CS, ND, CND, SPEC) \ | |
603 SND_CMP_OP1 (mx_el_lt, <, S, CS, ND, CND, SPEC) \ | |
604 SND_CMP_OP1 (mx_el_le, <=, S, CS, ND, CND, SPEC) \ | |
605 SND_CMP_OP1 (mx_el_ge, >=, S, CS, ND, CND, SPEC) \ | |
606 SND_CMP_OP1 (mx_el_gt, >, S, CS, ND, CND, SPEC) \ | |
607 SND_CMP_OP1 (mx_el_eq, ==, S, , ND, , SPEC) \ | |
608 SND_CMP_OP1 (mx_el_ne, !=, S, , ND, , SPEC) | |
609 | |
610 #define SND_CMP_OP2(F, OP, S, SC, ND, NDC, SPEC1, SPEC2) \ | |
611 boolNDArray \ | |
612 F (const S& s, const ND& m) \ | |
613 { \ | |
8983
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
614 boolNDArray r (m.dims ()); \ |
6119 | 615 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
616 octave_idx_type len = m.length (); \ |
6119 | 617 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
618 for (octave_idx_type i = 0; i < len; i++) \ |
6119 | 619 r.elem(i) = operator OP <SPEC1, SPEC2> (SC (s), NDC (m.elem(i))); \ |
620 \ | |
621 return r; \ | |
622 } | |
623 | |
624 #define SND_CMP_OPS2(S, CS, ND, CND, SPEC1, SPEC2) \ | |
625 SND_CMP_OP2 (mx_el_lt, <, S, CS, ND, CND, SPEC1, SPEC2) \ | |
626 SND_CMP_OP2 (mx_el_le, <=, S, CS, ND, CND, SPEC1, SPEC2) \ | |
627 SND_CMP_OP2 (mx_el_ge, >=, S, CS, ND, CND, SPEC1, SPEC2) \ | |
628 SND_CMP_OP2 (mx_el_gt, >, S, CS, ND, CND, SPEC1, SPEC2) \ | |
629 SND_CMP_OP2 (mx_el_eq, ==, S, , ND, , SPEC1, SPEC2) \ | |
630 SND_CMP_OP2 (mx_el_ne, !=, S, , ND, , SPEC1, SPEC2) | |
631 | |
8982
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
632 #define SND_BOOL_OP(F, OP, EQ, S, ND, LHS_ZERO, RHS_ZERO) \ |
4543 | 633 boolNDArray \ |
634 F (const S& s, const ND& m) \ | |
635 { \ | |
8982
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
636 boolNDArray r (m.dims ()); \ |
4543 | 637 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
638 octave_idx_type len = m.length (); \ |
4543 | 639 \ |
640 if (len > 0) \ | |
641 { \ | |
7922
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
642 if (xisnan (s)) \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
643 gripe_nan_to_logical_conversion (); \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
644 else \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
645 { \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
646 for (octave_idx_type i = 0; i < len; i++) \ |
7922
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
647 if (xisnan (m.elem(i))) \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
648 { \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
649 gripe_nan_to_logical_conversion (); \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
650 return r; \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
651 } \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
652 else \ |
8982
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
653 r.xelem(i) = (s != LHS_ZERO) OP (m.elem(i) EQ RHS_ZERO); \ |
7922
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
654 } \ |
4543 | 655 } \ |
656 \ | |
657 return r; \ | |
658 } | |
659 | |
5030 | 660 #define SND_BOOL_OPS2(S, ND, LHS_ZERO, RHS_ZERO) \ |
8982
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
661 SND_BOOL_OP (mx_el_and, &&, !=, S, ND, LHS_ZERO, RHS_ZERO) \ |
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
662 SND_BOOL_OP (mx_el_or, ||, !=, S, ND, LHS_ZERO, RHS_ZERO) \ |
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
663 SND_BOOL_OP (mx_el_and_not, &&, ==, S, ND, LHS_ZERO, RHS_ZERO) \ |
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
664 SND_BOOL_OP (mx_el_or_not, ||, ==, S, ND, LHS_ZERO, RHS_ZERO) |
5030 | 665 |
4543 | 666 #define SND_BOOL_OPS(S, ND, ZERO) \ |
5030 | 667 SND_BOOL_OPS2(S, ND, ZERO, ZERO) |
4543 | 668 |
669 // N-d matrix by N-d matrix operations. | |
670 | |
671 #define NDND_BIN_OP(R, OP, ND1, ND2, F) \ | |
672 R \ | |
673 OP (const ND1& m1, const ND2& m2) \ | |
674 { \ | |
675 R r; \ | |
676 \ | |
677 dim_vector m1_dims = m1.dims (); \ | |
678 dim_vector m2_dims = m2.dims (); \ | |
679 \ | |
680 if (m1_dims != m2_dims) \ | |
681 gripe_nonconformant (#OP, m1_dims, m2_dims); \ | |
682 else \ | |
683 { \ | |
8983
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
684 r = R (m1_dims); \ |
4543 | 685 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
686 octave_idx_type len = m1.length (); \ |
4543 | 687 \ |
688 if (len > 0) \ | |
689 F ## _vv (r.fortran_vec (), m1.data (), m2.data (), len); \ | |
690 } \ | |
691 \ | |
692 return r; \ | |
693 } | |
694 | |
695 #define NDND_BIN_OPS(R, ND1, ND2) \ | |
696 NDND_BIN_OP (R, operator +, ND1, ND2, mx_inline_add) \ | |
697 NDND_BIN_OP (R, operator -, ND1, ND2, mx_inline_subtract) \ | |
698 NDND_BIN_OP (R, product, ND1, ND2, mx_inline_multiply) \ | |
699 NDND_BIN_OP (R, quotient, ND1, ND2, mx_inline_divide) | |
700 | |
4826 | 701 #define NDND_CMP_OP(F, OP, ND1, C1, ND2, C2) \ |
4543 | 702 boolNDArray \ |
703 F (const ND1& m1, const ND2& m2) \ | |
704 { \ | |
705 boolNDArray r; \ | |
706 \ | |
707 dim_vector m1_dims = m1.dims (); \ | |
708 dim_vector m2_dims = m2.dims (); \ | |
709 \ | |
710 if (m1_dims == m2_dims) \ | |
711 { \ | |
8983
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
712 r = boolNDArray (m1_dims); \ |
4543 | 713 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
714 for (octave_idx_type i = 0; i < m1.length (); i++) \ |
8983
e781ab1aee39
optimize comparison ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8982
diff
changeset
|
715 r.xelem(i) = C1 (m1.elem(i)) OP C2 (m2.elem(i)); \ |
4543 | 716 } \ |
717 else \ | |
4826 | 718 gripe_nonconformant (#F, m1_dims, m2_dims); \ |
4543 | 719 \ |
720 return r; \ | |
721 } | |
722 | |
723 #define NDND_CMP_OPS(ND1, C1, ND2, C2) \ | |
4826 | 724 NDND_CMP_OP (mx_el_lt, <, ND1, C1, ND2, C2) \ |
725 NDND_CMP_OP (mx_el_le, <=, ND1, C1, ND2, C2) \ | |
726 NDND_CMP_OP (mx_el_ge, >=, ND1, C1, ND2, C2) \ | |
727 NDND_CMP_OP (mx_el_gt, >, ND1, C1, ND2, C2) \ | |
728 NDND_CMP_OP (mx_el_eq, ==, ND1, , ND2, ) \ | |
729 NDND_CMP_OP (mx_el_ne, !=, ND1, , ND2, ) | |
4543 | 730 |
8982
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
731 #define NDND_BOOL_OP(F, EQ1, OP, EQ2, ND1, ND2, LHS_ZERO, RHS_ZERO) \ |
4543 | 732 boolNDArray \ |
733 F (const ND1& m1, const ND2& m2) \ | |
734 { \ | |
735 boolNDArray r; \ | |
736 \ | |
737 dim_vector m1_dims = m1.dims (); \ | |
738 dim_vector m2_dims = m2.dims (); \ | |
739 \ | |
740 if (m1_dims == m2_dims) \ | |
741 { \ | |
742 if (! m1_dims.all_zero ()) \ | |
743 { \ | |
8982
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
744 r = boolNDArray (m1_dims); \ |
4543 | 745 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
746 for (octave_idx_type i = 0; i < m1.length (); i++) \ |
7922
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
747 if (xisnan (m1.elem(i)) || xisnan (m2.elem(i))) \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
748 { \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
749 gripe_nan_to_logical_conversion (); \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
750 return r; \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
751 } \ |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
752 else \ |
8982
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
753 r.xelem(i) = (m1.elem(i) EQ1 LHS_ZERO) OP (m2.elem(i) EQ2 RHS_ZERO); \ |
4543 | 754 } \ |
755 } \ | |
756 else \ | |
757 gripe_nonconformant (#F, m1_dims, m2_dims); \ | |
758 \ | |
759 return r; \ | |
760 } | |
761 | |
5030 | 762 #define NDND_BOOL_OPS2(ND1, ND2, LHS_ZERO, RHS_ZERO) \ |
8982
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
763 NDND_BOOL_OP (mx_el_and, !=, &&, !=, ND1, ND2, LHS_ZERO, RHS_ZERO) \ |
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
764 NDND_BOOL_OP (mx_el_or, !=, ||, !=, ND1, ND2, LHS_ZERO, RHS_ZERO) \ |
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
765 NDND_BOOL_OP (mx_el_and_not, != , &&, ==, ND1, ND2, LHS_ZERO, RHS_ZERO) \ |
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
766 NDND_BOOL_OP (mx_el_or_not, !=, ||, ==, ND1, ND2, LHS_ZERO, RHS_ZERO) \ |
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
767 NDND_BOOL_OP (mx_el_not_and, ==, &&, !=, ND1, ND2, LHS_ZERO, RHS_ZERO) \ |
dc6bda6f9994
implement compound logical ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
768 NDND_BOOL_OP (mx_el_not_or, ==, ||, !=, ND1, ND2, LHS_ZERO, RHS_ZERO) |
5030 | 769 |
4543 | 770 #define NDND_BOOL_OPS(ND1, ND2, ZERO) \ |
5030 | 771 NDND_BOOL_OPS2(ND1, ND2, ZERO, ZERO) |
4543 | 772 |
2870 | 773 // scalar by diagonal matrix operations. |
774 | |
775 #define SDM_BIN_OP(R, OP, S, DM, OPEQ) \ | |
2829 | 776 R \ |
777 OP (const S& s, const DM& dm) \ | |
778 { \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
779 octave_idx_type nr = dm.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
780 octave_idx_type nc = dm.cols (); \ |
2829 | 781 \ |
782 R r (nr, nc, s); \ | |
783 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
784 for (octave_idx_type i = 0; i < dm.length (); i++) \ |
2870 | 785 r.elem(i, i) OPEQ dm.elem(i, i); \ |
2829 | 786 \ |
787 return r; \ | |
788 } | |
789 | |
2870 | 790 #define SDM_BIN_OPS(R, S, DM) \ |
791 SDM_BIN_OP (R, operator +, S, DM, +=) \ | |
792 SDM_BIN_OP (R, operator -, S, DM, -=) | |
793 | |
794 // diagonal matrix by scalar operations. | |
795 | |
796 #define DMS_BIN_OP(R, OP, DM, S, SGN) \ | |
2829 | 797 R \ |
798 OP (const DM& dm, const S& s) \ | |
799 { \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
800 octave_idx_type nr = dm.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
801 octave_idx_type nc = dm.cols (); \ |
2829 | 802 \ |
803 R r (nr, nc, SGN s); \ | |
804 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
805 for (octave_idx_type i = 0; i < dm.length (); i++) \ |
2870 | 806 r.elem(i, i) += dm.elem(i, i); \ |
2829 | 807 \ |
808 return r; \ | |
809 } | |
810 | |
2870 | 811 #define DMS_BIN_OPS(R, DM, S) \ |
812 DMS_BIN_OP (R, operator +, DM, S, ) \ | |
813 DMS_BIN_OP (R, operator -, DM, S, -) | |
814 | |
815 // matrix by diagonal matrix operations. | |
816 | |
817 #define MDM_BIN_OP(R, OP, M, DM, OPEQ) \ | |
2829 | 818 R \ |
819 OP (const M& m, const DM& dm) \ | |
820 { \ | |
821 R r; \ | |
822 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
823 octave_idx_type m_nr = m.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
824 octave_idx_type m_nc = m.cols (); \ |
2829 | 825 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
826 octave_idx_type dm_nr = dm.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
827 octave_idx_type dm_nc = dm.cols (); \ |
2829 | 828 \ |
829 if (m_nr != dm_nr || m_nc != dm_nc) \ | |
830 gripe_nonconformant (#OP, m_nr, m_nc, dm_nr, dm_nc); \ | |
831 else \ | |
832 { \ | |
833 r.resize (m_nr, m_nc); \ | |
834 \ | |
835 if (m_nr > 0 && m_nc > 0) \ | |
836 { \ | |
3585 | 837 r = R (m); \ |
2829 | 838 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
839 octave_idx_type len = dm.length (); \ |
2829 | 840 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
841 for (octave_idx_type i = 0; i < len; i++) \ |
2870 | 842 r.elem(i, i) OPEQ dm.elem(i, i); \ |
2829 | 843 } \ |
844 } \ | |
845 \ | |
846 return r; \ | |
847 } | |
848 | |
5030 | 849 #define MDM_MULTIPLY_OP(R, M, DM, R_ZERO) \ |
2829 | 850 R \ |
851 operator * (const M& m, const DM& dm) \ | |
852 { \ | |
853 R r; \ | |
854 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
855 octave_idx_type m_nr = m.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
856 octave_idx_type m_nc = m.cols (); \ |
2829 | 857 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
858 octave_idx_type dm_nr = dm.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
859 octave_idx_type dm_nc = dm.cols (); \ |
2829 | 860 \ |
861 if (m_nc != dm_nr) \ | |
862 gripe_nonconformant ("operator *", m_nr, m_nc, dm_nr, dm_nc); \ | |
863 else \ | |
864 { \ | |
8366
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
865 r = R (m_nr, dm_nc); \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
866 R::element_type *rd = r.fortran_vec (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
867 const M::element_type *md = m.data (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
868 const DM::element_type *dd = dm.data (); \ |
4543 | 869 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
870 octave_idx_type len = dm.length (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
871 for (octave_idx_type i = 0; i < len; i++) \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
872 { \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
873 mx_inline_multiply_vs (rd, md, m_nr, dd[i]); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
874 rd += m_nr; md += m_nr; \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
875 } \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
876 mx_inline_fill_vs (rd, m_nr * (dm_nc - len), R_ZERO); \ |
2829 | 877 } \ |
878 \ | |
879 return r; \ | |
880 } | |
881 | |
5030 | 882 #define MDM_BIN_OPS(R, M, DM, R_ZERO) \ |
2870 | 883 MDM_BIN_OP (R, operator +, M, DM, +=) \ |
884 MDM_BIN_OP (R, operator -, M, DM, -=) \ | |
5030 | 885 MDM_MULTIPLY_OP (R, M, DM, R_ZERO) |
2829 | 886 |
2870 | 887 // diagonal matrix by matrix operations. |
888 | |
3585 | 889 #define DMM_BIN_OP(R, OP, DM, M, OPEQ, PREOP) \ |
2829 | 890 R \ |
891 OP (const DM& dm, const M& m) \ | |
892 { \ | |
893 R r; \ | |
894 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
895 octave_idx_type dm_nr = dm.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
896 octave_idx_type dm_nc = dm.cols (); \ |
2829 | 897 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
898 octave_idx_type m_nr = m.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
899 octave_idx_type m_nc = m.cols (); \ |
2829 | 900 \ |
901 if (dm_nr != m_nr || dm_nc != m_nc) \ | |
902 gripe_nonconformant (#OP, dm_nr, dm_nc, m_nr, m_nc); \ | |
903 else \ | |
904 { \ | |
905 if (m_nr > 0 && m_nc > 0) \ | |
906 { \ | |
3585 | 907 r = R (PREOP m); \ |
2829 | 908 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
909 octave_idx_type len = dm.length (); \ |
2829 | 910 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
911 for (octave_idx_type i = 0; i < len; i++) \ |
2870 | 912 r.elem(i, i) OPEQ dm.elem(i, i); \ |
2829 | 913 } \ |
914 else \ | |
915 r.resize (m_nr, m_nc); \ | |
916 } \ | |
917 \ | |
918 return r; \ | |
919 } | |
920 | |
5030 | 921 #define DMM_MULTIPLY_OP(R, DM, M, R_ZERO) \ |
2829 | 922 R \ |
923 operator * (const DM& dm, const M& m) \ | |
924 { \ | |
925 R r; \ | |
926 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
927 octave_idx_type dm_nr = dm.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
928 octave_idx_type dm_nc = dm.cols (); \ |
2829 | 929 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
930 octave_idx_type m_nr = m.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
931 octave_idx_type m_nc = m.cols (); \ |
2829 | 932 \ |
933 if (dm_nc != m_nr) \ | |
934 gripe_nonconformant ("operator *", dm_nr, dm_nc, m_nr, m_nc); \ | |
935 else \ | |
936 { \ | |
8366
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
937 r = R (dm_nr, m_nc); \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
938 R::element_type *rd = r.fortran_vec (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
939 const M::element_type *md = m.data (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
940 const DM::element_type *dd = dm.data (); \ |
4543 | 941 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
942 octave_idx_type len = dm.length (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
943 for (octave_idx_type i = 0; i < m_nc; i++) \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
944 { \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
945 mx_inline_multiply_vv (rd, md, dd, len); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
946 rd += len; md += m_nr; \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
947 mx_inline_fill_vs (rd, dm_nr - len, R_ZERO); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
948 rd += dm_nr - len; \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
949 } \ |
2829 | 950 } \ |
951 \ | |
952 return r; \ | |
953 } | |
954 | |
5030 | 955 #define DMM_BIN_OPS(R, DM, M, R_ZERO) \ |
3585 | 956 DMM_BIN_OP (R, operator +, DM, M, +=, ) \ |
957 DMM_BIN_OP (R, operator -, DM, M, +=, -) \ | |
5030 | 958 DMM_MULTIPLY_OP (R, DM, M, R_ZERO) |
2829 | 959 |
2870 | 960 // diagonal matrix by diagonal matrix operations. |
2829 | 961 |
2870 | 962 #define DMDM_BIN_OP(R, OP, DM1, DM2, F) \ |
2829 | 963 R \ |
964 OP (const DM1& dm1, const DM2& dm2) \ | |
965 { \ | |
966 R r; \ | |
967 \ | |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
968 octave_idx_type dm1_nr = dm1.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
969 octave_idx_type dm1_nc = dm1.cols (); \ |
2829 | 970 \ |
8380
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
971 octave_idx_type dm2_nr = dm2.rows (); \ |
dbe67764e628
fix & improve speed of diagonal matrix multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
8375
diff
changeset
|
972 octave_idx_type dm2_nc = dm2.cols (); \ |
2829 | 973 \ |
974 if (dm1_nr != dm2_nr || dm1_nc != dm2_nc) \ | |
975 gripe_nonconformant (#OP, dm1_nr, dm1_nc, dm2_nr, dm2_nc); \ | |
976 else \ | |
977 { \ | |
978 r.resize (dm1_nr, dm1_nc); \ | |
979 \ | |
980 if (dm1_nr > 0 && dm1_nc > 0) \ | |
981 F ## _vv (r.fortran_vec (), dm1.data (), dm2.data (), \ | |
8397 | 982 dm1.length ()); \ |
2829 | 983 } \ |
984 \ | |
985 return r; \ | |
986 } | |
987 | |
2870 | 988 #define DMDM_BIN_OPS(R, DM1, DM2) \ |
3769 | 989 DMDM_BIN_OP (R, operator +, DM1, DM2, mx_inline_add) \ |
990 DMDM_BIN_OP (R, operator -, DM1, DM2, mx_inline_subtract) \ | |
991 DMDM_BIN_OP (R, product, DM1, DM2, mx_inline_multiply) | |
2870 | 992 |
8774
b756ce0002db
split implementation and interface in mx-op-defs and MArray-defs
Jaroslav Hajek <highegg@gmail.com>
parents:
8397
diff
changeset
|
993 // scalar by N-d array min/max ops |
3582 | 994 |
7189 | 995 #define SND_MINMAX_FCN(FCN, OP, T) \ |
996 T ## NDArray \ | |
997 FCN (octave_ ## T d, const T ## NDArray& m) \ | |
998 { \ | |
999 dim_vector dv = m.dims (); \ | |
1000 octave_idx_type nel = dv.numel (); \ | |
1001 \ | |
1002 if (nel == 0) \ | |
1003 return T ## NDArray (dv); \ | |
1004 \ | |
1005 T ## NDArray result (dv); \ | |
1006 \ | |
1007 for (octave_idx_type i = 0; i < nel; i++) \ | |
1008 { \ | |
1009 OCTAVE_QUIT; \ | |
1010 result (i) = d OP m (i) ? d : m(i); \ | |
1011 } \ | |
1012 \ | |
1013 return result; \ | |
1014 } | |
1015 | |
1016 #define NDS_MINMAX_FCN(FCN, OP, T) \ | |
1017 T ## NDArray \ | |
1018 FCN (const T ## NDArray& m, octave_ ## T d) \ | |
1019 { \ | |
1020 dim_vector dv = m.dims (); \ | |
1021 octave_idx_type nel = dv.numel (); \ | |
1022 \ | |
1023 if (nel == 0) \ | |
1024 return T ## NDArray (dv); \ | |
1025 \ | |
1026 T ## NDArray result (dv); \ | |
1027 \ | |
1028 for (octave_idx_type i = 0; i < nel; i++) \ | |
1029 { \ | |
1030 OCTAVE_QUIT; \ | |
1031 result (i) = m (i) OP d ? m(i) : d; \ | |
1032 } \ | |
1033 \ | |
1034 return result; \ | |
1035 } | |
1036 | |
1037 #define NDND_MINMAX_FCN(FCN, OP, T) \ | |
1038 T ## NDArray \ | |
1039 FCN (const T ## NDArray& a, const T ## NDArray& b) \ | |
1040 { \ | |
1041 dim_vector dv = a.dims (); \ | |
1042 octave_idx_type nel = dv.numel (); \ | |
1043 \ | |
1044 if (dv != b.dims ()) \ | |
1045 { \ | |
1046 (*current_liboctave_error_handler) \ | |
1047 ("two-arg min expecting args of same size"); \ | |
1048 return T ## NDArray (); \ | |
1049 } \ | |
1050 \ | |
1051 if (nel == 0) \ | |
1052 return T ## NDArray (dv); \ | |
1053 \ | |
1054 T ## NDArray result (dv); \ | |
1055 \ | |
1056 for (octave_idx_type i = 0; i < nel; i++) \ | |
1057 { \ | |
1058 OCTAVE_QUIT; \ | |
1059 result (i) = a(i) OP b(i) ? a(i) : b(i); \ | |
1060 } \ | |
1061 \ | |
1062 return result; \ | |
1063 } | |
1064 | |
1065 #define MINMAX_FCNS(T) \ | |
1066 SND_MINMAX_FCN (min, <, T) \ | |
1067 NDS_MINMAX_FCN (min, <, T) \ | |
1068 NDND_MINMAX_FCN (min, <, T) \ | |
1069 SND_MINMAX_FCN (max, >, T) \ | |
1070 NDS_MINMAX_FCN (max, >, T) \ | |
1071 NDND_MINMAX_FCN (max, >, T) | |
1072 | |
8774
b756ce0002db
split implementation and interface in mx-op-defs and MArray-defs
Jaroslav Hajek <highegg@gmail.com>
parents:
8397
diff
changeset
|
1073 // permutation matrix by matrix ops and vice versa |
7189 | 1074 |
8367
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1075 #define PMM_MULTIPLY_OP(PM, M) \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1076 M operator * (const PM& p, const M& x) \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1077 { \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1078 octave_idx_type nr = x.rows (), nc = x.columns (); \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1079 M result; \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1080 if (p.columns () != nr) \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1081 gripe_nonconformant ("operator *", p.rows (), p.columns (), nr, nc); \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1082 else \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1083 { \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1084 if (p.is_col_perm ()) \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1085 { \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1086 result = M (nr, nc); \ |
8375
e3c9102431a9
fix design problems of diag & perm matrix classes
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
1087 result.assign (p.pvec (), idx_vector::colon, x); \ |
8367
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1088 } \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1089 else \ |
8375
e3c9102431a9
fix design problems of diag & perm matrix classes
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
1090 result = x.index (p.pvec (), idx_vector::colon); \ |
8367
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1091 } \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1092 \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1093 return result; \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1094 } |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1095 |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1096 #define MPM_MULTIPLY_OP(M, PM) \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1097 M operator * (const M& x, const PM& p) \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1098 { \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1099 octave_idx_type nr = x.rows (), nc = x.columns (); \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1100 M result; \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1101 if (p.rows () != nc) \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1102 gripe_nonconformant ("operator *", nr, nc, p.rows (), p.columns ()); \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1103 else \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1104 { \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1105 if (p.is_col_perm ()) \ |
8375
e3c9102431a9
fix design problems of diag & perm matrix classes
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
1106 result = x.index (idx_vector::colon, p.pvec ()); \ |
8367
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1107 else \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1108 { \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1109 result = M (nr, nc); \ |
8375
e3c9102431a9
fix design problems of diag & perm matrix classes
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
1110 result.assign (idx_vector::colon, p.pvec (), x); \ |
8367
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1111 } \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1112 } \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1113 \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1114 return result; \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1115 } |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1116 |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1117 #define PMM_BIN_OPS(R, PM, M) \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1118 PMM_MULTIPLY_OP(PM, M); |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1119 |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1120 #define MPM_BIN_OPS(R, M, PM) \ |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1121 MPM_MULTIPLY_OP(M, PM); |
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
1122 |
8998
a48fba01e4ac
optimize isnan/isinf/isfinite mappers
Jaroslav Hajek <highegg@gmail.com>
parents:
8983
diff
changeset
|
1123 #define NDND_MAPPER_BODY(R, NAME) \ |
a48fba01e4ac
optimize isnan/isinf/isfinite mappers
Jaroslav Hajek <highegg@gmail.com>
parents:
8983
diff
changeset
|
1124 R retval (dims ()); \ |
a48fba01e4ac
optimize isnan/isinf/isfinite mappers
Jaroslav Hajek <highegg@gmail.com>
parents:
8983
diff
changeset
|
1125 octave_idx_type n = numel (); \ |
a48fba01e4ac
optimize isnan/isinf/isfinite mappers
Jaroslav Hajek <highegg@gmail.com>
parents:
8983
diff
changeset
|
1126 for (octave_idx_type i = 0; i < n; i++) \ |
a48fba01e4ac
optimize isnan/isinf/isfinite mappers
Jaroslav Hajek <highegg@gmail.com>
parents:
8983
diff
changeset
|
1127 retval.xelem (i) = NAME (elem (i)); \ |
a48fba01e4ac
optimize isnan/isinf/isfinite mappers
Jaroslav Hajek <highegg@gmail.com>
parents:
8983
diff
changeset
|
1128 return retval; |
a48fba01e4ac
optimize isnan/isinf/isfinite mappers
Jaroslav Hajek <highegg@gmail.com>
parents:
8983
diff
changeset
|
1129 |
8774
b756ce0002db
split implementation and interface in mx-op-defs and MArray-defs
Jaroslav Hajek <highegg@gmail.com>
parents:
8397
diff
changeset
|
1130 #endif |
b756ce0002db
split implementation and interface in mx-op-defs and MArray-defs
Jaroslav Hajek <highegg@gmail.com>
parents:
8397
diff
changeset
|
1131 |
7189 | 1132 |
2829 | 1133 /* |
1134 ;;; Local Variables: *** | |
1135 ;;; mode: C++ *** | |
1136 ;;; End: *** | |
1137 */ |