Skip to content

Commit dca815d

Browse files
authored
Merge pull request #42 from JuliaMath/improve_compilation
Reduce time to first bessely
2 parents c8a6951 + 9947757 commit dca815d

File tree

8 files changed

+117
-136
lines changed

8 files changed

+117
-136
lines changed

src/Bessels.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,6 @@ include("Polynomials/besselj_polys.jl")
5757
include("asymptotics.jl")
5858
include("gamma.jl")
5959

60+
precompile(besselj, (Float64, Float64))
61+
6062
end

src/asymptotics.jl

Lines changed: 89 additions & 126 deletions
Large diffs are not rendered by default.

src/besselj.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,11 @@ nu and x must be real where nu and x can be positive or negative.
200200
"""
201201
besselj(nu::Real, x::Real) = _besselj(nu, float(x))
202202

203+
_besselj(nu, x::Float32) = Float32(_besselj(nu, Float64(x)))
204+
203205
_besselj(nu, x::Float16) = Float16(_besselj(nu, Float32(x)))
204206

205-
function _besselj(nu, x::T) where T <: Union{Float32, Float64}
207+
function _besselj(nu, x::T) where T <: Float64
206208
isinteger(nu) && return _besselj(Int(nu), x)
207209
abs_nu = abs(nu)
208210
abs_x = abs(x)
@@ -228,7 +230,7 @@ function _besselj(nu, x::T) where T <: Union{Float32, Float64}
228230
end
229231
end
230232

231-
function _besselj(nu::Integer, x::T) where T <: Union{Float32, Float64}
233+
function _besselj(nu::Integer, x::T) where T <: Float64
232234
abs_nu = abs(nu)
233235
abs_x = abs(x)
234236
sg = iseven(abs_nu) ? 1 : -1
@@ -318,7 +320,7 @@ function besselj_power_series(v, x::T) where T
318320
end
319321

320322
besselj_series_cutoff(v, x::Float64) = (x < 7.0) || v > (2 + x*(0.109 + 0.062x))
321-
besselj_series_cutoff(v, x::Float32) = (x < 20.0) || v > (14.4 + x*(-0.455 + 0.027x))
323+
#besselj_series_cutoff(v, x::Float32) = (x < 20.0) || v > (14.4 + x*(-0.455 + 0.027x))
322324

323325
#=
324326
# this needs a better way to sum these as it produces large errors

src/bessely.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,11 @@ nu and x must be real where nu and x can be positive or negative.
237237
"""
238238
bessely(nu::Real, x::Real) = _bessely(nu, float(x))
239239

240+
_bessely(nu, x::Float32) = Float32(_bessely(nu, Float64(x)))
241+
240242
_bessely(nu, x::Float16) = Float16(_bessely(nu, Float32(x)))
241243

242-
function _bessely(nu, x::T) where T <: Union{Float32, Float64}
244+
function _bessely(nu, x::T) where T <: Float64
243245
isnan(nu) || isnan(x) && return NaN
244246
isinteger(nu) && return _bessely(Int(nu), x)
245247
abs_nu = abs(nu)
@@ -264,7 +266,8 @@ function _bessely(nu, x::T) where T <: Union{Float32, Float64}
264266
end
265267
end
266268
end
267-
function _bessely(nu::Integer, x::T) where T <: Union{Float32, Float64}
269+
270+
function _bessely(nu::Integer, x::T) where T <: Float64
268271
abs_nu = abs(nu)
269272
abs_x = abs(x)
270273
sg = iseven(abs_nu) ? 1 : -1

src/sphericalbessel.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ solution to the radial part of the Helmholz equation in spherical coordinates.
2424
"""
2525
sphericalbesselj(nu::Real, x::Real) = _sphericalbesselj(nu, float(x))
2626

27+
_sphericalbesselj(nu, x::Float32) = Float32(_sphericalbesselj(nu, Float64(x)))
28+
2729
_sphericalbesselj(nu, x::Float16) = Float16(_sphericalbesselj(nu, Float32(x)))
2830

29-
function _sphericalbesselj(nu::Real, x::T) where T <: Union{Float32, Float64}
31+
function _sphericalbesselj(nu::Real, x::T) where T <: Float64
3032
x < zero(T) && return throw(DomainError(x, "Complex result returned for real arguments. Complex arguments are currently not supported"))
3133
if ~isfinite(x)
3234
isnan(x) && return x
@@ -110,9 +112,11 @@ known as a spherical Neumann function.
110112
"""
111113
sphericalbessely(nu::Real, x::Real) = _sphericalbessely(nu, float(x))
112114

115+
_sphericalbessely(nu, x::Float32) = Float32(_sphericalbessely(nu, Float64(x)))
116+
113117
_sphericalbessely(nu, x::Float16) = Float16(_sphericalbessely(nu, Float32(x)))
114118

115-
function _sphericalbessely(nu::Real, x::T) where T <: Union{Float32, Float64}
119+
function _sphericalbessely(nu::Real, x::T) where T <: Float64
116120
x < zero(T) && return throw(DomainError(x, "Complex result returned for real arguments. Complex arguments are currently not supported"))
117121
if ~isfinite(x)
118122
isnan(x) && return x

test/besselj_test.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,20 @@ for v in nu, xx in x
110110
@test isapprox(Bessels.besseljy_positive_args(v, xx)[1], Float64(sf), rtol=5e-11)
111111
end
112112

113-
# test Float16
113+
# test Float16 and Float32
114114
@test besselj(10, Float16(1.0)) isa Float16
115+
@test besselj(10.2f0, 1.0f0) isa Float32
115116

116117
## test large arguments
117118
@test isapprox(besselj(10.0, 150.0), SpecialFunctions.besselj(10.0, 150.0), rtol=1e-12)
119+
@test isapprox(Bessels.besseljy_large_argument(15.0, 100.0)[1], SpecialFunctions.besselj(15.0, 100.0), rtol=1e-12)
120+
@test isapprox(Bessels.besseljy_large_argument(15.0, 45.0)[1], SpecialFunctions.besselj(15.0, 45.0), rtol=1e-12)
121+
@test isapprox(Bessels.besseljy_large_argument(15.0, 25.5)[1], SpecialFunctions.besselj(15.0, 25.5), rtol=1e-12)
118122

119123
# test BigFloat for single point
120124
@test isapprox(Bessels.besseljy_debye(big"2000", big"1500.0")[1], SpecialFunctions.besselj(big"2000", big"1500"), rtol=5e-20)
121125
@test isapprox(Bessels.besseljy_large_argument(big"20", big"1500.0")[1], SpecialFunctions.besselj(big"20", big"1500"), rtol=5e-20)
122126

123-
124127
# need to test accuracy of negative orders and negative arguments and all combinations within
125128
# SpecialFunctions.jl doesn't provide these so will hand check against hard values
126129
# values taken from https://keisan.casio.com/exec/system/1180573474 which match mathematica

test/bessely_test.jl

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

9292
# test Float16
9393
@test bessely(10, Float16(1.0)) isa Float16
94+
@test bessely(10.2f0, 1.0f0) isa Float32
9495

9596
# test limits for small arguments see https://github.com/JuliaMath/Bessels.jl/issues/35
9697
@test bessely(185.0, 1.01) == -Inf

test/sphericalbessel_test.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ x = 1e-15
3232
@test isnan(Bessels.sphericalbessely(1.4, NaN))
3333
@test isnan(Bessels.sphericalbessely(4.0, NaN))
3434

35-
# test Float16 types
35+
# test Float16, Float32 types
3636
@test Bessels.sphericalbesselj(Float16(1.4), Float16(1.2)) isa Float16
3737
@test Bessels.sphericalbessely(Float16(1.4), Float16(1.2)) isa Float16
38+
@test Bessels.sphericalbesselj(1.4f0, 1.2f0) isa Float32
39+
@test Bessels.sphericalbessely(1.4f0, 1.2f0) isa Float32
40+
3841

3942
for x in 0.5:1.0:100.0, v in [0, 1, 5.5, 8.2, 10]
4043
@test isapprox(Bessels.sphericalbesselj(v, x), SpecialFunctions.sphericalbesselj(v, x), rtol=1e-12)

0 commit comments

Comments
 (0)