Mercurial > hg > octave-lyh
changeset 14113:dac62c415e8b stable
Do more error checking on accumarray and accumdim input.
* accumarray.m: Check that number indices matches number of values to
accumulate. Document this change.
* accumdim.m: Check that length of index vector equals length of array
along given dimension. Also let the extension parameter be empty in
addition to being zero. Document this change and reformat docstring.
* data.cc (do_accumdim_sum): Check that number of indices matches
number of values.
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Tue, 27 Dec 2011 13:45:30 -0500 |
parents | b8d9530e940e |
children | da67f032a712 |
files | scripts/general/accumarray.m scripts/general/accumdim.m src/data.cc |
diffstat | 3 files changed, 48 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/general/accumarray.m +++ b/scripts/general/accumarray.m @@ -26,7 +26,9 @@ ## the rows of the matrix @var{subs} and the values by @var{vals}. Each ## row of @var{subs} corresponds to one of the values in @var{vals}. If ## @var{vals} is a scalar, it will be used for each of the row of -## @var{subs}. +## @var{subs}. If @var{subs} is a cell array of vectors, all vectors +## must be of the same length, and the subscripts in the @var{k}th +## vector must correspond to the @var{k}th dimension of the result. ## ## The size of the matrix will be determined by the subscripts ## themselves. However, if @var{sz} is defined it determines the matrix @@ -107,14 +109,26 @@ print_usage (); endif + lenvals = length (vals); + if (iscell (subs)) - subs = cellfun ("vec", subs, "uniformoutput", false); + subs = cellfun (@vec, subs, "uniformoutput", false); ndims = numel (subs); if (ndims == 1) subs = subs{1}; endif + + lensubs = cellfun (@length, subs); + + if (any (lensubs != lensubs(1)) || (lenvals > 1 && lenvals != lensubs(1))) + error ("accumarray: dimension mismatch"); + endif + else ndims = columns (subs); + if (lenvals > 1 && lenvals != rows (subs)) + error ("accumarray: dimension mismatch") + endif endif if (isempty (fillval))
--- a/scripts/general/accumdim.m +++ b/scripts/general/accumdim.m @@ -22,29 +22,34 @@ ## positions defined by their subscripts along a specified dimension. ## The subscripts are defined by the index vector @var{subs}. ## The dimension is specified by @var{dim}. If not given, it defaults -## to the first non-singleton dimension. +## to the first non-singleton dimension. The length of @var{subs} must +## be equal to @code{size (@var{vals}, @var{dim})}. ## -## The extent of the result matrix in the working dimension will be determined -## by the subscripts themselves. -## However, if @var{n} is defined it determines this extent. +## The extent of the result matrix in the working dimension will be +## determined by the subscripts themselves. However, if @var{n} is +## defined it determines this extent. ## ## The default action of @code{accumdim} is to sum the subarrays with the -## same subscripts. This behavior can be modified by defining the @var{func} -## function. This should be a function or function handle that accepts an -## array and a dimension, and reduces the array along this dimension. -## As a special exception, the built-in @code{min} and @code{max} functions -## can be used directly, and @code{accumdim} accounts for the middle empty -## argument that is used in their calling. +## same subscripts. This behavior can be modified by defining the +## @var{func} function. This should be a function or function handle +## that accepts an array and a dimension, and reduces the array along +## this dimension. As a special exception, the built-in @code{min} and +## @code{max} functions can be used directly, and @code{accumdim} +## accounts for the middle empty argument that is used in their calling. ## -## The slices of the returned array that have no subscripts associated with -## them are set to zero. Defining @var{fillval} to some other value allows -## these values to be defined. +## The slices of the returned array that have no subscripts associated +## with them are set to zero. Defining @var{fillval} to some other +## value allows these values to be defined. ## ## An example of the use of @code{accumdim} is: ## ## @smallexample ## @group -## accumdim ([1, 2, 1, 2, 1], [7,-10,4;-5,-12,8;-12,2,8;-10,9,-3;-5,-3,-13]) +## accumdim ([1, 2, 1, 2, 1], [ 7, -10, 4; +## -5, -12, 8; +## -12, 2, 8; +## -10, 9, -3; +## -5, -3, -13]) ## @result{} ans = [-10,-11,-1;-15,-3,5] ## @end group ## @end smallexample @@ -68,7 +73,7 @@ error ("accumdim: indices must be positive integers"); else m = max (subs); - if (n == 0) + if (n == 0 || isempty (n)) n = m; elseif (n < m) error ("accumdim: N index out of range"); @@ -86,6 +91,10 @@ endif sz(dim) = n; + if (length (subs) != size (vals, dim)) + error ("accumdim: dimension mismatch") + endif + if (isempty (func) || func == @sum) ## Fast summation case. A = __accumdim_sum__ (subs, vals, dim, n);
--- a/src/data.cc +++ b/src/data.cc @@ -6636,9 +6636,10 @@ if (n < 0) n = idx.extent (0); else if (idx.extent (n) > n) - error ("accumarray: index out of range"); - - dim_vector rdv = vals.dims (); + error ("accumdim: index out of range"); + + dim_vector vals_dim = vals.dims (), rdv = vals_dim; + if (dim < 0) dim = vals.dims ().first_non_singleton (); else if (dim >= rdv.length ()) @@ -6648,7 +6649,11 @@ NDT retval (rdv, T()); + if (idx.length () != vals_dim(dim)) + error ("accumdim: dimension mismatch"); + retval.idx_add_nd (idx, vals, dim); + return retval; }