Mercurial > hg > octave-lyh
changeset 3354:87721841efd7
[project @ 1999-11-17 19:06:11 by jwe]
author | jwe |
---|---|
date | Wed, 17 Nov 1999 19:06:15 +0000 |
parents | 6b36317855ff |
children | c4983fc7318f |
files | examples/make_int.cc liboctave/Array2.cc liboctave/Array2.h liboctave/ChangeLog liboctave/dMatrix.cc liboctave/dMatrix.h src/Cell.cc src/Cell.h src/ChangeLog src/TEMPLATE-INST/Array-tc.cc src/data.cc src/ov-cell.cc src/utils.cc src/utils.h |
diffstat | 14 files changed, 282 insertions(+), 198 deletions(-) [+] |
line wrap: on
line diff
--- a/examples/make_int.cc +++ b/examples/make_int.cc @@ -82,6 +82,8 @@ int rows (void) const { return 1; } int columns (void) const { return 1; } + bool is_constant (void) const { return true; } + bool is_defined (void) const { return true; } bool is_real_scalar (void) const { return true; }
--- a/liboctave/Array2.cc +++ b/liboctave/Array2.cc @@ -172,23 +172,6 @@ } template <class T> -bool -Array2<T>::is_symmetric (void) const -{ - if (is_square () && d1 > 0) - { - for (int i = 0; i < d1; i++) - for (int j = i+1; j < d2; j++) - if (elem (i, j) != elem (j, i)) - return false; - - return true; - } - - return false; -} - -template <class T> Array2<T> Array2<T>::transpose (void) const {
--- a/liboctave/Array2.h +++ b/liboctave/Array2.h @@ -175,8 +175,6 @@ bool is_square (void) const { return (d1 == d2); } - bool is_symmetric (void) const; - Array2<T> transpose (void) const; #ifdef HEAVYWEIGHT_INDEXING
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,8 @@ +1999-11-16 John W. Eaton <jwe@bevo.che.wisc.edu> + + * dMatrix.cc (is_symmetric): Move here from Array2.cc. + * Array2.h (is_symmetric): Delete declaration. + 1999-11-10 John W. Eaton <jwe@bevo.che.wisc.edu> * oct-env.cc (do_get_user_name): Reverse sense of test.
--- a/liboctave/dMatrix.cc +++ b/liboctave/dMatrix.cc @@ -167,6 +167,22 @@ return !(*this == a); } +bool +Matrix::is_symmetric (void) const +{ + if (is_square () && rows () > 0) + { + for (int i = 0; i < rows (); i++) + for (int j = i+1; j < cols (); j++) + if (elem (i, j) != elem (j, i)) + return false; + + return true; + } + + return false; +} + Matrix& Matrix::insert (const Matrix& a, int r, int c) {
--- a/liboctave/dMatrix.h +++ b/liboctave/dMatrix.h @@ -73,6 +73,8 @@ bool operator == (const Matrix& a) const; bool operator != (const Matrix& a) const; + bool is_symmetric (void) const; + // destructive insert/delete/reorder operations Matrix& insert (const Matrix& a, int r, int c);
--- a/src/Cell.cc +++ b/src/Cell.cc @@ -33,6 +33,18 @@ octave_allocator Cell::allocator (sizeof (Cell)); +Cell +Cell::index (idx_vector& i) const +{ + return Cell (data.index (i)); +} + +Cell +Cell::index (idx_vector& i, idx_vector& j) const +{ + return Cell (data.index (i, j)); +} + /* ;;; Local Variables: *** ;;; mode: C++ ***
--- a/src/Cell.h +++ b/src/Cell.h @@ -46,6 +46,9 @@ Cell (int n, int m, const octave_value& val = octave_value ()) : data (n, m, val) { } + Cell (const Array2<octave_value>& c) + : data (c) { } + Cell (const Cell& c) : data (c.data) { } @@ -75,6 +78,10 @@ octave_value elem (int i, int j) const { return data.elem (i, j); } + Cell index (idx_vector& i) const; + + Cell index (idx_vector& i, idx_vector& j) const; + private: static octave_allocator allocator;
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,16 @@ +1999-11-16 John W. Eaton <jwe@bevo.che.wisc.edu> + + * ov-cell.cc (Fiscell, Fcell): New Functions. + (octave_cell::do_index_op): New function. + + * utils.cc (get_dimensions): Move here from data.cc and make extern. + (Vtreat_neg_dim_as_zero, treat_neg_dim_as_zero): Likewise. + (symbols_of_utils): New function. + + * data.cc (fill_matrix (const octave_value_list&, double, + const char*)): New function. + (Fones, Fzeros): Use it to avoid some code duplication. + 1999-11-15 John W. Eaton <jwe@bevo.che.wisc.edu> * pt-cell.h, pt-cell.cc, ov-cell.h, ov-cell.cc, Cell.h, Cell.cc:
--- a/src/TEMPLATE-INST/Array-tc.cc +++ b/src/TEMPLATE-INST/Array-tc.cc @@ -29,6 +29,12 @@ #include "Array.h" #include "Array.cc" +#include "Array2.h" +#include "Array2.cc" + +#include "DiagArray2.h" +#include "DiagArray2.cc" + #include "ov.h" extern template class Array<int>;
--- a/src/data.cc +++ b/src/data.cc @@ -49,12 +49,6 @@ #define ABS(x) (((x) < 0) ? (-x) : (x)) #endif -// Should expressions like ones (-1, 5) result in an empty matrix or -// an error? A positive value means yes. A negative value means -// yes, but print a warning message. Zero means it should be -// considered an error. -static int Vtreat_neg_dim_as_zero; - DEFUN (all, args, , "all (X): are all elements of X nonzero?") { @@ -889,131 +883,10 @@ return retval; } -static void -check_dimensions (int& nr, int& nc, const char *warnfor) -{ - if (nr < 0 || nc < 0) - { - if (Vtreat_neg_dim_as_zero) - { - nr = (nr < 0) ? 0 : nr; - nc = (nc < 0) ? 0 : nc; - - if (Vtreat_neg_dim_as_zero < 0) - warning ("%s: converting negative dimension to zero", - warnfor); - } - else - error ("%s: can't create a matrix with negative dimensions", - warnfor); - } -} - -static void -get_dimensions (const octave_value& a, const char *warn_for, - int& nr, int& nc) -{ - if (a.is_scalar_type ()) - { - nr = nc = a.nint_value (); - } - else - { - nr = a.rows (); - nc = a.columns (); - - if ((nr == 1 && nc == 2) || (nr == 2 && nc == 1)) - { - ColumnVector v = a.vector_value (); - - if (error_state) - return; - - nr = NINT (v (0)); - nc = NINT (v (1)); - } - else - warning ("%s (A): use %s (size (A)) instead", warn_for, warn_for); - } - - check_dimensions (nr, nc, warn_for); // May set error_state. -} - -static void -get_dimensions (const octave_value& a, const octave_value& b, - const char *warn_for, int& nr, int& nc) +static octave_value +fill_matrix (const octave_value_list& args, double val, const char *fcn) { - nr = a.is_empty () ? 0 : a.nint_value (); - nc = b.is_empty () ? 0 : b.nint_value (); - - if (error_state) - error ("%s: expecting two scalar arguments", warn_for); - else - check_dimensions (nr, nc, warn_for); // May set error_state. -} - -static octave_value -fill_matrix (const octave_value& a, double val, const char *warn_for) -{ - int nr, nc; - get_dimensions (a, warn_for, nr, nc); - - if (error_state) - return octave_value (); - - Matrix m (nr, nc, val); - - return m; -} - -static octave_value -fill_matrix (const octave_value& a, const octave_value& b, - double val, const char *warn_for) -{ - int nr, nc; - get_dimensions (a, b, warn_for, nr, nc); // May set error_state. - - if (error_state) - return octave_value (); - - Matrix m (nr, nc, val); - - return m; -} - -DEFUN (ones, args, , - "ones (N), ones (N, M), ones (X): create a matrix of all ones") -{ - octave_value_list retval; - - int nargin = args.length (); - - switch (nargin) - { - case 0: - retval = 1.0; - break; - - case 1: - retval = fill_matrix (args(0), 1.0, "ones"); - break; - - case 2: - retval = fill_matrix (args(0), args(1), 1.0, "ones"); - break; - - default: - print_usage ("ones"); - break; - } - - return retval; -} - -DEFUN (zeros, args, , - "zeros (N), zeros (N, M), zeros (X): create a matrix of all zeros") -{ - octave_value_list retval; + octave_value retval; int nargin = args.length (); @@ -1024,51 +897,48 @@ break; case 1: - retval = fill_matrix (args(0), 0.0, "zeros"); + { + int nr, nc; + get_dimensions (args(0), fcn, nr, nc); + + if (! error_state) + retval = Matrix (nr, nc, val); + } break; case 2: - retval = fill_matrix (args(0), args(1), 0.0, "zeros"); + { + int nr, nc; + get_dimensions (args(0), args(1), fcn, nr, nc); + + if (! error_state) + retval = Matrix (nr, nc, val); + } break; default: - print_usage ("zeros"); + print_usage (fcn); break; } return retval; } -static octave_value -identity_matrix (const octave_value& a) +DEFUN (ones, args, , + "ones (N), ones (N, M), ones (X): create a matrix of all ones") { - int nr, nc; - get_dimensions (a, "eye", nr, nc); // May set error_state. - - if (error_state) - return octave_value (); - - Matrix m (nr, nc, 0.0); - - if (nr > 0 && nc > 0) - { - int n = MIN (nr, nc); - for (int i = 0; i < n; i++) - m (i, i) = 1.0; - } - - return m; + return fill_matrix (args, 1.0, "ones"); } -static octave_value -identity_matrix (const octave_value& a, const octave_value& b) +DEFUN (zeros, args, , + "zeros (N), zeros (N, M), zeros (X): create a matrix of all zeros") { - int nr, nc; - get_dimensions (a, b, "eye", nr, nc); // May set error_state. + return fill_matrix (args, 0.0, "zeros"); +} - if (error_state) - return octave_value (); - +static Matrix +identity_matrix (int nr, int nc) +{ Matrix m (nr, nc, 0.0); if (nr > 0 && nc > 0) @@ -1084,7 +954,7 @@ DEFUN (eye, args, , "eye (N), eye (N, M), eye (X): create an identity matrix") { - octave_value_list retval; + octave_value retval; int nargin = args.length (); @@ -1095,11 +965,23 @@ break; case 1: - retval = identity_matrix (args(0)); + { + int nr, nc; + get_dimensions (args(0), "eye", nr, nc); + + if (! error_state) + retval = identity_matrix (nr, nc); + } break; case 2: - retval = identity_matrix (args(0), args(1)); + { + int nr, nc; + get_dimensions (args(0), args(1), "eye", nr, nc); + + if (! error_state) + retval = identity_matrix (nr, nc); + } break; default: @@ -1173,14 +1055,6 @@ return retval; } -static int -treat_neg_dim_as_zero (void) -{ - Vtreat_neg_dim_as_zero = check_preference ("treat_neg_dim_as_zero"); - - return 0; -} - void symbols_of_data (void) { @@ -1351,11 +1225,9 @@ @end iftex\n\ @end defvr"); - DEFVAR (treat_neg_dim_as_zero, 0.0, treat_neg_dim_as_zero, - "convert negative dimensions to zero"); - DEFCONST (true, true, "logical true value"); + } /*
--- a/src/ov-cell.cc +++ b/src/ov-cell.cc @@ -36,7 +36,9 @@ #include "defun.h" #include "error.h" #include "ov-cell.h" +#include "oct-obj.h" #include "unwind-prot.h" +#include "utils.h" DEFINE_OCTAVE_ALLOCATOR (octave_cell); @@ -47,16 +49,36 @@ { octave_value retval; -#if 0 - if (idx.length () == 1) + int len = idx.length (); + + switch (len) { - idx_vector i = idx (0).index_vector (); + case 2: + { + idx_vector i = idx (0).index_vector (); + idx_vector j = idx (1).index_vector (); + + retval = cell_val.index (i, j); + } + break; - retval = octave_value_list (lst.index (i)); + case 1: + { + idx_vector i = idx (0).index_vector (); + + retval = cell_val.index (i); + } + break; + + default: + { + string n = type_name (); + + error ("invalid number of indices (%d) for %s value", + len, n.c_str ()); + } + break; } - else - error ("lists may only be indexed by a single scalar"); -#endif return retval; } @@ -152,6 +174,58 @@ return false; } +DEFUN (iscell, args, , + "iscell (x): return nonzero if x is a cell array") +{ + octave_value retval; + + if (args.length () == 1) + retval = args(0).is_cell (); + else + print_usage ("iscell"); + + return retval; +} + +DEFUN (cell, args, , + "cell (N)\n\ +cell (M, N)\n\ +cell (size (A))") +{ + octave_value retval; + + int nargin = args.length (); + + switch (nargin) + { + case 1: + { + int nr, nc; + get_dimensions (args(0), "cell", nr, nc); + + if (! error_state) + retval = Cell (nr, nc, Matrix ()); + } + break; + + case 2: + { + int nr, nc; + get_dimensions (args(0), args(1), "cell", nr, nc); + + if (! error_state) + retval = Cell (nr, nc, Matrix ()); + } + break; + + default: + print_usage ("cell"); + break; + } + + return retval; +} + /* ;;; Local Variables: *** ;;; mode: C++ ***
--- a/src/utils.cc +++ b/src/utils.cc @@ -76,6 +76,12 @@ #include "utils.h" #include "variables.h" +// Should expressions like ones (-1, 5) result in an empty matrix or +// an error? A positive value means yes. A negative value means +// yes, but print a warning message. Zero means it should be +// considered an error. +static int Vtreat_neg_dim_as_zero; + // Top level context (?) extern jmp_buf toplevel; @@ -574,6 +580,85 @@ return pref; } +static void +check_dimensions (int& nr, int& nc, const char *warnfor) +{ + if (nr < 0 || nc < 0) + { + if (Vtreat_neg_dim_as_zero) + { + nr = (nr < 0) ? 0 : nr; + nc = (nc < 0) ? 0 : nc; + + if (Vtreat_neg_dim_as_zero < 0) + warning ("%s: converting negative dimension to zero", + warnfor); + } + else + error ("%s: can't create a matrix with negative dimensions", + warnfor); + } +} + +void +get_dimensions (const octave_value& a, const char *warn_for, + int& nr, int& nc) +{ + if (a.is_scalar_type ()) + { + nr = nc = a.nint_value (); + } + else + { + nr = a.rows (); + nc = a.columns (); + + if ((nr == 1 && nc == 2) || (nr == 2 && nc == 1)) + { + ColumnVector v = a.vector_value (); + + if (error_state) + return; + + nr = NINT (v (0)); + nc = NINT (v (1)); + } + else + warning ("%s (A): use %s (size (A)) instead", warn_for, warn_for); + } + + check_dimensions (nr, nc, warn_for); // May set error_state. +} + +void +get_dimensions (const octave_value& a, const octave_value& b, + const char *warn_for, int& nr, int& nc) +{ + nr = a.is_empty () ? 0 : a.nint_value (); + nc = b.is_empty () ? 0 : b.nint_value (); + + if (error_state) + error ("%s: expecting two scalar arguments", warn_for); + else + check_dimensions (nr, nc, warn_for); // May set error_state. +} + +static int +treat_neg_dim_as_zero (void) +{ + Vtreat_neg_dim_as_zero = check_preference ("treat_neg_dim_as_zero"); + + return 0; +} + +void +symbols_of_utils (void) +{ + DEFVAR (treat_neg_dim_as_zero, 0.0, treat_neg_dim_as_zero, + "convert negative dimensions to zero"); + +} + /* ;;; Local Variables: *** ;;; mode: C++ ***
--- a/src/utils.h +++ b/src/utils.h @@ -30,6 +30,7 @@ #include "lo-utils.h" +class octave_value; class octave_value_list; class string_vector; @@ -58,6 +59,14 @@ extern int check_preference (const string& var); +extern void +get_dimensions (const octave_value& a, const octave_value& b, + const char *warn_for, int& nr, int& nc); + +extern void +get_dimensions (const octave_value& a, + const char *warn_for, int& nr, int& nc); + extern "C" void octave_usleep (unsigned int useconds); extern "C" int octave_strcasecmp (const char *s1, const char *s2);