annotate scripts/specfun/perms.m @ 10821:693e22af08ae

Grammarcheck documentation of m-files Add newlines between @item fields for readability.
author Rik <octave@nomad.inbox5.com>
date Mon, 26 Jul 2010 21:25:36 -0700
parents 804c21f3659b
children fd0a3ac60b0e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9245
16f53d29049f update copyright notices
John W. Eaton <jwe@octave.org>
parents: 9051
diff changeset
1 ## Copyright (C) 2001, 2006, 2007, 2009 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 ##
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6391
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
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
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
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
45 v = v(:);
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
46 n = length (v);
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)
804c21f3659b improve perms
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
49 A = [];
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
50 else
9836
804c21f3659b improve perms
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
51 A = v(1);
804c21f3659b improve perms
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
52 for j = 2:n
804c21f3659b improve perms
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
53 B = A;
804c21f3659b improve perms
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
54 A = zeros (prod (2:j), n, class (v));
804c21f3659b improve perms
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
55 k = size (B, 1);
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
804c21f3659b improve perms
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
58 A(idx,1:i-1) = B(:,1:i-1);
804c21f3659b improve perms
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
59 A(idx,i) = v(j);
804c21f3659b improve perms
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
60 A(idx,i+1:j) = B(:,i:j-1);
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
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
65 endfunction