Mercurial > hg > octave-nkf
annotate scripts/specfun/beta.m @ 8517:81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
author | sh@sh-laptop |
---|---|
date | Wed, 14 Jan 2009 20:44:25 -0500 |
parents | 8a939b217863 |
children | e07e93c04080 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2005, 2006, |
2 ## 2007 John W. Eaton | |
2313 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
2313 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
1026 | 19 |
3381 | 20 ## -*- texinfo -*- |
3321 | 21 ## @deftypefn {Mapping Function} {} beta (@var{a}, @var{b}) |
7601
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
22 ## For real inputs, return the Beta function, |
3321 | 23 ## @iftex |
24 ## @tex | |
25 ## $$ | |
26 ## B (a, b) = {\Gamma (a) \Gamma (b) \over \Gamma (a + b)}. | |
27 ## $$ | |
28 ## @end tex | |
29 ## @end iftex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7601
diff
changeset
|
30 ## @ifnottex |
3426 | 31 ## |
3321 | 32 ## @example |
33 ## beta (a, b) = gamma (a) * gamma (b) / gamma (a + b). | |
34 ## @end example | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7601
diff
changeset
|
35 ## @end ifnottex |
3321 | 36 ## @end deftypefn |
2311 | 37 |
5428 | 38 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2312 | 39 ## Created: 13 June 1993 |
40 ## Adapted-By: jwe | |
41 | |
1026 | 42 function retval = beta (a, b) |
2325 | 43 |
1026 | 44 if (nargin != 2) |
6046 | 45 print_usage (); |
1026 | 46 endif |
47 | |
7601
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
48 if (any (size (a) != size (b)) && numel (a) != 1 && numel (b) != 1) |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
49 error ("beta: inputs have inconsistent sizes.") |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
50 endif |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
51 |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
52 if (! isreal (a) || ! isreal (b)) |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
53 error ("beta: inputs must be real.") |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
54 endif |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
55 |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
56 retval = real (exp (gammaln (a) + gammaln (b) - gammaln (a+b))); |
715 | 57 |
58 endfunction | |
7385 | 59 |
60 %!test | |
61 %! a=[1, 1.5, 2, 3]; | |
62 %! b=[4, 3, 2, 1]; | |
63 %! v1=beta(a,b); | |
64 %! v2=beta(b,a); | |
65 %! v3=gamma(a).*gamma(b)./gamma(a+b); | |
66 %! assert(all(abs(v1-v2)<sqrt(eps)) && all(abs(v2-v3)<sqrt(eps))); | |
67 | |
68 %!error beta(); | |
69 | |
70 %!error beta(1); | |
71 | |
7601
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
72 %!assert (1, beta (1, 1)) |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
73 |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
74 %!test |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
75 %! a = 2:10; |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
76 %! tol = 10 * max (a) * eps; |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
77 %! assert (-a, beta (-1./a, 1), tol) |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
78 %! assert (-a, beta (1, -1./a), tol) |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
79 |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
80 %!test |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
81 %! 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
|
82 %! tol = 10 * max (a) * eps; |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
83 %! assert (zeros (size (a)), beta (a, -a), tol) |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
84 %! assert (zeros (size (a)), beta (-a, a), tol) |