annotate scripts/specfun/nchoosek.m @ 30204:1cd077e9f127

allow nchoosek to accept character vectors (bug #61199) * nchoosek.m: Allow input of possible values to be a character array. Ensure output is the same type as array of possible values. New tests.
author John W. Eaton <jwe@octave.org>
date Thu, 23 Sep 2021 00:00:12 -0400
parents 7854d5752dd2
children 23a907b2dbd5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 ##
29358
0a5b15007766 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27985
diff changeset
3 ## Copyright (C) 2001-2021 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
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
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
7 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
8 ## This file is part of Octave.
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
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: 23220
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
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
14 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
15 ## 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
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
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 ## 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
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: 23220
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
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
25
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 19833
diff changeset
27 ## @deftypefn {} {@var{c} =} nchoosek (@var{n}, @var{k})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 19833
diff changeset
28 ## @deftypefnx {} {@var{c} =} nchoosek (@var{set}, @var{k})
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
29 ##
19059
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
30 ## Compute the binomial coefficient of @var{n} or list all possible
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
31 ## combinations of a @var{set} of items.
14090
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
32 ##
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
33 ## If @var{n} is a scalar then calculate the binomial coefficient
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
34 ## of @var{n} and @var{k} which is defined as
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
35 ## @tex
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
36 ## $$
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
37 ## {n \choose k} = {n (n-1) (n-2) \cdots (n-k+1) \over k!}
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6575
diff changeset
38 ## = {n! \over k! (n-k)!}
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
39 ## $$
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
40 ## @end tex
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 8506
diff changeset
41 ## @ifnottex
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
42 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
43 ## @example
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
44 ## @group
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
45 ## / \
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
46 ## | n | n (n-1) (n-2) @dots{} (n-k+1) n!
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6575
diff changeset
47 ## | | = ------------------------- = ---------
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6575
diff changeset
48 ## | k | k! k! (n-k)!
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
49 ## \ /
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
50 ## @end group
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
51 ## @end example
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10800
diff changeset
52 ##
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 8506
diff changeset
53 ## @end ifnottex
14090
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
54 ## @noindent
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
55 ## This is the number of combinations of @var{n} items taken in groups of
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
56 ## size @var{k}.
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
57 ##
14093
050bc580cb60 doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 14090
diff changeset
58 ## If the first argument is a vector, @var{set}, then generate all
050bc580cb60 doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 14090
diff changeset
59 ## combinations of the elements of @var{set}, taken @var{k} at a time, with
050bc580cb60 doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 14090
diff changeset
60 ## one row per combination. The result @var{c} has @var{k} columns and
14090
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
61 ## @w{@code{nchoosek (length (@var{set}), @var{k})}} rows.
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
62 ##
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
63 ## For example:
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
64 ##
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
65 ## How many ways can three items be grouped into pairs?
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
66 ##
14090
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
67 ## @example
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
68 ## @group
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
69 ## nchoosek (3, 2)
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
70 ## @result{} 3
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
71 ## @end group
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
72 ## @end example
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
73 ##
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
74 ## What are the possible pairs?
8404
868149aac690 nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents: 8391
diff changeset
75 ##
14090
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
76 ## @example
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
77 ## @group
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
78 ## nchoosek (1:3, 2)
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
79 ## @result{} 1 2
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
80 ## 1 3
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
81 ## 2 3
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
82 ## @end group
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
83 ## @end example
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
84 ##
19059
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
85 ## Programming Note: When calculating the binomial coefficient @code{nchoosek}
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
86 ## works only for non-negative, integer arguments. Use @code{bincoeff} for
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
87 ## non-integer and negative scalar arguments, or for computing many binomial
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
88 ## coefficients at once with vector inputs for @var{n} or @var{k}.
14090
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
89 ##
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
90 ## @seealso{bincoeff, perms}
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
91 ## @end deftypefn
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
92
19059
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
93 function C = nchoosek (v, k)
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
94
8404
868149aac690 nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents: 8391
diff changeset
95 if (nargin != 2
19059
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
96 || ! (isreal (k) && isscalar (k))
30204
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
97 || ! ((isnumeric (v) || ischar (v)) && isvector (v)))
6316
a3a2580435c2 [project @ 2007-02-16 07:23:49 by jwe]
jwe
parents: 5827
diff changeset
98 print_usage ();
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
99 endif
19059
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
100 if (k < 0 || k != fix (k))
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
101 error ("nchoosek: K must be an integer >= 0");
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
102 elseif (isscalar (v) && (iscomplex (v) || v < k || v < 0 || v != fix (v)))
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
103 error ("nchoosek: N must be a non-negative integer >= K");
8404
868149aac690 nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents: 8391
diff changeset
104 endif
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
105
6318
bae85c1e0e2a [project @ 2007-02-16 08:15:25 by jwe]
jwe
parents: 6316
diff changeset
106 n = length (v);
bae85c1e0e2a [project @ 2007-02-16 08:15:25 by jwe]
jwe
parents: 6316
diff changeset
107
bae85c1e0e2a [project @ 2007-02-16 08:15:25 by jwe]
jwe
parents: 6316
diff changeset
108 if (n == 1)
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8404
diff changeset
109 ## Improve precision at next step.
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8404
diff changeset
110 k = min (k, v-k);
19059
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
111 C = round (prod ((v-k+1:v)./(1:k)));
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
112 if (C*2*k*eps >= 0.5)
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
113 warning ("nchoosek: possible loss of precision");
8391
343f0fbca6eb implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents: 8361
diff changeset
114 endif
8404
868149aac690 nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents: 8391
diff changeset
115 elseif (k == 0)
30204
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
116 if (is_sq_string (v))
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
117 C = resize ('', 1, 0);
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
118 elseif (is_dq_string (v))
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
119 C = resize ("", 1, 0);
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
120 else
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
121 C = zeros (1, 0, class (v));
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
122 endif
6318
bae85c1e0e2a [project @ 2007-02-16 08:15:25 by jwe]
jwe
parents: 6316
diff changeset
123 elseif (k == 1)
19059
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
124 C = v(:);
8404
868149aac690 nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents: 8391
diff changeset
125 elseif (k == n)
19059
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
126 C = v(:).';
8391
343f0fbca6eb implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents: 8361
diff changeset
127 elseif (k > n)
30204
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
128 if (is_sq_string (v))
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
129 C = resize ('', 0, k);
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
130 elseif (is_dq_string (v))
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
131 C = resize ("", 0, k);
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
132 else
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
133 C = zeros (0, k, class (v));
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
134 endif
10800
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
135 elseif (k == 2)
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
136 ## Can do it without transpose.
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
137 x = repelems (v(1:n-1), [1:n-1; n-1:-1:1]).';
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
138 y = cat (1, cellslices (v(:), 2:n, n*ones (1, n-1)){:});
19059
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
139 C = [x, y];
10800
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
140 elseif (k < n)
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
141 v = v(:).';
19059
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
142 C = v(k:n);
10800
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
143 l = 1:n-k+1;
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
144 for j = 2:k
19059
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
145 c = columns (C);
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
146 cA = cellslices (C, l, c*ones (1, n-k+1), 2);
10800
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
147 l = c-l+1;
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
148 b = repelems (v(k-j+1:n-j+1), [1:n-k+1; l]);
19059
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
149 C = [b; cA{:}];
10800
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
150 l = cumsum (l);
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
151 l = [1, 1 + l(1:n-k)];
8391
343f0fbca6eb implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents: 8361
diff changeset
152 endfor
19059
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
153 C = C.';
6318
bae85c1e0e2a [project @ 2007-02-16 08:15:25 by jwe]
jwe
parents: 6316
diff changeset
154 endif
19059
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
155
8361
cf620941af1a Set max_recursion_depth and use a subfunction in nchoosek
Francesco Potortì <pot@gnu.org>
parents: 7017
diff changeset
156 endfunction
6318
bae85c1e0e2a [project @ 2007-02-16 08:15:25 by jwe]
jwe
parents: 6316
diff changeset
157
14090
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
158
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
159 %!assert (nchoosek (80,10), bincoeff (80,10))
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
160 %!assert (nchoosek (1:5,3), [1:3;1,2,4;1,2,5;1,3,4;1,3,5;1,4,5;2:4;2,3,5;2,4,5;3:5])
18795
1514f5337781 nchoosek.m: nchoosek(N,0) now returns [](1x0) when N is a vector (bug #41890).
Pedro Angelo <fonini@poli.ufrj.br>
parents: 17744
diff changeset
161 %!assert (size (nchoosek (1:5,0)), [1 0])
14090
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
162
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
163 ## Test input validation
28886
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 27985
diff changeset
164 %!error <Invalid call> nchoosek ()
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 27985
diff changeset
165 %!error <Invalid call> nchoosek (1)
19059
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
166
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
167 %!error nchoosek (100, 2i)
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
168 %!error nchoosek (100, [2 3])
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
169 %!error nchoosek (100*ones (2, 2), 45)
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
170 %!error <K must be an integer .= 0> nchoosek (100, -45)
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
171 %!error <K must be an integer .= 0> nchoosek (100, 45.5)
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
172 %!error <N must be a non-negative integer .= K> nchoosek (100i, 2)
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
173 %!error <N must be a non-negative integer .= K> nchoosek (100, 145)
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
174 %!error <N must be a non-negative integer .= K> nchoosek (-100, 45)
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
175 %!error <N must be a non-negative integer .= K> nchoosek (100.5, 45)
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18795
diff changeset
176 %!warning <possible loss of precision> nchoosek (100, 45);
30204
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
177
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
178 %!assert (nchoosek ('a':'b', 2), 'ab')
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
179 %!assert (nchoosek ("a":"b", 2), "ab")
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
180
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
181 %!test
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
182 %! x = nchoosek ('a':'b', 3);
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
183 %! assert (size (x), [0, 3]);
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
184 %! assert (is_sq_string (x));
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
185 %! x = nchoosek ('a':'b', 0);
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
186 %! assert (size (x), [1, 0]);
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
187 %! assert (is_sq_string (x));
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
188 %!
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
189 %! x = nchoosek ("a":"b", 3);
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
190 %! assert (size (x), [0, 3]);
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
191 %! assert (is_dq_string (x));
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
192 %! x = nchoosek ("a":"b", 0);
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
193 %! assert (size (x), [1, 0]);
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
194 %! assert (is_dq_string (x));
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
195 %!
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
196 %! x = nchoosek (uint8(1):uint8(2), 3);
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
197 %! assert (size (x), [0, 3]);
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
198 %! assert (class (x), "uint8");
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
199 %! x = nchoosek (uint8(1):uint8(2), 0);
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
200 %! assert (size (x), [1, 0]);
1cd077e9f127 allow nchoosek to accept character vectors (bug #61199)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
201 %! assert (class (x), "uint8");