-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] port of Cephes implementation of Beta function (fixes #14256) #14349
Changes from 5 commits
ef1e530
360e4d1
d0055a1
67b47cf
088508c
7fbfaff
8601710
fb8e4ba
efce6cc
d2de1b3
dab853d
0b93ab9
8065f51
5e2e09f
cbea222
c5e346d
8bf6196
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -404,13 +404,124 @@ invdigamma(x::Float32) = Float32(invdigamma(Float64(x))) | |
invdigamma(x::Real) = invdigamma(Float64(x)) | ||
@vectorize_1arg Real invdigamma | ||
|
||
function beta(x::Number, w::Number) | ||
yx, sx = lgamma_r(x) | ||
yw, sw = lgamma_r(w) | ||
yxw, sxw = lgamma_r(x+w) | ||
return exp(yx + yw - yxw) * (sx*sw*sxw) | ||
const MAXGAM = 171.624376956302725 | ||
const ASYMP_FACTOR = 1e6 | ||
|
||
function beta(a::Number, b::Number) | ||
real(a) <= 0.0 && isinteger(a) && return beta_negint(a, b) | ||
real(b) <= 0.0 && isinteger(b) && return beta_negint(b, a) | ||
|
||
if abs(a) < abs(b) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that |
||
a, b = b, a | ||
end | ||
|
||
if abs(a) > ASYMP_FACTOR * abs(b) && a > ASYMP_FACTOR | ||
# Avoid loss of precision in lgamma(a + b) - lgamma(a) | ||
y, s = lbeta_asymp(a, b) | ||
return s * exp(y) | ||
end | ||
|
||
y = a + b | ||
if abs(y) > MAXGAM || abs(a) > MAXGAM || abs(b) > MAXGAM | ||
y, s = lgamma_r(y) | ||
yb, sb = lgamma_r(b) | ||
y = yb - y | ||
ya, sa = lgamma_r(a) | ||
y = ya + y | ||
# if (y > MAXLOG) { | ||
# goto overflow; | ||
# } | ||
return s*sa*sb * exp(y) | ||
end | ||
|
||
y = gamma(y) | ||
a = gamma(a) | ||
b = gamma(b) | ||
y == 0.0 && return Inf | ||
|
||
if abs(abs(a) - abs(y)) > abs(abs(b) - abs(y)) | ||
y = b / y | ||
y *= a | ||
else | ||
y = a / y | ||
y *= b | ||
end | ||
|
||
return y | ||
end | ||
|
||
function lbeta(a::Number, b::Number) | ||
real(a) <= 0.0 && isinteger(a) && return lbeta_negint(a, b) | ||
real(b) <= 0.0 && isinteger(b) && return lbeta_negint(b, a) | ||
|
||
if abs(a) < abs(b) | ||
a, b = b, a | ||
end | ||
|
||
if abs(a) > ASYMP_FACTOR * abs(b) && a > ASYMP_FACTOR | ||
# Avoid loss of precision in lgamma(a + b) - lgamma(a) | ||
y, s = lbeta_asymp(a, b) | ||
return y | ||
end | ||
|
||
y = a + b | ||
if abs(y) > MAXGAM || abs(a) > MAXGAM || abs(b) > MAXGAM | ||
y, s = lgamma_r(y) | ||
yb, sb = lgamma_r(b) | ||
y = yb - y | ||
ya, sa = lgamma_r(a) | ||
y = ya + y | ||
return y | ||
end | ||
|
||
y = gamma(y) | ||
a = gamma(a) | ||
b = gamma(b) | ||
y == 0.0 && return Inf | ||
|
||
if abs(abs(a) - abs(y)) > abs(abs(b) - abs(y)) | ||
y = b / y | ||
y *= a | ||
else | ||
y = a / y | ||
y *= b | ||
end | ||
|
||
return real(y) < 0 ? log(-y) : log(y) | ||
end | ||
|
||
# assuming isinteger(x) and x < 0. | ||
function beta_negint(x::Number, w::Number) | ||
if isinteger(w) && 1 - x - w > 0 | ||
s = ifelse(w % 2 == 0, 1., -1.) | ||
return s * beta(1 - x - w, w) | ||
else | ||
return Inf | ||
end | ||
end | ||
lbeta(x::Number, w::Number) = lgamma(x)+lgamma(w)-lgamma(x+w) | ||
|
||
# assuming isinteger(x) and x < 0. | ||
function lbeta_negint(x::Number, w::Number) | ||
if isinteger(w) && 1 - x - w > 0 | ||
s = ifelse(w % 2 == 0 , 1., -1.) | ||
return s * lbeta(1 - x - w, w) | ||
else | ||
return Inf | ||
end | ||
end | ||
|
||
# Asymptotic expansion for ln(|B(a, b)|) for a > ASYMP_FACTOR*max(|b|, 1). | ||
function lbeta_asymp(a::Number, b::Number) | ||
r, s = lgamma_r(b) | ||
r -= b * log(a) | ||
|
||
r += b*(1-b)/(2*a); | ||
r += b*(1-b)*(1-2*b)/(12*a*a) | ||
r += - b*b*(1-b)*(1-b)/(12*a*a*a) | ||
|
||
return r, s | ||
end | ||
|
||
@vectorize_2arg Number beta | ||
@vectorize_2arg Number lbeta | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you port something, you should always have a comment indicating the original source and the license thereof.