# HG changeset patch # User jwe # Date 1076911011 0 # Node ID e941e1470d7b0ab40c6e046e335917a90affbcc1 # Parent 86c748d5f0af9aac435d84c5029f8578df4bff7d [project @ 2004-02-16 05:56:50 by jwe] diff -r 86c748d5f0af -r e941e1470d7b liboctave/Array.cc --- a/liboctave/Array.cc Mon Feb 16 05:15:50 2004 +0000 +++ b/liboctave/Array.cc Mon Feb 16 05:56:51 2004 +0000 @@ -931,18 +931,47 @@ Array& Array::insert (const Array& a, int r, int c) { - int a_rows = a.rows (); - int a_cols = a.cols (); - - if (r < 0 || r + a_rows > rows () || c < 0 || c + a_cols > cols ()) + dim_vector a_dv = a.dims (); + + int n = a_dv.length (); + + if (n == dimensions.length ()) { - (*current_liboctave_error_handler) ("range error for insert"); - return *this; + Array a_ra_idx (a_dv.length (), 0); + + a_ra_idx.elem (0) = r; + a_ra_idx.elem (1) = c; + + for (int i = 0; i < n; i++) + { + if (a_ra_idx (i) < 0 || (a_ra_idx (i) + a_dv (i)) > dimensions (i)) + { + (*current_liboctave_error_handler) + ("Array::insert: range error for insert"); + return *this; + } + } + + a_ra_idx.elem (0) = 0; + a_ra_idx.elem (1) = 0; + + int n_elt = a.numel (); + + for (int i = 0; i < n_elt; i++) + { + Array ra_idx = a_ra_idx; + + ra_idx.elem (0) = a_ra_idx (0) + r; + ra_idx.elem (1) = a_ra_idx (1) + c; + + elem (ra_idx) = a.elem (a_ra_idx); + + increment_index (a_ra_idx, a_dv); + } } - - for (int j = 0; j < a_cols; j++) - for (int i = 0; i < a_rows; i++) - elem (r+i, c+j) = a.elem (i, j); + else + (*current_liboctave_error_handler) + ("Array::insert: invalid indexing operation"); return *this; } @@ -967,6 +996,7 @@ } } + #if 0 // XXX FIXME XXX -- need to copy elements @@ -2999,29 +3029,29 @@ } template -bool +bool cat_ra (Array& ra_cat, const Array& ra_arg, int dim, int add_dim) { bool retval = false; - + dim_vector dv = ra_arg.dims (); - + Array ra_idx (dv.length (), 0); - + for (int i = 0; i < ra_arg.length (); i++) { if (i != 0) increment_index (ra_idx, dv); - + Array ra_idx2 = ra_idx; - + if (dim >= ra_idx2.length ()) { ra_idx2.resize_and_fill (dim + 1, 0); - + retval = true; } - + ra_idx2(dim) = ra_idx2(dim) + add_dim; ra_cat(ra_idx2) = ra_arg(ra_idx); diff -r 86c748d5f0af -r e941e1470d7b liboctave/ArrayN.h --- a/liboctave/ArrayN.h Mon Feb 16 05:15:50 2004 +0000 +++ b/liboctave/ArrayN.h Mon Feb 16 05:56:51 2004 +0000 @@ -96,6 +96,12 @@ return *this; } + ArrayN& insert (const ArrayN& a, int r, int c) + { + Array::insert (a, r, c); + return *this; + } + ArrayN index (idx_vector& i, int resize_ok = 0, const T& rfv = resize_fill_value (T ())) const { diff -r 86c748d5f0af -r e941e1470d7b liboctave/CNDArray.cc --- a/liboctave/CNDArray.cc Mon Feb 16 05:15:50 2004 +0000 +++ b/liboctave/CNDArray.cc Mon Feb 16 05:56:51 2004 +0000 @@ -250,6 +250,63 @@ return retval; } +ComplexNDArray& +ComplexNDArray::insert (const NDArray& a, int r, int c) +{ + dim_vector a_dv = a.dims (); + + int n = a_dv.length (); + + if (n == dimensions.length ()) + { + Array a_ra_idx (a_dv.length (), 0); + + a_ra_idx.elem (0) = r; + a_ra_idx.elem (1) = c; + + for (int i = 0; i < n; i++) + { + if (a_ra_idx (i) < 0 || (a_ra_idx (i) + a_dv (i)) > dimensions (i)) + { + (*current_liboctave_error_handler) + ("Array::insert: range error for insert"); + return *this; + } + } + + a_ra_idx.elem (0) = 0; + a_ra_idx.elem (1) = 0; + + int n_elt = a.numel (); + + // IS make_unique () NECCESSARY HERE?? + + for (int i = 0; i < n_elt; i++) + { + Array ra_idx = a_ra_idx; + + ra_idx.elem (0) = a_ra_idx (0) + r; + ra_idx.elem (1) = a_ra_idx (1) + c; + + elem (ra_idx) = a.elem (a_ra_idx); + + increment_index (a_ra_idx, a_dv); + } + } + else + (*current_liboctave_error_handler) + ("Array::insert: invalid indexing operation"); + + return *this; +} + +ComplexNDArray& +ComplexNDArray::insert (const ComplexNDArray& a, int r, int c) +{ + Array::insert (a, r, c); + return *this; +} + ComplexMatrix ComplexNDArray::matrix_value (void) const { diff -r 86c748d5f0af -r e941e1470d7b liboctave/CNDArray.h --- a/liboctave/CNDArray.h Mon Feb 16 05:15:50 2004 +0000 +++ b/liboctave/CNDArray.h Mon Feb 16 05:56:51 2004 +0000 @@ -89,6 +89,9 @@ ComplexNDArray sumsq (int dim = -1) const; bool cat (const ComplexNDArray& ra_arg, int dim, int add_dim); + ComplexNDArray& insert (const NDArray& a, int r, int c); + ComplexNDArray& insert (const ComplexNDArray& a, int r, int c); + NDArray abs (void) const; ComplexMatrix matrix_value (void) const; diff -r 86c748d5f0af -r e941e1470d7b liboctave/ChangeLog --- a/liboctave/ChangeLog Mon Feb 16 05:15:50 2004 +0000 +++ b/liboctave/ChangeLog Mon Feb 16 05:56:51 2004 +0000 @@ -1,3 +1,16 @@ +2004-02-15 Petter Risholm + + * Array.cc (Array::insert (const Array&, int, int)): + Make it work for N-d arrays. + + * ArrayN.h (ArrayN::insert (const ArrayN& a, int, int)): + New function. + + * CNDArray.cc (ComplexNDArray::insert (const NDArray&, int, int), + ComplexNDArray::insert (const ComplexNDArray&, int, int)): + New functions. + * CNDArray.h: Provide decls. + 2004-02-14 John W. Eaton * Makefile.in (LINK_DEPS): Always define. diff -r 86c748d5f0af -r e941e1470d7b src/ChangeLog --- a/src/ChangeLog Mon Feb 16 05:15:50 2004 +0000 +++ b/src/ChangeLog Mon Feb 16 05:56:51 2004 +0000 @@ -1,3 +1,11 @@ +2004-02-15 John W. Eaton + + * lex.l (yywrap): Don't #undef this symbol. + +2004-02-15 Petter Risholm + + * pt-mat.cc: Make [,] concatenation work for N-d arrays. + 2004-02-15 John W. Eaton * data.cc (do_cat): Merge with Fcat. diff -r 86c748d5f0af -r e941e1470d7b src/lex.l --- a/src/lex.l Mon Feb 16 05:15:50 2004 +0000 +++ b/src/lex.l Mon Feb 16 05:56:51 2004 +0000 @@ -955,9 +955,6 @@ // Include these so that we don't have to link to libfl.a. -#ifdef yywrap -#undef yywrap -#endif int yywrap (void) { diff -r 86c748d5f0af -r e941e1470d7b src/pt-mat.cc --- a/src/pt-mat.cc Mon Feb 16 05:15:50 2004 +0000 +++ b/src/pt-mat.cc Mon Feb 16 05:56:51 2004 +0000 @@ -65,12 +65,12 @@ public: tm_row_const_rep (void) - : count (1), nr (0), nc (0), + : count (1), dv (), all_str (false), some_str (false), is_cmplx (false), all_mt (true), ok (false) { } tm_row_const_rep (const tree_argument_list& row) - : count (1), nr (0), nc (0), + : count (1), dv (), all_str (false), some_str (false), is_cmplx (false), all_mt (true), ok (false) { init (row); } @@ -79,8 +79,7 @@ int count; - int nr; - int nc; + dim_vector dv; bool all_str; bool some_str; @@ -145,8 +144,13 @@ delete rep; } - int rows (void) { return rep->nr; } - int cols (void) { return rep->nc; } + int rows (void) + { return (rep->dv.length () > 0 ? rep->dv(0) : 0); } + + int cols (void) + { return (rep->dv.length () > 1 ? rep->dv(1) : 0); } + + dim_vector dims (void) { return rep->dv; } bool all_strings_p (void) const { return rep->all_str; } bool some_strings_p (void) const { return rep->some_str; } @@ -174,7 +178,9 @@ int this_elt_nr = val.rows (); int this_elt_nc = val.columns (); - if (this_elt_nr > 0 || this_elt_nc > 0) + dim_vector this_elt_dv = val.dims (); + + if (!this_elt_dv.all_zero ()) { all_mt = false; @@ -182,16 +188,32 @@ { first_elem = false; - nr = this_elt_nr; + dv.resize (this_elt_dv.length ()); + for (int i = 2; i < dv.length (); i++) + dv.elem (i) = this_elt_dv.elem (i); + + dv.elem (0) = this_elt_nr; + + dv.elem (1) = 0; } - else if (this_elt_nr != nr) + else { - eval_error ("number of rows must match", - elt->line (), elt->column (), this_elt_nr, nr); - return false; + if (this_elt_nr != dv (0)) + { + eval_error ("number of rows must match", + elt->line (), elt->column (), this_elt_nr, dv (0)); + return false; + } + for (int i = 2; i < this_elt_dv.length (); i++) + { + if (this_elt_dv (i) != dv (i)) + { + eval_error ("dimensions mismatch", elt->line (), elt->column (), this_elt_dv (i), dv (i)); + return false; + } + } } - - nc += this_elt_nc; + dv.elem (1) = dv.elem (1) + this_elt_nc; append (val); } @@ -289,15 +311,16 @@ public: tm_const (const tree_matrix& tm) - : nr (0), nc (0), - all_str (false), some_str (false), is_cmplx (false), + : dv (), all_str (false), some_str (false), is_cmplx (false), all_mt (true), ok (false) { init (tm); } ~tm_const (void) { } - int rows (void) const { return nr; } - int cols (void) const { return nc; } + int rows (void) const { return (dv.length () > 0 ? dv.elem (0) : 0); } + int cols (void) const { return (dv.length () > 1 ? dv.elem (1) : 0); } + + dim_vector dims (void) const { return dv; } bool all_strings_p (void) const { return all_str; } bool some_strings_p (void) const { return some_str; } @@ -308,8 +331,7 @@ private: - int nr; - int nc; + dim_vector dv; bool all_str; bool some_str; @@ -374,7 +396,9 @@ int this_elt_nr = elt.rows (); int this_elt_nc = elt.cols (); - if (this_elt_nr > 0 || this_elt_nc > 0) + dim_vector this_elt_dv = elt.dims (); + + if (!this_elt_dv.all_zero ()) { all_mt = false; @@ -382,21 +406,44 @@ { first_elem = false; - nc = this_elt_nc; + dv.resize (this_elt_dv.length ()); + for (int i = 2; i < dv.length (); i++) + dv.elem (i) = this_elt_dv.elem (i); + + dv.elem (0) = 0; + + dv.elem (1) = this_elt_nc; } else if (all_str) { - if (this_elt_nc > nc) - nc = this_elt_nc; + if (this_elt_nc > cols ()) + dv.elem (1) = this_elt_nc; } - else if (this_elt_nc != nc) + else { - ::error ("number of columns must match (%d != %d)", - this_elt_nc, nc); - break; + bool get_out = false; + + for (int i = 1; i < this_elt_dv.length (); i++) + { + if (i == 1 && this_elt_nc != dv (1)) + { + ::error ("number of columns must match (%d != %d)", + this_elt_nc, dv (1)); + get_out = true; + break; + } + else if (this_elt_dv (i) != dv (i)) + { + ::error ("dimensions mismatch (dim = %i, %d != %d)", i+1, this_elt_dv (i), dv (i)); + get_out = true; + break; + } + } + + if (get_out) + break; } - - nr += this_elt_nr; + dv.elem (0) = dv.elem (0) + this_elt_nr; } else if (Vwarn_empty_list_elements) warning ("empty matrix found in matrix list"); @@ -478,12 +525,11 @@ if (tmp) { - int nr = tmp.rows (); - int nc = tmp.cols (); + dim_vector dv = tmp.dims (); - Matrix m; - ComplexMatrix cm; - charMatrix chm; + NDArray nd; + ComplexNDArray cnd; + charNDArray chnd; // Now, extract the values from the individual elements and // insert them in the result matrix. @@ -497,11 +543,11 @@ frc_str_conv = some_strings_p; if (all_strings_p) - chm.resize (nr, nc, Vstring_fill_char); + chnd.resize_and_fill (dv, Vstring_fill_char); else if (found_complex) - cm.resize (nr, nc, 0.0); + cnd.resize_and_fill (dv, 0.0); else - m.resize (nr, nc, 0.0); + nd.resize_and_fill (dv, 0.0); int put_row = 0; @@ -518,51 +564,51 @@ if (found_complex) { if (elt.is_real_scalar ()) - cm (put_row, put_col) = elt.double_value (); + cnd (put_row, put_col) = elt.double_value (); else if (elt.is_real_matrix () || elt.is_range ()) - cm.insert (elt.matrix_value (), put_row, put_col); + cnd.insert (elt.array_value (), put_row, put_col); else if (elt.is_complex_scalar ()) - cm (put_row, put_col) = elt.complex_value (); + cnd (put_row, put_col) = elt.complex_value (); else { - ComplexMatrix cm_elt = elt.complex_matrix_value (); + ComplexNDArray cnd_elt = elt.complex_array_value (); if (error_state) goto done; - cm.insert (cm_elt, put_row, put_col); + cnd.insert (cnd_elt, put_row, put_col); } } else { if (elt.is_real_scalar ()) - m (put_row, put_col) = elt.double_value (); + nd (put_row, put_col) = elt.double_value (); else if (elt.is_string () && all_strings_p) { - charMatrix chm_elt = elt.char_matrix_value (); + charNDArray chnd_elt = elt.char_array_value (); if (error_state) goto done; - chm.insert (chm_elt, put_row, put_col); + chnd.insert (chnd_elt, put_row, put_col); } else { - Matrix m_elt = elt.matrix_value (frc_str_conv); + NDArray nd_elt = elt.array_value (frc_str_conv); if (error_state) goto done; - m.insert (m_elt, put_row, put_col); + nd.insert (nd_elt, put_row, put_col); } } - if (all_strings_p && chm.rows () > 0 && chm.cols () > 0) - retval = octave_value (chm, true); + if (all_strings_p && chnd.rows () > 0 && chnd.cols () > 0) + retval = octave_value (chnd, true); else if (found_complex) - retval = cm; + retval = cnd; else - retval = m; + retval = nd; put_col += elt.columns (); } @@ -580,7 +626,7 @@ if (all_strings_p) retval = ""; else - retval = Matrix (); + retval = NDArray (); } else if (frc_str_conv && ! retval.is_string ()) retval = retval.convert_to_str ();