annotate scripts/specfun/betainc.m @ 31551:fd29c7a50a78 stable

maint: use commas, semicolons consistently with Octave conventions. * makeValidName.m: Remove %!test and move BIST %!asserts to column 1. * base64decode.m, base64encode.m, which.m, logm.m, uniquetol.m, perms.m: Delete semicolon (';') at end of %!assert BIST. * lin2mu.m, interp2.m, interpn.m, lsqnonneg.m, pqpnonneg.m, uniquetol.m, betainc.m, normalize.m: Add semicolon (';') to end of assert statement within %!test BIST. * __memoize__.m, tar_is_bsd.m, publish.m: Add semicolon (';') to line with keyword "persistent". * stft.m: Use comma (',') after "case" keyword when code immediately follows. * gallery.m: Align commas used in case statements in massive switch block. Remove unnecessary parentheses around a numeric case argument. * ranks.m: Remove semicolon (';') from case statemnt argument.
author Rik <rik@octave.org>
date Sat, 26 Nov 2022 06:32:08 -0800
parents 74f8854958d2
children 597f3ee61a48
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 ##
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 30400
diff changeset
3 ## Copyright (C) 2018-2022 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26946
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/>.
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
7 ##
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
8 ## This file is part of Octave.
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
9 ##
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
28944
d39b09b4c5db maint: Use identical Copyright text in scripts/.
Rik <rik@octave.org>
parents: 28907
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
13 ## (at your option) any later version.
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
14 ##
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
18 ## GNU General Public License for more details.
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
19 ##
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
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 ########################################################################
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
25
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
26 ## -*- texinfo -*-
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
27 ## @deftypefn {} {@var{I} =} betainc (@var{x}, @var{a}, @var{b})
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
28 ## @deftypefnx {} {@var{I} =} betainc (@var{x}, @var{a}, @var{b}, @var{tail})
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
29 ## Compute the incomplete beta function.
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
30 ##
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
31 ## This is defined as
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
32 ## @tex
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
33 ## $$
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
34 ## I_x (a, b) = {1 \over {B(a,b)}} \displaystyle{\int_0^x t^{a-1} (1-t)^{b-1} dt}
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
35 ## $$
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
36 ## @end tex
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
37 ## @ifnottex
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
38 ##
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
39 ## @example
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
40 ## @group
29664
2a1f57067fbf betainc.m, betaincinv.m: Correct non-Tex definition of beta incomplete integral.
Rik <rik@octave.org>
parents: 29358
diff changeset
41 ## x
2a1f57067fbf betainc.m, betaincinv.m: Correct non-Tex definition of beta incomplete integral.
Rik <rik@octave.org>
parents: 29358
diff changeset
42 ## /
2a1f57067fbf betainc.m, betaincinv.m: Correct non-Tex definition of beta incomplete integral.
Rik <rik@octave.org>
parents: 29358
diff changeset
43 ## 1 |
2a1f57067fbf betainc.m, betaincinv.m: Correct non-Tex definition of beta incomplete integral.
Rik <rik@octave.org>
parents: 29358
diff changeset
44 ## I_x (a, b) = ---------- | t^(a-1) (1-t)^(b-1) dt
2a1f57067fbf betainc.m, betaincinv.m: Correct non-Tex definition of beta incomplete integral.
Rik <rik@octave.org>
parents: 29358
diff changeset
45 ## beta (a,b) |
2a1f57067fbf betainc.m, betaincinv.m: Correct non-Tex definition of beta incomplete integral.
Rik <rik@octave.org>
parents: 29358
diff changeset
46 ## /
2a1f57067fbf betainc.m, betaincinv.m: Correct non-Tex definition of beta incomplete integral.
Rik <rik@octave.org>
parents: 29358
diff changeset
47 ## 0
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
48 ## @end group
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
49 ## @end example
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
50 ##
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
51 ## @end ifnottex
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
52 ##
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
53 ## with real @var{x} in the range [0,1]. The inputs @var{a} and @var{b} must
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
54 ## be real and strictly positive (> 0). If one of the inputs is not a scalar
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
55 ## then the other inputs must be scalar or of compatible dimensions.
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
56 ##
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
57 ## By default, @var{tail} is @qcode{"lower"} and the incomplete beta function
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
58 ## integrated from 0 to @var{x} is computed. If @var{tail} is @qcode{"upper"}
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
59 ## then the complementary function integrated from @var{x} to 1 is calculated.
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
60 ## The two choices are related by
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
61 ##
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
62 ## betainc (@var{x}, @var{a}, @var{b}, @qcode{"upper"}) =
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
63 ## 1 - betainc (@var{x}, @var{a}, @var{b}, @qcode{"lower"}).
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
64 ##
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
65 ## @code{betainc} uses a more sophisticated algorithm than subtraction to
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
66 ## get numerically accurate results when the @qcode{"lower"} value is small.
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
67 ##
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
68 ## Reference: @nospell{A. Cuyt, V. Brevik Petersen, B. Verdonk, H. Waadeland,
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
69 ## W.B. Jones}, @cite{Handbook of Continued Fractions for Special Functions},
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
70 ## ch.@: 18.
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
71 ##
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
72 ## @seealso{beta, betaincinv, betaln}
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
73 ## @end deftypefn
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
74
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
75 function I = betainc (x, a, b, tail = "lower")
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
76
28789
28de41192f3c Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents: 27923
diff changeset
77 if (nargin < 3)
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
78 print_usage ();
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
79 endif
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
80
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
81 [err, x, a, b] = common_size (x, a, b);
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
82 if (err > 0)
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
83 error ("betainc: X, A, and B must be of common size or scalars");
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
84 endif
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
85
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
86 if (iscomplex (x) || iscomplex (a) || iscomplex (b))
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
87 error ("betainc: all inputs must be real");
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
88 endif
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
89
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
90 ## Remember original shape of data, but convert to column vector for calcs.
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
91 orig_sz = size (x);
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
92 x = x(:);
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
93 a = a(:);
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
94 b = b(:);
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
95
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
96 if (any ((x < 0) | (x > 1)))
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
97 error ("betainc: X must be in the range [0, 1]");
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
98 endif
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
99
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
100 if (any (a <= 0))
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
101 error ("betainc: A must be strictly positive");
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
102 endif
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
103
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
104 if (any (b <= 0))
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
105 error ("betainc: B must be strictly positive");
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
106 endif
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
107
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
108 ## If any of the arguments is single then the output should be as well.
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
109 if (strcmp (class (x), "single") || strcmp (class (a), "single")
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
110 || strcmp (class (b), "single"))
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
111 a = single (a);
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
112 b = single (b);
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
113 x = single (x);
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
114 endif
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
115
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
116 ## Convert to floating point if necessary
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
117 if (isinteger (x))
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
118 I = double (x);
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
119 endif
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
120 if (isinteger (a))
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
121 a = double (a);
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
122 endif
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
123 if (isinteger (b))
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
124 b = double (b);
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
125 endif
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
126
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
127 ## Initialize output array
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
128 I = zeros (size (x), class (x));
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
129
30016
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
130 ## Trivial cases (long code here trades memory for speed)
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
131 a_one = (a == 1);
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
132 b_one = (b == 1);
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
133 a_b_one = a_one & b_one;
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
134 a_not_one = ! a_one;
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
135 b_not_one = ! b_one;
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
136 non_trivial = a_not_one & b_not_one;
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
137 a_one &= b_not_one;
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
138 b_one &= a_not_one;
30400
168da23530b4 maint: strip trailing spaces from code base.
Rik <rik@octave.org>
parents: 30330
diff changeset
139
30016
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
140 if (strcmpi (tail, "lower"))
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
141 I(a_b_one) = x(a_b_one);
30947
fe898ae23e0e betainc.m: Use sophisticated technique for calculating exponents to avoid innacuracies (bug #62329)
Nir Krakauer <nkrakauer@ccny.cuny.edu>
parents: 30564
diff changeset
142 ## See bug #62329.
fe898ae23e0e betainc.m: Use sophisticated technique for calculating exponents to avoid innacuracies (bug #62329)
Nir Krakauer <nkrakauer@ccny.cuny.edu>
parents: 30564
diff changeset
143 ## equivalent to "1 - (1 - x(a_one)) .^ b(a_one)", but less roundoff error
30948
74f8854958d2 maint: merge stable to default.
Rik <rik@octave.org>
parents: 30875 30947
diff changeset
144 I(a_one) = - expm1 (log1p (- x(a_one)) .* b(a_one));
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
145 I(b_one) = x(b_one) .^ a(b_one);
30016
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
146 elseif (strcmpi (tail, "upper"))
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
147 I(a_b_one) = 1 - x(a_b_one);
30947
fe898ae23e0e betainc.m: Use sophisticated technique for calculating exponents to avoid innacuracies (bug #62329)
Nir Krakauer <nkrakauer@ccny.cuny.edu>
parents: 30564
diff changeset
148 ## equivalent to "(1 - x(a_one)) .^ b(a_one)", but less roundoff error
30948
74f8854958d2 maint: merge stable to default.
Rik <rik@octave.org>
parents: 30875 30947
diff changeset
149 I(a_one) = exp (log1p (- x(a_one)) .* b(a_one));
30947
fe898ae23e0e betainc.m: Use sophisticated technique for calculating exponents to avoid innacuracies (bug #62329)
Nir Krakauer <nkrakauer@ccny.cuny.edu>
parents: 30564
diff changeset
150 ## equivalent to "1 - x(b_one) .^ a(b_one)", but less roundoff error
30948
74f8854958d2 maint: merge stable to default.
Rik <rik@octave.org>
parents: 30875 30947
diff changeset
151 I(b_one) = - expm1 (log (x(b_one)) .* a(b_one));
30016
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
152 endif
30400
168da23530b4 maint: strip trailing spaces from code base.
Rik <rik@octave.org>
parents: 30330
diff changeset
153
30016
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
154 ## Non-Trivial cases
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
155 ## In the following, we use the fact that the continued fraction Octave uses
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
156 ## is more efficient when x <= a / (a + b). Moreover, to compute the upper
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
157 ## version, which is defined as I_x(a,b,"upper") = 1 - I_x(a,b) we use the
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
158 ## property I_x(a,b) + I_(1-x) (b,a) = 1.
30400
168da23530b4 maint: strip trailing spaces from code base.
Rik <rik@octave.org>
parents: 30330
diff changeset
159
30016
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
160 x = x(non_trivial);
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
161 a = a(non_trivial);
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
162 b = b(non_trivial);
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
163
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
164 if (strcmpi (tail, "lower"))
30016
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
165 fflag = (x > a./(a + b));
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
166 x(fflag) = 1 - x(fflag);
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
167 [a(fflag), b(fflag)] = deal (b(fflag), a(fflag));
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
168 elseif (strcmpi (tail, "upper"))
24923
40ab8be7d7ec Fixed style in specfun scripts
Michele Ginesi <michele.ginesi@gmail.com>
parents: 24921
diff changeset
169 fflag = (x < (a ./ (a + b)));
40ab8be7d7ec Fixed style in specfun scripts
Michele Ginesi <michele.ginesi@gmail.com>
parents: 24921
diff changeset
170 x(! fflag) = 1 - x(! fflag);
40ab8be7d7ec Fixed style in specfun scripts
Michele Ginesi <michele.ginesi@gmail.com>
parents: 24921
diff changeset
171 [a(! fflag), b(! fflag)] = deal (b(! fflag), a(! fflag));
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
172 else
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
173 error ("betainc: invalid value for TAIL");
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
174 endif
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
175
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
176 f = zeros (size (x), class (x));
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
177
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
178 ## Continued fractions: CPVWJ, formula 18.5.20, modified Lentz algorithm
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
179 ## implemented in a separate .cc file. This particular continued fraction
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
180 ## gives (B(a,b) * I_x(a,b)) / (x^a * (1-x)^b).
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
181
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
182 f = __betainc__ (x, a, b);
24919
ed6f6bbed604 betainc: vectorized the Lentz's algorithm
Michele Ginesi <michele.ginesi@gmail.com>
parents: 24907
diff changeset
183
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
184 ## Divide continued fraction by B(a,b) / (x^a * (1-x)^b) to obtain I_x(a,b).
30016
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
185 y_nt = a .* log (x) + b .* log1p (-x) ...
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
186 + (gammaln (a + b) - gammaln (a) - gammaln (b)) + log (f);
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
187 y_nt = real (exp (y_nt));
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
188 y_nt(fflag) = 1 - y_nt(fflag);
30400
168da23530b4 maint: strip trailing spaces from code base.
Rik <rik@octave.org>
parents: 30330
diff changeset
189
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
190 I(non_trivial) = y_nt;
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
191
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
192 ## Restore original shape
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
193 I = reshape (I, orig_sz);
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
194
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
195 endfunction
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
196
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
197
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
198 ## Double precision
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
199 %!test
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
200 %! a = [1, 1.5, 2, 3];
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
201 %! b = [4, 3, 2, 1];
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
202 %! v1 = betainc (1, a, b);
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
203 %! v2 = [1,1,1,1];
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
204 %! x = [.2, .4, .6, .8];
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
205 %! v3 = betainc (x, a, b);
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
206 %! v4 = 1 - betainc (1-x, b, a);
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
207 %! assert (v1, v2, sqrt (eps));
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
208 %! assert (v3, v4, sqrt (eps));
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
209
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
210 ## Single precision
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
211 %!test
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
212 %! a = single ([1, 1.5, 2, 3]);
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
213 %! b = single ([4, 3, 2, 1]);
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
214 %! v1 = betainc (1, a, b);
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
215 %! v2 = single ([1,1,1,1]);
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
216 %! x = single ([.2, .4, .6, .8]);
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
217 %! v3 = betainc (x, a, b);
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
218 %! v4 = 1 - betainc (1-x, b, a);
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
219 %! assert (v1, v2, sqrt (eps ("single")));
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
220 %! assert (v3, v4, sqrt (eps ("single")));
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
221
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
222 ## Mixed double/single precision
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
223 %!test
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
224 %! a = single ([1, 1.5, 2, 3]);
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
225 %! b = [4, 3, 2, 1];
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
226 %! v1 = betainc (1,a,b);
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
227 %! v2 = single ([1,1,1,1]);
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
228 %! x = [.2, .4, .6, .8];
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
229 %! v3 = betainc (x, a, b);
30326
cd7c824f3f22 eliminate use of .+ and .- operators in Octave sources
John W. Eaton <jwe@octave.org>
parents: 30016
diff changeset
230 %! v4 = 1 - betainc (1. - x, b, a);
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
231 %! assert (v1, v2, sqrt (eps ("single")));
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
232 %! assert (v3, v4, sqrt (eps ("single")));
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
233
26946
04e5cb5e2cb3 update bug status in tests
John W. Eaton <jwe@octave.org>
parents: 26514
diff changeset
234 %!test <*51157>
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
235 %! y = betainc ([0.00780;0.00782;0.00784],250.005,49750.995);
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
236 %! y_ex = [0.999999999999989; 0.999999999999992; 0.999999999999995];
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
237 %! assert (y, y_ex, -1e-14);
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
238
28907
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28886
diff changeset
239 %!assert (betainc (0.001, 20, 30), 2.750687665855991e-47, -3e-14)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28886
diff changeset
240 %!assert (betainc (0.0001, 20, 30), 2.819953178893307e-67, -7e-14)
30330
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 30326
diff changeset
241 %!assert <*54383> (betainc (0.99, 20, 30, "upper"),
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 30326
diff changeset
242 %! 1.5671643161872703e-47, -7e-14)
28907
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28886
diff changeset
243 %!assert (betainc (0.999, 20, 30, "upper"), 1.850806276141535e-77, -7e-14)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28886
diff changeset
244 %!assert (betainc (0.5, 200, 300), 0.9999964565197356, -1e-15)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28886
diff changeset
245 %!assert (betainc (0.5, 200, 300, "upper"), 3.54348026439253e-06, -3e-13)
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
246
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
247 ## Test trivial values
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
248 %!test
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
249 %! [a,b] = ndgrid (linspace (1e-4, 100, 20), linspace (1e-4, 100, 20));
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
250 %! assert (betainc (0, a, b), zeros (20));
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
251 %! assert (betainc (1, a, b), ones (20));
30947
fe898ae23e0e betainc.m: Use sophisticated technique for calculating exponents to avoid innacuracies (bug #62329)
Nir Krakauer <nkrakauer@ccny.cuny.edu>
parents: 30564
diff changeset
252 %! assert (betainc (0, a, b, "upper"), ones (20));
fe898ae23e0e betainc.m: Use sophisticated technique for calculating exponents to avoid innacuracies (bug #62329)
Nir Krakauer <nkrakauer@ccny.cuny.edu>
parents: 30564
diff changeset
253 %! assert (betainc (1, a, b, "upper"), zeros (20));
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
254
26946
04e5cb5e2cb3 update bug status in tests
John W. Eaton <jwe@octave.org>
parents: 26514
diff changeset
255 %!test <*34405>
25015
baa7e37453b1 Added more tests for betainc and expint.
Michele Ginesi <michele.ginesi@gmail.com>
parents: 24996
diff changeset
256 %! assert (betainc (NaN, 1, 2), NaN);
30016
ad6a57b215e8 betainc.m: Improve accuracy for certain special integer inputs (bug #60682).
Michele Ginesi <michele.ginesi@gmail.com>
parents: 29665
diff changeset
257 %! assert (betainc (0.5, 1, Inf), 1);
25015
baa7e37453b1 Added more tests for betainc and expint.
Michele Ginesi <michele.ginesi@gmail.com>
parents: 24996
diff changeset
258
30947
fe898ae23e0e betainc.m: Use sophisticated technique for calculating exponents to avoid innacuracies (bug #62329)
Nir Krakauer <nkrakauer@ccny.cuny.edu>
parents: 30564
diff changeset
259 %!test <*62329>
31551
fd29c7a50a78 maint: use commas, semicolons consistently with Octave conventions.
Rik <rik@octave.org>
parents: 30948
diff changeset
260 %! assert (betainc (2e-20, 1, 0.5), 1e-20, -1e-15);
fd29c7a50a78 maint: use commas, semicolons consistently with Octave conventions.
Rik <rik@octave.org>
parents: 30948
diff changeset
261 %! assert (betainc (2e-5, 1, 0.5), 2e-5 / (1 + sqrt (1 - 2e-5)), -1e-15);
fd29c7a50a78 maint: use commas, semicolons consistently with Octave conventions.
Rik <rik@octave.org>
parents: 30948
diff changeset
262 %! assert (betainc (0.99, 1, 0.5, "upper"), 0.1, -1e-15);
fd29c7a50a78 maint: use commas, semicolons consistently with Octave conventions.
Rik <rik@octave.org>
parents: 30948
diff changeset
263 %! assert (betainc (0.99, 0.5, 1, "upper"), - expm1 (log (0.99)/2), -1e-15);
30947
fe898ae23e0e betainc.m: Use sophisticated technique for calculating exponents to avoid innacuracies (bug #62329)
Nir Krakauer <nkrakauer@ccny.cuny.edu>
parents: 30564
diff changeset
264
24907
bd89440407aa Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff changeset
265 ## Test input validation
28886
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 28789
diff changeset
266 %!error <Invalid call> betainc ()
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 28789
diff changeset
267 %!error <Invalid call> betainc (1)
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 28789
diff changeset
268 %!error <Invalid call> betainc (1,2)
24927
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
269 %!error <must be of common size or scalars> betainc (ones (2,2), ones (1,2), 1)
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
270 %!error <all inputs must be real> betainc (0.5i, 1, 2)
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
271 %!error <all inputs must be real> betainc (0, 1i, 1)
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
272 %!error <all inputs must be real> betainc (0, 1, 1i)
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
273 %!error <X must be in the range \[0, 1\]> betainc (-0.1,1,1)
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
274 %!error <X must be in the range \[0, 1\]> betainc (1.1,1,1)
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
275 %!error <X must be in the range \[0, 1\]>
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
276 %! x = ones (1, 1, 2);
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
277 %! x(1,1,2) = -1;
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
278 %! betainc (x,1,1);
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
279 %!error <A must be strictly positive> betainc (0.5,0,1)
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
280 %!error <A must be strictly positive>
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
281 %! a = ones (1, 1, 2);
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
282 %! a(1,1,2) = 0;
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
283 %! betainc (1,a,1);
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
284 %!error <B must be strictly positive> betainc (0.5,1,0)
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
285 %!error <B must be strictly positive>
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
286 %! b = ones (1, 1, 2);
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
287 %! b(1,1,2) = 0;
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
288 %! betainc (1,1,b);
c280560d9c96 Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents: 24923
diff changeset
289 %!error <invalid value for TAIL> betainc (1,2,3, "foobar")