Skip to content

Commit f29a465

Browse files
committed
add besselj method
1 parent 6c8be6f commit f29a465

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/besselj.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ besselj(nu, x::Real) = _besselj(nu, float(x))
224224

225225
_besselj(nu::Union{Int16, Float16}, x::Union{Int16, Float16}) = Float16(_besselj(Float32(nu), Float32(x)))
226226

227+
_besselj(nu::AbstractRange, x::T) where T = besselj!(zeros(T, length(nu)), nu, x)
228+
227229
function _besselj(nu::T, x::T) where T <: Union{Float32, Float64}
228230
isinteger(nu) && return _besselj(Int(nu), x)
229231
abs_nu = abs(nu)
@@ -269,12 +271,15 @@ function _besselj(nu::Integer, x::T) where T <: Union{Float32, Float64}
269271
end
270272
end
271273

272-
function _besselj(nu::AbstractRange, x::T) where T
274+
besselj!(out::DenseVector, nu::AbstractRange, x) = _besselj!(out, nu, float(x))
275+
276+
function _besselj!(out::DenseVector{T}, nu::AbstractVector, x::T) where T <: Union{Float32, Float64}
273277
(nu[1] >= 0 && step(nu) == 1) || throw(ArgumentError("nu must be >= 0 with step(nu)=1"))
274-
len = length(nu)
278+
len = length(out)
279+
!isequal(len, length(nu)) && throw(ArgumentError("out and nu must have the same length"))
280+
275281
isone(len) && return [besselj(nu[1], x)]
276282

277-
out = zeros(T, len)
278283
if nu[end] < x
279284
out[1], out[2] = _besselj(nu[1], x), _besselj(nu[2], x)
280285
return besselj_up_recurrence!(out, x, nu)

test/besselj_test.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ end
124124
@test besselj(0:95, 100.0) SpecialFunctions.besselj.(0:95, 100.0) rtol=1e-11
125125
@test besselj(0.5:1:150.5, 2.0) SpecialFunctions.besselj.(0.5:1:150.5, 2.0) rtol=1e-11
126126
@test besselj(0.5:1:10.5, 40.0) SpecialFunctions.besselj.(0.5:1:10.5, 40.0) rtol=1e-11
127+
@test Bessels.besselj!(zeros(Float64, 10), 1:10, 1.0) besselj(1:10, 1.0)
127128

128129
# test Float16 and Float32
129130
@test besselj(Int16(10), Float16(1.0)) isa Float16

0 commit comments

Comments
 (0)