Skip to content

Commit 789c46e

Browse files
authored
Merge branch 'master' into ellipk
2 parents 2575b8b + 46a2874 commit 789c46e

File tree

12 files changed

+151
-85
lines changed

12 files changed

+151
-85
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,13 @@ jobs:
1717
- 'min'
1818
- 'lts'
1919
- '1'
20+
- 'pre'
2021
os:
2122
- ubuntu-latest
2223
- macOS-latest
2324
- windows-latest
24-
exclude:
25-
- os: macOS-latest # Apple Silicon
26-
version: 'min'
27-
include:
28-
- os: macOS-13 # Intel
29-
version: 'min'
3025
steps:
31-
- uses: actions/checkout@v4
26+
- uses: actions/checkout@v5
3227
- uses: julia-actions/setup-julia@v2
3328
with:
3429
version: ${{ matrix.version }}
@@ -42,13 +37,13 @@ jobs:
4237
with:
4338
token: ${{ secrets.CODECOV_TOKEN }} # required
4439
fail_ci_if_error: true
45-
file: ./lcov.info
40+
files: ./lcov.info
4641
flags: unittests
4742
docs:
4843
name: Documentation
4944
runs-on: ubuntu-latest
5045
steps:
51-
- uses: actions/checkout@v4
46+
- uses: actions/checkout@v5
5247
- uses: julia-actions/setup-julia@v2
5348
with:
5449
version: "1"

Project.toml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "SpecialFunctions"
22
uuid = "276daf66-3868-5448-9aa4-cd146d93841b"
3-
version = "2.5.1"
3+
version = "2.6.1"
44

55
[deps]
66
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
@@ -16,19 +16,27 @@ ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
1616
SpecialFunctionsChainRulesCoreExt = "ChainRulesCore"
1717

1818
[compat]
19+
Aqua = "0.8.14"
1920
ChainRulesCore = "0.9.44, 0.10, 1"
2021
ChainRulesTestUtils = "0.6.8, 0.7, 1"
22+
ExplicitImports = "1.13.2"
2123
IrrationalConstants = "0.1, 0.2"
24+
JET = "0.9, 0.10"
2225
LogExpFunctions = "0.3.2"
2326
OpenLibm_jll = "0.7, 0.8"
2427
OpenSpecFun_jll = "0.5"
25-
julia = "1.5"
28+
Random = "<0.0.1, 1"
29+
Test = "<0.0.1, 1"
30+
julia = "1.10"
2631

2732
[extras]
33+
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
2834
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
2935
ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a"
36+
ExplicitImports = "7d51a73a-1435-4ff3-83d9-f097790105c7"
37+
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
3038
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
3139
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3240

3341
[targets]
34-
test = ["ChainRulesCore", "ChainRulesTestUtils", "Random", "Test"]
42+
test = ["Aqua", "ChainRulesCore", "ChainRulesTestUtils", "ExplicitImports", "JET", "Random", "Test"]

ext/SpecialFunctionsChainRulesCoreExt.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
module SpecialFunctionsChainRulesCoreExt
22

3-
using SpecialFunctions, ChainRulesCore
3+
using SpecialFunctions
4+
using ChainRulesCore: ChainRulesCore
45

5-
import SpecialFunctions: sqrtπ, invπ
6+
using SpecialFunctions: sqrtπ, invπ
67

78
const BESSEL_ORDER_INFO = """
89
derivatives of Bessel functions with respect to the order are not implemented currently:

src/SpecialFunctions.jl

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ using IrrationalConstants:
1515

1616
import LogExpFunctions
1717

18-
using OpenLibm_jll
19-
using OpenSpecFun_jll
18+
using OpenLibm_jll: libopenlibm
19+
using OpenSpecFun_jll: libopenspecfun
2020

2121
export
2222
airyai,
@@ -103,9 +103,6 @@ include("gamma.jl")
103103
include("gamma_inc.jl")
104104
include("betanc.jl")
105105
include("beta_inc.jl")
106-
if !isdefined(Base, :get_extension)
107-
include("../ext/SpecialFunctionsChainRulesCoreExt.jl")
108-
end
109106
include("deprecated.jl")
110107

111108
for f in (:digamma, :erf, :erfc, :erfcinv, :erfcx, :erfi, :erfinv, :logerfc, :logerfcx,
@@ -119,13 +116,9 @@ for f in (:beta, :lbeta)
119116
end
120117
polygamma(m::Integer, x::Missing) = missing
121118

122-
# In future just use `fastabs` from Base.Math
123-
# https://github.com/JuliaLang/julia/blob/93fb785831dcfcc442f82fab8746f0244c5274ae/base/special/trig.jl#L1057
124-
if isdefined(Base.Math, :fastabs)
125-
import Base.Math: fastabs
126-
else
127-
fastabs(x::Number) = abs(x)
128-
fastabs(x::Complex) = abs(real(x)) + abs(imag(x))
129-
end
119+
# `fastabs` is identical to `Base.Math.fastabs` which is not used here since it is not public
120+
# https://github.com/JuliaLang/julia/blob/93fb785831dcfcc442f82fab8746f0244c5274ae/base/special/trig.jl#L1057
121+
fastabs(x::Number) = abs(x)
122+
fastabs(x::Complex) = abs(real(x)) + abs(imag(x))
130123

131124
end # module

src/ellip.jl

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,10 @@ function _ellipk(m::Float64)
4040
(m === -Inf) && return 0.0
4141

4242
flag_is_m_neg = false
43-
if m < 0.0
43+
if flag_is_m_neg
4444
x = m / (m-1) #dealing with negative args
45-
flag_is_m_neg = true
46-
elseif m >= 0.0
45+
else
4746
x = m
48-
flag_is_m_neg = false
4947
end
5048

5149
if x == 0.0
@@ -57,7 +55,7 @@ function _ellipk(m::Float64)
5755
elseif x > 1.0
5856
throw(DomainError(m, "`m` must lie between -Inf and 1 ---- Domain: (-Inf,1.0]"))
5957

60-
elseif 0.0 <= x < 0.1 #Table 2 from paper
58+
elseif 0.0 < x < 0.1 #Table 2 from paper
6159
t = x-0.05
6260
t = @horner(t,
6361
1.591003453790792180 , 0.416000743991786912 , 0.245791514264103415 ,
@@ -149,7 +147,8 @@ function _ellipk(m::Float64)
149147
1170222242422.439893 , 8777948323668.937971 , 66101242752484.95041 ,
150148
499488053713388.7989 , 37859743397240299.20)
151149

152-
elseif x >= 0.9
150+
else
151+
@assert 0.9 <= x < 1
153152
td = 1-x
154153
td1 = td-0.05
155154
qd = @horner(td,
@@ -214,12 +213,10 @@ function _ellipe(m::Float64)
214213
(m === -Inf) && return Inf
215214

216215
flag_is_m_neg = false
217-
if m < 0.0
216+
if flag_is_m_neg
218217
x = m / (m-1) #dealing with negative args
219-
flag_is_m_neg = true
220-
elseif m >= 0.0
218+
else
221219
x = m
222-
flag_is_m_neg = false
223220
end
224221

225222
if x == 0.0
@@ -230,7 +227,7 @@ function _ellipe(m::Float64)
230227
elseif x > 1.0
231228
throw(DomainError(m,"`m` must lie between -Inf and 1 ---- Domain : (-inf,1.0]"))
232229

233-
elseif 0.0 <= x < 0.1 #Table 2 from paper
230+
elseif 0.0 < x < 0.1 #Table 2 from paper
234231
t = x-0.05
235232
t = @horner(t ,
236233
+1.550973351780472328 , -0.400301020103198524 , -0.078498619442941939 ,
@@ -317,7 +314,8 @@ function _ellipe(m::Float64)
317314
-16120097.81581656797 , -109209938.5203089915 , -749380758.1942496220 ,
318315
-5198725846.725541393 , -36409256888.12139973)
319316

320-
elseif x >= 0.9
317+
else
318+
@assert 0.9 <= x < 1.0
321319
td = 1-x
322320
td1 = td-0.05
323321

src/gamma.jl

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function _digamma(z::ComplexOrReal{Float64})
2626
# argument," Computer Phys. Commun. vol. 4, pp. 221–226 (1972).
2727
x = real(z)
2828
if x <= 0 # reflection formula
29-
ψ = -π * _cotpi(z)
29+
ψ = -π / tanpi(z)
3030
z = 1 - z
3131
x = real(z)
3232
else
@@ -55,13 +55,6 @@ function _digamma(x::BigFloat)
5555
return z
5656
end
5757

58-
"""
59-
_cotpi(x) = cot(π * x)
60-
61-
Accurate for integer arguments
62-
"""
63-
_cotpi(x) = @static VERSION >= v"1.10.0-DEV.525" ? inv(tanpi(x)) : cospi(x) / sinpi(x)
64-
6558
"""
6659
trigamma(x)
6760
@@ -139,12 +132,12 @@ const cotderiv_Q = [cotderiv_q(m) for m in 1:100]
139132
function cotderiv(m::Integer, z)
140133
isinf(imag(z)) && return zero(z)
141134
if m <= 0
142-
m == 0 && return π * _cotpi(z)
135+
m == 0 && return π / tanpi(z)
143136
throw(DomainError(m, "`m` must be nonnegative."))
144137
end
145138
if m <= length(cotderiv_Q)
146139
q = cotderiv_Q[m]
147-
x = _cotpi(z)
140+
x = inv(tanpi(z))
148141
y = x*x
149142
s = q[1] + q[2] * y
150143
t = y
@@ -816,8 +809,7 @@ function logabsbeta(a::T, b::T) where T<:Real
816809
if a <= 0 && isinteger(a)
817810
if a + b <= 0 && isinteger(b)
818811
r = logbeta(1 - a - b, b)
819-
# in julia ≥ 1.7, iseven doesn't require Int (julia#38976)
820-
sgn = iseven(@static VERSION v"1.7" ? b : Int(b)) ? 1 : -1
812+
sgn = iseven(b) ? 1 : -1
821813
return r, sgn
822814
else
823815
return -log(zero(a)), 1

src/gamma_inc.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,7 @@ end
10151015

10161016
function __gamma_inc_inv(a::Float64, minpq::Float64, pcase::Bool)
10171017
haseta = false
1018+
fp = NaN
10181019

10191020
logp = pcase ? log(minpq) : log1p(-minpq)
10201021
loggamma1pa = a <= 1.0 ? loggamma1p(a) : loggamma(a + 1.0)

src/logabsgamma/e_lgamma_r.jl

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,32 +92,37 @@ function _logabsgamma(x::Float64)
9292
lx = ux % UInt32
9393

9494
#= purge off +-inf, NaN, +-0, tiny and negative arguments =#
95-
signgam = 1
96-
isneg = hx < Int32(0)
9795
ix = hx & 0x7fffffff
98-
ix 0x7ff00000 && return x * x, signgam
99-
ix | lx == 0x00000000 && return Inf, signgam
96+
ix 0x7ff00000 && return x * x, 1
97+
ix | lx == 0x00000000 && return Inf, 1
98+
isneg = hx < Int32(0)
10099
if ix < 0x3b900000 #= |x|<2**-70, return -log(|x|) =#
101100
if isneg
102-
signgam = -1
103-
return -log(-x), signgam
101+
return -log(-x), -1
104102
else
105-
return -log(x), signgam
103+
return -log(x), 1
106104
end
107105
end
106+
107+
# remaining cases
108108
if isneg
109109
# ix ≥ 0x43300000 && return Inf, signgam #= |x|>=2**52, must be -integer =#
110110
t = sinpi(x)
111-
iszero(t) && return Inf, signgam #= -integer =#
112-
nadj = logπ - log(abs(t * x))
113-
if t < 0.0; signgam = -1; end
114-
x = -x
111+
iszero(t) && return Inf, 1 #= -integer =#
112+
r = logπ - log(abs(t * x)) - _logabsgamma_pos(-x, ix)
113+
signgam = t < 0.0 ? -1 : 1
114+
else
115+
r = _logabsgamma_pos(x, ix)
116+
signgam = 1
115117
end
118+
return r, signgam
119+
end
120+
function _logabsgamma_pos(x::Float64, ix::UInt32)
116121
if ix < 0x40000000 #= x < 2.0 =#
117122
i = round(x, RoundToZero)
118123
f = x - i
119124
if f == 0.0 #= purge off 1; 2 handled by x < 8.0 branch =#
120-
return 0.0, signgam
125+
return 0.0
121126
elseif i == 1.0
122127
r = 0.0
123128
c = 1.0
@@ -169,8 +174,5 @@ function _logabsgamma(x::Float64)
169174
else #= 2^58 ≤ x ≤ Inf =#
170175
r = muladd(x, log(x), -x)
171176
end
172-
if isneg
173-
r = nadj - r
174-
end
175-
return r, signgam
177+
return r
176178
end

src/logabsgamma/e_lgammaf_r.jl

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,37 @@ function _logabsgamma(x::Float32)
2020
hx = reinterpret(Int32, x)
2121

2222
#= purge off +-inf, NaN, +-0, tiny and negative arguments =#
23-
signgam = 1
24-
isneg = hx < Int32(0)
2523
ix = hx & 0x7fffffff
26-
ix 0x7f800000 && return x * x, signgam
27-
ix == 0x00000000 && return Inf32, signgam
24+
ix 0x7f800000 && return x * x, 1
25+
ix == 0x00000000 && return Inf32, 1
26+
isneg = hx < Int32(0)
2827
if ix < 0x35000000 #= |x|<2**-21, return -log(|x|) =#
2928
if isneg
30-
signgam = -1
31-
return -log(-x), signgam
29+
return -log(-x), -1
3230
else
33-
return -log(x), signgam
31+
return -log(x), 1
3432
end
3533
end
34+
35+
# remaining cases
3636
if isneg
3737
# ix ≥ 0x4b000000 && return Inf32, signgam #= |x|>=2**23, must be -integer =#
3838
t = sinpi(x)
39-
t == 0.0f0 && return Inf32, signgam #= -integer =#
40-
nadj = logπ - log(abs(t * x))
41-
if t < 0.0f0; signgam = -1; end
42-
x = -x
39+
t == 0.0f0 && return Inf32, 1 #= -integer =#
40+
r = logπ - log(abs(t * x)) - _logabsgamma_pos(-x, ix)
41+
signgam = copysign(1, t)
42+
else
43+
r = _logabsgamma_pos(x, ix)
44+
signgam = 1
4345
end
44-
46+
return r, signgam
47+
end
48+
function _logabsgamma_pos(x::Float32, ix::UInt32)
4549
if ix < 0x40000000 #= x < 2.0 =#
4650
i = round(x, RoundToZero)
4751
f = x - i
4852
if f == 0.0f0 #= purge off 1; 2 handled by x < 8.0 branch =#
49-
return 0.0f0, signgam
53+
return 0.0f0
5054
elseif i == 1.0f0
5155
r = 0.0f0
5256
c = 1.0f0
@@ -99,8 +103,5 @@ function _logabsgamma(x::Float32)
99103
#= 2^58 ≤ x ≤ Inf =#
100104
r = muladd(x, log(x), -x)
101105
end
102-
if isneg
103-
r = nadj - r
104-
end
105-
return r, signgam
106+
return r
106107
end

test/logabsgamma.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,11 @@ x = 8.000001f0
156156
# (i.e. prevfloat(8.000001f0) == 8.0f0)
157157
# We still check appropriate behavior at 8.0f0
158158
@test ulp(8.0f0) < 0.4006594736129046
159+
160+
@testset "JET" begin
161+
# issue #502
162+
JET.@test_call logabsgamma(1.0)
163+
JET.@test_opt logabsgamma(1.0)
164+
JET.@test_call logabsgamma(1f0)
165+
JET.@test_opt logabsgamma(1f0)
166+
end

0 commit comments

Comments
 (0)