Skip to content

Commit 08d3600

Browse files
committed
add other methods
1 parent f29a465 commit 08d3600

File tree

7 files changed

+33
-17
lines changed

7 files changed

+33
-17
lines changed

src/besseli.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ besseli(nu, x::Real) = _besseli(nu, float(x))
169169

170170
_besseli(nu::Union{Int16, Float16}, x::Union{Int16, Float16}) = Float16(_besseli(Float32(nu), Float32(x)))
171171

172+
_besseli(nu::AbstractRange, x::T) where T = besseli!(zeros(T, length(nu)), nu, x)
173+
172174
function _besseli(nu::T, x::T) where T <: Union{Float32, Float64}
173175
isinteger(nu) && return _besseli(Int(nu), x)
174176
~isfinite(x) && return x
@@ -206,11 +208,13 @@ function _besseli(nu::Integer, x::T) where T <: Union{Float32, Float64}
206208
end
207209
end
208210

209-
function _besseli(nu::AbstractRange, x::T) where T
211+
besseli!(out::DenseVector, nu::AbstractRange, x) = _besseli!(out, nu, float(x))
212+
213+
function _besseli!(out::DenseVector{T}, nu::AbstractRange, x::T) where T
210214
(nu[1] >= 0 && step(nu) == 1) || throw(ArgumentError("nu must be >= 0 with step(nu)=1"))
211-
len = length(nu)
212-
isone(len) && return [besseli(nu[1], x)]
213-
out = zeros(T, len)
215+
len = length(out)
216+
!isequal(len, length(nu)) && throw(ArgumentError("out and nu must have the same length"))
217+
214218
k = len
215219
inu = zero(T)
216220
while abs(inu) < floatmin(T)

src/besselj.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,14 @@ end
274274
besselj!(out::DenseVector, nu::AbstractRange, x) = _besselj!(out, nu, float(x))
275275

276276
function _besselj!(out::DenseVector{T}, nu::AbstractVector, x::T) where T <: Union{Float32, Float64}
277-
(nu[1] >= 0 && step(nu) == 1) || throw(ArgumentError("nu must be >= 0 with step(nu)=1"))
277+
(nu[begin] >= 0 && step(nu) == 1) || throw(ArgumentError("nu must be >= 0 with step(nu)=1"))
278278
len = length(out)
279279
!isequal(len, length(nu)) && throw(ArgumentError("out and nu must have the same length"))
280280

281-
isone(len) && return [besselj(nu[1], x)]
281+
isone(len) && return [besselj(nu[begin], x)]
282282

283283
if nu[end] < x
284-
out[1], out[2] = _besselj(nu[1], x), _besselj(nu[2], x)
284+
out[begin], out[begin + 1] = _besselj(nu[begin], x), _besselj(nu[begin + 1], x)
285285
return besselj_up_recurrence!(out, x, nu)
286286
else
287287
k = len
@@ -298,8 +298,8 @@ function _besselj!(out::DenseVector{T}, nu::AbstractVector, x::T) where T <: Uni
298298
end
299299
if k > 1
300300
out[k] = _besselj(nu[k], x)
301-
tmp = @view out[1:k+1]
302-
besselj_down_recurrence!(tmp, x, nu[1:k+1])
301+
tmp = @view out[begin:k+1]
302+
besselj_down_recurrence!(tmp, x, nu[begin:k+1])
303303
return out
304304
else
305305
return out

src/besselk.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ besselk(nu, x::Real) = _besselk(nu, float(x))
191191

192192
_besselk(nu::Union{Int16, Float16}, x::Union{Int16, Float16}) = Float16(_besselk(Float32(nu), Float32(x)))
193193

194+
_besselk(nu::AbstractRange, x::T) where T = besselk!(zeros(T, length(nu)), nu, x)
195+
194196
function _besselk(nu::T, x::T) where T <: Union{Float32, Float64}
195197
isinteger(nu) && return _besselk(Int(nu), x)
196198
abs_nu = abs(nu)
@@ -216,11 +218,14 @@ function _besselk(nu::Integer, x::T) where T <: Union{Float32, Float64}
216218
end
217219
end
218220

219-
function _besselk(nu::AbstractRange, x::T) where T
221+
besselk!(out::DenseVector, nu::AbstractRange, x) = _besselk!(out, nu, float(x))
222+
223+
function _besselk!(out::DenseVector{T}, nu::AbstractRange, x::T) where T
220224
(nu[1] >= 0 && step(nu) == 1) || throw(ArgumentError("nu must be >= 0 with step(nu)=1"))
221-
len = length(nu)
225+
len = length(out)
226+
!isequal(len, length(nu)) && throw(ArgumentError("out and nu must have the same length"))
222227
isone(len) && return [besselk(nu[1], x)]
223-
out = zeros(T, len)
228+
224229
k = 1
225230
knu = zero(T)
226231
while abs(knu) < floatmin(T)

src/bessely.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ bessely(nu, x::Real) = _bessely(nu, float(x))
261261

262262
_bessely(nu::Union{Int16, Float16}, x::Union{Int16, Float16}) = Float16(_bessely(Float32(nu), Float32(x)))
263263

264+
_bessely(nu::AbstractRange, x::T) where T = bessely!(zeros(T, length(nu)), nu, x)
265+
264266
function _bessely(nu::T, x::T) where T <: Union{Float32, Float64}
265267
isnan(nu) || isnan(x) && return NaN
266268
isinteger(nu) && return _bessely(Int(nu), x)
@@ -310,10 +312,12 @@ function _bessely(nu::Integer, x::T) where T <: Union{Float32, Float64}
310312
end
311313
end
312314

313-
function _bessely(nu::AbstractRange, x::T) where T
314-
(nu[1] >= 0 && step(nu) == 1) || throw(ArgumentError("nu must be >= 0 with step(nu)=1"))
315-
out = Vector{T}(undef, length(nu))
316-
out[1], out[2] = _bessely(nu[1], x), _bessely(nu[2], x)
315+
bessely!(out::DenseVector, nu::AbstractRange, x) = _bessely!(out, nu, float(x))
316+
317+
function _bessely!(out::DenseVector{T}, nu::AbstractRange, x::T) where T
318+
(nu[begin] >= 0 && step(nu) == 1) || throw(ArgumentError("nu must be >= 0 with step(nu)=1"))
319+
!isequal(length(out), length(nu)) && throw(ArgumentError("out and nu must have the same length"))
320+
out[begin], out[begin + 1] = _bessely(nu[begin], x), _bessely(nu[begin + 1], x)
317321
return besselj_up_recurrence!(out, x, nu)
318322
end
319323

test/besseli_test.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ end
109109
# test nu_range
110110
@test besseli(0:250, 2.0) SpecialFunctions.besseli.(0:250, 2.0) rtol=1e-13
111111
@test besseli(0.5:1:10.5, 2.0) SpecialFunctions.besseli.(0.5:1:10.5, 2.0) rtol=1e-13
112+
@test Bessels.besseli!(zeros(Float64, 10), 1:10, 1.0) besseli(1:10, 1.0)
112113

113114
### need to fix method ambiguities for other functions ######
114115

test/besselk_test.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ end
130130
@test besselk(0:50, 2.0) SpecialFunctions.besselk.(0:50, 2.0) rtol=1e-13
131131
@test besselk(0.5:1:10.5, 12.0) SpecialFunctions.besselk.(0.5:1:10.5, 12.0) rtol=1e-13
132132
@test besselk(1:700, 800.0) SpecialFunctions.besselk.(1:700, 800.0)
133+
@test Bessels.besselk!(zeros(Float64, 10), 1:10, 1.0) besselk(1:10, 1.0)
133134

134135
# test Float16
135136
@test besselk(Int16(10), Float16(1.0)) isa Float16

test/bessely_test.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ end
8888

8989
# test decimal orders
9090
# SpecialFunctions.jl can give errors over 1e-12 so need to soften tolerance to match
91-
# need to switch tests over to ArbNumerics.jl for better precision tests
91+
# need to switch tests over to ArbNumerics.jl for better precision tests
9292
x = [0.05, 0.1, 0.2, 0.25, 0.3, 0.4, 0.5,0.55, 0.6,0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.92, 0.95, 0.97, 0.99, 1.0, 1.01, 1.05, 1.08, 1.1, 1.2, 1.4, 1.5, 1.6, 1.8, 2.0, 2.5, 3.0, 4.0, 4.5, 4.99, 5.1]
9393
nu = [0.1, 0.4567, 0.8123, 1.5, 2.5, 4.1234, 6.8, 12.3, 18.9, 28.2345, 38.1235, 51.23, 72.23435, 80.5, 98.5, 104.2]
9494
for v in nu, xx in x
@@ -104,6 +104,7 @@ end
104104
@test bessely(0:50, 100.0) SpecialFunctions.bessely.(0:50, 100.0) rtol=1e-11
105105
@test bessely(0.5:1:10.5, 2.0) SpecialFunctions.bessely.(0.5:1:10.5, 2.0) rtol=1e-11
106106
@test bessely(0.5:1:10.5, 40.0) SpecialFunctions.bessely.(0.5:1:10.5, 40.0) rtol=1e-11
107+
@test Bessels.bessely!(zeros(Float64, 10), 1:10, 1.0) bessely(1:10, 1.0)
107108

108109
# test Float16
109110
@test bessely(Int16(10), Float16(1.0)) isa Float16

0 commit comments

Comments
 (0)