Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move SpecialFunctions to an extension #100

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ Calculus = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9"
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"

[weakdeps]
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"

[extensions]
DualNumbersSpecialFunctionsExt = "SpecialFunctions"

[compat]
Calculus = "0.5"
NaNMath = "0.3, 1"
Expand All @@ -15,7 +21,8 @@ julia = "0.7, 1.0"

[extras]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["LinearAlgebra", "Test"]
test = ["LinearAlgebra", "Test", "SpecialFunctions"]
29 changes: 29 additions & 0 deletions ext/DualNumbersSpecialFunctionsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module DualNumbersSpecialFunctionsExt

using DualNumbers
using DualNumbers: value, epsilon, to_nanmath
using NaNMath
using Calculus
using SpecialFunctions

for (funsym, expr) in Calculus.symbolic_derivatives_1arg()
if isdefined(SpecialFunctions, funsym) && !isdefined(Base, funsym)
@eval function SpecialFunctions.$(funsym)(z::Dual)
x = value(z)
xp = epsilon(z)
Dual($(funsym)(x),xp*$expr)
end
end
# extend corresponding NaNMath methods
if funsym in (:lgamma,)
funsym = Expr(:.,:NaNMath,Base.Meta.quot(funsym))
@eval function $(funsym)(z::Dual)
x = value(z)
xp = epsilon(z)
Dual($(funsym)(x),xp*$(to_nanmath(expr)))

Check warning on line 23 in ext/DualNumbersSpecialFunctionsExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/DualNumbersSpecialFunctionsExt.jl#L20-L23

Added lines #L20 - L23 were not covered by tests
end
end
end


end
5 changes: 4 additions & 1 deletion src/DualNumbers.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module DualNumbers

using SpecialFunctions
import NaNMath
import Calculus

Expand All @@ -26,4 +25,8 @@ export
ɛ,
imɛ

if !isdefined(Base, :get_extension)
include("../ext/DualNumbersSpecialFunctionsExt.jl")
end

end # module
16 changes: 5 additions & 11 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -303,31 +303,25 @@ to_nanmath(x) = x



for (funsym, exp) in Calculus.symbolic_derivatives_1arg()
for (funsym, expr) in Calculus.symbolic_derivatives_1arg()
funsym == :exp && continue
funsym == :abs2 && continue
funsym == :inv && continue
if isdefined(SpecialFunctions, funsym)
@eval function SpecialFunctions.$(funsym)(z::Dual)
x = value(z)
xp = epsilon(z)
Dual($(funsym)(x),xp*$exp)
end
elseif isdefined(Base, funsym)
if isdefined(Base, funsym)
@eval function Base.$(funsym)(z::Dual)
x = value(z)
xp = epsilon(z)
Dual($(funsym)(x),xp*$exp)
Dual($(funsym)(x),xp*$expr)
end
end
# extend corresponding NaNMath methods
if funsym in (:sin, :cos, :tan, :asin, :acos, :acosh, :atanh, :log, :log2, :log10,
:lgamma, :log1p)
:log1p)
funsym = Expr(:.,:NaNMath,Base.Meta.quot(funsym))
@eval function $(funsym)(z::Dual)
x = value(z)
xp = epsilon(z)
Dual($(funsym)(x),xp*$(to_nanmath(exp)))
Dual($(funsym)(x),xp*$(to_nanmath(expr)))
end
end
end
Expand Down
Loading