# HG changeset patch # User jwe # Date 861413199 0 # Node ID c691fdf6bebb67637b21ef47cd09fea141283b32 # Parent 3241d0057e786bad50f45ebc00e4eabc2d9315e1 [project @ 1997-04-19 01:26:38 by jwe] diff --git a/src/ov-bool-mat.cc b/src/ov-bool-mat.cc new file mode 100644 --- /dev/null +++ b/src/ov-bool-mat.cc @@ -0,0 +1,223 @@ +/* + +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__) +#pragma implementation +#endif + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "lo-ieee.h" +#include "mx-base.h" + +#include "gripes.h" +#include "oct-obj.h" +#include "ops.h" +#include "ov-bool.h" +#include "ov-bool-mat.h" +#include "ov-re-mat.h" +#include "pr-output.h" + +octave_allocator +octave_bool_matrix::allocator (sizeof (octave_bool_matrix)); + +int +octave_bool_matrix::t_id (-1); + +const string +octave_bool_matrix::t_name ("bool matrix"); + +static octave_value * +default_numeric_conversion_function (const octave_value& a) +{ + CAST_CONV_ARG (const octave_bool_matrix&); + + return new octave_matrix (Matrix (v.bool_matrix_value ())); +} + +type_conv_fcn +octave_bool_matrix::numeric_conversion_function (void) const +{ + return default_numeric_conversion_function; +} + +octave_value * +octave_bool_matrix::try_narrowing_conversion (void) +{ + octave_value *retval = 0; + + int nr = matrix.rows (); + int nc = matrix.cols (); + + if (nr == 1 && nc == 1) + retval = new octave_bool (matrix (0, 0)); + + return retval; +} + +octave_value +octave_bool_matrix::index (const octave_value_list& idx) const +{ + octave_value retval; + + int len = idx.length (); + + switch (len) + { + case 2: + { + idx_vector i = idx (0).index_vector (); + idx_vector j = idx (1).index_vector (); + + retval = boolMatrix (matrix.index (i, j)); + } + break; + + case 1: + { + idx_vector i = idx (0).index_vector (); + + retval = boolMatrix (matrix.index (i)); + } + break; + + default: + error ("invalid number of indices (%d) for matrix value", len); + break; + } + + return retval; +} + +extern void assign (Array2&, const Array2&); + +void +octave_bool_matrix::assign (const octave_value_list& idx, + const boolMatrix& rhs) +{ + int len = idx.length (); + + switch (len) + { + case 2: + { + idx_vector i = idx (0).index_vector (); + idx_vector j = idx (1).index_vector (); + + matrix.set_index (i); + matrix.set_index (j); + + ::assign (matrix, rhs); + } + break; + + case 1: + { + idx_vector i = idx (0).index_vector (); + + matrix.set_index (i); + + ::assign (matrix, rhs); + } + break; + + default: + error ("invalid number of indices (%d) for indexed matrix assignment", + len); + break; + } +} + +bool +octave_bool_matrix::valid_as_scalar_index (void) const +{ + // XXX FIXME XXX + return false; +} + +bool +octave_bool_matrix::is_true (void) const +{ + bool retval = false; + + if (rows () == 0 || columns () == 0) + { + int flag = Vpropagate_empty_matrices; + + if (flag < 0) + warning ("empty matrix used in conditional expression"); + else if (flag == 0) + error ("empty matrix used in conditional expression"); + } + else + { + boolMatrix m = (matrix.all ()) . all (); + + retval = (m.rows () == 1 && m.columns () == 1 && m (0, 0)); + } + + return retval; +} + +double +octave_bool_matrix::double_value (bool) const +{ + double retval = octave_NaN; + + if ((rows () == 1 && columns () == 1) + || (Vdo_fortran_indexing && rows () > 0 && columns () > 0)) + retval = matrix (0, 0); + else + gripe_invalid_conversion ("bool matrix", "real scalar"); + + return retval; +} + +Complex +octave_bool_matrix::complex_value (bool) const +{ + Complex retval (octave_NaN, octave_NaN); + + if ((rows () == 1 && columns () == 1) + || (Vdo_fortran_indexing && rows () > 0 && columns () > 0)) + retval = matrix (0, 0); + else + gripe_invalid_conversion ("bool matrix", "complex scalar"); + + return retval; +} + +void +octave_bool_matrix::print (ostream& os, bool pr_as_read_syntax) +{ + Matrix tmp (matrix); + + octave_print_internal (os, tmp, pr_as_read_syntax, struct_indent); +} + +/* +;;; Local Variables: *** +;;; mode: C++ *** +;;; End: *** +*/ diff --git a/src/ov-bool-mat.h b/src/ov-bool-mat.h new file mode 100644 --- /dev/null +++ b/src/ov-bool-mat.h @@ -0,0 +1,155 @@ +/* + +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_bool_matrix_h) +#define octave_bool_matrix_h 1 + +#if defined (__GNUG__) +#pragma interface +#endif + +#include + +#include + +class ostream; + +#include "mx-base.h" +#include "oct-alloc.h" + +#include "error.h" +#include "ov-base.h" +#include "ov-typeinfo.h" + +class Octave_map; +class octave_value_list; + +class tree_walker; + +// Character matrix values. + +class +octave_bool_matrix : public octave_base_value +{ +public: + + octave_bool_matrix (void) + : octave_base_value () { } + + octave_bool_matrix (const boolMatrix& bm) + : octave_base_value (), matrix (bm) { } + + octave_bool_matrix (const octave_bool_matrix& bm) + : octave_base_value (), matrix (bm.matrix) { } + + ~octave_bool_matrix (void) { } + + octave_value *clone (void) { return new octave_bool_matrix (*this); } + + void *operator new (size_t size) + { return allocator.alloc (size); } + + void operator delete (void *p, size_t size) + { allocator.free (p, size); } + + type_conv_fcn numeric_conversion_function (void) const; + + octave_value *try_narrowing_conversion (void); + + octave_value index (const octave_value_list& idx) const; + + void assign (const octave_value_list& idx, const boolMatrix& rhs); + + idx_vector index_vector (void) const { return idx_vector (matrix); } + + int rows (void) const { return matrix.rows (); } + int columns (void) const { return matrix.columns (); } + + bool is_defined (void) const { return true; } + + bool is_bool_matrix (void) const { return true; } + + octave_value all (void) const { return matrix.all (); } + octave_value any (void) const { return matrix.any (); } + + bool is_real_type (void) const { return true; } + + bool is_matrix_type (void) const { return true; } + + bool is_numeric_type (void) const { return true; } + + bool valid_as_scalar_index (void) const; + + bool valid_as_zero_index (void) const { return is_zero_by_zero (); } + + bool is_true (void) const; + + double double_value (bool = false) const; + + Matrix matrix_value (bool = false) const { return matrix; } + + Complex complex_value (bool = false) const; + + ComplexMatrix complex_matrix_value (bool = false) const { return matrix; } + + boolMatrix bool_matrix_value (bool = false) const { return matrix; } + + octave_value convert_to_str (void) const + { return octave_value (matrix); } + + octave_value transpose (void) const + { return octave_value (matrix.transpose ()); } + + octave_value hermitian (void) const + { return octave_value (matrix.transpose ()); } + + void print (ostream& os, bool pr_as_read_syntax = false); + + int type_id (void) const { return t_id; } + + string type_name (void) const { return t_name; } + + static int static_type_id (void) { return t_id; } + + static void register_type (void) + { t_id = octave_value_typeinfo::register_type (t_name); } + +protected: + + boolMatrix matrix; + + static octave_allocator allocator; + + // Type id of bool matrix objects, set by register_type(). + static int t_id; + + // Type name of bool matrix objects, defined in ov-bool-mat.cc. + static const string t_name; +}; + +#endif + +/* +;;; Local Variables: *** +;;; mode: C++ *** +;;; End: *** +*/ diff --git a/src/ov-bool.cc b/src/ov-bool.cc new file mode 100644 --- /dev/null +++ b/src/ov-bool.cc @@ -0,0 +1,123 @@ +/* + +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__) +#pragma implementation +#endif + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "mx-base.h" + +#include "gripes.h" +#include "oct-obj.h" +#include "ops.h" +#include "ov-bool.h" +#include "ov-re-mat.h" +#include "ov-scalar.h" +#include "pr-output.h" + +octave_allocator +octave_bool::allocator (sizeof (octave_bool)); + +int +octave_bool::t_id (-1); + +const string +octave_bool::t_name ("bool"); + +static octave_value * +default_numeric_conversion_function (const octave_value& a) +{ + CAST_CONV_ARG (const octave_bool&); + + return new octave_scalar (v.bool_value ()); +} + +type_conv_fcn +octave_bool::numeric_conversion_function (void) const +{ + return default_numeric_conversion_function; +} + +static inline bool +valid_scalar_indices (const octave_value_list& args) +{ + int nargin = args.length (); + + for (int i = 0; i < nargin; i++) + if (! args(i).valid_as_scalar_index ()) + return false; + + return true; +} + +octave_value +octave_bool::index (const octave_value_list& idx) const +{ + octave_value retval; + + if (valid_scalar_indices (idx)) + retval = scalar; + else + { + // XXX FIXME XXX -- this doesn't solve the problem of + // + // a = 1; a([1,1], [1,1], [1,1]) + // + // and similar constructions. Hmm... + + // XXX FIXME XXX -- using this constructor avoids narrowing the + // 1x1 matrix back to a scalar value. Need a better solution + // to this problem. + + octave_value tmp (new octave_matrix (matrix_value ())); + + retval = tmp.index (idx); + } + + return retval; +} + +octave_value +octave_bool::convert_to_str (void) const +{ + char s[2]; + s[0] = (char) scalar; + s[1] = '\0'; + + return octave_value (s); +} + +void +octave_bool::print (ostream& os, bool pr_as_read_syntax) +{ + octave_print_internal (os, scalar, pr_as_read_syntax); +} + +/* +;;; Local Variables: *** +;;; mode: C++ *** +;;; End: *** +*/ diff --git a/src/ov-bool.h b/src/ov-bool.h new file mode 100644 --- /dev/null +++ b/src/ov-bool.h @@ -0,0 +1,157 @@ +/* + +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_bool_h) +#define octave_bool_h 1 + +#if defined (__GNUG__) +#pragma interface +#endif + +#include + +#include + +class ostream; + +#include "lo-utils.h" +#include "mx-base.h" +#include "oct-alloc.h" +#include "str-vec.h" + +#include "mappers.h" +#include "ov-base.h" +#include "ov-typeinfo.h" + +class Octave_map; +class octave_value_list; + +class tree_walker; + +// Real scalar values. + +class +octave_bool : public octave_base_value +{ +public: + + octave_bool (void) + : octave_base_value (), scalar (false) { } + + octave_bool (bool b) + : octave_base_value (), scalar (b) { } + + octave_bool (const octave_bool& s) + : octave_base_value (), scalar (s.scalar) { } + + ~octave_bool (void) { } + + octave_value *clone (void) { return new octave_bool (*this); } + + void *operator new (size_t size) + { return allocator.alloc (size); } + + void operator delete (void *p, size_t size) + { allocator.free (p, size); } + + type_conv_fcn numeric_conversion_function (void) const; + + octave_value index (const octave_value_list& idx) const; + + idx_vector index_vector (void) const { return idx_vector (scalar); } + + int rows (void) const { return 1; } + int columns (void) const { return 1; } + + bool is_defined (void) const { return true; } + bool is_real_scalar (void) const { return true; } + + octave_value all (void) const { return scalar; } + octave_value any (void) const { return scalar; } + + bool is_real_type (void) const { return true; } + bool is_scalar_type (void) const { return true; } + bool is_numeric_type (void) const { return true; } + + bool valid_as_scalar_index (void) const { return scalar; } + + bool valid_as_zero_index (void) const { return ! scalar; } + + bool is_true (void) const { return scalar; } + + double double_value (bool = false) const { return scalar; } + + Matrix matrix_value (bool = false) const { return Matrix (1, 1, scalar); } + + Complex complex_value (bool = false) const { return scalar; } + + ComplexMatrix complex_matrix_value (bool = false) const + { return ComplexMatrix (1, 1, Complex (scalar)); } + + bool bool_value (void) const { return scalar; } + + boolMatrix bool_matrix_value (void) const + { return boolMatrix (1, 1, scalar); } + + octave_value not (void) const { return octave_value (! scalar); } + + octave_value uminus (void) const { return octave_value (- (double) scalar); } + + octave_value transpose (void) const { return octave_value (scalar); } + + octave_value hermitian (void) const { return octave_value (scalar); } + + octave_value convert_to_str (void) const; + + void print (ostream& os, bool pr_as_read_syntax = false); + + int type_id (void) const { return t_id; } + + string type_name (void) const { return t_name; } + + static int static_type_id (void) { return t_id; } + + static void register_type (void) + { t_id = octave_value_typeinfo::register_type (t_name); } + +private: + + // The value of this scalar. + bool scalar; + + // For custom memory management. + static octave_allocator allocator; + + // Type id of bool objects, set by register_type(). + static int t_id; + + // Type name of bool objects, defined in ov-bool.cc. + static const string t_name; +}; + +#endif + +/* +;;; Local Variables: *** +;;; mode: C++ *** +;;; End: *** +*/