/src/r-source/src/nmath/lbeta.c
Line | Count | Source |
1 | | /* |
2 | | * Mathlib : A C Library of Special Functions |
3 | | * Copyright (C) 1998 Ross Ihaka |
4 | | * Copyright (C) 2000-12 The R Core Team |
5 | | * Copyright (C) 2003 The R Foundation |
6 | | * |
7 | | * This program is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 2 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * This program is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, a copy is available at |
19 | | * https://www.R-project.org/Licenses/ |
20 | | * |
21 | | * SYNOPSIS |
22 | | * |
23 | | * #include <Rmath.h> |
24 | | * double lbeta(double a, double b); |
25 | | * |
26 | | * DESCRIPTION |
27 | | * |
28 | | * This function returns the value of the log beta function |
29 | | * |
30 | | * log B(a,b) = log G(a) + log G(b) - log G(a+b) |
31 | | * |
32 | | * NOTES |
33 | | * |
34 | | * This routine is a translation into C of a Fortran subroutine |
35 | | * by W. Fullerton of Los Alamos Scientific Laboratory. |
36 | | */ |
37 | | |
38 | | #include "nmath.h" |
39 | | |
40 | | double lbeta(double a, double b) |
41 | 0 | { |
42 | 0 | double corr, p, q; |
43 | |
|
44 | 0 | #ifdef IEEE_754 |
45 | 0 | if(ISNAN(a) || ISNAN(b)) |
46 | 0 | return a + b; |
47 | 0 | #endif |
48 | 0 | p = q = a; |
49 | 0 | if(b < p) p = b;/* := min(a,b) */ |
50 | 0 | if(b > q) q = b;/* := max(a,b) */ |
51 | | |
52 | | /* both arguments must be >= 0 */ |
53 | 0 | if (p < 0) |
54 | 0 | ML_WARN_return_NAN |
55 | 0 | else if (p == 0) { |
56 | 0 | return ML_POSINF; |
57 | 0 | } |
58 | 0 | else if (!R_FINITE(q)) { /* q == +Inf */ |
59 | 0 | return ML_NEGINF; |
60 | 0 | } |
61 | | |
62 | 0 | if (p >= 10) { |
63 | | /* p and q are big. */ |
64 | 0 | corr = lgammacor(p) + lgammacor(q) - lgammacor(p + q); |
65 | 0 | return log(q) * -0.5 + M_LN_SQRT_2PI + corr |
66 | 0 | + (p - 0.5) * log(p / (p + q)) + q * log1p(-p / (p + q)); |
67 | 0 | } |
68 | 0 | else if (q >= 10) { |
69 | | /* p is small, but q is big. */ |
70 | 0 | corr = lgammacor(q) - lgammacor(p + q); |
71 | 0 | return lgammafn(p) + corr + p - p * log(p + q) |
72 | 0 | + (q - 0.5) * log1p(-p / (p + q)); |
73 | 0 | } |
74 | 0 | else { /* p and q are small: p <= q < 10. */ |
75 | | /* R change for very small args */ |
76 | 0 | if (p < 1e-306) return lgamma(p) + (lgamma(q) - lgamma(p+q)); |
77 | 0 | else return log(gammafn(p) * (gammafn(q) / gammafn(p + q))); |
78 | 0 | } |
79 | 0 | } |