Mercurial > hg > octave-nkf
changeset 5731:c7d5a534afa5
[project @ 2006-04-03 19:33:26 by jwe]
author | jwe |
---|---|
date | Mon, 03 Apr 2006 19:33:27 +0000 |
parents | 109fdf7b3dcb |
children | 17d87fbd7010 |
files | liboctave/ChangeLog liboctave/Sparse.cc scripts/ChangeLog scripts/general/tril.m scripts/general/triu.m scripts/special-matrix/hankel.m scripts/special-matrix/toeplitz.m src/ChangeLog src/data.cc src/oct-map.cc src/oct-map.h src/ov-base-mat.cc src/ov-base-mat.h src/ov-base-sparse.cc src/ov-base-sparse.h src/ov-base.cc src/ov-base.h src/ov-bool.cc src/ov-bool.h src/ov-complex.cc src/ov-complex.h src/ov-intx.h src/ov-range.cc src/ov-range.h src/ov-scalar.cc src/ov-scalar.h src/ov-str-mat.cc src/ov-str-mat.h src/ov.h |
diffstat | 29 files changed, 235 insertions(+), 70 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,5 +1,8 @@ 2006-04-03 David Bateman <dbateman@free.fr> + * Sparse.cc (Sparse<T>::resize): Use xcidx rather than cdix, etc + to avoid copy of original matrix. + * Makefile.in (INCLUDES): Add randgamma.h, randpoisson.h and randmtzig.h to the list. (LIBOCTAVE_C_SOURCES): Add randgamma.c, randpoisson.c and
--- a/liboctave/Sparse.cc +++ b/liboctave/Sparse.cc @@ -851,7 +851,8 @@ if (r == dim1 () && c == dim2 ()) return; - typename Sparse<T>::SparseRep *old_rep = Sparse<T>::rep; + typename Sparse<T>::SparseRep *old_rep = rep; + octave_idx_type nc = cols (); octave_idx_type nr = rows (); @@ -866,35 +867,35 @@ if (r >= nr) { if (c > nc) - n = cidx(nc); + n = xcidx(nc); else - n = cidx(c); + n = xcidx(c); tmpval = Sparse<T> (r, c, n); if (c > nc) { for (octave_idx_type i = 0; i < nc; i++) - tmpval.cidx(i) = cidx(i); + tmpval.cidx(i) = xcidx(i); for (octave_idx_type i = nc+2; i < c; i++) tmpval.cidx(i) = tmpval.cidx(i-1); } else if (c <= nc) for (octave_idx_type i = 0; i < c; i++) - tmpval.cidx(i) = cidx(i); + tmpval.cidx(i) = xcidx(i); for (octave_idx_type i = 0; i < n; i++) { - tmpval.data(i) = data(i); - tmpval.ridx(i) = ridx(i); + tmpval.data(i) = xdata(i); + tmpval.ridx(i) = xridx(i); } } else { // Count how many non zero terms before we do anything for (octave_idx_type i = 0; i < c; i++) - for (octave_idx_type j = cidx(i); j < cidx(i+1); j++) - if (ridx(j) < r) + for (octave_idx_type j = xcidx(i); j < xcidx(i+1); j++) + if (xridx(j) < r) n++; if (n) @@ -905,11 +906,11 @@ tmpval.cidx(0); for (octave_idx_type i = 0, ii = 0; i < c; i++) { - for (octave_idx_type j = cidx(i); j < cidx(i+1); j++) - if (ridx(j) < r) + for (octave_idx_type j = xcidx(i); j < xcidx(i+1); j++) + if (xridx(j) < r) { - tmpval.data(ii) = data(j); - tmpval.ridx(ii++) = ridx(j); + tmpval.data(ii) = xdata(j); + tmpval.ridx(ii++) = xridx(j); } tmpval.cidx(i+1) = ii; }
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,10 @@ +2006-04-02 David Bateman <dbateman@free.fr> + + * general/tril.m, general.triu.m: + Use resize (resize (x, 0), nr, nc) rather than zeros (nr, nc) + to allow user types to work correctly. + * special-matrix/hankel.m, special-matrix/toeplitz.m: Ditto. + 2006-04-03 David Bateman <dbateman@free.fr> * plot/grid.m: Cache the state of the grid to allow toggling.
--- a/scripts/general/tril.m +++ b/scripts/general/tril.m @@ -68,11 +68,7 @@ if (nargin > 0) [nr, nc] = size (x); - if (isa (x, "sparse")) - retval = sparse (nr, nc); - else - retval = zeros (nr, nc, class (x)); - endif + retval = resize (resize (x, 0), nr, nc); endif if (nargin == 1)
--- a/scripts/general/triu.m +++ b/scripts/general/triu.m @@ -28,11 +28,7 @@ if (nargin > 0) [nr, nc] = size (x); - if (isa (x, "sparse")) - retval = sparse (nr, nc); - else - retval = zeros (nr, nc, class (x)); - endif + retval = resize (resize (x, 0), nr, nc); endif if (nargin == 1)
--- a/scripts/special-matrix/hankel.m +++ b/scripts/special-matrix/hankel.m @@ -51,7 +51,7 @@ function retval = hankel (c, r) if (nargin == 1) - r = zeros (size (c)); + r = resize (resize (c, 0), size(c)); elseif (nargin != 2) usage ("hankel (c, r)"); endif @@ -84,7 +84,7 @@ ## This should probably be done with the colon operator... - retval = zeros (nr, nc); + retval = resize (resize (c, 0), nr, nc); for i = 1:min (nr, nc) retval (1:nr-i+1, i) = c (i:nr); @@ -100,3 +100,9 @@ endfor endfunction + +%!assert(hankel(1:3),[1,2,3;2,3,0;3,0,0]) +%!assert(hankel(1),[1]); +%!assert(hankel(1:3,3:6),[1,2,3,4;2,3,4,5;3,4,5,6]); +%!assert(hankel(1:3,3:4),[1,2;2,3;3,4]); +%!assert(hankel(1:3,4:6),[1,2,3;2,3,5;3,5,6]);
--- a/scripts/special-matrix/toeplitz.m +++ b/scripts/special-matrix/toeplitz.m @@ -97,7 +97,7 @@ nc = length (r); nr = length (c); - retval = zeros (nr, nc); + retval = resize (resize (c, 0), nr, nc); for i = 1:min (nc, nr) retval (i:nr, i) = c (1:nr-i+1);
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,5 +1,13 @@ 2006-04-03 David Bateman <dbateman@free.fr> + * data.cc (Fresize): New function. + * oct-map.cc, ov-base-mat.cc, ov-base-sparse.cc, ov-base.cc, + ov-bool.cc, ov-complex.cc, ov-range.cc, ov-scalar.cc, + ov-str-mat.cc (resize): Add boolean fill argument. + * oct-map.h, ov-base-mat.h, ov-base-sparse.h, ov-base.h, + ov-bool.h, ov-complex.h, ov-intx.h, ov-range.h, ov-scalar.h, + ov-str-mat.h, ov.h (resize): Ditto. + * DLD-FUNCTIONS/rand.cc (do_rand): Additional argument for gamma and poisson distributions. Change "state" and "seed" arguments so that they choose between generators.
--- a/src/data.cc +++ b/src/data.cc @@ -1863,6 +1863,57 @@ return retval; } +DEFUN (resize, args, , + "-*- texinfo -*-\n\ +@deftypefn {Built-in Function} {} resize (@var{x}, @var{m})\n\ +@deftypefnx {Built-in Function} {} resize (@var{x}, @var{m}, @var{n})\n\ +Resize @var{x} to be dimension @var{m}-by-@var{n} where @var{m}\n\ +and @var{n} are scalar. If @var{n} is not supplied, then resize @var{x}\n\ +to be dimension @var{m}-by-@var{m} if @var{m} is scalar, or if\n\ +@var{m} is a vector, then the values of this vector respresent the\n\ +dimensions of the resized matrix.\n\ +@end deftypefn") +{ + octave_value retval; + int nargin = args.length (); + + if (nargin == 2) + { + Array<double> vec = args(1).vector_value (); + int ndim = vec.length (); + if (ndim == 1) + { + octave_idx_type m = static_cast<octave_idx_type> (vec(0)); + retval = args(0); + retval = retval.resize (dim_vector (m, m), true); + } + else + { + dim_vector dv; + dv.resize (ndim); + for (int i = 0; i < ndim; i++) + dv(i) = static_cast<octave_idx_type> (vec(i)); + retval = args(0); + retval = retval.resize (dv, true); + } + } + else if (nargin == 3) + { + octave_idx_type m = static_cast<octave_idx_type> + (args(1).scalar_value()); + octave_idx_type n = static_cast<octave_idx_type> + (args(2).scalar_value()); + if (!error_state) + { + retval = args(0); + retval = retval.resize (dim_vector (m, n), true); + } + } + else + print_usage ("resize"); + return retval; +} + DEFUN (reshape, args, , "-*- texinfo -*-\n\ @deftypefn {Function File} {} reshape (@var{a}, @var{m}, @var{n}, @dots{})\n\
--- a/src/oct-map.cc +++ b/src/oct-map.cc @@ -141,7 +141,7 @@ } Octave_map -Octave_map::resize (const dim_vector& dv) const +Octave_map::resize (const dim_vector& dv, bool fill) const { Octave_map retval; @@ -150,7 +150,10 @@ for (const_iterator p = begin (); p != end (); p++) { Cell tmp = contents(p); - tmp.resize(dv); + if (fill) + tmp.resize(dv, Cell::resize_fill_value ()); + else + tmp.resize(dv); retval.assign (key(p), tmp); }
--- a/src/oct-map.h +++ b/src/oct-map.h @@ -127,7 +127,7 @@ Octave_map reshape (const dim_vector& new_dims) const; - Octave_map resize (const dim_vector& dv) const; + Octave_map resize (const dim_vector& dv, bool fill = false) const; octave_idx_type numel (void) const;
--- a/src/ov-base-mat.cc +++ b/src/ov-base-mat.cc @@ -202,6 +202,18 @@ } template <class MT> +octave_value +octave_base_matrix<MT>::resize (const dim_vector& dv, bool fill) const +{ + MT retval (matrix); + if (fill) + retval.resize (dv, 0); + else + retval.resize (dv); + return retval; +} + +template <class MT> bool octave_base_matrix<MT>::is_true (void) const {
--- a/src/ov-base-mat.h +++ b/src/ov-base-mat.h @@ -102,8 +102,7 @@ octave_value permute (const Array<int>& vec, bool inv = false) const { return MT (matrix.permute (vec, inv)); } - octave_value resize (const dim_vector& dv) const - { MT retval (matrix); retval.resize (dv); return retval; } + octave_value resize (const dim_vector& dv, bool fill = false) const; octave_value all (int dim = 0) const { return matrix.all (dim); } octave_value any (int dim = 0) const { return matrix.any (dim); }
--- a/src/ov-base-sparse.cc +++ b/src/ov-base-sparse.cc @@ -40,6 +40,7 @@ #include "boolSparse.h" #include "ov-base-sparse.h" +#include "pager.h" template <class T> octave_value @@ -193,6 +194,14 @@ typ.invalidate_type (); } +template <class T> +octave_value +octave_base_sparse<T>::resize (const dim_vector& dv, bool) const +{ + T retval (matrix); + retval.resize (dv); + return retval; +} template <class T> bool
--- a/src/ov-base-sparse.h +++ b/src/ov-base-sparse.h @@ -112,8 +112,7 @@ octave_value permute (const Array<int>& vec, bool inv = false) const { return T (matrix.permute (vec, inv)); } - octave_value resize (const dim_vector& dv) const - { T retval (matrix); retval.resize (dv); return retval; } + octave_value resize (const dim_vector& dv, bool = false) const; octave_value all (int dim = 0) const { return matrix.all (dim); } octave_value any (int dim = 0) const { return matrix.any (dim); }
--- a/src/ov-base.cc +++ b/src/ov-base.cc @@ -202,7 +202,7 @@ } octave_value -octave_base_value::resize (const dim_vector&) const +octave_base_value::resize (const dim_vector&, bool) const { gripe_wrong_type_arg ("octave_base_value::resize ()", type_name ()); return octave_value ();
--- a/src/ov-base.h +++ b/src/ov-base.h @@ -107,7 +107,7 @@ octave_value permute (const Array<int>& vec, bool = false) const; - octave_value resize (const dim_vector&) const; + octave_value resize (const dim_vector&, bool fill = false) const; bool is_defined (void) const { return false; }
--- a/src/ov-bool.cc +++ b/src/ov-bool.cc @@ -103,6 +103,25 @@ return retval; } +octave_value +octave_bool::resize (const dim_vector& dv, bool fill) const +{ + if (fill) + { + boolNDArray retval (dv, false); + if (dv.numel()) + retval(0) = scalar; + return retval; + } + else + { + boolNDArray retval (dv); + if (dv.numel()) + retval(0) = scalar; + return retval; + } +} + octave_value octave_bool::convert_to_str_internal (bool, bool, char type) const {
--- a/src/ov-bool.h +++ b/src/ov-bool.h @@ -149,8 +149,7 @@ boolNDArray bool_array_value (void) const { return boolNDArray (dim_vector (1, 1), scalar); } - octave_value resize (const dim_vector& dv) const - { boolNDArray retval (dv); if (dv.numel()) retval(0) = scalar; return retval; } + octave_value resize (const dim_vector& dv, bool fill = false) const; octave_value convert_to_str_internal (bool pad, bool force, char type) const;
--- a/src/ov-complex.cc +++ b/src/ov-complex.cc @@ -149,6 +149,29 @@ return ComplexNDArray (dim_vector (1, 1), scalar); } +octave_value +octave_complex::resize (const dim_vector& dv, bool fill) const +{ + if (fill) + { + ComplexNDArray retval (dv, ComplexNDArray::resize_fill_value ()); + + if (dv.numel ()) + retval(0) = scalar; + + return retval; + } + else + { + ComplexNDArray retval (dv); + + if (dv.numel ()) + retval(0) = scalar; + + return retval; + } +} + bool octave_complex::save_ascii (std::ostream& os, bool& infnan_warned, bool strip_nan_and_inf)
--- a/src/ov-complex.h +++ b/src/ov-complex.h @@ -101,15 +101,7 @@ NDArray array_value (bool = false) const; - octave_value resize (const dim_vector& dv) const - { - ComplexNDArray retval (dv); - - if (dv.numel ()) - retval(0) = scalar; - - return retval; - } + octave_value resize (const dim_vector& dv, bool fill = false) const; Complex complex_value (bool = false) const;
--- a/src/ov-intx.h +++ b/src/ov-intx.h @@ -255,12 +255,22 @@ uint64_array_value (void) const { return uint64NDArray (dim_vector (1, 1), scalar); } - octave_value resize (const dim_vector& dv) const + octave_value resize (const dim_vector& dv, bool fill = false) const { - OCTAVE_INT_NDARRAY_T retval (dv); - if (dv.numel()) - retval(0) = scalar; - return retval; + if (fill) + { + OCTAVE_INT_NDARRAY_T retval (dv, 0); + if (dv.numel()) + retval(0) = scalar; + return retval; + } + else + { + OCTAVE_INT_NDARRAY_T retval (dv); + if (dv.numel()) + retval(0) = scalar; + return retval; + } } double double_value (bool = false) const { return double (scalar); }
--- a/src/ov-range.cc +++ b/src/ov-range.cc @@ -208,6 +208,17 @@ return retval; } +octave_value +octave_range::resize (const dim_vector& dv, bool fill) const +{ + NDArray retval = array_value (); + if (fill) + retval.resize (dv, NDArray::resize_fill_value()); + else + retval.resize (dv); + return retval; +} + octave_value octave_range::convert_to_str_internal (bool pad, bool force, char type) const {
--- a/src/ov-range.h +++ b/src/ov-range.h @@ -106,8 +106,8 @@ return dim_vector (n > 0, n); } - octave_value resize (const dim_vector& dv) const - { NDArray retval = array_value (); retval.resize (dv); return retval; } + octave_value resize (const dim_vector& dv, bool fill = false) const; + size_t byte_size (void) const { return 3 * sizeof (double); }
--- a/src/ov-scalar.cc +++ b/src/ov-scalar.cc @@ -106,6 +106,29 @@ return retval; } +octave_value +octave_scalar::resize (const dim_vector& dv, bool fill) const +{ + if (fill) + { + NDArray retval (dv, NDArray::resize_fill_value()); + + if (dv.numel ()) + retval(0) = scalar; + + return retval; + } + else + { + NDArray retval (dv); + + if (dv.numel ()) + retval(0) = scalar; + + return retval; + } +} + octave_value octave_scalar::convert_to_str_internal (bool, bool, char type) const {
--- a/src/ov-scalar.h +++ b/src/ov-scalar.h @@ -138,15 +138,7 @@ NDArray array_value (bool = false) const { return NDArray (dim_vector (1, 1), scalar); } - octave_value resize (const dim_vector& dv) const - { - NDArray retval (dv); - - if (dv.numel ()) - retval(0) = scalar; - - return retval; - } + octave_value resize (const dim_vector& dv, bool fill = false) const; Complex complex_value (bool = false) const { return scalar; }
--- a/src/ov-str-mat.cc +++ b/src/ov-str-mat.cc @@ -149,6 +149,17 @@ ::assign (matrix, tmp, Vstring_fill_char); } +octave_value +octave_char_matrix_str::resize (const dim_vector& dv, bool fill) const +{ + charNDArray retval (matrix); + if (fill) + retval.resize (dv, charNDArray::resize_fill_value()); + else + retval.resize (dv); + return octave_value (retval, true); +} + bool octave_char_matrix_str::valid_as_scalar_index (void) const {
--- a/src/ov-str-mat.h +++ b/src/ov-str-mat.h @@ -96,12 +96,7 @@ octave_value permute (const Array<int>& vec, bool inv = false) const { return octave_value (charNDArray (matrix.permute (vec, inv)), true); } - octave_value resize (const dim_vector& dv) const - { - charNDArray retval (matrix); - retval.resize (dv); - return octave_value (retval, true); - } + octave_value resize (const dim_vector& dv, bool fill = false) const; bool is_string (void) const { return true; }
--- a/src/ov.h +++ b/src/ov.h @@ -391,8 +391,8 @@ octave_value ipermute (const Array<int>& vec) const { return rep->permute (vec, true); } - virtual octave_value resize (const dim_vector& dv) const - { return rep->resize (dv);} + virtual octave_value resize (const dim_vector& dv, bool fill = false) const + { return rep->resize (dv, fill);} // Does this constant have a type? Both of these are provided since // it is sometimes more natural to write is_undefined() instead of