26 #if ! defined (octave_mx_op_defs_h)
27 #define octave_mx_op_defs_h 1
29 #include "octave-config.h"
36 if (octave::math::isnan (s)) \
37 octave::err_nan_to_logical_conversion ()
39 #define MNANCHK(m, MT) \
40 if (do_mx_check (m, mx_inline_any_nan<MT>)) \
41 octave::err_nan_to_logical_conversion ()
45 #define VS_BIN_OP(R, F, OP, V, S) \
47 F (const V& v, const S& s) \
49 return do_ms_binary_op<R::element_type, V::element_type, S> (v, s, OP); \
52 #define VS_BIN_OPS(R, V, S) \
53 VS_BIN_OP (R, operator +, mx_inline_add, V, S) \
54 VS_BIN_OP (R, operator -, mx_inline_sub, V, S) \
55 VS_BIN_OP (R, operator *, mx_inline_mul, V, S) \
56 VS_BIN_OP (R, operator /, mx_inline_div, V, S)
60 #define SV_BIN_OP(R, F, OP, S, V) \
62 F (const S& s, const V& v) \
64 return do_sm_binary_op<R::element_type, S, V::element_type> (s, v, OP); \
67 #define SV_BIN_OPS(R, S, V) \
68 SV_BIN_OP (R, operator +, mx_inline_add, S, V) \
69 SV_BIN_OP (R, operator -, mx_inline_sub, S, V) \
70 SV_BIN_OP (R, operator *, mx_inline_mul, S, V) \
71 SV_BIN_OP (R, operator /, mx_inline_div, S, V)
75 #define VV_BIN_OP(R, F, OP, V1, V2) \
77 F (const V1& v1, const V2& v2) \
79 return do_mm_binary_op<R::element_type, V1::element_type, V2::element_type> (v1, v2, OP, OP, OP, #F); \
82 #define VV_BIN_OPS(R, V1, V2) \
83 VV_BIN_OP (R, operator +, mx_inline_add, V1, V2) \
84 VV_BIN_OP (R, operator -, mx_inline_sub, V1, V2) \
85 VV_BIN_OP (R, product, mx_inline_mul, V1, V2) \
86 VV_BIN_OP (R, quotient, mx_inline_div, V1, V2)
90 #define MS_BIN_OP(R, OP, M, S, F) \
92 OP (const M& m, const S& s) \
94 return do_ms_binary_op<R::element_type, M::element_type, S> (m, s, F); \
97 #define MS_BIN_OPS(R, M, S) \
98 MS_BIN_OP (R, operator +, M, S, mx_inline_add) \
99 MS_BIN_OP (R, operator -, M, S, mx_inline_sub) \
100 MS_BIN_OP (R, operator *, M, S, mx_inline_mul) \
101 MS_BIN_OP (R, operator /, M, S, mx_inline_div)
103 #define MS_CMP_OP(F, OP, M, S) \
105 F (const M& m, const S& s) \
107 return do_ms_binary_op<bool, M::element_type, S> (m, s, OP); \
110 #define MS_CMP_OPS(M, S) \
111 MS_CMP_OP (mx_el_lt, mx_inline_lt, M, S) \
112 MS_CMP_OP (mx_el_le, mx_inline_le, M, S) \
113 MS_CMP_OP (mx_el_ge, mx_inline_ge, M, S) \
114 MS_CMP_OP (mx_el_gt, mx_inline_gt, M, S) \
115 MS_CMP_OP (mx_el_eq, mx_inline_eq, M, S) \
116 MS_CMP_OP (mx_el_ne, mx_inline_ne, M, S)
118 #define MS_BOOL_OP(F, OP, M, S) \
120 F (const M& m, const S& s) \
122 MNANCHK (m, M::element_type); \
124 return do_ms_binary_op<bool, M::element_type, S> (m, s, OP); \
127 #define MS_BOOL_OPS(M, S) \
128 MS_BOOL_OP (mx_el_and, mx_inline_and, M, S) \
129 MS_BOOL_OP (mx_el_or, mx_inline_or, M, S)
133 #define SM_BIN_OP(R, OP, S, M, F) \
135 OP (const S& s, const M& m) \
137 return do_sm_binary_op<R::element_type, S, M::element_type> (s, m, F); \
140 #define SM_BIN_OPS(R, S, M) \
141 SM_BIN_OP (R, operator +, S, M, mx_inline_add) \
142 SM_BIN_OP (R, operator -, S, M, mx_inline_sub) \
143 SM_BIN_OP (R, operator *, S, M, mx_inline_mul) \
144 SM_BIN_OP (R, operator /, S, M, mx_inline_div)
146 #define SM_CMP_OP(F, OP, S, M) \
148 F (const S& s, const M& m) \
150 return do_sm_binary_op<bool, S, M::element_type> (s, m, OP); \
153 #define SM_CMP_OPS(S, M) \
154 SM_CMP_OP (mx_el_lt, mx_inline_lt, S, M) \
155 SM_CMP_OP (mx_el_le, mx_inline_le, S, M) \
156 SM_CMP_OP (mx_el_ge, mx_inline_ge, S, M) \
157 SM_CMP_OP (mx_el_gt, mx_inline_gt, S, M) \
158 SM_CMP_OP (mx_el_eq, mx_inline_eq, S, M) \
159 SM_CMP_OP (mx_el_ne, mx_inline_ne, S, M)
161 #define SM_BOOL_OP(F, OP, S, M) \
163 F (const S& s, const M& m) \
166 MNANCHK (m, M::element_type); \
167 return do_sm_binary_op<bool, S, M::element_type> (s, m, OP); \
170 #define SM_BOOL_OPS(S, M) \
171 SM_BOOL_OP (mx_el_and, mx_inline_and, S, M) \
172 SM_BOOL_OP (mx_el_or, mx_inline_or, S, M)
176 #define MM_BIN_OP(R, OP, M1, M2, F) \
178 OP (const M1& m1, const M2& m2) \
180 return do_mm_binary_op<R::element_type, M1::element_type, M2::element_type> (m1, m2, F, F, F, #OP); \
183 #define MM_BIN_OPS(R, M1, M2) \
184 MM_BIN_OP (R, operator +, M1, M2, mx_inline_add) \
185 MM_BIN_OP (R, operator -, M1, M2, mx_inline_sub) \
186 MM_BIN_OP (R, product, M1, M2, mx_inline_mul) \
187 MM_BIN_OP (R, quotient, M1, M2, mx_inline_div)
189 #define MM_CMP_OP(F, OP, M1, M2) \
191 F (const M1& m1, const M2& m2) \
193 return do_mm_binary_op<bool, M1::element_type, M2::element_type> (m1, m2, OP, OP, OP, #F); \
196 #define MM_CMP_OPS(M1, M2) \
197 MM_CMP_OP (mx_el_lt, mx_inline_lt, M1, M2) \
198 MM_CMP_OP (mx_el_le, mx_inline_le, M1, M2) \
199 MM_CMP_OP (mx_el_ge, mx_inline_ge, M1, M2) \
200 MM_CMP_OP (mx_el_gt, mx_inline_gt, M1, M2) \
201 MM_CMP_OP (mx_el_eq, mx_inline_eq, M1, M2) \
202 MM_CMP_OP (mx_el_ne, mx_inline_ne, M1, M2)
204 #define MM_BOOL_OP(F, OP, M1, M2) \
206 F (const M1& m1, const M2& m2) \
208 MNANCHK (m1, M1::element_type); \
209 MNANCHK (m2, M2::element_type); \
210 return do_mm_binary_op<bool, M1::element_type, M2::element_type> (m1, m2, OP, OP, OP, #F); \
213 #define MM_BOOL_OPS(M1, M2) \
214 MM_BOOL_OP (mx_el_and, mx_inline_and, M1, M2) \
215 MM_BOOL_OP (mx_el_or, mx_inline_or, M1, M2)
219 #define NDS_BIN_OP(R, OP, ND, S, F) \
221 OP (const ND& m, const S& s) \
223 return do_ms_binary_op<R::element_type, ND::element_type, S> (m, s, F); \
226 #define NDS_BIN_OPS(R, ND, S) \
227 NDS_BIN_OP (R, operator +, ND, S, mx_inline_add) \
228 NDS_BIN_OP (R, operator -, ND, S, mx_inline_sub) \
229 NDS_BIN_OP (R, operator *, ND, S, mx_inline_mul) \
230 NDS_BIN_OP (R, operator /, ND, S, mx_inline_div)
232 #define NDS_CMP_OP(F, OP, ND, S) \
234 F (const ND& m, const S& s) \
236 return do_ms_binary_op<bool, ND::element_type, S> (m, s, OP); \
239 #define NDS_CMP_OPS(ND, S) \
240 NDS_CMP_OP (mx_el_lt, mx_inline_lt, ND, S) \
241 NDS_CMP_OP (mx_el_le, mx_inline_le, ND, S) \
242 NDS_CMP_OP (mx_el_ge, mx_inline_ge, ND, S) \
243 NDS_CMP_OP (mx_el_gt, mx_inline_gt, ND, S) \
244 NDS_CMP_OP (mx_el_eq, mx_inline_eq, ND, S) \
245 NDS_CMP_OP (mx_el_ne, mx_inline_ne, ND, S)
247 #define NDS_BOOL_OP(F, OP, ND, S) \
249 F (const ND& m, const S& s) \
251 MNANCHK (m, ND::element_type); \
253 return do_ms_binary_op<bool, ND::element_type, S> (m, s, OP); \
256 #define NDS_BOOL_OPS(ND, S) \
257 NDS_BOOL_OP (mx_el_and, mx_inline_and, ND, S) \
258 NDS_BOOL_OP (mx_el_or, mx_inline_or, ND, S) \
259 NDS_BOOL_OP (mx_el_not_and, mx_inline_not_and, ND, S) \
260 NDS_BOOL_OP (mx_el_not_or, mx_inline_not_or, ND, S) \
261 NDS_BOOL_OP (mx_el_and_not, mx_inline_and_not, ND, S) \
262 NDS_BOOL_OP (mx_el_or_not, mx_inline_or_not, ND, S)
266 #define SND_BIN_OP(R, OP, S, ND, F) \
268 OP (const S& s, const ND& m) \
270 return do_sm_binary_op<R::element_type, S, ND::element_type> (s, m, F); \
273 #define SND_BIN_OPS(R, S, ND) \
274 SND_BIN_OP (R, operator +, S, ND, mx_inline_add) \
275 SND_BIN_OP (R, operator -, S, ND, mx_inline_sub) \
276 SND_BIN_OP (R, operator *, S, ND, mx_inline_mul) \
277 SND_BIN_OP (R, operator /, S, ND, mx_inline_div)
279 #define SND_CMP_OP(F, OP, S, ND) \
281 F (const S& s, const ND& m) \
283 return do_sm_binary_op<bool, S, ND::element_type> (s, m, OP); \
286 #define SND_CMP_OPS(S, ND) \
287 SND_CMP_OP (mx_el_lt, mx_inline_lt, S, ND) \
288 SND_CMP_OP (mx_el_le, mx_inline_le, S, ND) \
289 SND_CMP_OP (mx_el_ge, mx_inline_ge, S, ND) \
290 SND_CMP_OP (mx_el_gt, mx_inline_gt, S, ND) \
291 SND_CMP_OP (mx_el_eq, mx_inline_eq, S, ND) \
292 SND_CMP_OP (mx_el_ne, mx_inline_ne, S, ND)
294 #define SND_BOOL_OP(F, OP, S, ND) \
296 F (const S& s, const ND& m) \
299 MNANCHK (m, ND::element_type); \
300 return do_sm_binary_op<bool, S, ND::element_type> (s, m, OP); \
303 #define SND_BOOL_OPS(S, ND) \
304 SND_BOOL_OP (mx_el_and, mx_inline_and, S, ND) \
305 SND_BOOL_OP (mx_el_or, mx_inline_or, S, ND) \
306 SND_BOOL_OP (mx_el_not_and, mx_inline_not_and, S, ND) \
307 SND_BOOL_OP (mx_el_not_or, mx_inline_not_or, S, ND) \
308 SND_BOOL_OP (mx_el_and_not, mx_inline_and_not, S, ND) \
309 SND_BOOL_OP (mx_el_or_not, mx_inline_or_not, S, ND)
313 #define NDND_BIN_OP(R, OP, ND1, ND2, F) \
315 OP (const ND1& m1, const ND2& m2) \
317 return do_mm_binary_op<R::element_type, ND1::element_type, ND2::element_type> (m1, m2, F, F, F, #OP); \
320 #define NDND_BIN_OPS(R, ND1, ND2) \
321 NDND_BIN_OP (R, operator +, ND1, ND2, mx_inline_add) \
322 NDND_BIN_OP (R, operator -, ND1, ND2, mx_inline_sub) \
323 NDND_BIN_OP (R, product, ND1, ND2, mx_inline_mul) \
324 NDND_BIN_OP (R, quotient, ND1, ND2, mx_inline_div)
326 #define NDND_CMP_OP(F, OP, ND1, ND2) \
328 F (const ND1& m1, const ND2& m2) \
330 return do_mm_binary_op<bool, ND1::element_type, ND2::element_type> (m1, m2, OP, OP, OP, #F); \
333 #define NDND_CMP_OPS(ND1, ND2) \
334 NDND_CMP_OP (mx_el_lt, mx_inline_lt, ND1, ND2) \
335 NDND_CMP_OP (mx_el_le, mx_inline_le, ND1, ND2) \
336 NDND_CMP_OP (mx_el_ge, mx_inline_ge, ND1, ND2) \
337 NDND_CMP_OP (mx_el_gt, mx_inline_gt, ND1, ND2) \
338 NDND_CMP_OP (mx_el_eq, mx_inline_eq, ND1, ND2) \
339 NDND_CMP_OP (mx_el_ne, mx_inline_ne, ND1, ND2)
341 #define NDND_BOOL_OP(F, OP, ND1, ND2) \
343 F (const ND1& m1, const ND2& m2) \
345 MNANCHK (m1, ND1::element_type); \
346 MNANCHK (m2, ND2::element_type); \
347 return do_mm_binary_op<bool, ND1::element_type, ND2::element_type> (m1, m2, OP, OP, OP, #F); \
350 #define NDND_BOOL_OPS(ND1, ND2) \
351 NDND_BOOL_OP (mx_el_and, mx_inline_and, ND1, ND2) \
352 NDND_BOOL_OP (mx_el_or, mx_inline_or, ND1, ND2) \
353 NDND_BOOL_OP (mx_el_not_and, mx_inline_not_and, ND1, ND2) \
354 NDND_BOOL_OP (mx_el_not_or, mx_inline_not_or, ND1, ND2) \
355 NDND_BOOL_OP (mx_el_and_not, mx_inline_and_not, ND1, ND2) \
356 NDND_BOOL_OP (mx_el_or_not, mx_inline_or_not, ND1, ND2)
360 #define SDM_BIN_OP(R, OP, S, DM) \
362 operator OP (const S& s, const DM& dm) \
364 R r (dm.rows (), dm.cols ()); \
366 for (octave_idx_type i = 0; i < dm.length (); i++) \
367 r.dgxelem (i) = s OP dm.dgelem (i); \
372 #define SDM_BIN_OPS(R, S, DM) \
373 SDM_BIN_OP (R, *, S, DM)
377 #define DMS_BIN_OP(R, OP, DM, S) \
379 operator OP (const DM& dm, const S& s) \
381 R r (dm.rows (), dm.cols ()); \
383 for (octave_idx_type i = 0; i < dm.length (); i++) \
384 r.dgxelem (i) = dm.dgelem (i) OP s; \
389 #define DMS_BIN_OPS(R, DM, S) \
390 DMS_BIN_OP (R, *, DM, S) \
391 DMS_BIN_OP (R, /, DM, S)
395 #define MDM_BIN_OP(R, OP, M, DM, OPEQ) \
397 OP (const M& m, const DM& dm) \
401 octave_idx_type m_nr = m.rows (); \
402 octave_idx_type m_nc = m.cols (); \
404 octave_idx_type dm_nr = dm.rows (); \
405 octave_idx_type dm_nc = dm.cols (); \
407 if (m_nr != dm_nr || m_nc != dm_nc) \
408 octave::err_nonconformant (#OP, m_nr, m_nc, dm_nr, dm_nc); \
410 r.resize (m_nr, m_nc); \
412 if (m_nr > 0 && m_nc > 0) \
416 octave_idx_type len = dm.length (); \
418 for (octave_idx_type i = 0; i < len; i++) \
419 r.elem (i, i) OPEQ dm.elem (i, i); \
425 #define MDM_MULTIPLY_OP(R, M, DM) \
427 operator * (const M& m, const DM& dm) \
431 R::element_type r_zero = R::element_type (); \
433 octave_idx_type m_nr = m.rows (); \
434 octave_idx_type m_nc = m.cols (); \
436 octave_idx_type dm_nr = dm.rows (); \
437 octave_idx_type dm_nc = dm.cols (); \
440 octave::err_nonconformant ("operator *", m_nr, m_nc, dm_nr, dm_nc); \
442 r = R (m_nr, dm_nc); \
443 R::element_type *rd = r.fortran_vec (); \
444 const M::element_type *md = m.data (); \
445 const DM::element_type *dd = dm.data (); \
447 octave_idx_type len = dm.length (); \
448 for (octave_idx_type i = 0; i < len; i++) \
450 mx_inline_mul (m_nr, rd, md, dd[i]); \
451 rd += m_nr; md += m_nr; \
453 mx_inline_fill (m_nr * (dm_nc - len), rd, r_zero); \
458 #define MDM_BIN_OPS(R, M, DM) \
459 MDM_BIN_OP (R, operator +, M, DM, +=) \
460 MDM_BIN_OP (R, operator -, M, DM, -=) \
461 MDM_MULTIPLY_OP (R, M, DM)
465 #define DMM_BIN_OP(R, OP, DM, M, OPEQ, PREOP) \
467 OP (const DM& dm, const M& m) \
471 octave_idx_type dm_nr = dm.rows (); \
472 octave_idx_type dm_nc = dm.cols (); \
474 octave_idx_type m_nr = m.rows (); \
475 octave_idx_type m_nc = m.cols (); \
477 if (dm_nr != m_nr || dm_nc != m_nc) \
478 octave::err_nonconformant (#OP, dm_nr, dm_nc, m_nr, m_nc); \
481 if (m_nr > 0 && m_nc > 0) \
485 octave_idx_type len = dm.length (); \
487 for (octave_idx_type i = 0; i < len; i++) \
488 r.elem (i, i) OPEQ dm.elem (i, i); \
491 r.resize (m_nr, m_nc); \
497 #define DMM_MULTIPLY_OP(R, DM, M) \
499 operator * (const DM& dm, const M& m) \
503 R::element_type r_zero = R::element_type (); \
505 octave_idx_type dm_nr = dm.rows (); \
506 octave_idx_type dm_nc = dm.cols (); \
508 octave_idx_type m_nr = m.rows (); \
509 octave_idx_type m_nc = m.cols (); \
512 octave::err_nonconformant ("operator *", dm_nr, dm_nc, m_nr, m_nc); \
514 r = R (dm_nr, m_nc); \
515 R::element_type *rd = r.fortran_vec (); \
516 const M::element_type *md = m.data (); \
517 const DM::element_type *dd = dm.data (); \
519 octave_idx_type len = dm.length (); \
520 for (octave_idx_type i = 0; i < m_nc; i++) \
522 mx_inline_mul (len, rd, md, dd); \
523 rd += len; md += m_nr; \
524 mx_inline_fill (dm_nr - len, rd, r_zero); \
531 #define DMM_BIN_OPS(R, DM, M) \
532 DMM_BIN_OP (R, operator +, DM, M, +=, ) \
533 DMM_BIN_OP (R, operator -, DM, M, +=, -) \
534 DMM_MULTIPLY_OP (R, DM, M)
538 #define DMDM_BIN_OP(R, OP, DM1, DM2, F) \
540 OP (const DM1& dm1, const DM2& dm2) \
544 octave_idx_type dm1_nr = dm1.rows (); \
545 octave_idx_type dm1_nc = dm1.cols (); \
547 octave_idx_type dm2_nr = dm2.rows (); \
548 octave_idx_type dm2_nc = dm2.cols (); \
550 if (dm1_nr != dm2_nr || dm1_nc != dm2_nc) \
551 octave::err_nonconformant (#OP, dm1_nr, dm1_nc, dm2_nr, dm2_nc); \
553 r.resize (dm1_nr, dm1_nc); \
555 if (dm1_nr > 0 && dm1_nc > 0) \
556 F (dm1.length (), r.fortran_vec (), dm1.data (), dm2.data ()); \
561 #define DMDM_BIN_OPS(R, DM1, DM2) \
562 DMDM_BIN_OP (R, operator +, DM1, DM2, mx_inline_add) \
563 DMDM_BIN_OP (R, operator -, DM1, DM2, mx_inline_sub) \
564 DMDM_BIN_OP (R, product, DM1, DM2, mx_inline_mul)
568 #define SND_MINMAX_FCN(FCN, OP, T, S) \
570 FCN (S d, const T& m) \
572 return do_sm_binary_op<T::element_type, S, T::element_type> (d, m, mx_inline_x##FCN); \
575 #define NDS_MINMAX_FCN(FCN, OP, T, S) \
577 FCN (const T& m, S d) \
579 return do_ms_binary_op<T::element_type, T::element_type, S> (m, d, mx_inline_x##FCN); \
582 #define NDND_MINMAX_FCN(FCN, OP, T, S) \
584 FCN (const T& a, const T& b) \
586 return do_mm_binary_op<T::element_type, T::element_type, T::element_type> (a, b, mx_inline_x##FCN, mx_inline_x##FCN, mx_inline_x##FCN, #FCN); \
589 #define MINMAX_FCNS(T, S) \
590 SND_MINMAX_FCN (min, <, T, S) \
591 NDS_MINMAX_FCN (min, <, T, S) \
592 NDND_MINMAX_FCN (min, <, T, S) \
593 SND_MINMAX_FCN (max, >, T, S) \
594 NDS_MINMAX_FCN (max, >, T, S) \
595 NDND_MINMAX_FCN (max, >, T, S)
599 #define PMM_MULTIPLY_OP(PM, M) \
600 M operator * (const PM& p, const M& x) \
602 octave_idx_type nr = x.rows (); \
603 octave_idx_type nc = x.columns (); \
605 if (p.columns () != nr) \
606 octave::err_nonconformant ("operator *", p.rows (), p.columns (), nr, nc); \
609 result = M (nr, nc); \
610 result.assign (p.col_perm_vec (), octave::idx_vector::colon, x); \
616 #define MPM_MULTIPLY_OP(M, PM) \
617 M operator * (const M& x, const PM& p) \
619 octave_idx_type nr = x.rows (); \
620 octave_idx_type nc = x.columns (); \
622 if (p.rows () != nc) \
623 octave::err_nonconformant ("operator *", nr, nc, p.rows (), p.columns ()); \
625 result = x.index (octave::idx_vector::colon, p.col_perm_vec ()); \
630 #define PMM_BIN_OPS(R, PM, M) \
631 PMM_MULTIPLY_OP(PM, M);
633 #define MPM_BIN_OPS(R, M, PM) \
634 MPM_MULTIPLY_OP(M, PM);
636 #define NDND_MAPPER_BODY(R, NAME) \
637 R retval (dims ()); \
638 octave_idx_type n = numel (); \
639 for (octave_idx_type i = 0; i < n; i++) \
640 retval.xelem (i) = NAME (elem (i)); \