Mercurial > octave
annotate scripts/specfun/perms.m @ 31026:f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
perms.m: Add new function to return only unique permutations, expand input validation,
expand docstring.
NEWS.8.md: Add note about perms taking a new input option.
author | Arun Giridhar <arungiridhar@gmail.com> |
---|---|
date | Wed, 25 May 2022 17:58:13 -0400 |
parents | 5d3faba0342e |
children | 8629bb61ef17 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
3 ## Copyright (C) 2001-2022 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26946
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
5827 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24274
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
5827 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24274
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## (at your option) any later version. |
5827 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
5827 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24274
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
5827 | 25 |
26 ## -*- texinfo -*- | |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
27 ## @deftypefn {} {@var{P} =} perms (@var{v}) |
31026
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
28 ## @deftypefnx {} {@var{P} =} perms (@var{v}, "unique") |
23250
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
29 ## Generate all permutations of vector @var{v} with one row per permutation. |
5827 | 30 ## |
23250
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
31 ## Results are returned in inverse lexicographic order. The result has size |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
32 ## @code{factorial (@var{n}) * @var{n}}, where @var{n} is the length of |
31026
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
33 ## @var{v}. Any repetitions are included in the output. |
5827 | 34 ## |
31026
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
35 ## If the optional second argument is the string "unique", then only unique |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
36 ## permutations are returned, using a method that uses less memory than |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
37 ## @code{unique (perms (@var{v}), "rows")} and can be faster for |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
38 ## certain inputs. In this case, the results are not guaranteed to be in any |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
39 ## particular order. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9836
diff
changeset
|
40 ## |
31026
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
41 ## Example 1 |
6754 | 42 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
43 ## @group |
19102
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
44 ## perms ([1, 2, 3]) |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
45 ## @result{} |
23250
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
46 ## 3 2 1 |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
47 ## 3 1 2 |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
48 ## 2 3 1 |
6754 | 49 ## 2 1 3 |
50 ## 1 3 2 | |
23250
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
51 ## 1 2 3 |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
52 ## @end group |
6754 | 53 ## @end example |
19102
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
54 ## |
31026
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
55 ## Example 2 |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
56 ## @example |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
57 ## @group |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
58 ## perms ([1, 1, 2, 2], "unique") |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
59 ## @result{} |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
60 ## 2 2 1 1 |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
61 ## 2 1 2 1 |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
62 ## 2 1 1 2 |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
63 ## 1 2 2 1 |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
64 ## 1 2 1 2 |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
65 ## 1 1 2 2 |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
66 ## @end group |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
67 ## @end example |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
68 ## |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
69 ## Programming Notes: If the "unique" argument is not used, the length of |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
70 ## @var{v} should be no more than 10-12 to limit memory consumption. To get |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
71 ## unique results, test both @code{perms (@var{v}, "unique")} and |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
72 ## @code{unique (perms (@var{v}), "rows")} to compare speed and |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
73 ## memory usage. |
19102
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
74 ## @seealso{permute, randperm, nchoosek} |
5827 | 75 ## @end deftypefn |
76 | |
23250
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
77 ## FIXME: In principle it should be more efficient to do indexing using uint8 |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
78 ## type. However, benchmarking shows doubles are faster. If this changes in |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
79 ## a later version of Octave the index variables here can be made uint8. |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
80 |
31026
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
81 function P = perms (v, opt) |
19102
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
82 |
28891
de5f2f9a64ff
maint: Use same coding style when checking for a minimum of 1 input.
Rik <rik@octave.org>
parents:
28886
diff
changeset
|
83 if (nargin < 1) |
6391 | 84 print_usage (); |
31026
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
85 elseif (nargin == 2) |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
86 if (! isequal (opt, "unique")) |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
87 error ("perms: Second option must be the string ""unique""."); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
88 else |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
89 tmp = numel (unique (v)); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
90 if (tmp == 1) |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
91 P = reshape (v, 1, []); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
92 return |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
93 elseif (tmp < numel (v)) |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
94 P = __perms_unique__ (v); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
95 return |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
96 endif |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
97 endif |
6391 | 98 endif |
19102
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
99 |
31026
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
100 ## If we are here, then we want all permutations, not just unique ones. |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
101 ## Or the user passed the "unique" option but the input had no repetitions. |
24273
52023a3fe713
scripts/specfun/perms.m: support data of any indexable type (bug #52431)
Carnë Draug <carandraug@octave.org>
parents:
24268
diff
changeset
|
102 v = v(:).'; |
52023a3fe713
scripts/specfun/perms.m: support data of any indexable type (bug #52431)
Carnë Draug <carandraug@octave.org>
parents:
24268
diff
changeset
|
103 if (isnumeric (v) || ischar (v)) |
52023a3fe713
scripts/specfun/perms.m: support data of any indexable type (bug #52431)
Carnë Draug <carandraug@octave.org>
parents:
24268
diff
changeset
|
104 ## Order of output is only dependent on the actual values for |
52023a3fe713
scripts/specfun/perms.m: support data of any indexable type (bug #52431)
Carnë Draug <carandraug@octave.org>
parents:
24268
diff
changeset
|
105 ## character and numeric arrays. |
52023a3fe713
scripts/specfun/perms.m: support data of any indexable type (bug #52431)
Carnë Draug <carandraug@octave.org>
parents:
24268
diff
changeset
|
106 v = sort (v, "ascend"); |
23250
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
107 endif |
24273
52023a3fe713
scripts/specfun/perms.m: support data of any indexable type (bug #52431)
Carnë Draug <carandraug@octave.org>
parents:
24268
diff
changeset
|
108 n = numel (v); |
9836 | 109 |
23250
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
110 if (n < 4) # special cases for small n |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
111 switch (n) |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
112 case 0 |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
113 P = reshape (v, 1, 0); |
23250
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
114 case 1 |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
115 P = v; |
23250
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
116 case 2 |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
117 P = [v([2 1]);v]; |
23250
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
118 case 3 |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
119 P = v([3 2 1; 3 1 2; 2 3 1; 2 1 3; 1 3 2; 1 2 3]); |
23250
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
120 endswitch |
5827 | 121 else |
24273
52023a3fe713
scripts/specfun/perms.m: support data of any indexable type (bug #52431)
Carnë Draug <carandraug@octave.org>
parents:
24268
diff
changeset
|
122 v = v(end:-1:1); |
23250
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
123 n-= 1; |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
124 |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
125 idx = zeros (factorial (n), n); |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
126 idx(1:6, n-2:n) = [1, 2, 3;1, 3, 2;2, 1, 3;2, 3, 1;3, 1, 2;3, 2, 1]+(n-3); |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
127 f = 2; # jump-start for efficiency with medium n |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
128 for j = 3:n-1 |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
129 b = 1:n; |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
130 f *= j; |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
131 perm = idx(1:f, n-(j-1):n); |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
132 idx(1:(j+1)*f, n-j) = (n-j:n)(ones (f, 1),:)(:); |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
133 for i=0:j |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
134 b(i+n-j) -= 1; |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
135 idx((1:f)+i*f, n-(j-1):n) = b(perm); |
9836 | 136 endfor |
5827 | 137 endfor |
23250
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
138 |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
139 n += 1; |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
140 f *= n-1; |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
141 P = v(1)(ones (factorial (n), n)); |
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
142 P(:,1) = v(ones (f, 1),:)(:); |
19102
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
143 |
23250
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
144 for i = 1:n |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
145 b = v([1:i-1 i+1:n]); |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
146 P((1:f)+(i-1)*f, 2:end) = b(idx); |
24273
52023a3fe713
scripts/specfun/perms.m: support data of any indexable type (bug #52431)
Carnë Draug <carandraug@octave.org>
parents:
24268
diff
changeset
|
147 endfor |
23250
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
148 endif |
19102
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
149 |
5827 | 150 endfunction |
12815
918610ea2f34
codesprint: new tests for specfun directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
151 |
31026
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
152 function P = __perms_unique__ (v) |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
153 uvec = unique (v); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
154 len = numel (v); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
155 lenvec = (1:len); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
156 ulen = numel (uvec); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
157 |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
158 ## Calculate frequencies. Slightly faster to not use hist() or histc(). |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
159 for i = ulen:-1:1 |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
160 freq (i) = nnz (v == uvec(i)); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
161 endfor |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
162 |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
163 ## Performance is improved by handling the most frequent elements first. |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
164 ## This can mess with the output order though. Currently we do not promise |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
165 ## the results will be in any specific order. |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
166 [freq, order] = sort (freq, "descend"); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
167 uvec = uvec (order); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
168 |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
169 ## Call nchoosek for each choice and do a Cartesian set product, |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
170 ## avoiding collisions. |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
171 indx = nchoosek (lenvec, freq(1)); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
172 for i = 2:ulen |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
173 this = nchoosek (lenvec, freq(i)); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
174 |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
175 new = zeros (rows (indx) + 1e5, columns (indx) + columns (this)); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
176 ## Preallocate 1e5 extra rows. |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
177 pos = 0; |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
178 for j = 1:rows (this) |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
179 tmp = indx; |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
180 for k = this(j,:) |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
181 tmp = tmp (all (tmp ~= k, 2), :); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
182 endfor |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
183 |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
184 r = rows (tmp); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
185 c = columns (tmp); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
186 if (pos + r > rows(new)) # allocate more memory on the fly |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
187 new (pos+r+1e5, 1) = 0; |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
188 endif |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
189 |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
190 new ((pos+1):(pos+r), 1:c) = tmp; |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
191 new ((pos+1):(pos+r), (c+1):end) = repmat (this(j,:), r, 1); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
192 pos += r; |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
193 |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
194 # fprintf (1, "%d\t%d\t%d\t%d\t%d\t%f\n", i, j, r, pos, rows(new), toc); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
195 endfor |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
196 indx = new (1:pos, :); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
197 endfor |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
198 clear new tmp this # clear large variables before building P |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
199 |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
200 indx = (indx-1) * rows (indx) + (1:rows (indx))'; |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
201 |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
202 ## Initialize P to be the same size as indx with the same class as v. |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
203 P = repmat (v, rows(indx), 1); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
204 pos = 0; |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
205 for i = 1:ulen |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
206 P (indx (:, (pos + 1):(pos + freq(i)))) = uvec (i); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
207 pos += freq(i); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
208 endfor |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
209 |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
210 endfunction |
12815
918610ea2f34
codesprint: new tests for specfun directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
211 |
23250
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
212 %!assert (rows (perms (1:6)), factorial (6)) |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
213 %!assert (perms (pi), pi) |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
214 %!assert (perms ([pi, e]), [pi, e; e, pi]) |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
215 %!assert (perms ([1,2,3]), [3,2,1;3,1,2;2,3,1;2,1,3;1,3,2;1,2,3]) |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
216 %!assert (perms (1:5), perms ([2 5 4 1 3]')) |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
217 %!assert (perms ("abc"), char ("cba", "cab", "bca", "bac", "acb", "abc")) |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
218 %!assert (perms ("fobar"), sortrows (unique (perms ("fobar"), "rows"), -(1:5))) |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
219 %!assert (unique (perms (1:5)(:))', 1:5) |
b7da08507fae
perms.m: Recode for performance and Matlab-compatible output order (bug #50426)
Michael Leitner
parents:
23220
diff
changeset
|
220 %!assert (perms (int8 (1:4)), int8 (perms (1:4))) |
12815
918610ea2f34
codesprint: new tests for specfun directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
221 |
31026
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
222 %!assert (sortrows (perms ("abb", "unique")), ["abb"; "bab"; "bba"]); |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
223 %!assert (size (perms ([1 1 1 1 2 2 2 3 3], "unique")), [1260 9]) |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
224 %!assert (size (perms (int8([1 1 1 1 1 2 2 2 2 3 3 3]), "unique")), [27720 12]) |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
225 |
28886
d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
226 %!error <Invalid call> perms () |
31026
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
227 %!error <.*Second option must be the string.*> perms (1:5, "nonexistent") |
f03e1eebf46d
perms.m: Add new input option "unique" to return only unique permutations (bug #60364)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30875
diff
changeset
|
228 %!error <.*Second option must be the string.*> perms (1:5, {"foo"}) |
24267
ba51a6fd6a18
scripts/specfun/perms.m: add tests for non numeric data (bug #52431)
Carnë Draug <carandraug@octave.org>
parents:
23250
diff
changeset
|
229 |
ba51a6fd6a18
scripts/specfun/perms.m: add tests for non numeric data (bug #52431)
Carnë Draug <carandraug@octave.org>
parents:
23250
diff
changeset
|
230 ## Should work for any array type, such as cells and structs, and not |
ba51a6fd6a18
scripts/specfun/perms.m: add tests for non numeric data (bug #52431)
Carnë Draug <carandraug@octave.org>
parents:
23250
diff
changeset
|
231 ## only for numeric data. |
24274
c1940e9e3399
* perms.m: Split groups of tests to individual asserts.
John W. Eaton <jwe@octave.org>
parents:
24273
diff
changeset
|
232 |
26946
04e5cb5e2cb3
update bug status in tests
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
233 %!assert <*52431> (perms ({1}), {1}) |
04e5cb5e2cb3
update bug status in tests
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
234 %!assert <*52431> (perms ({0.1, "foo"}), {"foo", 0.1; 0.1, "foo"}) |
04e5cb5e2cb3
update bug status in tests
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
235 %!assert <*52431> (perms ({"foo", 0.1}), {0.1, "foo"; "foo", 0.1}) |
04e5cb5e2cb3
update bug status in tests
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
236 %!assert <*52431> (perms ({"foo"; 0.1}), {0.1, "foo"; "foo", 0.1}) |
04e5cb5e2cb3
update bug status in tests
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
237 %!assert <*52431> (perms ({0.1; "foo"}), {"foo", 0.1; 0.1, "foo"}) |
04e5cb5e2cb3
update bug status in tests
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
238 %!assert <*52431> (perms ({"foo", "bar"}), {"bar", "foo"; "foo", "bar"}) |
04e5cb5e2cb3
update bug status in tests
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
239 %!assert <*52431> (perms ({"bar", "foo"}), {"foo", "bar"; "bar", "foo"}) |
24267
ba51a6fd6a18
scripts/specfun/perms.m: add tests for non numeric data (bug #52431)
Carnë Draug <carandraug@octave.org>
parents:
23250
diff
changeset
|
240 %! |
26946
04e5cb5e2cb3
update bug status in tests
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
241 %!assert <*52431> (perms (struct ()), struct ()) |
04e5cb5e2cb3
update bug status in tests
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
242 %!assert <*52431> (perms (struct ("foo", {1, 2})), |
24274
c1940e9e3399
* perms.m: Split groups of tests to individual asserts.
John W. Eaton <jwe@octave.org>
parents:
24273
diff
changeset
|
243 %! struct ("foo", {2, 1; 1, 2})) |
26946
04e5cb5e2cb3
update bug status in tests
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
244 %!assert <*52431> (perms (struct ("foo", {1, 2}, "bar", {3, 4})), |
24274
c1940e9e3399
* perms.m: Split groups of tests to individual asserts.
John W. Eaton <jwe@octave.org>
parents:
24273
diff
changeset
|
245 %! struct ("foo", {2, 1; 1, 2}, "bar", {4, 3; 3, 4})) |
24268
94c4ebecca6e
scripts/specfun/perms.m: fix return type and size for empty arrays (bug #52432)
Carnë Draug <carandraug@octave.org>
parents:
24267
diff
changeset
|
246 |
24273
52023a3fe713
scripts/specfun/perms.m: support data of any indexable type (bug #52431)
Carnë Draug <carandraug@octave.org>
parents:
24268
diff
changeset
|
247 ## Also sort logical input with order dependent on the input order and |
52023a3fe713
scripts/specfun/perms.m: support data of any indexable type (bug #52431)
Carnë Draug <carandraug@octave.org>
parents:
24268
diff
changeset
|
248 ## not their values. |
24274
c1940e9e3399
* perms.m: Split groups of tests to individual asserts.
John W. Eaton <jwe@octave.org>
parents:
24273
diff
changeset
|
249 |
26946
04e5cb5e2cb3
update bug status in tests
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
250 %!assert <*52431> (perms (logical ([1 0])), logical ([0 1;, 1 0])) |
04e5cb5e2cb3
update bug status in tests
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
251 %!assert <*52431> (perms (logical ([0 1])), logical ([1 0; 0 1])) |
04e5cb5e2cb3
update bug status in tests
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
252 %!assert <*52431> (perms (logical ([0 1 0])), |
24274
c1940e9e3399
* perms.m: Split groups of tests to individual asserts.
John W. Eaton <jwe@octave.org>
parents:
24273
diff
changeset
|
253 %! logical ([0 1 0; 0 0 1; 1 0 0; 1 0 0; 0 0 1; 0 1 0])) |
26946
04e5cb5e2cb3
update bug status in tests
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
254 %!assert <*52431> (perms (logical ([0 1 1])), |
24274
c1940e9e3399
* perms.m: Split groups of tests to individual asserts.
John W. Eaton <jwe@octave.org>
parents:
24273
diff
changeset
|
255 %! logical ([1 1 0; 1 0 1; 1 1 0; 1 0 1; 0 1 1; 0 1 1])) |
c1940e9e3399
* perms.m: Split groups of tests to individual asserts.
John W. Eaton <jwe@octave.org>
parents:
24273
diff
changeset
|
256 |
26946
04e5cb5e2cb3
update bug status in tests
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
257 %!assert <*52432> (perms ([]), reshape ([], 1, 0)) |
04e5cb5e2cb3
update bug status in tests
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
258 %!assert <*52432> (perms (single ([])), reshape (single ([]), 1, 0)) |
04e5cb5e2cb3
update bug status in tests
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
259 %!assert <*52432> (perms (int8 ([])), reshape (int8 ([]), 1, 0)) |
04e5cb5e2cb3
update bug status in tests
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
260 %!assert <*52432> (perms ({}), cell (1, 0)) |
24273
52023a3fe713
scripts/specfun/perms.m: support data of any indexable type (bug #52431)
Carnë Draug <carandraug@octave.org>
parents:
24268
diff
changeset
|
261 |
26946
04e5cb5e2cb3
update bug status in tests
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
262 %!test <*52432> |
24268
94c4ebecca6e
scripts/specfun/perms.m: fix return type and size for empty arrays (bug #52432)
Carnë Draug <carandraug@octave.org>
parents:
24267
diff
changeset
|
263 %! s = struct (); |
94c4ebecca6e
scripts/specfun/perms.m: fix return type and size for empty arrays (bug #52432)
Carnë Draug <carandraug@octave.org>
parents:
24267
diff
changeset
|
264 %! s(1) = []; |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28891
diff
changeset
|
265 %! assert (perms (reshape (s, 0, 0)), reshape (s, 1, 0)); |
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28891
diff
changeset
|
266 %! assert (perms (reshape (s, 0, 1)), reshape (s, 1, 0)); |