Skip to content

Commit

Permalink
Dualbesselix (#11)
Browse files Browse the repository at this point in the history
* bugfix Dual of besselix
  • Loading branch information
jwscook authored Oct 20, 2023
1 parent 7393b7b commit a51e8e7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/Maths.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ end


import SpecialFunctions.besselix
function besselix(n::Integer, x::DualNumbers.Dual)
function besselix(n::Integer, x::DualNumbers.Dual{T}) where T
r, d = realpart(x), dualpart(x)
bix = besselix(n, r)
didr = (besseli(n - 1, r) + besseli(n + 1, r)) / 2
return Dual(bix,
d * ((besselix(n - 1, r) + besselix(n + 1, r)) / 2 - abs(real(r)) * bix))
d * (didr * exp(-abs(real(r))) - sign(real(r)) * bix))
end


Expand Down
17 changes: 16 additions & 1 deletion test/maths/Maths.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ println("$(now()) $(@__FILE__)")

using LinearMaxwellVlasov
using Test, SpecialFunctions, QuadGK, HCubature, StaticArrays, Random
using DualNumbers
using DualNumbers, ForwardDiff

const LMV = LinearMaxwellVlasov

Expand Down Expand Up @@ -128,4 +128,19 @@ const LMV = LinearMaxwellVlasov
(besselj(0+im, 1.0) - besselj(2.0+im, 1.0)) / 2
end

@testset "Duals vs ForwardDiff" begin
for n in (3, 4, -3, -4), x in (-5.0, 5.0)
@test DualNumbers.dualpart(besselix(n, Dual(x, 1)))
ForwardDiff.derivative(z->besselix(n, z), x)
@test DualNumbers.dualpart(besselj(n, Dual(x, 1)))
ForwardDiff.derivative(z->besselj(n, z), x)
end
for n in (3, 4), x in (-5.0, 5.0)
@assert ForwardDiff.derivative(z->besseli(n, z), x)
(besseli(n-1, x) + besseli(n+1, x)) / 2
@test DualNumbers.dualpart(n^Dual(x, 1))
ForwardDiff.derivative(z->n^z, x)
end
end

end

0 comments on commit a51e8e7

Please sign in to comment.