Mercurial > hg > octave-lyh
annotate scripts/specfun/beta.m @ 11305:c9df571efe95
subplot.m: Add suppport for "align" and "replace" options.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Thu, 02 Dec 2010 18:46:10 -0500 |
parents | 693e22af08ae |
children | fd0a3ac60b0e |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2005, 2006, |
8920 | 2 ## 2007, 2008, 2009 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 ## @tex |
24 ## $$ | |
25 ## B (a, b) = {\Gamma (a) \Gamma (b) \over \Gamma (a + b)}. | |
26 ## $$ | |
27 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7601
diff
changeset
|
28 ## @ifnottex |
3426 | 29 ## |
3321 | 30 ## @example |
31 ## beta (a, b) = gamma (a) * gamma (b) / gamma (a + b). | |
32 ## @end example | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
33 ## |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7601
diff
changeset
|
34 ## @end ifnottex |
3321 | 35 ## @end deftypefn |
2311 | 36 |
5428 | 37 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2312 | 38 ## Created: 13 June 1993 |
39 ## Adapted-By: jwe | |
40 | |
1026 | 41 function retval = beta (a, b) |
2325 | 42 |
1026 | 43 if (nargin != 2) |
6046 | 44 print_usage (); |
1026 | 45 endif |
46 | |
7601
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
47 if (any (size (a) != size (b)) && numel (a) != 1 && numel (b) != 1) |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
48 error ("beta: inputs A and B have inconsistent sizes"); |
7601
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
49 endif |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
50 |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
51 if (! isreal (a) || ! isreal (b)) |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
52 error ("beta: inputs A and B must be real"); |
7601
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
53 endif |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
54 |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
55 retval = real (exp (gammaln (a) + gammaln (b) - gammaln (a+b))); |
715 | 56 |
57 endfunction | |
7385 | 58 |
59 %!test | |
60 %! a=[1, 1.5, 2, 3]; | |
61 %! b=[4, 3, 2, 1]; | |
62 %! v1=beta(a,b); | |
63 %! v2=beta(b,a); | |
64 %! v3=gamma(a).*gamma(b)./gamma(a+b); | |
65 %! assert(all(abs(v1-v2)<sqrt(eps)) && all(abs(v2-v3)<sqrt(eps))); | |
66 | |
67 %!error beta(); | |
68 | |
69 %!error beta(1); | |
70 | |
7601
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
71 %!assert (1, beta (1, 1)) |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
72 |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
73 %!test |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
74 %! a = 2:10; |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
75 %! tol = 10 * max (a) * eps; |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
76 %! 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
|
77 %! 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
|
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 = 0.25 + (0:5) * 0.5; |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
81 %! tol = 10 * max (a) * eps; |
8a939b217863
Treat negative values to lgamma and beta correctly
David Bateman <dbateman@free.fr>
parents:
7385
diff
changeset
|
82 %! 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
|
83 %! assert (zeros (size (a)), beta (-a, a), tol) |