Mercurial > octave
annotate scripts/specfun/beta.m @ 31191:bb9d776eafac stable
Fix wrong color in PDF printout of some latex strings (bug #62884)
* octave-svgconvert (draw): For "rect" elements only set brush color if
necessary and eventually restore to previous color.
author | Pantxo Diribarne <pantxo.diribarne@gmail.com> |
---|---|
date | Sun, 14 Aug 2022 18:24:07 +0200 |
parents | 796f54d4ddbf |
children | 5d3faba0342e |
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:
29359
diff
changeset
|
3 ## Copyright (C) 1994-2022 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/>. |
2313 | 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 |
2313 | 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. |
2313 | 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. |
2313 | 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 ######################################################################## |
1026 | 25 |
3381 | 26 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
27 ## @deftypefn {} {} beta (@var{a}, @var{b}) |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19102
diff
changeset
|
28 ## Compute the Beta function for real inputs @var{a} and @var{b}. |
19102
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
29 ## |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
30 ## The Beta function definition is |
3321 | 31 ## @tex |
32 ## $$ | |
33 ## B (a, b) = {\Gamma (a) \Gamma (b) \over \Gamma (a + b)}. | |
34 ## $$ | |
35 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7601
diff
changeset
|
36 ## @ifnottex |
3426 | 37 ## |
3321 | 38 ## @example |
39 ## beta (a, b) = gamma (a) * gamma (b) / gamma (a + b). | |
40 ## @end example | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
41 ## |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7601
diff
changeset
|
42 ## @end ifnottex |
19102
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
43 ## |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
44 ## The Beta function can grow quite large and it is often more useful to work |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
45 ## with the logarithm of the output rather than the function directly. |
28959
5394d688d456
doc: Use @code{} within alternate text for @xref,@pxref macros for better Info display.
Rik <rik@octave.org>
parents:
28886
diff
changeset
|
46 ## @xref{XREFbetaln,,@code{betaln}}, for computing the logarithm of the Beta |
5394d688d456
doc: Use @code{} within alternate text for @xref,@pxref macros for better Info display.
Rik <rik@octave.org>
parents:
28886
diff
changeset
|
47 ## function in an efficient manner. |
19102
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
48 ## @seealso{betaln, betainc, betaincinv} |
3321 | 49 ## @end deftypefn |
2311 | 50 |
1026 | 51 function retval = beta (a, b) |
2325 | 52 |
1026 | 53 if (nargin != 2) |
6046 | 54 print_usage (); |
1026 | 55 endif |
56 | |
7601
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
57 if (! isreal (a) || ! isreal (b)) |
19102
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
58 error ("beta: A and B must be real"); |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
59 elseif (! size_equal (a, b) && numel (a) != 1 && numel (b) != 1) |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
60 error ("beta: A and B must have consistent sizes"); |
7601
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
61 endif |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
62 |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
63 retval = real (exp (gammaln (a) + gammaln (b) - gammaln (a+b))); |
715 | 64 |
65 endfunction | |
7385 | 66 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
67 |
7385 | 68 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
69 %! a = [1, 1.5, 2, 3]; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
70 %! b = [4, 3, 2, 1]; |
19102
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
71 %! v1 = beta (a, b); |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
72 %! v2 = beta (b, a); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
73 %! v3 = gamma (a).*gamma (b) ./ gamma (a+b); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
74 %! assert (v1, v2, sqrt (eps)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
75 %! assert (v2, v3, sqrt (eps)); |
7385 | 76 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
77 %!assert (beta (1, 1), 1) |
7601
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
78 |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
79 %!test |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
80 %! a = 2:10; |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
81 %! tol = 10 * max (a) * eps; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
82 %! assert (-a, beta (-1./a, 1), tol); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
83 %! assert (-a, beta (1, -1./a), tol); |
7601
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
84 |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
85 %!test |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
86 %! a = 0.25 + (0:5) * 0.5; |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
87 %! tol = 10 * max (a) * eps; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
88 %! assert (zeros (size (a)), beta (a, -a), tol); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
89 %! assert (zeros (size (a)), beta (-a, a), tol); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
90 |
28886
d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents:
27985
diff
changeset
|
91 %!error <Invalid call> beta () |
d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents:
27985
diff
changeset
|
92 %!error <Invalid call> beta (1) |
19102
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
93 %!error <A and B must be real> beta (1i, 2) |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
94 %!error <A and B must be real> beta (2, 1i) |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
95 %!error <A and B must have consistent sizes> beta ([1 2], [1 2 3]) |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
96 %!error <A and B must have consistent sizes> beta ([1 2 3], [1 2]) |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
97 %!error <A and B must have consistent sizes> beta ([1 2 3], [1 2 3]') |