Mercurial > octave
annotate scripts/specfun/nchoosek.m @ 30206:aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
* nchoosek.m: Change input validation to avoid numeric checks if input V is
scalar but not a number. Add BIST tests for exceptional values of scalar
structs and struct arrays.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 23 Sep 2021 12:19:54 -0700 |
parents | 23a907b2dbd5 |
children | 54774a713d7c |
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 | 7 ## |
8 ## This file is part of Octave. | |
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 | 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 | 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:
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 | 25 |
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 | 29 ## |
19059 | 30 ## Compute the binomial coefficient of @var{n} or list all possible |
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 | 35 ## @tex |
36 ## $$ | |
37 ## {n \choose k} = {n (n-1) (n-2) \cdots (n-k+1) \over k!} | |
6754 | 38 ## = {n! \over k! (n-k)!} |
5827 | 39 ## $$ |
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 | 42 ## |
43 ## @example | |
44 ## @group | |
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 | 47 ## | | = ------------------------- = --------- |
48 ## | k | k! k! (n-k)! | |
5827 | 49 ## \ / |
50 ## @end group | |
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 | 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 | 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 | 85 ## Programming Note: When calculating the binomial coefficient @code{nchoosek} |
86 ## works only for non-negative, integer arguments. Use @code{bincoeff} for | |
87 ## non-integer and negative scalar arguments, or for computing many binomial | |
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 | 91 ## @end deftypefn |
92 | |
19059 | 93 function C = nchoosek (v, k) |
5827 | 94 |
30205
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
95 if (nargin != 2) |
6316 | 96 print_usage (); |
5827 | 97 endif |
30205
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
98 |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
99 if (! isvector (v)) |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
100 error ("nchoosek: first argument must be a scalar or a vector"); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
101 endif |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
102 if (! (isreal (k) && isscalar (k) && k >= 0 && k == fix (k))) |
19059 | 103 error ("nchoosek: K must be an integer >= 0"); |
30205
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
104 endif |
30206
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
105 if (isscalar (v)) |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
106 if (isnumeric (v) && (iscomplex (v) || v < k || v < 0 || v != fix (v))) |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
107 error ("nchoosek: N must be a non-negative integer >= K"); |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
108 endif |
8404
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
109 endif |
5827 | 110 |
30205
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
111 n = numel (v); |
6318 | 112 |
30205
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
113 if (n == 1 && isnumeric (v)) |
8506 | 114 ## Improve precision at next step. |
115 k = min (k, v-k); | |
19059 | 116 C = round (prod ((v-k+1:v)./(1:k))); |
117 if (C*2*k*eps >= 0.5) | |
118 warning ("nchoosek: possible loss of precision"); | |
8391
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
119 endif |
8404
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
120 elseif (k == 0) |
30205
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
121 C = v(zeros (1, 0)); # Return 1x0 object for Matlab compatibility |
6318 | 122 elseif (k == 1) |
19059 | 123 C = v(:); |
8404
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
124 elseif (k == n) |
19059 | 125 C = v(:).'; |
8391
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
126 elseif (k > n) |
30205
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
127 C = v(zeros (0, k)); # return 0xk object for Matlab compatibility |
10800 | 128 elseif (k == 2) |
129 ## Can do it without transpose. | |
130 x = repelems (v(1:n-1), [1:n-1; n-1:-1:1]).'; | |
131 y = cat (1, cellslices (v(:), 2:n, n*ones (1, n-1)){:}); | |
19059 | 132 C = [x, y]; |
10800 | 133 elseif (k < n) |
134 v = v(:).'; | |
19059 | 135 C = v(k:n); |
10800 | 136 l = 1:n-k+1; |
137 for j = 2:k | |
19059 | 138 c = columns (C); |
139 cA = cellslices (C, l, c*ones (1, n-k+1), 2); | |
10800 | 140 l = c-l+1; |
141 b = repelems (v(k-j+1:n-j+1), [1:n-k+1; l]); | |
19059 | 142 C = [b; cA{:}]; |
10800 | 143 l = cumsum (l); |
144 l = [1, 1 + l(1:n-k)]; | |
8391
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
145 endfor |
19059 | 146 C = C.'; |
6318 | 147 endif |
19059 | 148 |
8361
cf620941af1a
Set max_recursion_depth and use a subfunction in nchoosek
Francesco Potortì <pot@gnu.org>
parents:
7017
diff
changeset
|
149 endfunction |
6318 | 150 |
14090
281ecc6fb431
nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents:
14062
diff
changeset
|
151 |
30205
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
152 %!assert (nchoosek (80, 10), bincoeff (80, 10)) |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
153 %!assert (nchoosek (1:5, 3), |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
154 %! [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]) |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
155 |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
156 # Test basic behavior for various input types |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
157 %!assert (nchoosek ('a':'b', 2), 'ab') |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
158 %!assert (nchoosek ("a":"b", 2), "ab") |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
159 %!assert (nchoosek ({1,2}, 2), {1,2}) |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
160 %!test |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
161 %! s(1).a = 1; |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
162 %! s(2).a = 2; |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
163 %! assert (nchoosek (s, 1), s(:)); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
164 %! assert (nchoosek (s, 2), s); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
165 |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
166 # Verify Matlab compatibility of return sizes & types |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
167 %!test |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
168 %! x = nchoosek (1:2, 0); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
169 %! assert (size (x), [1, 0]); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
170 %! assert (isa (x, "double")); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
171 %! x = nchoosek (1:2, 3); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
172 %! assert (size (x), [0, 3]); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
173 %! assert (isa (x, "double")); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
174 |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
175 %!test |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
176 %! x = nchoosek (single (1:2), 0); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
177 %! assert (size (x), [1, 0]); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
178 %! assert (isa (x, "single")); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
179 %! x = nchoosek (single (1:2), 3); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
180 %! assert (size (x), [0, 3]); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
181 %! assert (isa (x, "single")); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
182 |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
183 %!test |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
184 %! x = nchoosek ('a':'b', 0); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
185 %! assert (size (x), [1, 0]); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
186 %! assert (is_sq_string (x)); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
187 %! x = nchoosek ('a':'b', 3); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
188 %! assert (size (x), [0, 3]); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
189 %! assert (is_sq_string (x)); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
190 |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
191 %!test |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
192 %! x = nchoosek ("a":"b", 0); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
193 %! assert (size (x), [1, 0]); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
194 %! assert (is_dq_string (x)); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
195 %! x = nchoosek ("a":"b", 3); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
196 %! assert (size (x), [0, 3]); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
197 %! assert (is_dq_string (x)); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
198 |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
199 %!test |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
200 %! x = nchoosek (uint8(1):uint8(2), 0); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
201 %! assert (size (x), [1, 0]); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
202 %! assert (isa (x, "uint8")); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
203 %! x = nchoosek (uint8(1):uint8(2), 3); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
204 %! assert (size (x), [0, 3]); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
205 %! assert (isa (x, "uint8")); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
206 |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
207 %!test |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
208 %! x = nchoosek ({1, 2}, 0); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
209 %! assert (size (x), [1, 0]); |
30206
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
210 %! assert (iscell (x)); |
30205
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
211 %! x = nchoosek ({1, 2}, 3); |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
212 %! assert (size (x), [0, 3]); |
30206
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
213 %! assert (iscell (x)); |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
214 |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
215 %!test |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
216 %! s.a = [1 2 3]; |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
217 %! s.b = [4 5 6]; |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
218 %! x = nchoosek (s, 0); |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
219 %! assert (size (x), [1, 0]); |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
220 %! assert (isstruct (x)); |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
221 %! assert (fieldnames (x), {"a"; "b"}); |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
222 %! x = nchoosek (s, 3); |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
223 %! assert (size (x), [0, 3]); |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
224 %! assert (isstruct (x)); |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
225 %! assert (fieldnames (x), {"a"; "b"}); |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
226 |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
227 %!test |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
228 %! s.a = [1 2 3]; |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
229 %! s.b = [4 5 6]; |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
230 %! s(2).a = 1; # make s a struct array rather than scalar struct |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
231 %! x = nchoosek (s, 0); |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
232 %! assert (size (x), [1, 0]); |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
233 %! assert (isstruct (x)); |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
234 %! assert (fieldnames (x), {"a"; "b"}); |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
235 %! x = nchoosek (s, 3); |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
236 %! assert (size (x), [0, 3]); |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
237 %! assert (isstruct (x)); |
aaee7b170cb1
nchoosek.m: Allow struct inputs (bug #61119)
Rik <rik@octave.org>
parents:
30205
diff
changeset
|
238 %! assert (fieldnames (x), {"a"; "b"}); |
14090
281ecc6fb431
nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents:
14062
diff
changeset
|
239 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
240 ## 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
|
241 %!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
|
242 %!error <Invalid call> nchoosek (1) |
30205
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
243 %!error <first argument must be a scalar or a vector> nchoosek (ones (3, 3), 1) |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
244 %!error <K must be an integer .= 0> nchoosek (100, 2i) |
23a907b2dbd5
nchoosek.m: Allow "char" and other non-numeric inputs (bug #61119)
Rik <rik@octave.org>
parents:
30204
diff
changeset
|
245 %!error <K must be an integer .= 0> nchoosek (100, [2 3]) |
19059 | 246 %!error <K must be an integer .= 0> nchoosek (100, -45) |
247 %!error <K must be an integer .= 0> nchoosek (100, 45.5) | |
248 %!error <N must be a non-negative integer .= K> nchoosek (100i, 2) | |
249 %!error <N must be a non-negative integer .= K> nchoosek (100, 145) | |
250 %!error <N must be a non-negative integer .= K> nchoosek (-100, 45) | |
251 %!error <N must be a non-negative integer .= K> nchoosek (100.5, 45) | |
252 %!warning <possible loss of precision> nchoosek (100, 45); |