The R Project SVN R

Rev

Rev 42302 | Rev 59039 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 42302 Rev 42303
Line -... Line 1...
-
 
1
/*
-
 
2
 *  Mathlib : A C Library of Special Functions
-
 
3
 *  Copyright (C) 1998 Ross Ihaka
-
 
4
 *  Copyright (C) 2000 The R Development 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
 *  http://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
 *  NOTES
-
 
31
 *
-
 
32
 *    This routine is a translation into C of a Fortran subroutine
-
 
33
 *    by W. Fullerton of Los Alamos Scientific Laboratory.
-
 
34
 */
-
 
35
 
-
 
36
#include "nmath.h"
-
 
37
 
-
 
38
double lbeta(double a, double b)
-
 
39
{
-
 
40
    double corr, p, q;
-
 
41
 
-
 
42
    p = q = a;
-
 
43
    if(b < p) p = b;/* := min(a,b) */
-
 
44
    if(b > q) q = b;/* := max(a,b) */
-
 
45
 
-
 
46
#ifdef IEEE_754
-
 
47
    if(ISNAN(a) || ISNAN(b))
-
 
48
	return a + b;
-
 
49
#endif
-
 
50
 
-
 
51
    /* both arguments must be >= 0 */
-
 
52
 
-
 
53
    if (p < 0)
-
 
54
	ML_ERR_return_NAN
-
 
55
    else if (p == 0) {
-
 
56
	return ML_POSINF;
-
 
57
    }
-
 
58
    else if (!R_FINITE(q)) {
-
 
59
	return ML_NEGINF;
-
 
60
    }
-
 
61
 
-
 
62
    if (p >= 10) {
-
 
63
	/* p and q are big. */
-
 
64
	corr = lgammacor(p) + lgammacor(q) - lgammacor(p + q);
-
 
65
	return log(q) * -0.5 + M_LN_SQRT_2PI + corr
-
 
66
		+ (p - 0.5) * log(p / (p + q)) + q * log1p(-p / (p + q));
-
 
67
    }
-
 
68
    else if (q >= 10) {
-
 
69
	/* p is small, but q is big. */
-
 
70
	corr = lgammacor(q) - lgammacor(p + q);
-
 
71
	return lgammafn(p) + corr + p - p * log(p + q)
-
 
72
		+ (q - 0.5) * log1p(-p / (p + q));
-
 
73
    }
-
 
74
    else
-
 
75
	/* p and q are small: p <= q < 10. */
-
 
76
	return log(gammafn(p) * (gammafn(q) / gammafn(p + q)));
-
 
77
}