# HG changeset patch # User jwe # Date 1063242404 0 # Node ID 2bc437206787eb67f9e99571e91c703a4c8e04f3 # Parent 65f47f8a92a2e07466a047ccf532571b614c4c70 [project @ 2003-09-11 01:06:43 by jwe] diff --git a/liboctave/MArrayN.cc b/liboctave/MArrayN.cc new file mode 100644 --- /dev/null +++ b/liboctave/MArrayN.cc @@ -0,0 +1,163 @@ +/* + +Copyright (C) 1996, 1997 John W. Eaton + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) any +later version. + +Octave is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, write to the Free +Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) +#pragma implementation +#endif + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "MArrayN.h" +#include "ArrayN-idx.h" +#include "lo-error.h" + +#include "MArray-defs.h" + +// Two dimensional array with math ops. + +// Element by element MArrayN by scalar ops. + + +#if 0 +template +MArrayN& +operator += (MArrayN& a, const T& s) +{ + DO_VS_OP2 (+=) + return a; +} + +template +MArrayN& +operator -= (MArrayN& a, const T& s) +{ + DO_VS_OP2 (-=) + return a; +} + + +#define MARRAYN_NDS_OP(OP) \ + template \ + MArrayN \ + operator OP (const MArrayN& a, const T& s) \ + { \ + MArrayN result (a.dims ()); \ + T *r = result.fortran_vec (); \ + int l = a.length (); \ + const T *v = a.data (); \ + DO_VS_OP (r, l, v, OP, s); \ + return result; \ + } + +MARRAYN_NDS_OP (+) +MARRAYN_NDS_OP (-) +MARRAYN_NDS_OP (*) +MARRAYN_NDS_OP (/) + +// Element by element MArrayN by scalar ops. + +#define MARRAYN_SND_OP(OP) \ + template \ + MArrayN \ + operator OP (const T& s, const MArrayN& a) \ + { \ + MArrayN result (a.dims ()); \ + T *r = result.fortran_vec (); \ + int l = a.length (); \ + const T *v = a.data (); \ + DO_SV_OP (r, l, s, OP, v); \ + return result; \ + } + +MARRAYN_SND_OP (+) +MARRAYN_SND_OP (-) +MARRAYN_SND_OP (*) +MARRAYN_SND_OP (/) + +#define MARRAY_NDND_OP(FCN, OP) \ +template \ +MArrayN \ +FCN (const MArrayN& a, const MArrayN& b) \ +{ \ +Array a_dims = a.dims (); \ +Array b_dims = b.dims (); \ +int dims_ok = 1; \ +int any_dims_zero = 0; \ +if (a_dims.length () != b_dims.length ()) \ + dims_ok = 0; \ + else \ + { \ + for (int i = 0; i < a_dims.length (); i++) \ + { \ + if (a_dims (i) != b_dims (i)) \ + { dims_ok = 0; break; } \ + if (a_dims (i) == 0) \ + any_dims_zero = 1; \ + } \ + } \ + if (!dims_ok) \ + { \ + gripe_nonconformant (#FCN, a_dims, b_dims); \ + return MArrayN (); \ + } \ + if (any_dims_zero) \ + return MArrayN (a_dims); \ + int l = a.length (); \ + MArrayN result (a_dims); \ + T* r = result.fortran_vec (); \ + const T *x = a.data (); \ + const T *y = b.data (); \ + DO_VV_OP (r, l, x, OP, y); \ + return result; \ +} + +MARRAY_NDND_OP (operator +, +) +MARRAY_NDND_OP (operator -, -) + +template +MArrayN +operator + (const MArrayN& a) +{ + return a; +} + +template +MArrayN +operator - (const MArrayN& a) +{ + int l = a.length (); + MArrayN result (a.dims ()); + T *r = result.fortran_vec (); + const T *x = a.data (); + NEG_V (r, l, x); + return result; +} + +#endif + +/* +;;; Local Variables: *** +;;; mode: C++ *** +;;; End: *** +*/ diff --git a/liboctave/MArrayN.h b/liboctave/MArrayN.h new file mode 100644 --- /dev/null +++ b/liboctave/MArrayN.h @@ -0,0 +1,82 @@ +// Template array classes with like-type math ops +/* + +Copyright (C) 1996, 1997 John W. Eaton + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) any +later version. + +Octave is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, write to the Free +Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) +#pragma interface +#endif + +#if !defined (octave_MArrayN_h) +#define octave_MArrayN_h 1 + +#include "ArrayN.h" +// Two dimensional array with math ops. + +// But first, some preprocessor abuse... + +#include "MArray-defs.h" + +class Matrix; + +MARRAY_OPS_FORWARD_DECLS (MArrayN) + +template +class +MArrayN : public ArrayN +{ + protected: + + MArrayN (T *d, const Array& dims) : ArrayN (d, dims) + { } + + public: + + MArrayN (void) : ArrayN () {} + + MArrayN (const Array& dims) : ArrayN (dims) + { } + + MArrayN (const Array& dims, const T& val) + : ArrayN (dims, val) { } + + MArrayN (const ArrayN& a) : ArrayN (a) { } + + //MArrayN (const Array& a) : ArrayN (a) { } + + MArrayN (const MArrayN& a) : ArrayN (a) { } + + MArrayN (const Matrix& m) : ArrayN (m) { } + + ~MArrayN (void) { } + + MArrayN& operator = (const MArrayN& a) + { + ArrayN::operator = (a); + return *this; + } + +}; + +extern void +gripe_nonconformant (const char *op, Array& op1_dims, Array& op2_dims); + +#endif diff --git a/liboctave/NDArray.cc b/liboctave/NDArray.cc new file mode 100644 --- /dev/null +++ b/liboctave/NDArray.cc @@ -0,0 +1,84 @@ +//N-D Array manipulations. +/* + +Copyright (C) 1996, 1997 John W. Eaton + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) any +later version. + +Octave is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, write to the Free +Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) +#pragma implementation +#endif + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "NDArray.h" +#include "mx-base.h" +#include "lo-ieee.h" + +bool +NDArray::any_element_is_negative (bool neg_zero) const +{ + int n = length (); + if (neg_zero) + { + for (int i = 0; i < n; i++) + if (lo_ieee_signbit (Array::elem (i))) + return true; + } + else + { + for (int i = 0; i < n; i++) + if (Array::elem (i) < 0) + return true; + } + + return false; +} + +bool +NDArray::all_integers (double& max_val, double& min_val) const +{ + int n = length (); + + if (n > 0) + { + max_val = Array::elem (0); + min_val = Array::elem (0); + } + else + return false; + + for (int i = 0; i < n; i++) + { + double val = Array::elem (0); + + if (val > max_val) + max_val = val; + + if (val < min_val) + min_val = val; + + if (D_NINT (val) != val) + return false; + } + + return true; +} diff --git a/liboctave/NDArray.h b/liboctave/NDArray.h new file mode 100644 --- /dev/null +++ b/liboctave/NDArray.h @@ -0,0 +1,82 @@ +/* + +Copyright (C) 1996, 1997 John W. Eaton + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) any +later version. + +Octave is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, write to the Free +Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#if !defined (octave_NDArray_int_h) +#define octave_NDArray_int_h 1 + +#if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) +#pragma interface +#endif + +#include "MArrayN.h" +//#include "mx-base.h" + +#include "mx-defs.h" +#include "mx-op-defs.h" + +#include "data-conv.h" +#include "mach-info.h" + +class NDArray : public MArrayN +{ + public: + + NDArray (void) : MArrayN () { } + + NDArray (Array& dims) : MArrayN (dims) { } + + NDArray (Array& dims, double val) : MArrayN (dims, val) { } + + NDArray (const NDArray& a): MArrayN (a) { } + + NDArray (const MArrayN& a) : MArrayN (a) { } + + NDArray (const Matrix& m) : MArrayN (m) { } + + NDArray (const ArrayN& a) : MArrayN (a) { } + + //NDArray (const Array& a) : MArrayN (a) { } + + NDArray& operator = (const NDArray& a) + { + MArrayN::operator = (a); + return *this; + } + + // i/o + + // friend std::ostream& operator << (std::ostream& os, const NDArray& a); + // friend std::istream& operator >> (std::istream& is, NDArray& a); + + static double resize_fill_value (void) { return 0; } + + bool any_element_is_negative (bool = false) const; + bool all_integers (double& max_val, double& min_val) const; + + private: + + NDArray (double *d, Array& dims) : MArrayN (d, dims) { } +}; + +MARRAY_FORWARD_DEFS (MArrayN, NDArray, double) + +#endif diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,29 @@ +2003-09-10 John W. Eaton + + * OPERATORS/op-m-cs.cc (complex_matrix_conv): Delete function. + (install_m_cs_ops): Don't install complex_matrix_conv here. + + * OPERATORS/op-s-cs.cc (complex_matrix_conv): Delete function. + (install_s_cs_ops): Don't install complex_matrix_conv here. + + * OPERATORS/op-s-s.cc (matrix_conv): Delete function. + (install_s_s_ops): Don't install matrix_conv here. + + * OPERATORS/op-cs-s.cc (complex_matrix_conv): Delete function. + (install_cs_s_ops): Don't install complex_matrix_conv here. + + * OPERATORS/op-cs-m.cc (complex_matrix_conv): Delete function. + (install_cs_m_ops): Don't install complex_matrix_conv here. + + * OPERATORS/op-cs-cs.cc (complex_matrix_conv): Delete function. + (install_cs_cs_ops): Don't install complex_matrix_conv here. + + * ov-typeinfo.cc (do_register_unary_op, + do_register_non_const_unary_op, do_register_binary_op, + do_register_assign_op, do_register_assignany_op, + do_register_pref_assign_conv, do_register_widening_op): + Print warning if installing a duplicate function. + 2003-09-10 Petter Risholm * data.cc, ov-base.cc, ov-base.h, ov.h, ov.cc, ov-re-mat.h, diff --git a/src/OPERATORS/op-cs-cs.cc b/src/OPERATORS/op-cs-cs.cc --- a/src/OPERATORS/op-cs-cs.cc +++ b/src/OPERATORS/op-cs-cs.cc @@ -176,13 +176,6 @@ return v1.complex_value () != 0.0 || v2.complex_value () != 0.0; } -DEFCONV (complex_matrix_conv, complex, complex_matrix) -{ - CAST_CONV_ARG (const octave_complex&); - - return new octave_complex_matrix (v.complex_matrix_value ()); -} - void install_cs_cs_ops (void) { @@ -214,8 +207,6 @@ INSTALL_BINOP (op_el_or, octave_complex, octave_complex, el_or); INSTALL_ASSIGNCONV (octave_complex, octave_complex, octave_complex_matrix); - - INSTALL_WIDENOP (octave_complex, octave_complex_matrix, complex_matrix_conv); } /* diff --git a/src/OPERATORS/op-cs-m.cc b/src/OPERATORS/op-cs-m.cc --- a/src/OPERATORS/op-cs-m.cc +++ b/src/OPERATORS/op-cs-m.cc @@ -98,13 +98,6 @@ DEFBINOP_FN (el_and, complex, matrix, mx_el_and) DEFBINOP_FN (el_or, complex, matrix, mx_el_or) -DEFCONV (complex_matrix_conv, complex, complex_matrix) -{ - CAST_CONV_ARG (const octave_complex&); - - return new octave_complex_matrix (v.complex_matrix_value ()); -} - void install_cs_m_ops (void) { @@ -128,8 +121,6 @@ INSTALL_BINOP (op_el_or, octave_complex, octave_matrix, el_or); INSTALL_ASSIGNCONV (octave_complex, octave_matrix, octave_complex_matrix); - - INSTALL_WIDENOP (octave_complex, octave_complex_matrix, complex_matrix_conv); } /* diff --git a/src/OPERATORS/op-cs-s.cc b/src/OPERATORS/op-cs-s.cc --- a/src/OPERATORS/op-cs-s.cc +++ b/src/OPERATORS/op-cs-s.cc @@ -155,13 +155,6 @@ return v1.complex_value () != 0.0 || v2.double_value (); } -DEFCONV (complex_matrix_conv, complex, complex_matrix) -{ - CAST_CONV_ARG (const octave_complex&); - - return new octave_complex_matrix (v.complex_matrix_value ()); -} - void install_cs_s_ops (void) { @@ -185,8 +178,6 @@ INSTALL_BINOP (op_el_or, octave_complex, octave_scalar, el_or); INSTALL_ASSIGNCONV (octave_complex, octave_scalar, octave_complex_matrix); - - INSTALL_WIDENOP (octave_complex, octave_complex_matrix, complex_matrix_conv); } /* diff --git a/src/OPERATORS/op-m-cs.cc b/src/OPERATORS/op-m-cs.cc --- a/src/OPERATORS/op-m-cs.cc +++ b/src/OPERATORS/op-m-cs.cc @@ -105,13 +105,6 @@ DEFBINOP_FN (el_and, matrix, complex, mx_el_and) DEFBINOP_FN (el_or, matrix, complex, mx_el_or) -DEFCONV (complex_matrix_conv, matrix, complex_matrix) -{ - CAST_CONV_ARG (const octave_matrix&); - - return new octave_complex_matrix (ComplexMatrix (v.matrix_value ())); -} - void install_m_cs_ops (void) { @@ -135,8 +128,6 @@ INSTALL_BINOP (op_el_or, octave_matrix, octave_complex, el_or); INSTALL_ASSIGNCONV (octave_matrix, octave_complex, octave_complex_matrix); - - INSTALL_WIDENOP (octave_matrix, octave_complex_matrix, complex_matrix_conv); } /* diff --git a/src/OPERATORS/op-s-cs.cc b/src/OPERATORS/op-s-cs.cc --- a/src/OPERATORS/op-s-cs.cc +++ b/src/OPERATORS/op-s-cs.cc @@ -155,13 +155,6 @@ return octave_value (v1.double_value () || (v2.complex_value () != 0.0)); } -DEFCONV (complex_matrix_conv, scalar, complex_matrix) -{ - CAST_CONV_ARG (const octave_scalar&); - - return new octave_complex_matrix (v.complex_matrix_value ()); -} - void install_s_cs_ops (void) { @@ -185,8 +178,6 @@ INSTALL_BINOP (op_el_or, octave_scalar, octave_complex, el_or); INSTALL_ASSIGNCONV (octave_scalar, octave_complex, octave_complex_matrix); - - INSTALL_WIDENOP (octave_scalar, octave_complex_matrix, complex_matrix_conv); } /* diff --git a/src/OPERATORS/op-s-s.cc b/src/OPERATORS/op-s-s.cc --- a/src/OPERATORS/op-s-s.cc +++ b/src/OPERATORS/op-s-s.cc @@ -118,13 +118,6 @@ DEFBINOP_OP (el_and, scalar, scalar, &&) DEFBINOP_OP (el_or, scalar, scalar, ||) -DEFCONV (matrix_conv, scalar, matrix) -{ - CAST_CONV_ARG (const octave_scalar&); - - return new octave_matrix (v.matrix_value ()); -} - void install_s_s_ops (void) { @@ -156,8 +149,6 @@ INSTALL_BINOP (op_el_or, octave_scalar, octave_scalar, el_or); INSTALL_ASSIGNCONV (octave_scalar, octave_scalar, octave_matrix); - - INSTALL_WIDENOP (octave_scalar, octave_matrix, matrix_conv); } /* diff --git a/src/ov-typeinfo.cc b/src/ov-typeinfo.cc --- a/src/ov-typeinfo.cc +++ b/src/ov-typeinfo.cc @@ -193,6 +193,15 @@ octave_value_typeinfo::do_register_unary_op (octave_value::unary_op op, int t, unary_op_fcn f) { + if (lookup_unary_op (op, t)) + { + std::string op_name = octave_value::unary_op_as_string (op); + std::string type_name = types(t); + + warning ("duplicate unary operator `%s' for type `%s'", + op_name.c_str (), type_name.c_str ()); + } + unary_ops.checkelem (static_cast (op), t) = f; return false; @@ -202,6 +211,15 @@ octave_value_typeinfo::do_register_non_const_unary_op (octave_value::unary_op op, int t, non_const_unary_op_fcn f) { + if (lookup_non_const_unary_op (op, t)) + { + std::string op_name = octave_value::unary_op_as_string (op); + std::string type_name = types(t); + + warning ("duplicate unary operator `%s' for type `%s'", + op_name.c_str (), type_name.c_str ()); + } + non_const_unary_ops.checkelem (static_cast (op), t) = f; return false; @@ -212,6 +230,16 @@ int t1, int t2, binary_op_fcn f) { + if (lookup_binary_op (op, t1, t2)) + { + std::string op_name = octave_value::binary_op_as_string (op); + std::string t1_name = types(t1); + std::string t2_name = types(t2); + + warning ("duplicate binary operator `%s' for types `%s' and `%s'", + op_name.c_str (), t1_name.c_str (), t1_name.c_str ()); + } + binary_ops.checkelem (static_cast (op), t1, t2) = f; return false; @@ -222,6 +250,16 @@ int t_lhs, int t_rhs, assign_op_fcn f) { + if (lookup_assign_op (op, t_lhs, t_rhs)) + { + std::string op_name = octave_value::assign_op_as_string (op); + std::string t_lhs_name = types(t_lhs); + std::string t_rhs_name = types(t_rhs); + + warning ("duplicate assignment operator `%s' for types `%s' and `%s'", + op_name.c_str (), t_lhs_name.c_str (), t_rhs_name.c_str ()); + } + assign_ops.checkelem (static_cast (op), t_lhs, t_rhs) = f; return false; @@ -229,8 +267,17 @@ bool octave_value_typeinfo::do_register_assignany_op (octave_value::assign_op op, - int t_lhs, assign_op_fcn f) + int t_lhs, assign_op_fcn f) { + if (lookup_assignany_op (op, t_lhs)) + { + std::string op_name = octave_value::assign_op_as_string (op); + std::string t_lhs_name = types(t_lhs); + + warning ("duplicate assignment operator `%s' for types `%s'", + op_name.c_str (), t_lhs_name.c_str ()); + } + assignany_ops.checkelem (static_cast (op), t_lhs) = f; return false; @@ -240,6 +287,15 @@ octave_value_typeinfo::do_register_pref_assign_conv (int t_lhs, int t_rhs, int t_result) { + if (lookup_pref_assign_conv (t_lhs, t_rhs) >= 0) + { + std::string t_lhs_name = types(t_lhs); + std::string t_rhs_name = types(t_rhs); + + warning ("overriding assignment conversion for types `%s' and `%s'", + t_lhs_name.c_str (), t_rhs_name.c_str ()); + } + pref_assign_conv.checkelem (t_lhs, t_rhs) = t_result; return false; @@ -249,6 +305,15 @@ octave_value_typeinfo::do_register_widening_op (int t, int t_result, type_conv_fcn f) { + if (lookup_widening_op (t, t_result)) + { + std::string t_name = types(t); + std::string t_result_name = types(t_result); + + warning ("overriding widening op for `%s' to `%s'", + t_name.c_str (), t_result_name.c_str ()); + } + widening_ops.checkelem (t, t_result) = f; return false;