Mercurial > octave-nkf
annotate scripts/specfun/beta.m @ 20595:c1a6c31ac29a
eliminate more simple uses of error_state
* ov-classdef.cc: Eliminate simple uses of error_state.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 06 Oct 2015 00:20:02 -0400 |
parents | 4197fc428c7d |
children |
rev | line source |
---|---|
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19631
diff
changeset
|
1 ## Copyright (C) 1994-2015 John W. Eaton |
2313 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
1026 | 18 |
3381 | 19 ## -*- texinfo -*- |
3321 | 20 ## @deftypefn {Mapping Function} {} beta (@var{a}, @var{b}) |
19631
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19136
diff
changeset
|
21 ## Compute the Beta function for real inputs @var{a} and @var{b}. |
19136
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
22 ## |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
23 ## The Beta function definition is |
3321 | 24 ## @tex |
25 ## $$ | |
26 ## B (a, b) = {\Gamma (a) \Gamma (b) \over \Gamma (a + b)}. | |
27 ## $$ | |
28 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7601
diff
changeset
|
29 ## @ifnottex |
3426 | 30 ## |
3321 | 31 ## @example |
32 ## beta (a, b) = gamma (a) * gamma (b) / gamma (a + b). | |
33 ## @end example | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
34 ## |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7601
diff
changeset
|
35 ## @end ifnottex |
19136
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
36 ## |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
37 ## 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
|
38 ## with the logarithm of the output rather than the function directly. |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
39 ## @xref{XREFbetaln,,betaln}, for computing the logarithm of the Beta function |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
40 ## in an efficient manner. |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
41 ## @seealso{betaln, betainc, betaincinv} |
3321 | 42 ## @end deftypefn |
2311 | 43 |
5428 | 44 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2312 | 45 ## Created: 13 June 1993 |
46 ## Adapted-By: jwe | |
47 | |
1026 | 48 function retval = beta (a, b) |
2325 | 49 |
1026 | 50 if (nargin != 2) |
6046 | 51 print_usage (); |
1026 | 52 endif |
53 | |
7601
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
54 if (! isreal (a) || ! isreal (b)) |
19136
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
55 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
|
56 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
|
57 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
|
58 endif |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
59 |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
60 retval = real (exp (gammaln (a) + gammaln (b) - gammaln (a+b))); |
715 | 61 |
62 endfunction | |
7385 | 63 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
64 |
7385 | 65 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
66 %! 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
|
67 %! b = [4, 3, 2, 1]; |
19136
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
68 %! v1 = beta (a, b); |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
69 %! 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
|
70 %! 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
|
71 %! 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
|
72 %! assert (v2, v3, sqrt (eps)); |
7385 | 73 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
74 %!assert (beta (1, 1), 1) |
7601
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
75 |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
76 %!test |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
77 %! a = 2:10; |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
78 %! 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
|
79 %! 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
|
80 %! 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
|
81 |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
82 %!test |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
83 %! 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
|
84 %! 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
|
85 %! 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
|
86 %! 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
|
87 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
88 %!error beta () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
89 %!error beta (1) |
19136
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
90 %!error beta (1,2,3) |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
91 %!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
|
92 %!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
|
93 %!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
|
94 %!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
|
95 %!error <A and B must have consistent sizes> beta ([1 2 3], [1 2 3]') |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
96 |