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

add ForwardDiff and ChainRules extension #29

Merged
merged 17 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 9 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,13 @@ julia = "1.0"
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[weakdeps]
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

[extensions]
PolyLogForwardDiffExt = "ForwardDiff"
PolyLogChainRulesExt = "ChainRulesCore"

[targets]
test = ["Test", "DelimitedFiles"]
test = ["Test", "DelimitedFiles", "ForwardDiff"]
10 changes: 10 additions & 0 deletions ext/PolyLogChainRulesExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module PolyLogChainRulesExt

using PolyLog
using ChainRulesCore
@scalar_rule reli4(x) reli3(x)/x
@scalar_rule reli3(x) reli2(x)/x
@scalar_rule reli2(x) reli1(x)/x
@scalar_rule reli1(x) one(x)/(one(x)-x)
@scalar_rule reli(n,x) reli(n-1,x)/x
end #module
Expander marked this conversation as resolved.
Show resolved Hide resolved
41 changes: 41 additions & 0 deletions ext/PolyLogForwardDiffExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module PolyLogForwardDiffExt

using PolyLog
using ForwardDiff
using ForwardDiff: Dual, partials
function PolyLog.reli4(d::Dual{T}) where T
val = ForwardDiff.value(d)
x = reli4(val)
dx = reli3(val)/val
return Dual{T}(x, dx * partials(d))
end

function PolyLog.reli3(d::Dual{T}) where T
val = ForwardDiff.value(d)
x = reli3(val)
dx = reli2(val)/val
return Dual{T}(x, dx * partials(d))
end

function PolyLog.reli2(d::Dual{T}) where T
val = ForwardDiff.value(d)
x = reli2(val)
dx = reli1(val)/val
return Dual{T}(x, dx * partials(d))
end

function PolyLog.reli1(d::Dual{T}) where T
val = ForwardDiff.value(d)
x = reli1(val)
dx = one(val) / (one(val) - val)
return Dual{T}(x, dx*partials(d))
end

function PolyLog.reli(n::Integer,d::Dual{T}) where T
val = ForwardDiff.value(d)
x = reli(val)
dx = PolyLog.reli(n-1,val)/val
return Dual{T}(x, dx*partials(d))
end

end #module
Expander marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions test/Li.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,7 @@ end
end
end
end

#ForwardDiff Test
@test ForwardDiff.derivative(Base.Fix1(PolyLog.reli,6),float(pi)) == PolyLog.reli(5,float(pi))/float(pi)
end
3 changes: 3 additions & 0 deletions test/Li1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@
# test value that causes overflow if squared
@test PolyLog.li1(1e300 + 1im) ≈ -690.77552789821371 + 3.14159265358979im rtol=eps(Float64)
@test PolyLog.li1(1.0 + 1e300im) ≈ -690.77552789821371 + 1.5707963267948966im rtol=eps(Float64)

#ForwardDiff Test
@test ForwardDiff.derivative(PolyLog.reli1,float(pi)) == 1/(1 - pi)
end
3 changes: 3 additions & 0 deletions test/Li2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,7 @@
# test value that causes overflow if squared
@test PolyLog.li2(1e300 + 1im) ≈ -238582.12510339421 + 2170.13532372464im rtol=eps(Float64)
@test PolyLog.li2(1.0 + 1e300im) ≈ -238585.82620504462 + 1085.06766186232im rtol=eps(Float64)

#ForwardDiff Test
@test ForwardDiff.derivative(PolyLog.reli2,float(pi)) == reli1(float(pi))/float(pi)
end
3 changes: 3 additions & 0 deletions test/Li3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,7 @@
# test value that causes overflow if squared
@test PolyLog.li3(1e300 + 1im) ≈ -5.4934049431527088e7 + 749538.186928224im rtol=eps(Float64)
@test PolyLog.li3(1.0 + 1e300im) ≈ -5.4936606061973454e7 + 374771.031356405im rtol=eps(Float64)

#ForwardDiff Test
@test ForwardDiff.derivative(PolyLog.reli3,float(pi)) == reli2(float(pi))/float(pi)
end
3 changes: 3 additions & 0 deletions test/Li4.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,7 @@
# test value that causes overflow if squared
@test PolyLog.li4(1e300 + 1im) ≈ -9.4863817894708364e9 + 1.725875455850714e8im rtol=eps(Float64)
@test PolyLog.li4(1.0 + 1e300im) ≈ -9.4872648206269765e9 + 8.62951114411071e7im rtol=eps(Float64)

#ForwardDiff Test
@test ForwardDiff.derivative(PolyLog.reli4,float(pi)) == reli3(float(pi))/float(pi)
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Test
import PolyLog
import ForwardDiff

include("TestPrecision.jl")
include("DataReader.jl")
Expand Down
Loading