annotate scripts/set/union.m @ 10509:ddbd812d09aa

properly compress sparse matrices after assembly
author Jaroslav Hajek <highegg@gmail.com>
date Mon, 12 Apr 2010 12:57:44 +0200
parents 5edee330d4cb
children fbd7843974fa
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8898
diff changeset
1 ## Copyright (C) 2008, 2009 Jaroslav Hajek
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
2 ## Copyright (C) 1994, 1996, 1997, 1999, 2000, 2003, 2004, 2005, 2006,
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8898
diff changeset
3 ## 2007 John W. Eaton
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
4 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
5 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
6 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
7 ## Octave is free software; you can redistribute it and/or modify it
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
8 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
9 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
10 ## your option) any later version.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
11 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
12 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
15 ## General Public License for more details.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
16 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
17 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
18 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
19 ## <http://www.gnu.org/licenses/>.
2303
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 904
diff changeset
20
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
21 ## -*- texinfo -*-
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
22 ## @deftypefn {Function File} {} union (@var{a}, @var{b})
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
23 ## @deftypefnx{Function File} {} union (@var{a}, @var{b}, "rows")
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
24 ## Return the set of elements that are in either of the sets @var{a} and
10088
5edee330d4cb better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents: 9659
diff changeset
25 ## @var{b}. @var{a}, @var{b} may be cell arrays of string(s).
5edee330d4cb better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents: 9659
diff changeset
26 ## For example,
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3406
diff changeset
27 ##
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
28 ## @example
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
29 ## @group
7344
4571f691b0ce [project @ 2008-01-04 18:18:22 by jwe]
jwe
parents: 7017
diff changeset
30 ## union ([1, 2, 4], [2, 3, 5])
4571f691b0ce [project @ 2008-01-04 18:18:22 by jwe]
jwe
parents: 7017
diff changeset
31 ## @result{} [1, 2, 3, 4, 5]
4571f691b0ce [project @ 2008-01-04 18:18:22 by jwe]
jwe
parents: 7017
diff changeset
32 ## @end group
4571f691b0ce [project @ 2008-01-04 18:18:22 by jwe]
jwe
parents: 7017
diff changeset
33 ## @end example
4571f691b0ce [project @ 2008-01-04 18:18:22 by jwe]
jwe
parents: 7017
diff changeset
34 ##
4571f691b0ce [project @ 2008-01-04 18:18:22 by jwe]
jwe
parents: 7017
diff changeset
35 ## If the optional third input argument is the string "rows" each row of
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
36 ## the matrices @var{a} and @var{b} will be considered an element of sets.
7344
4571f691b0ce [project @ 2008-01-04 18:18:22 by jwe]
jwe
parents: 7017
diff changeset
37 ## For example,
4571f691b0ce [project @ 2008-01-04 18:18:22 by jwe]
jwe
parents: 7017
diff changeset
38 ## @example
4571f691b0ce [project @ 2008-01-04 18:18:22 by jwe]
jwe
parents: 7017
diff changeset
39 ## @group
4571f691b0ce [project @ 2008-01-04 18:18:22 by jwe]
jwe
parents: 7017
diff changeset
40 ## union([1, 2; 2, 3], [1, 2; 3, 4], "rows")
4571f691b0ce [project @ 2008-01-04 18:18:22 by jwe]
jwe
parents: 7017
diff changeset
41 ## @result{} 1 2
4571f691b0ce [project @ 2008-01-04 18:18:22 by jwe]
jwe
parents: 7017
diff changeset
42 ## 2 3
4571f691b0ce [project @ 2008-01-04 18:18:22 by jwe]
jwe
parents: 7017
diff changeset
43 ## 3 4
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
44 ## @end group
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
45 ## @end example
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
46 ##
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
47 ## @deftypefnx {Function File} {[@var{c}, @var{ia}, @var{ib}] =} union (@var{a}, @var{b})
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
48 ##
9659
0bcfeadb6178 fix union, setxor docstrings
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
49 ## Return index vectors @var{ia} and @var{ib} such that @code{a(ia)} and @code{b(ib)} are
0bcfeadb6178 fix union, setxor docstrings
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
50 ## disjoint sets whose union is @var{c}.
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
51 ##
8898
ca032de5fbf2 Remove references to deprecated function create_set.
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8507
diff changeset
52 ## @seealso{intersect, complement, unique}
3406
772cc0f88f09 [project @ 2000-01-05 20:51:46 by jwe]
jwe
parents: 3405
diff changeset
53 ## @end deftypefn
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
54
2314
949ab8eba8bc [project @ 1996-07-12 03:58:02 by jwe]
jwe
parents: 2313
diff changeset
55 ## Author: jwe
949ab8eba8bc [project @ 1996-07-12 03:58:02 by jwe]
jwe
parents: 2313
diff changeset
56
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
57 function [y, ia, ib] = union (a, b, varargin)
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
58
7344
4571f691b0ce [project @ 2008-01-04 18:18:22 by jwe]
jwe
parents: 7017
diff changeset
59 if (nargin < 2 || nargin > 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
60 print_usage ();
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
61 endif
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
62
10088
5edee330d4cb better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents: 9659
diff changeset
63 [a, b] = validargs ("union", a, b, varargin{:});
7344
4571f691b0ce [project @ 2008-01-04 18:18:22 by jwe]
jwe
parents: 7017
diff changeset
64
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
65 if (nargin == 2)
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
66 y = [a(:); b(:)];
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
67 na = numel (a); nb = numel (b);
4317
1ced186d4b6b [project @ 2003-02-11 16:10:09 by jwe]
jwe
parents: 3426
diff changeset
68 if (size (a, 1) == 1 || size (b, 1) == 1)
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
69 y = y.';
4317
1ced186d4b6b [project @ 2003-02-11 16:10:09 by jwe]
jwe
parents: 3426
diff changeset
70 endif
10088
5edee330d4cb better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents: 9659
diff changeset
71 else
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
72 y = [a; b];
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
73 na = rows (a); nb = rows (b);
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
74 endif
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
75
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
76 if (nargout == 1)
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
77 y = unique (y, varargin{:});
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
78 else
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
79 [y, i] = unique (y, varargin{:});
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
80 ia = i(i <= na);
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
81 ib = i(i > na) - na;
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
82 endif
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
83
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
84 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7344
diff changeset
85
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7344
diff changeset
86 %!assert(all (all (union ([1, 2, 4], [2, 3, 5]) == [1, 2, 3, 4, 5])));
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7344
diff changeset
87
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7344
diff changeset
88 %!assert(all (all (union ([1; 2; 4], [2, 3, 5]) == [1, 2, 3, 4, 5])));
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7344
diff changeset
89
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7344
diff changeset
90 %!assert(all (all (union ([1, 2, 3], [5; 7; 9]) == [1, 2, 3, 5, 7, 9])));
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7344
diff changeset
91
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7344
diff changeset
92 %!error union (1);
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7344
diff changeset
93
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7344
diff changeset
94 %!error union (1, 2, 3);
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7344
diff changeset
95
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
96 %!test
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
97 %! a = [3, 1, 4, 1, 5]; b = [1, 2, 3, 4];
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
98 %! [y, ia, ib] = union (a, b.');
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
99 %! assert(y, [1, 2, 3, 4, 5]);
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7411
diff changeset
100 %! assert(y, sort([a(ia), b(ib)]));