Mercurial > hg > octave-nkf
annotate scripts/specfun/perms.m @ 19002:492c56149535 stable rc-3-8-2-1
3.8.2 release candidate 1
* configure.ac (OCTAVE_VERSION): Bump to 3.8.2-rc1.
(OCTAVE_MINOR_VERSION): Bump to 2-rc1.
(OCTAVE_RELEASE_DATE): Set to 2014-06-06.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 06 Jun 2014 11:43:17 -0400 |
parents | d63878346099 |
children | bb20384acf7b |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17212
diff
changeset
|
1 ## Copyright (C) 2001-2013 Paul Kienzle |
9836 | 2 ## Copyright (C) 2009 VZLU Prague |
5827 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
5827 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5827 | 19 |
20 ## -*- texinfo -*- | |
21 ## @deftypefn {Function File} {} perms (@var{v}) | |
22 ## | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
23 ## Generate all permutations of @var{v}, one row per permutation. The |
5827 | 24 ## result has size @code{factorial (@var{n}) * @var{n}}, where @var{n} |
25 ## is the length of @var{v}. | |
26 ## | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
27 ## As an example, @code{perms ([1, 2, 3])} returns the matrix |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9836
diff
changeset
|
28 ## |
6754 | 29 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
30 ## @group |
6754 | 31 ## 1 2 3 |
32 ## 2 1 3 | |
33 ## 1 3 2 | |
34 ## 2 3 1 | |
35 ## 3 1 2 | |
36 ## 3 2 1 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
37 ## @end group |
6754 | 38 ## @end example |
5827 | 39 ## @end deftypefn |
40 | |
15899
f59797321a1b
perms.m: Match documentation variable names to function variable names.
Rik <rik@octave.org>
parents:
15564
diff
changeset
|
41 function A = perms (v) |
6391 | 42 if (nargin != 1) |
43 print_usage (); | |
44 endif | |
17212
9336d0e1d1ec
perms.m: Use uint8 to reduce memory usage by 1/8th.
Arun Giridha
parents:
15899
diff
changeset
|
45 vidx = uint8 ([1:length(v)]'); |
15899
f59797321a1b
perms.m: Match documentation variable names to function variable names.
Rik <rik@octave.org>
parents:
15564
diff
changeset
|
46 n = length (vidx); |
9836 | 47 |
48 if (n == 0) | |
15564
ed6385e23420
perms.m: make it work for string arguments
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14872
diff
changeset
|
49 p = []; |
5827 | 50 else |
15899
f59797321a1b
perms.m: Match documentation variable names to function variable names.
Rik <rik@octave.org>
parents:
15564
diff
changeset
|
51 p = vidx(1); |
9836 | 52 for j = 2:n |
15564
ed6385e23420
perms.m: make it work for string arguments
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14872
diff
changeset
|
53 B = p; |
17212
9336d0e1d1ec
perms.m: Use uint8 to reduce memory usage by 1/8th.
Arun Giridha
parents:
15899
diff
changeset
|
54 p = zeros (prod (2:j), n, "uint8"); |
14872
c2dbdeaa25df
maint: use rows() and columns() to clarify m-files.
Rik <octave@nomad.inbox5.com>
parents:
14868
diff
changeset
|
55 k = rows (B); |
9836 | 56 idx = 1:k; |
57 for i = j:-1:1 | |
15564
ed6385e23420
perms.m: make it work for string arguments
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14872
diff
changeset
|
58 p(idx,1:i-1) = B(:,1:i-1); |
15899
f59797321a1b
perms.m: Match documentation variable names to function variable names.
Rik <rik@octave.org>
parents:
15564
diff
changeset
|
59 p(idx,i) = vidx(j); |
15564
ed6385e23420
perms.m: make it work for string arguments
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14872
diff
changeset
|
60 p(idx,i+1:j) = B(:,i:j-1); |
9836 | 61 idx += k; |
62 endfor | |
5827 | 63 endfor |
64 endif | |
15899
f59797321a1b
perms.m: Match documentation variable names to function variable names.
Rik <rik@octave.org>
parents:
15564
diff
changeset
|
65 A = v(p); |
5827 | 66 endfunction |
12815
918610ea2f34
codesprint: new tests for specfun directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
67 |
918610ea2f34
codesprint: new tests for specfun directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
68 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
69 %!assert (perms ([1,2,3]), [1,2,3;2,1,3;1,3,2;2,3,1;3,1,2;3,2,1]) |
15564
ed6385e23420
perms.m: make it work for string arguments
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14872
diff
changeset
|
70 %!assert (perms ("abc"), ["abc"; "bac"; "acb"; "bca"; "cab"; "cba"]) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
71 %!assert (perms (int8 ([1,2,3])), int8 ([1,2,3;2,1,3;1,3,2;2,3,1;3,1,2;3,2,1])) |
12815
918610ea2f34
codesprint: new tests for specfun directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
72 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
73 %!error perms () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
74 %!error perms (1, 2) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
75 |