Mercurial > octave
annotate scripts/general/bincoeff.m @ 28886:d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Remove BIST tests now that interpreter enforces calling function with no more than
the number of arguments defined in the function prototype.
* makeUniqueStrings.m, makeValidName.m, base64decode.m, base64encode.m,
record.m, sound.m, soundsc.m, acosd.m, acot.m, acotd.m, acoth.m, acsc.m,
acscd.m, acsch.m, asec.m, asecd.m, asech.m, asind.m, atan2d.m, atand.m, cosd.m,
cot.m, cotd.m, coth.m, csc.m, cscd.m, csch.m, sec.m, secd.m, sech.m, sind.m,
tand.m, accumdim.m, bincoeff.m, bitget.m, bitset.m, cell2mat.m, circshift.m,
cplxpair.m, cumtrapz.m, deg2rad.m, flip.m, fliplr.m, flipud.m, int2str.m,
interpft.m, logspace.m, num2str.m, postpad.m, prepad.m, rad2deg.m, rot90.m,
rotdim.m, shift.m, subsindex.m, trapz.m, rotx.m, roty.m, rotz.m, getappdata.m,
getpixelposition.m, guidata.m, isappdata.m, waitforbuttonpress.m,
__gripe_missing_component__.m, bessel.m, hsv2rgb.m, ind2gray.m, rgb2gray.m,
rgb2hsv.m, rgbplot.m, fileread.m, javachk.m, usejava.m, findstr.m,
genvarname.m, isdir.m, vectorize.m, cond.m, cross.m, duplication_matrix.m,
expm.m, isbanded.m, isdefinite.m, isdiag.m, ishermitian.m, issymmetric.m,
istril.m, istriu.m, trace.m, vech.m, vecnorm.m, cast.m, citation.m,
compare_versions.m, grabcode.m, inputname.m, isfile.m, isfolder.m, ismac.m,
ispc.m, isunix.m, list_primes.m, methods.m, news.m, open.m, run.m, swapbytes.m,
tar.m, verLessThan.m, zip.m, odeget.m, optimget.m, stemleaf.m, allchild.m,
ancestor.m, close.m, groot.m, hgsave.m, isaxes.m, isfigure.m, isgraphics.m,
ishandle.m, isprop.m, linkaxes.m, rotate.m, saveas.m, conv.m, poly.m,
polyint.m, polyreduce.m, ppval.m, roots.m, addpref.m, getpref.m, ispref.m,
rmpref.m, setpref.m, profile.m, profshow.m, powerset.m, autoreg_matrix.m,
bartlett.m, blackman.m, detrend.m, fftconv.m, fftfilt.m, fftshift.m, hamming.m,
hanning.m, ifftshift.m, sinewave.m, spectral_adf.m, spectral_xdf.m, unwrap.m,
nonzeros.m, spconvert.m, sprand.m, sprandn.m, beta.m, betainc.m, betaln.m,
cosint.m, expint.m, factorial.m, legendre.m, nchoosek.m, nthroot.m, perms.m,
pow2.m, primes.m, reallog.m, realpow.m, realsqrt.m, sinint.m, hankel.m, hilb.m,
invhilb.m, magic.m, pascal.m, toeplitz.m, vander.m, wilkinson.m, center.m,
corr.m, cov.m, discrete_cdf.m, discrete_inv.m, discrete_pdf.m, empirical_cdf.m,
empirical_inv.m, empirical_pdf.m, iqr.m, kendall.m, kurtosis.m, mad.m,
meansq.m, median.m, moment.m, prctile.m, quantile.m, range.m, run_count.m,
skewness.m, spearman.m, statistics.m, std.m, var.m, base2dec.m, bin2dec.m,
blanks.m, dec2base.m, dec2bin.m, dec2hex.m, endsWith.m, erase.m, hex2dec.m,
index.m, isletter.m, isstring.m, isstrprop.m, mat2str.m, ostrsplit.m, rindex.m,
startsWith.m, strtrunc.m, substr.m, fail.m, addtodate.m, asctime.m, ctime.m,
date.m, datestr.m, eomday.m, etime.m, is_leap_year.m, now.m:
remove unnecessary BIST tests in m-files checking for excessive number of inputs.
author | Rik <rik@octave.org> |
---|---|
date | Fri, 09 Oct 2020 13:28:53 -0700 |
parents | 9f9ac219896d |
children | 7854d5752dd2 |
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 ## |
27919
1891570abac8
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27918
diff
changeset
|
3 ## Copyright (C) 1995-2020 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/>. |
3426 | 7 ## |
3922 | 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 |
3922 | 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. |
3426 | 14 ## |
3922 | 15 ## Octave is distributed in the hope that it will be useful, but |
2538 | 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. |
3426 | 19 ## |
2538 | 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 ######################################################################## |
2538 | 25 |
3321 | 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 {} {} bincoeff (@var{n}, @var{k}) |
25106
d7ad543255c5
doc: Shorten very long first sentences of docstrings (bug #53388).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
28 ## Return the binomial coefficient of @var{n} and @var{k}. |
d7ad543255c5
doc: Shorten very long first sentences of docstrings (bug #53388).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
29 ## |
d7ad543255c5
doc: Shorten very long first sentences of docstrings (bug #53388).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
30 ## The binomial coefficient is defined as |
3321 | 31 ## @tex |
32 ## $$ | |
33 ## {n \choose k} = {n (n-1) (n-2) \cdots (n-k+1) \over k!} | |
34 ## $$ | |
35 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8435
diff
changeset
|
36 ## @ifnottex |
3426 | 37 ## |
3321 | 38 ## @example |
39 ## @group | |
40 ## / \ | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
41 ## | n | n (n-1) (n-2) @dots{} (n-k+1) |
3321 | 42 ## | | = ------------------------- |
43 ## | k | k! | |
44 ## \ / | |
45 ## @end group | |
46 ## @end example | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
47 ## |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8435
diff
changeset
|
48 ## @end ifnottex |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
49 ## For example: |
3426 | 50 ## |
3321 | 51 ## @example |
52 ## @group | |
53 ## bincoeff (5, 2) | |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
54 ## @result{} 10 |
3321 | 55 ## @end group |
56 ## @end example | |
8435
69e27978114a
bincoeff.m: make reference to nchoosek
Francesco Potortì <pot@gnu.org>
parents:
7385
diff
changeset
|
57 ## |
69e27978114a
bincoeff.m: make reference to nchoosek
Francesco Potortì <pot@gnu.org>
parents:
7385
diff
changeset
|
58 ## In most cases, the @code{nchoosek} function is faster for small |
69e27978114a
bincoeff.m: make reference to nchoosek
Francesco Potortì <pot@gnu.org>
parents:
7385
diff
changeset
|
59 ## scalar integer arguments. It also warns about loss of precision for |
69e27978114a
bincoeff.m: make reference to nchoosek
Francesco Potortì <pot@gnu.org>
parents:
7385
diff
changeset
|
60 ## big arguments. |
69e27978114a
bincoeff.m: make reference to nchoosek
Francesco Potortì <pot@gnu.org>
parents:
7385
diff
changeset
|
61 ## |
69e27978114a
bincoeff.m: make reference to nchoosek
Francesco Potortì <pot@gnu.org>
parents:
7385
diff
changeset
|
62 ## @seealso{nchoosek} |
3321 | 63 ## @end deftypefn |
2538 | 64 |
65 function b = bincoeff (n, k) | |
3426 | 66 |
2538 | 67 if (nargin != 2) |
6046 | 68 print_usage (); |
2538 | 69 endif |
3426 | 70 |
2538 | 71 [retval, n, k] = common_size (n, k); |
72 if (retval > 0) | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
73 error ("bincoeff: N and K must be of common size or scalars"); |
2538 | 74 endif |
3426 | 75 |
13169
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
76 if (iscomplex (n) || iscomplex (k)) |
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
77 error ("bincoeff: N and K must not be complex"); |
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
78 endif |
3426 | 79 |
13169
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
80 b = zeros (size (n)); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
81 |
13169
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
82 ok = (k >= 0) & (k == fix (k)) & (! isnan (n)); |
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
83 b(! ok) = NaN; |
2538 | 84 |
13169
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
85 n_int = (n == fix (n)); |
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
86 idx = n_int & (n < 0) & ok; |
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
87 b(idx) = (-1) .^ k(idx) .* exp (gammaln (abs (n(idx)) + k(idx)) |
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
88 - gammaln (k(idx) + 1) |
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
89 - gammaln (abs (n(idx)))); |
3426 | 90 |
13169
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
91 idx = (n >= k) & ok; |
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
92 b(idx) = exp (gammaln (n(idx) + 1) |
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
93 - gammaln (k(idx) + 1) |
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
94 - gammaln (n(idx) - k(idx) + 1)); |
6902 | 95 |
13169
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
96 idx = (! n_int) & (n < k) & ok; |
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
97 b(idx) = (1/pi) * exp (gammaln (n(idx) + 1) |
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
98 - gammaln (k(idx) + 1) |
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
99 + gammaln (k(idx) - n(idx)) |
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
100 + log (sin (pi * (n(idx) - k(idx) + 1)))); |
6902 | 101 |
102 ## Clean up rounding errors. | |
13169
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
103 b(n_int) = round (b(n_int)); |
6902 | 104 |
13169
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
105 idx = ! n_int; |
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
106 b(idx) = real (b(idx)); |
6902 | 107 |
2538 | 108 endfunction |
109 | |
13169
078729410a0d
bincoeff.m: 15% speed improvement and better input validation
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
110 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
111 %!assert (bincoeff (4, 2), 6) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
112 %!assert (bincoeff (2, 4), 0) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
113 %!assert (bincoeff (-4, 2), 10) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
114 %!assert (bincoeff (5, 2), 10) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
115 %!assert (bincoeff (50, 6), 15890700) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
116 %!assert (bincoeff (0.4, 2), -.12, 8*eps) |
7385 | 117 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
118 %!assert (bincoeff ([4 NaN 4], [-1, 2, 2.5]), NaN (1, 3)) |
7385 | 119 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
120 ## 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
|
121 %!error <Invalid call> bincoeff () |
d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents:
27985
diff
changeset
|
122 %!error <Invalid call> bincoeff (1) |
d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents:
27985
diff
changeset
|
123 %!error bincoeff (ones (3), ones (2)) |
d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents:
27985
diff
changeset
|
124 %!error bincoeff (ones (2), ones (3)) |