Mercurial > hg > octave-lojdl
changeset 9557:3a1dd361f978
optimize .*=, ./= operators
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 24 Aug 2009 09:45:34 +0200 |
parents | 948795dc1974 |
children | 8b3cee6166b8 |
files | liboctave/ChangeLog liboctave/MArray-decl.h liboctave/MArray-defs.h liboctave/MArray.cc liboctave/MArray2.cc liboctave/MArrayN.cc src/ChangeLog src/OPERATORS/op-cm-cm.cc src/OPERATORS/op-fcm-fcm.cc src/OPERATORS/op-fm-fm.cc src/OPERATORS/op-int.h src/OPERATORS/op-m-m.cc |
diffstat | 12 files changed, 189 insertions(+), 11 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,18 @@ +2009-08-24 Jaroslav Hajek <highegg@gmail.com> + + * MArray.cc (MArray<T>::product_eq, MArray<T>::quotient_eq): New + methods. + * MArray2.cc (MArray2<T>::product_eq, MArray2<T>::quotient_eq): Ditto. + * MArrayN.cc (MArrayN<T>::product_eq, MArrayN<T>::quotient_eq): Ditto. + * MArray-decl.h (MARRAY_OP_ASSIGN_DECLX): New macro. + (MARRAY_OP_ASSIGN_DECLS, MARRAY_OP_ASSIGN_FWD_DEFS): Include + product_eq and quotient_eq. + (MARRAY_FORWARD_DEFS): Use MARRAY_OP_ASSIGN_FWD_DEFS1. + (MDIAGARRAY2_OPS_FORWARD_DECLS): Don't instantiate + OP= operators for diag matrices. + (MDIAGARRAY2_FORWARD_DEFS): Ditto. + * MArray-defs.h (INSTANTIATE_MDIAGARRAY2_FRIENDS): Ditto. + 2009-08-23 Jaroslav Hajek <highegg@gmail.com> * Array.h (Array::make_unique, Array::~Array, Array::operator =):
--- a/liboctave/MArray-decl.h +++ b/liboctave/MArray-decl.h @@ -28,10 +28,16 @@ PFX API A_T<E_T>& \ operator OP LTGT (A_T<E_T>&, const RHS_T&) +#define MARRAY_OP_ASSIGN_DECLX(A_T, E_T, OP, PFX, API, LTGT, RHS_T) \ + PFX API A_T<E_T>& \ + OP LTGT (A_T<E_T>&, const RHS_T&) + // All the OP= operators that we care about. #define MARRAY_OP_ASSIGN_DECLS(A_T, E_T, PFX, API, LTGT, RHS_T) \ MARRAY_OP_ASSIGN_DECL (A_T, E_T, +=, PFX, API, LTGT, RHS_T); \ - MARRAY_OP_ASSIGN_DECL (A_T, E_T, -=, PFX, API, LTGT, RHS_T); + MARRAY_OP_ASSIGN_DECL (A_T, E_T, -=, PFX, API, LTGT, RHS_T); \ + MARRAY_OP_ASSIGN_DECLX (A_T, E_T, product_eq, PFX, API, LTGT, RHS_T); \ + MARRAY_OP_ASSIGN_DECLX (A_T, E_T, quotient_eq, PFX, API, LTGT, RHS_T); #define MARRAY_OP_ASSIGN_DECLS1(A_T, E_T, PFX, API, LTGT, RHS_T) \ MARRAY_OP_ASSIGN_DECL (A_T, E_T, +=, PFX, API, LTGT, RHS_T); \ @@ -65,7 +71,9 @@ // All the OP= operators that we care about forwarding. #define MARRAY_OP_ASSIGN_FWD_DEFS(R, T, C_X, X_T, C_Y, Y_T) \ MARRAY_OP_ASSIGN_FWD_FCN (R, operator +=, T, C_X, X_T, C_Y, Y_T) \ - MARRAY_OP_ASSIGN_FWD_FCN (R, operator -=, T, C_X, X_T, C_Y, Y_T) + MARRAY_OP_ASSIGN_FWD_FCN (R, operator -=, T, C_X, X_T, C_Y, Y_T) \ + MARRAY_OP_ASSIGN_FWD_FCN (R, product_eq, T, C_X, X_T, C_Y, Y_T) \ + MARRAY_OP_ASSIGN_FWD_FCN (R, quotient_eq, T, C_X, X_T, C_Y, Y_T) #define MARRAY_OP_ASSIGN_FWD_DEFS1(R, T, C_X, X_T, C_Y, Y_T) \ MARRAY_OP_ASSIGN_FWD_FCN (R, operator +=, T, C_X, X_T, C_Y, Y_T) \ @@ -209,7 +217,6 @@ template <class T> \ class A_T; \ \ - MARRAY_OP_ASSIGN_FWD_DECLS (A_T, A_T<T>, API) \ MARRAY_UNOP_FWD_DECLS (A_T, API) \ MDIAGARRAY2_BINOP_FWD_DECLS (A_T, API) @@ -228,7 +235,7 @@ // Define all the MArray forwarding functions for return type R and // MArray element type T #define MARRAY_FORWARD_DEFS(B, R, T) \ - MARRAY_OP_ASSIGN_FWD_DEFS \ + MARRAY_OP_ASSIGN_FWD_DEFS1 \ (R, T, dynamic_cast<B<T>&>, R, , T) \ \ MARRAY_OP_ASSIGN_FWD_DEFS \ @@ -248,10 +255,6 @@ (R, T, dynamic_cast<const B<T>&>, R, dynamic_cast<const B<T>&>, R) #define MDIAGARRAY2_FORWARD_DEFS(B, R, T) \ - MARRAY_OP_ASSIGN_FWD_DEFS \ - (R, T, \ - dynamic_cast<B<T>&>, R, dynamic_cast<const B<T>&>, R) \ - \ MARRAY_UNOP_FWD_DEFS \ (R, T, dynamic_cast<const B<T>&>, R) \ \
--- a/liboctave/MArray-defs.h +++ b/liboctave/MArray-defs.h @@ -120,7 +120,6 @@ // Instantiate all the MDiagArray2 friends for MDiagArray2 element type T. #define INSTANTIATE_MDIAGARRAY2_FRIENDS(T, API) \ - MARRAY_OP_ASSIGN_DEFS (MDiagArray2, T, MDiagArray2<T>, API) \ MARRAY_UNOP_DEFS (MDiagArray2, T, API) \ MDIAGARRAY2_BINOP_DEFS (MDiagArray2, T, API)
--- a/liboctave/MArray.cc +++ b/liboctave/MArray.cc @@ -190,6 +190,42 @@ return a; } +template <class T> +MArray<T>& +product_eq (MArray<T>& a, const MArray<T>& b) +{ + if (a.is_shared ()) + return a = product (a, b); + octave_idx_type l = a.length (); + if (l > 0) + { + octave_idx_type bl = b.length (); + if (l != bl) + gripe_nonconformant ("operator .*=", l, bl); + else + DO_VV_OP2 (T, a, *=, b); + } + return a; +} + +template <class T> +MArray<T>& +quotient_eq (MArray<T>& a, const MArray<T>& b) +{ + if (a.is_shared ()) + return a = quotient (a, b); + octave_idx_type l = a.length (); + if (l > 0) + { + octave_idx_type bl = b.length (); + if (l != bl) + gripe_nonconformant ("operator ./=", l, bl); + else + DO_VV_OP2 (T, a, /=, b); + } + return a; +} + // Element by element MArray by scalar ops. #define MARRAY_AS_OP(OP) \
--- a/liboctave/MArray2.cc +++ b/liboctave/MArray2.cc @@ -123,6 +123,53 @@ return a; } + +template <class T> +MArray2<T>& +product_eq (MArray2<T>& a, const MArray2<T>& b) +{ + if (a.is_shared ()) + return a = product (a, b); + octave_idx_type r = a.rows (); + octave_idx_type c = a.cols (); + octave_idx_type br = b.rows (); + octave_idx_type bc = b.cols (); + if (r != br || c != bc) + gripe_nonconformant ("operator .*=", r, c, br, bc); + else + { + if (r > 0 && c > 0) + { + octave_idx_type l = a.length (); + DO_VV_OP2 (T, a, *=, b); + } + } + return a; +} + +template <class T> +MArray2<T>& +quotient_eq (MArray2<T>& a, const MArray2<T>& b) +{ + if (a.is_shared ()) + return a = quotient (a, b); + octave_idx_type r = a.rows (); + octave_idx_type c = a.cols (); + octave_idx_type br = b.rows (); + octave_idx_type bc = b.cols (); + if (r != br || c != bc) + gripe_nonconformant ("operator ./=", r, c, br, bc); + else + { + if (r > 0 && c > 0) + { + octave_idx_type l = a.length (); + DO_VV_OP2 (T, a, /=, b); + } + } + return a; +} + // Element by element MArray2 by scalar ops. #define MARRAY_A2S_OP(OP) \
--- a/liboctave/MArrayN.cc +++ b/liboctave/MArrayN.cc @@ -122,6 +122,52 @@ return a; } +template <class T> +MArrayN<T>& +product_eq (MArrayN<T>& a, const MArrayN<T>& b) +{ + if (a.is_shared ()) + return a = product (a, b); + + dim_vector a_dims = a.dims (); + dim_vector b_dims = b.dims (); + + if (a_dims != b_dims) + gripe_nonconformant ("operator .*=", a_dims, b_dims); + else + { + octave_idx_type l = a.length (); + + if (l > 0) + DO_VV_OP2 (T, a, *=, b); + } + + return a; +} + +template <class T> +MArrayN<T>& +quotient_eq (MArrayN<T>& a, const MArrayN<T>& b) +{ + if (a.is_shared ()) + return a = quotient (a, b); + + dim_vector a_dims = a.dims (); + dim_vector b_dims = b.dims (); + + if (a_dims != b_dims) + gripe_nonconformant ("operator ./=", a_dims, b_dims); + else + { + octave_idx_type l = a.length (); + + if (l > 0) + DO_VV_OP2 (T, a, /=, b); + } + + return a; +} + // Element by element MArrayN by scalar ops. #define MARRAYN_NDS_OP(OP) \
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,11 @@ +2009-08-24 Jaroslav Hajek <highegg@gmail.com> + + * OPERATORS/op-m-m.cc: Install .*= and ./= operators. + * OPERATORS/op-fm-fm.cc: Ditto. + * OPERATORS/op-cm-cm.cc: Ditto. + * OPERATORS/op-fcm-fcm.cc: Ditto. + * OPERATORS/op-int.h: Ditto. + 2009-08-22 Jaroslav Hajek <highegg@gmail.com> * data.cc (UNARY_OP_DEFUN_BODY,
--- a/src/OPERATORS/op-cm-cm.cc +++ b/src/OPERATORS/op-cm-cm.cc @@ -165,6 +165,8 @@ DEFNDASSIGNOP_OP (assign_add, complex_matrix, complex_matrix, complex_array, +=) DEFNDASSIGNOP_OP (assign_sub, complex_matrix, complex_matrix, complex_array, -=) +DEFNDASSIGNOP_FNOP (assign_el_mul, complex_matrix, complex_matrix, complex_array, product_eq) +DEFNDASSIGNOP_FNOP (assign_el_div, complex_matrix, complex_matrix, complex_array, quotient_eq) CONVDECL (complex_matrix_to_float_complex_matrix) { @@ -218,6 +220,8 @@ INSTALL_ASSIGNOP (op_add_eq, octave_complex_matrix, octave_complex_matrix, assign_add); INSTALL_ASSIGNOP (op_sub_eq, octave_complex_matrix, octave_complex_matrix, assign_sub); + INSTALL_ASSIGNOP (op_el_mul_eq, octave_complex_matrix, octave_complex_matrix, assign_el_mul); + INSTALL_ASSIGNOP (op_el_div_eq, octave_complex_matrix, octave_complex_matrix, assign_el_div); INSTALL_CONVOP (octave_complex_matrix, octave_float_complex_matrix, complex_matrix_to_float_complex_matrix);
--- a/src/OPERATORS/op-fcm-fcm.cc +++ b/src/OPERATORS/op-fcm-fcm.cc @@ -193,6 +193,10 @@ float_complex_matrix, float_complex_array, +=) DEFNDASSIGNOP_OP (assign_sub, float_complex_matrix, float_complex_matrix, float_complex_array, -=) +DEFNDASSIGNOP_FNOP (assign_el_mul, float_complex_matrix, float_complex_matrix, + float_complex_array, product_eq) +DEFNDASSIGNOP_FNOP (assign_el_div, float_complex_matrix, float_complex_matrix, + float_complex_array, quotient_eq) CONVDECL (float_complex_matrix_to_complex_matrix) { @@ -281,6 +285,10 @@ octave_float_complex_matrix, assign_add); INSTALL_ASSIGNOP (op_sub_eq, octave_float_complex_matrix, octave_float_complex_matrix, assign_sub); + INSTALL_ASSIGNOP (op_el_mul_eq, octave_float_complex_matrix, + octave_float_complex_matrix, assign_el_mul); + INSTALL_ASSIGNOP (op_el_div_eq, octave_float_complex_matrix, + octave_float_complex_matrix, assign_el_div); INSTALL_CONVOP (octave_float_complex_matrix, octave_complex_matrix, float_complex_matrix_to_complex_matrix);
--- a/src/OPERATORS/op-fm-fm.cc +++ b/src/OPERATORS/op-fm-fm.cc @@ -167,6 +167,8 @@ DEFNDASSIGNOP_OP (assign_add, float_matrix, float_matrix, float_array, +=) DEFNDASSIGNOP_OP (assign_sub, float_matrix, float_matrix, float_array, -=) +DEFNDASSIGNOP_FNOP (assign_el_mul, float_matrix, float_matrix, float_array, product_eq) +DEFNDASSIGNOP_FNOP (assign_el_div, float_matrix, float_matrix, float_array, quotient_eq) CONVDECL (float_matrix_to_matrix) { @@ -229,6 +231,8 @@ INSTALL_ASSIGNOP (op_add_eq, octave_float_matrix, octave_float_matrix, assign_add); INSTALL_ASSIGNOP (op_sub_eq, octave_float_matrix, octave_float_matrix, assign_sub); + INSTALL_ASSIGNOP (op_el_mul_eq, octave_float_matrix, octave_float_matrix, assign_el_mul); + INSTALL_ASSIGNOP (op_el_div_eq, octave_float_matrix, octave_float_matrix, assign_el_div); INSTALL_CONVOP (octave_float_matrix, octave_matrix, float_matrix_to_matrix); }
--- a/src/OPERATORS/op-int.h +++ b/src/OPERATORS/op-int.h @@ -756,7 +756,9 @@ #define OCTAVE_MM_INT_ASSIGNEQ_OPS(PFX, TM) \ DEFNDASSIGNOP_OP (PFX ## _assign_add, TM ## matrix, TM ## matrix, TM ## array, +=) \ - DEFNDASSIGNOP_OP (PFX ## _assign_sub, TM ## matrix, TM ## matrix, TM ## array, -=) + DEFNDASSIGNOP_OP (PFX ## _assign_sub, TM ## matrix, TM ## matrix, TM ## array, -=) \ + DEFNDASSIGNOP_FNOP (PFX ## _assign_el_mul, TM ## matrix, TM ## matrix, TM ## array, product_eq) \ + DEFNDASSIGNOP_FNOP (PFX ## _assign_el_div, TM ## matrix, TM ## matrix, TM ## array, quotient_eq) #define OCTAVE_MM_POW_OPS(T1, T2) \ octave_value \ @@ -1129,7 +1131,9 @@ #define OCTAVE_INSTALL_MM_INT_ASSIGNEQ_OPS(PFX, TLHS, TRHS) \ INSTALL_ASSIGNOP (op_add_eq, octave_ ## TLHS ## matrix, octave_ ## TRHS ## matrix, PFX ## _assign_add) \ - INSTALL_ASSIGNOP (op_sub_eq, octave_ ## TLHS ## matrix, octave_ ## TRHS ## matrix, PFX ## _assign_sub) + INSTALL_ASSIGNOP (op_sub_eq, octave_ ## TLHS ## matrix, octave_ ## TRHS ## matrix, PFX ## _assign_sub) \ + INSTALL_ASSIGNOP (op_el_mul_eq, octave_ ## TLHS ## matrix, octave_ ## TRHS ## matrix, PFX ## _assign_el_mul) \ + INSTALL_ASSIGNOP (op_el_div_eq, octave_ ## TLHS ## matrix, octave_ ## TRHS ## matrix, PFX ## _assign_el_div) #define OCTAVE_INSTALL_MM_INT_OPS(TYPE) \ OCTAVE_INSTALL_M_INT_UNOPS (TYPE) \
--- a/src/OPERATORS/op-m-m.cc +++ b/src/OPERATORS/op-m-m.cc @@ -140,6 +140,8 @@ DEFNDASSIGNOP_OP (assign_add, matrix, matrix, array, +=) DEFNDASSIGNOP_OP (assign_sub, matrix, matrix, array, -=) +DEFNDASSIGNOP_FNOP (assign_el_mul, matrix, matrix, array, product_eq) +DEFNDASSIGNOP_FNOP (assign_el_div, matrix, matrix, array, quotient_eq) CONVDECL (matrix_to_float_matrix) { @@ -198,6 +200,8 @@ INSTALL_ASSIGNOP (op_add_eq, octave_matrix, octave_matrix, assign_add); INSTALL_ASSIGNOP (op_sub_eq, octave_matrix, octave_matrix, assign_sub); + INSTALL_ASSIGNOP (op_el_mul_eq, octave_matrix, octave_matrix, assign_el_mul); + INSTALL_ASSIGNOP (op_el_div_eq, octave_matrix, octave_matrix, assign_el_div); INSTALL_CONVOP (octave_matrix, octave_float_matrix, matrix_to_float_matrix); }