Mercurial > hg > octave-lojdl
changeset 10143:f3c34b518422
simplify pairwise min/max implementations
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 20 Jan 2010 10:58:55 +0100 |
parents | 829e69ec3110 |
children | 1b883ce66707 |
files | liboctave/CNDArray.cc liboctave/ChangeLog liboctave/dNDArray.cc liboctave/fCNDArray.cc liboctave/fNDArray.cc liboctave/mx-op-defs.h |
diffstat | 6 files changed, 52 insertions(+), 488 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/CNDArray.cc +++ b/liboctave/CNDArray.cc @@ -914,139 +914,40 @@ return is; } -// FIXME -- it would be nice to share code among the min/max -// functions below. - -#define EMPTY_RETURN_CHECK(T) \ - if (nel == 0) \ - return T (dv); - ComplexNDArray -min (const Complex& c, const ComplexNDArray& m) +min (const Complex& s, const ComplexNDArray& m) { - dim_vector dv = m.dims (); - int nel = dv.numel (); - - EMPTY_RETURN_CHECK (ComplexNDArray); - - ComplexNDArray result (dv); - - for (int i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmin (c, m (i)); - } - - return result; + return do_sm_binary_op<ComplexNDArray> (s, m, mx_inline_xmin); } ComplexNDArray -min (const ComplexNDArray& m, const Complex& c) +min (const ComplexNDArray& m, const Complex& s) { - dim_vector dv = m.dims (); - int nel = dv.numel (); - - EMPTY_RETURN_CHECK (ComplexNDArray); - - ComplexNDArray result (dv); - - for (int i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmin (c, m (i)); - } - - return result; + return do_ms_binary_op<ComplexNDArray> (m, s, mx_inline_xmin); } ComplexNDArray min (const ComplexNDArray& a, const ComplexNDArray& b) { - dim_vector dv = a.dims (); - int nel = dv.numel (); - - if (dv != b.dims ()) - { - (*current_liboctave_error_handler) - ("two-arg min expecting args of same size"); - return ComplexNDArray (); - } - - EMPTY_RETURN_CHECK (ComplexNDArray); - - ComplexNDArray result (dv); - - for (int i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmin (a (i), b (i)); - } - - return result; + return do_mm_binary_op<ComplexNDArray> (a, b, mx_inline_xmin, "min"); } ComplexNDArray -max (const Complex& c, const ComplexNDArray& m) +max (const Complex& s, const ComplexNDArray& m) { - dim_vector dv = m.dims (); - int nel = dv.numel (); - - EMPTY_RETURN_CHECK (ComplexNDArray); - - ComplexNDArray result (dv); - - for (int i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmax (c, m (i)); - } - - return result; + return do_sm_binary_op<ComplexNDArray> (s, m, mx_inline_xmax); } ComplexNDArray -max (const ComplexNDArray& m, const Complex& c) +max (const ComplexNDArray& m, const Complex& s) { - dim_vector dv = m.dims (); - int nel = dv.numel (); - - EMPTY_RETURN_CHECK (ComplexNDArray); - - ComplexNDArray result (dv); - - for (int i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmax (c, m (i)); - } - - return result; + return do_ms_binary_op<ComplexNDArray> (m, s, mx_inline_xmax); } ComplexNDArray max (const ComplexNDArray& a, const ComplexNDArray& b) { - dim_vector dv = a.dims (); - int nel = dv.numel (); - - if (dv != b.dims ()) - { - (*current_liboctave_error_handler) - ("two-arg max expecting args of same size"); - return ComplexNDArray (); - } - - EMPTY_RETURN_CHECK (ComplexNDArray); - - ComplexNDArray result (dv); - - for (int i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmax (a (i), b (i)); - } - - return result; + return do_mm_binary_op<ComplexNDArray> (a, b, mx_inline_xmax, "max"); } NDS_CMP_OPS (ComplexNDArray, Complex)
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,12 @@ +2010-01-20 Jaroslav Hajek <highegg@gmail.com> + + * mx-op-defs.h (SND_MINMAX_FCN, NDS_MINMAX_FCN, NDND_MINMAX_FCN): + Simplify using mx_inlines machinery. + * dNDArray (min, max): Simplify. + * fNDArray (min, max): Simplify. + * CNDArray (min, max): Simplify. + * fCNDArray (min, max): Simplify. + 2010-01-20 Jaroslav Hajek <highegg@gmail.com> * Array.h: Substitute OCTAVE_QUIT -> octave_quit ().
--- a/liboctave/dNDArray.cc +++ b/liboctave/dNDArray.cc @@ -981,139 +981,40 @@ return is; } -// FIXME -- it would be nice to share code among the min/max -// functions below. - -#define EMPTY_RETURN_CHECK(T) \ - if (nel == 0) \ - return T (dv); - NDArray -min (double d, const NDArray& m) +min (double s, const NDArray& m) { - dim_vector dv = m.dims (); - octave_idx_type nel = dv.numel (); - - EMPTY_RETURN_CHECK (NDArray); - - NDArray result (dv); - - for (octave_idx_type i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmin (d, m (i)); - } - - return result; + return do_sm_binary_op<NDArray> (s, m, mx_inline_xmin); } NDArray -min (const NDArray& m, double d) +min (const NDArray& m, double s) { - dim_vector dv = m.dims (); - octave_idx_type nel = dv.numel (); - - EMPTY_RETURN_CHECK (NDArray); - - NDArray result (dv); - - for (octave_idx_type i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmin (d, m (i)); - } - - return result; + return do_ms_binary_op<NDArray> (m, s, mx_inline_xmin); } NDArray min (const NDArray& a, const NDArray& b) { - dim_vector dv = a.dims (); - octave_idx_type nel = dv.numel (); - - if (dv != b.dims ()) - { - (*current_liboctave_error_handler) - ("two-arg min expecting args of same size"); - return NDArray (); - } - - EMPTY_RETURN_CHECK (NDArray); - - NDArray result (dv); - - for (octave_idx_type i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmin (a (i), b (i)); - } - - return result; + return do_mm_binary_op<NDArray> (a, b, mx_inline_xmin, "min"); } NDArray -max (double d, const NDArray& m) +max (double s, const NDArray& m) { - dim_vector dv = m.dims (); - octave_idx_type nel = dv.numel (); - - EMPTY_RETURN_CHECK (NDArray); - - NDArray result (dv); - - for (octave_idx_type i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmax (d, m (i)); - } - - return result; + return do_sm_binary_op<NDArray> (s, m, mx_inline_xmax); } NDArray -max (const NDArray& m, double d) +max (const NDArray& m, double s) { - dim_vector dv = m.dims (); - octave_idx_type nel = dv.numel (); - - EMPTY_RETURN_CHECK (NDArray); - - NDArray result (dv); - - for (octave_idx_type i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmax (d, m (i)); - } - - return result; + return do_ms_binary_op<NDArray> (m, s, mx_inline_xmax); } NDArray max (const NDArray& a, const NDArray& b) { - dim_vector dv = a.dims (); - octave_idx_type nel = dv.numel (); - - if (dv != b.dims ()) - { - (*current_liboctave_error_handler) - ("two-arg max expecting args of same size"); - return NDArray (); - } - - EMPTY_RETURN_CHECK (NDArray); - - NDArray result (dv); - - for (octave_idx_type i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmax (a (i), b (i)); - } - - return result; + return do_mm_binary_op<NDArray> (a, b, mx_inline_xmax, "max"); } NDS_CMP_OPS (NDArray, double)
--- a/liboctave/fCNDArray.cc +++ b/liboctave/fCNDArray.cc @@ -909,139 +909,40 @@ return is; } -// FIXME -- it would be nice to share code among the min/max -// functions below. - -#define EMPTY_RETURN_CHECK(T) \ - if (nel == 0) \ - return T (dv); - FloatComplexNDArray -min (const FloatComplex& c, const FloatComplexNDArray& m) +min (const FloatComplex& s, const FloatComplexNDArray& m) { - dim_vector dv = m.dims (); - int nel = dv.numel (); - - EMPTY_RETURN_CHECK (FloatComplexNDArray); - - FloatComplexNDArray result (dv); - - for (int i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmin (c, m (i)); - } - - return result; + return do_sm_binary_op<FloatComplexNDArray> (s, m, mx_inline_xmin); } FloatComplexNDArray -min (const FloatComplexNDArray& m, const FloatComplex& c) +min (const FloatComplexNDArray& m, const FloatComplex& s) { - dim_vector dv = m.dims (); - int nel = dv.numel (); - - EMPTY_RETURN_CHECK (FloatComplexNDArray); - - FloatComplexNDArray result (dv); - - for (int i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmin (c, m (i)); - } - - return result; + return do_ms_binary_op<FloatComplexNDArray> (m, s, mx_inline_xmin); } FloatComplexNDArray min (const FloatComplexNDArray& a, const FloatComplexNDArray& b) { - dim_vector dv = a.dims (); - int nel = dv.numel (); - - if (dv != b.dims ()) - { - (*current_liboctave_error_handler) - ("two-arg min expecting args of same size"); - return FloatComplexNDArray (); - } - - EMPTY_RETURN_CHECK (FloatComplexNDArray); - - FloatComplexNDArray result (dv); - - for (int i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmin (a (i), b (i)); - } - - return result; + return do_mm_binary_op<FloatComplexNDArray> (a, b, mx_inline_xmin, "min"); } FloatComplexNDArray -max (const FloatComplex& c, const FloatComplexNDArray& m) +max (const FloatComplex& s, const FloatComplexNDArray& m) { - dim_vector dv = m.dims (); - int nel = dv.numel (); - - EMPTY_RETURN_CHECK (FloatComplexNDArray); - - FloatComplexNDArray result (dv); - - for (int i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmax (c, m (i)); - } - - return result; + return do_sm_binary_op<FloatComplexNDArray> (s, m, mx_inline_xmax); } FloatComplexNDArray -max (const FloatComplexNDArray& m, const FloatComplex& c) +max (const FloatComplexNDArray& m, const FloatComplex& s) { - dim_vector dv = m.dims (); - int nel = dv.numel (); - - EMPTY_RETURN_CHECK (FloatComplexNDArray); - - FloatComplexNDArray result (dv); - - for (int i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmax (c, m (i)); - } - - return result; + return do_ms_binary_op<FloatComplexNDArray> (m, s, mx_inline_xmax); } FloatComplexNDArray max (const FloatComplexNDArray& a, const FloatComplexNDArray& b) { - dim_vector dv = a.dims (); - int nel = dv.numel (); - - if (dv != b.dims ()) - { - (*current_liboctave_error_handler) - ("two-arg max expecting args of same size"); - return FloatComplexNDArray (); - } - - EMPTY_RETURN_CHECK (FloatComplexNDArray); - - FloatComplexNDArray result (dv); - - for (int i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmax (a (i), b (i)); - } - - return result; + return do_mm_binary_op<FloatComplexNDArray> (a, b, mx_inline_xmax, "max"); } NDS_CMP_OPS (FloatComplexNDArray, FloatComplex)
--- a/liboctave/fNDArray.cc +++ b/liboctave/fNDArray.cc @@ -939,139 +939,40 @@ return is; } -// FIXME -- it would be nice to share code among the min/max -// functions below. - -#define EMPTY_RETURN_CHECK(T) \ - if (nel == 0) \ - return T (dv); - FloatNDArray -min (float d, const FloatNDArray& m) +min (float s, const FloatNDArray& m) { - dim_vector dv = m.dims (); - octave_idx_type nel = dv.numel (); - - EMPTY_RETURN_CHECK (FloatNDArray); - - FloatNDArray result (dv); - - for (octave_idx_type i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmin (d, m (i)); - } - - return result; + return do_sm_binary_op<FloatNDArray> (s, m, mx_inline_xmin); } FloatNDArray -min (const FloatNDArray& m, float d) +min (const FloatNDArray& m, float s) { - dim_vector dv = m.dims (); - octave_idx_type nel = dv.numel (); - - EMPTY_RETURN_CHECK (FloatNDArray); - - FloatNDArray result (dv); - - for (octave_idx_type i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmin (d, m (i)); - } - - return result; + return do_ms_binary_op<FloatNDArray> (m, s, mx_inline_xmin); } FloatNDArray min (const FloatNDArray& a, const FloatNDArray& b) { - dim_vector dv = a.dims (); - octave_idx_type nel = dv.numel (); - - if (dv != b.dims ()) - { - (*current_liboctave_error_handler) - ("two-arg min expecting args of same size"); - return FloatNDArray (); - } - - EMPTY_RETURN_CHECK (FloatNDArray); - - FloatNDArray result (dv); - - for (octave_idx_type i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmin (a (i), b (i)); - } - - return result; + return do_mm_binary_op<FloatNDArray> (a, b, mx_inline_xmin, "min"); } FloatNDArray -max (float d, const FloatNDArray& m) +max (float s, const FloatNDArray& m) { - dim_vector dv = m.dims (); - octave_idx_type nel = dv.numel (); - - EMPTY_RETURN_CHECK (FloatNDArray); - - FloatNDArray result (dv); - - for (octave_idx_type i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmax (d, m (i)); - } - - return result; + return do_sm_binary_op<FloatNDArray> (s, m, mx_inline_xmax); } FloatNDArray -max (const FloatNDArray& m, float d) +max (const FloatNDArray& m, float s) { - dim_vector dv = m.dims (); - octave_idx_type nel = dv.numel (); - - EMPTY_RETURN_CHECK (FloatNDArray); - - FloatNDArray result (dv); - - for (octave_idx_type i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmax (d, m (i)); - } - - return result; + return do_ms_binary_op<FloatNDArray> (m, s, mx_inline_xmax); } FloatNDArray max (const FloatNDArray& a, const FloatNDArray& b) { - dim_vector dv = a.dims (); - octave_idx_type nel = dv.numel (); - - if (dv != b.dims ()) - { - (*current_liboctave_error_handler) - ("two-arg max expecting args of same size"); - return FloatNDArray (); - } - - EMPTY_RETURN_CHECK (FloatNDArray); - - FloatNDArray result (dv); - - for (octave_idx_type i = 0; i < nel; i++) - { - octave_quit (); - result (i) = xmax (a (i), b (i)); - } - - return result; + return do_mm_binary_op<FloatNDArray> (a, b, mx_inline_xmax, "max"); } NDS_CMP_OPS (FloatNDArray, float)
--- a/liboctave/mx-op-defs.h +++ b/liboctave/mx-op-defs.h @@ -550,70 +550,21 @@ T ## NDArray \ FCN (octave_ ## T d, const T ## NDArray& m) \ { \ - dim_vector dv = m.dims (); \ - octave_idx_type nel = dv.numel (); \ -\ - if (nel == 0) \ - return T ## NDArray (dv); \ -\ - T ## NDArray result (dv); \ -\ - for (octave_idx_type i = 0; i < nel; i++) \ - { \ - octave_quit (); \ - result (i) = d OP m (i) ? d : m(i); \ - } \ -\ - return result; \ + return do_sm_binary_op<T##NDArray, octave_##T, T##NDArray> (d, m, mx_inline_x##FCN); \ } #define NDS_MINMAX_FCN(FCN, OP, T) \ T ## NDArray \ FCN (const T ## NDArray& m, octave_ ## T d) \ { \ - dim_vector dv = m.dims (); \ - octave_idx_type nel = dv.numel (); \ -\ - if (nel == 0) \ - return T ## NDArray (dv); \ -\ - T ## NDArray result (dv); \ -\ - for (octave_idx_type i = 0; i < nel; i++) \ - { \ - octave_quit (); \ - result (i) = m (i) OP d ? m(i) : d; \ - } \ -\ - return result; \ + return do_ms_binary_op<T##NDArray, T##NDArray, octave_##T> (m, d, mx_inline_x##FCN); \ } #define NDND_MINMAX_FCN(FCN, OP, T) \ T ## NDArray \ FCN (const T ## NDArray& a, const T ## NDArray& b) \ { \ - dim_vector dv = a.dims (); \ - octave_idx_type nel = dv.numel (); \ -\ - if (dv != b.dims ()) \ - { \ - (*current_liboctave_error_handler) \ - ("two-arg min expecting args of same size"); \ - return T ## NDArray (); \ - } \ -\ - if (nel == 0) \ - return T ## NDArray (dv); \ -\ - T ## NDArray result (dv); \ -\ - for (octave_idx_type i = 0; i < nel; i++) \ - { \ - octave_quit (); \ - result (i) = a(i) OP b(i) ? a(i) : b(i); \ - } \ -\ - return result; \ + return do_mm_binary_op<T##NDArray, T##NDArray, T##NDArray> (a, b, mx_inline_x##FCN, #FCN); \ } #define MINMAX_FCNS(T) \