annotate scripts/specfun/perms.m @ 16033:23a7661e529a ss-3-7-2

snapshot version 3.7.2 * configure.ac (AC_INIT): Set version to 3.7.2. (OCTAVE_RELEASE_DATE): Set to 2013-02-09. (OCTAVE_COPYRIGHT): Update year.
author John W. Eaton <jwe@octave.org>
date Sun, 10 Feb 2013 00:59:19 -0500
parents f59797321a1b
children 9336d0e1d1ec
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 12815
diff changeset
1 ## Copyright (C) 2001-2012 Paul Kienzle
9836
804c21f3659b improve perms
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
2 ## Copyright (C) 2009 VZLU Prague
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
3 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
5 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
7 ## 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: 6754
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
9 ## your option) any later version.
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
10 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
14 ## General Public License for more details.
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
15 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
16 ## 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: 6754
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
18 ## <http://www.gnu.org/licenses/>.
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
19
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
21 ## @deftypefn {Function File} {} perms (@var{v})
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
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
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
24 ## result has size @code{factorial (@var{n}) * @var{n}}, where @var{n}
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
25 ## is the length of @var{v}.
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
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
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6391
diff changeset
29 ## @example
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 7017
diff changeset
30 ## @group
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6391
diff changeset
31 ## 1 2 3
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6391
diff changeset
32 ## 2 1 3
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6391
diff changeset
33 ## 1 3 2
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6391
diff changeset
34 ## 2 3 1
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6391
diff changeset
35 ## 3 1 2
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6391
diff changeset
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
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6391
diff changeset
38 ## @end example
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
39 ## @end deftypefn
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
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
3f3e86e9fb57 [project @ 2007-03-07 18:14:44 by jwe]
jwe
parents: 5827
diff changeset
42 if (nargin != 1)
3f3e86e9fb57 [project @ 2007-03-07 18:14:44 by jwe]
jwe
parents: 5827
diff changeset
43 print_usage ();
3f3e86e9fb57 [project @ 2007-03-07 18:14:44 by jwe]
jwe
parents: 5827
diff changeset
44 endif
15899
f59797321a1b perms.m: Match documentation variable names to function variable names.
Rik <rik@octave.org>
parents: 15564
diff changeset
45 vidx = [1:length(v)]';
f59797321a1b perms.m: Match documentation variable names to function variable names.
Rik <rik@octave.org>
parents: 15564
diff changeset
46 n = length (vidx);
9836
804c21f3659b improve perms
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
47
804c21f3659b improve perms
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
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
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
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
804c21f3659b improve perms
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
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;
ed6385e23420 perms.m: make it work for string arguments
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14872
diff changeset
54 p = zeros (prod (2:j), n);
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
804c21f3659b improve perms
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
56 idx = 1:k;
804c21f3659b improve perms
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
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
804c21f3659b improve perms
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
61 idx += k;
804c21f3659b improve perms
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
62 endfor
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
63 endfor
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
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
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
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