diff scripts/set/union.m @ 19003:d00f6b09258f @

Overhaul functions in scripts/set directory. * set.txi: Rewrite documentation for set functions. * intersect.m: Rewrite docstring. Use by_rows variable for code clarity. Return output orientation which is compatible with Matlab. Add %!tests for output orientation and N-dimensional inputs. * setdiff.m: Rewrite docstring. Use by_rows variable for code clarity. Rename output i to ia to clarify it is an index into the set a. Return output orientation which is compatible with Matlab. Add %!tests for N-dimensional inputs. * setxor.m: Rewrite docstring. Use by_rows variable for code clarity. Return output orientation which is compatible with Matlab. Add %!tests for output orientation and N-dimensional inputs. * union.m: Rewrite docstring. Use by_rows variable for code clarity. Return output orientation which is compatible with Matlab. Add %!tests for output orientation and N-dimensional inputs. Add %!tests for validsetargs which are common to all set functions. * unique.m: Rewrite docstring. Verify that input is numeric or cell array of strings. Avoid computing idx for optional i,j outputs unless required. Add %!error tests for input validation. * ismember.m: Rewrite docstring. Use input variable 'a' instead of 'A' for conformance with rest of set functions. Rename output index variable to s_idx for clarity that it is an index into the set s. * powerset.m: Rewrite doctring. Add input validation on nargin. Add %!error input validation tests. * module.mk: Include validsetargs.m in build system. * validsetargs.m: Function renamed from validargs which was too general. * validargs.m: Function renamed to validsetargs.
author Rik <rik@octave.org>
date Mon, 11 Aug 2014 09:39:45 -0700
parents d63878346099
children
line wrap: on
line diff
--- a/scripts/set/union.m	Sun Aug 10 08:18:18 2014 -0700
+++ b/scripts/set/union.m	Mon Aug 11 09:39:45 2014 -0700
@@ -18,38 +18,26 @@
 ## <http://www.gnu.org/licenses/>.
 
 ## -*- texinfo -*-
-## @deftypefn  {Function File} {} union (@var{a}, @var{b})
-## @deftypefnx {Function File} {} union (@var{a}, @var{b}, "rows")
-## @deftypefnx {Function File} {[@var{c}, @var{ia}, @var{ib}] =} union (@var{a}, @var{b})
+## @deftypefn  {Function File} {@var{c} =} union (@var{a}, @var{b})
+## @deftypefnx {Function File} {@var{c} =} union (@var{a}, @var{b}, "rows")
+## @deftypefnx {Function File} {[@var{c}, @var{ia}, @var{ib}] =} union (@dots{})
 ##
-## Return the set of elements that are in either of the sets @var{a} and
-## @var{b}.  @var{a}, @var{b} may be cell arrays of strings.
-## For example:
-##
-## @example
-## @group
-## union ([1, 2, 4], [2, 3, 5])
-##     @result{} [1, 2, 3, 4, 5]
-## @end group
-## @end example
+## Return the elements that are in either @var{a} or @var{b} sorted in
+## ascending order with duplicates removed.
 ##
-## If the optional third input argument is the string @qcode{"rows"} then
-## each row of the matrices @var{a} and @var{b} will be considered as a
-## single set element.  For example:
+## If @var{a} and @var{b} are both column vectors return a column vector;
+## Otherwise, return a row vector.  The inputs may also be cell arrays of
+## strings.
 ##
-## @example
-## @group
-## union ([1, 2; 2, 3], [1, 2; 3, 4], "rows")
-##    @result{}  1   2
-##        2   3
-##        3   4
-## @end group
-## @end example
+## If the optional input @qcode{"rows"} is given then return rows that are in
+## either @var{a} or @var{b}.  The inputs must be 2-D matrices to use this
+## option.
+## 
+## The optional outputs @var{ia} and @var{ib} are index vectors such that
+## @code{@var{a}(@var{ia})} and @code{@var{b}(@var{ib})} are disjoint sets
+## whose union is @var{c}.
 ##
-## The optional outputs @var{ia} and @var{ib} are index vectors such that
-## @code{a(ia)} and @code{b(ib)} are disjoint sets whose union is @var{c}.
-##
-## @seealso{intersect, setdiff, unique}
+## @seealso{unique, intersect, setdiff, setxor, ismember}
 ## @end deftypefn
 
 ## Author: jwe
@@ -60,40 +48,59 @@
     print_usage ();
   endif
 
-  [a, b] = validargs ("union", a, b, varargin{:});
+  [a, b] = validsetargs ("union", a, b, varargin{:});
+
+  by_rows = nargin == 3;
+  iscol = isvector (a) && isvector (b) && iscolumn (a) && iscolumn (b);
 
-  if (nargin == 2)
+  if (by_rows)
+    y = [a; b];
+  else
     y = [a(:); b(:)];
-    na = numel (a); nb = numel (b);
-    if (rows (a) == 1 || rows (b) == 1)
+    ## Adjust output orientation for Matlab compatibility
+    if (! iscol)
       y = y.';
     endif
-  else
-    y = [a; b];
-    na = rows (a); nb = rows (b);
   endif
 
-  if (nargout == 1)
+  if (nargout <= 1)
     y = unique (y, varargin{:});
   else
-    [y, i] = unique (y, varargin{:});
-    ia = i(i <= na);
-    ib = i(i > na) - na;
+    [y, idx] = unique (y, varargin{:});
+    na = numel (a);
+    ia = idx(idx <= na);
+    ib = idx(idx > na) - na;
   endif
 
 endfunction
 
 
-%!assert (union ([1, 2, 4], [2, 3, 5]), [1, 2, 3, 4, 5]);
-%!assert (union ([1; 2; 4], [2, 3, 5]), [1, 2, 3, 4, 5]);
-%!assert (union ([1, 2, 3], [5; 7; 9]), [1, 2, 3, 5, 7, 9]);
+%!assert (union ([1, 2, 4], [2, 3, 5]), [1, 2, 3, 4, 5])
+%!assert (union ([1; 2; 4], [2, 3, 5]), [1, 2, 3, 4, 5])
+%!assert (union ([1; 2; 4], [2; 3; 5]), [1; 2; 3; 4; 5])
+%!assert (union ([1, 2, 3], [5; 7; 9]), [1, 2, 3, 5, 7, 9])
+
+## Test multi-dimensional arrays
+%!test
+%! a = rand (3,3,3);
+%! b = a;
+%! b(1,1,1) = 2;
+%! assert (union (a, b), sort ([a(1:end), 2]));
 
 %!test
-%! a = [3, 1, 4, 1, 5];  b = [1, 2, 3, 4];
+%! a = [3, 1, 4, 1, 5]; 
+%! b = [1, 2, 3, 4];
 %! [y, ia, ib] = union (a, b.');
 %! assert (y, [1, 2, 3, 4, 5]);
 %! assert (y, sort ([a(ia), b(ib)]));
 
-%!error union (1)
-%!error union (1, 2, 3)
 
+%% Test common input validation for set routines contained in validsetargs
+%!error <cell array of strings cannot be combined> union ({"a"}, 1)
+%!error <A and B must be arrays or cell arrays> union (@sin, 1)
+%!error <invalid option: columns> union (1, 2, "columns")
+%!error <cells not supported with "rows"> union ({"a"}, {"b"}, "rows")
+%!error <A and B must be arrays or cell arrays> union (@sin, 1, "rows")
+%!error <A and B must be 2-dimensional matrices> union (rand(2,2,2), 1, "rows")
+%!error <number of columns in A and B must match> union ([1 2], 1, "rows")
+