Mercurial > hg > octave-lyh
changeset 4870:2cbc6f37b0c2
[project @ 2004-04-21 17:30:51 by jwe]
author | jwe |
---|---|
date | Wed, 21 Apr 2004 17:30:52 +0000 |
parents | b92d59213e63 |
children | 9c89c1408c32 |
files | liboctave/Array.cc liboctave/ChangeLog scripts/ChangeLog scripts/elfun/lcm.m scripts/general/prepad.m |
diffstat | 5 files changed, 84 insertions(+), 48 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/Array.cc +++ b/liboctave/Array.cc @@ -560,8 +560,6 @@ if (same_size) return; - int old_len = length (); - typename Array<T>::ArrayRep *old_rep = rep; const T *old_data = data (); @@ -569,16 +567,26 @@ rep = new typename Array<T>::ArrayRep (ts); + dim_vector dv_old = dimensions; + int dv_old_orig_len = dv_old.length (); dimensions = dv; - if (ts > 0) + if (ts > 0 && dv_old_orig_len > 0) { Array<int> ra_idx (dimensions.length (), 0); - for (int i = 0; i < old_len; i++) + if (n > dv_old_orig_len) { - if (index_in_bounds (ra_idx, dimensions)) - xelem (ra_idx) = old_data[i]; + dv_old.resize (n); + + for (int i = dv_old_orig_len; i < n; i++) + dv_old.elem (i) = 1; + } + + for (int i = 0; i < ts; i++) + { + if (index_in_bounds (ra_idx, dv_old)) + rep->elem (i) = old_data[get_scalar_idx (ra_idx, dv_old)]; increment_index (ra_idx, dimensions); } @@ -894,47 +902,39 @@ typename Array<T>::ArrayRep *old_rep = rep; const T *old_data = data (); - int old_len = length (); - int len = get_size (dv); rep = new typename Array<T>::ArrayRep (len); dim_vector dv_old = dimensions; - int dv_old_orig_len = dv_old.length (); - - if (n > dv_old_orig_len) - { - dv_old.resize (n); - - for (int i = dv_old_orig_len; i < n; i++) - dv_old.elem (i) = 1; - } - dimensions = dv; - if (len > 0) + if (len > 0 && dv_old_orig_len > 0) { Array<int> ra_idx (dimensions.length (), 0); - - // XXX FIXME XXX -- it is much simpler to fill the whole array - // first, but probably slower for large arrays, or if the assignment - // operator for the type T is expensive. OTOH, the logic for - // deciding whether an element needs the copied value or the filled - // value might be more expensive. + + if (n > dv_old_orig_len) + { + dv_old.resize (n); + + for (int i = dv_old_orig_len; i < n; i++) + dv_old.elem (i) = 1; + } for (int i = 0; i < len; i++) - rep->elem (i) = val; - - for (int i = 0; i < old_len; i++) { if (index_in_bounds (ra_idx, dv_old)) - xelem (ra_idx) = old_data[get_scalar_idx (ra_idx, dv_old)]; - - increment_index (ra_idx, dv_old); + rep->elem (i) = old_data[get_scalar_idx (ra_idx, dv_old)]; + else + rep->elem (i) = val; + + increment_index (ra_idx, dimensions); } } + else + for (int i = 0; i < len; i++) + rep->elem (i) = val; if (--old_rep->count <= 0) delete old_rep;
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,5 +1,9 @@ 2004-04-06 David Bateman <dbateman@free.fr> + * Array.cc (Array<T>::resize_no_fill (const dim_vector& dv), + Array<T>::resize_and_fill (const dim_vector& dv, const T& val)): + Make their behavior equivalent except for filling vs. not filling. + * oct-sort.cc: New template class for arbitrary sorting. * oct-sort.h: Declaration of sort class. * Makefile: Add them to the appropriate lists.
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,5 +1,7 @@ 2004-04-21 David Bateman <dbateman@free.fr> + * scripts/elfun/lcm.m: Make N-d aware. + * general/diff.m: Make the code N-d array aware. Allow an optional argument to define the dimension along which to perform the differences and allow the order of the differences to be larger
--- a/scripts/elfun/lcm.m +++ b/scripts/elfun/lcm.m @@ -33,36 +33,62 @@ ## lcm ([a1, ..., ak]). ## @end example ## @end deftypefn +## +## All elements must be the same size or scalar. ## @seealso{gcd, min, max, ceil, and floor} ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> ## Created: 16 September 1994 ## Adapted-By: jwe -function l = lcm (a, varargin) +function l = lcm (varargin) if (nargin == 0) usage ("lcm (a, ...)"); endif - if (nargin > 1) - k = 1; - for i = 2:nargin - a = [a, varargin{k++}]; - endfor - endif + if (nargin == 1) + a = varargin{1}; + + if (round (a) != a) + error ("lcm: all arguments must be integer"); + endif - if (round (a) != a) - error ("lcm: all arguments must be integer"); - endif + if (any (a) == 0) + l = 0; + else + a = abs (a); + l = a (1); + for k = 1:(length (a) - 1) + l = l * a(k+1) / gcd (l, a(k+1)); + endfor + endif + else + + l = varargin{1}; + sz = size (l); + nel = numel (l); + + for i=2:nargin + a = varargin{i}; - if (any (a) == 0) - l = 0; - else - a = abs (a); - l = a (1); - for k = 1:(length (a) - 1) - l = l * a(k+1) / gcd (l, a(k+1)); + if (size (a) != sz) + if (nel == 1) + sz = size (a); + nel = numel (a); + elseif (numel (a) != 1) + error ("lcm: all arguments must be the same size or scalar"); + endif + endif + + if (round (a) != a) + error ("lcm: all arguments must be integer"); + endif + + idx = find (l == 0 || a == 0); + a = abs (a); + l = l .* a ./ gcd (l, a); + l(idx) = 0; endfor endif
--- a/scripts/general/prepad.m +++ b/scripts/general/prepad.m @@ -20,6 +20,7 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {} prepad (@var{x}, @var{l}, @var{c}) ## @deftypefnx {Function File} {} postpad (@var{x}, @var{l}, @var{c}) +## @deftypefnx {Function File} {} postpad (@var{x}, @var{l}, @var{c}, @var{dim}) ## ## Prepends (appends) the scalar value @var{c} to the vector @var{x} ## until it is of length @var{l}. If the third argument is not @@ -29,6 +30,9 @@ ## @var{x} are removed until a vector of length @var{l} is obtained. ## ## If @var{x} is a matrix, elements are prepended or removed from each row. +## +## If the optional @var{dim} argument is given, then operate along this +## dimension. ## @end deftypefn ## Author: Tony Richardson <arichard@stark.cc.oh.us>