From 232907da58277c3669776f2f729a49b09e0ece34 Mon Sep 17 00:00:00 2001 From: longemen3000 Date: Sun, 2 Jun 2024 01:06:51 -0400 Subject: [PATCH 01/16] add ForwardDiff and ChainRules extension --- Project.toml | 10 ++++++++- ext/PolyLogChainRulesExt.jl | 10 +++++++++ ext/PolyLogForwardDiffExt.jl | 41 ++++++++++++++++++++++++++++++++++++ test/Li.jl | 3 +++ test/Li1.jl | 3 +++ test/Li2.jl | 3 +++ test/Li3.jl | 3 +++ test/Li4.jl | 3 +++ test/runtests.jl | 1 + 9 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 ext/PolyLogChainRulesExt.jl create mode 100644 ext/PolyLogForwardDiffExt.jl diff --git a/Project.toml b/Project.toml index 8f4fd2c0..9cae4038 100644 --- a/Project.toml +++ b/Project.toml @@ -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"] diff --git a/ext/PolyLogChainRulesExt.jl b/ext/PolyLogChainRulesExt.jl new file mode 100644 index 00000000..e16bba9a --- /dev/null +++ b/ext/PolyLogChainRulesExt.jl @@ -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 \ No newline at end of file diff --git a/ext/PolyLogForwardDiffExt.jl b/ext/PolyLogForwardDiffExt.jl new file mode 100644 index 00000000..32188c5d --- /dev/null +++ b/ext/PolyLogForwardDiffExt.jl @@ -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 \ No newline at end of file diff --git a/test/Li.jl b/test/Li.jl index 4b93d5e4..870a567b 100644 --- a/test/Li.jl +++ b/test/Li.jl @@ -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)) end diff --git a/test/Li1.jl b/test/Li1.jl index d3dc8fde..cbf1a76d 100644 --- a/test/Li1.jl +++ b/test/Li1.jl @@ -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 diff --git a/test/Li2.jl b/test/Li2.jl index 580d9378..e937b22b 100644 --- a/test/Li2.jl +++ b/test/Li2.jl @@ -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)) end diff --git a/test/Li3.jl b/test/Li3.jl index dc0dddb6..c37bcc9c 100644 --- a/test/Li3.jl +++ b/test/Li3.jl @@ -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)) end diff --git a/test/Li4.jl b/test/Li4.jl index 70e6aaf2..620e2ddb 100644 --- a/test/Li4.jl +++ b/test/Li4.jl @@ -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)) end diff --git a/test/runtests.jl b/test/runtests.jl index 5a8a7fc9..52a3bac9 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,5 +1,6 @@ using Test import PolyLog +import ForwardDiff include("TestPrecision.jl") include("DataReader.jl") From 4213578233117a25dcb59e752c75ddaf4dae434f Mon Sep 17 00:00:00 2001 From: longemen3000 Date: Sun, 2 Jun 2024 01:15:50 -0400 Subject: [PATCH 02/16] forgot to divide by x in test --- test/Li.jl | 2 +- test/Li2.jl | 2 +- test/Li3.jl | 2 +- test/Li4.jl | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/Li.jl b/test/Li.jl index 870a567b..c07eb9d5 100644 --- a/test/Li.jl +++ b/test/Li.jl @@ -130,5 +130,5 @@ end end #ForwardDiff Test - @test ForwardDiff.derivative(Base.Fix1(PolyLog.reli,6),float(pi)) == PolyLog.reli(5,float(pi)) + @test ForwardDiff.derivative(Base.Fix1(PolyLog.reli,6),float(pi)) == PolyLog.reli(5,float(pi))/float(pi) end diff --git a/test/Li2.jl b/test/Li2.jl index e937b22b..a23a4dbc 100644 --- a/test/Li2.jl +++ b/test/Li2.jl @@ -68,5 +68,5 @@ @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)) + @test ForwardDiff.derivative(PolyLog.reli2,float(pi)) == reli1(float(pi))/float(pi) end diff --git a/test/Li3.jl b/test/Li3.jl index c37bcc9c..3a855798 100644 --- a/test/Li3.jl +++ b/test/Li3.jl @@ -62,5 +62,5 @@ @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)) + @test ForwardDiff.derivative(PolyLog.reli3,float(pi)) == reli2(float(pi))/float(pi) end diff --git a/test/Li4.jl b/test/Li4.jl index 620e2ddb..f3e14eb8 100644 --- a/test/Li4.jl +++ b/test/Li4.jl @@ -54,5 +54,5 @@ @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)) + @test ForwardDiff.derivative(PolyLog.reli4,float(pi)) == reli3(float(pi))/float(pi) end From feb22c76d72cc2a9a2f793c7507ef1d445b9d16a Mon Sep 17 00:00:00 2001 From: longemen3000 Date: Sun, 2 Jun 2024 13:12:05 -0400 Subject: [PATCH 03/16] add missing module prefixes and spaces --- ext/PolyLogChainRulesExt.jl | 13 +++++++------ test/Li2.jl | 2 +- test/Li3.jl | 2 +- test/Li4.jl | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/ext/PolyLogChainRulesExt.jl b/ext/PolyLogChainRulesExt.jl index e16bba9a..caad9137 100644 --- a/ext/PolyLogChainRulesExt.jl +++ b/ext/PolyLogChainRulesExt.jl @@ -2,9 +2,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 \ No newline at end of file +@scalar_rule PolyLog.reli4(x) PolyLog.reli3(x)/x +@scalar_rule PolyLog.reli3(x) PolyLog.reli2(x)/x +@scalar_rule PolyLog.reli2(x) PolyLog.reli1(x)/x +@scalar_rule PolyLog.reli1(x) one(x)/(one(x)-x) +@scalar_rule PolyLog.reli(n,x) PolyLog.reli(n-1,x)/x +end #module + diff --git a/test/Li2.jl b/test/Li2.jl index a23a4dbc..688db0c5 100644 --- a/test/Li2.jl +++ b/test/Li2.jl @@ -68,5 +68,5 @@ @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) + @test ForwardDiff.derivative(PolyLog.reli2,float(pi)) == ForwardDiff.reli1(float(pi))/float(pi) end diff --git a/test/Li3.jl b/test/Li3.jl index 3a855798..9dc2d7e7 100644 --- a/test/Li3.jl +++ b/test/Li3.jl @@ -62,5 +62,5 @@ @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) + @test ForwardDiff.derivative(PolyLog.reli3,float(pi)) == ForwardDiff.reli2(float(pi))/float(pi) end diff --git a/test/Li4.jl b/test/Li4.jl index f3e14eb8..9458b2a1 100644 --- a/test/Li4.jl +++ b/test/Li4.jl @@ -54,5 +54,5 @@ @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) + @test ForwardDiff.derivative(PolyLog.reli4,float(pi)) == ForwardDiff.reli3(float(pi))/float(pi) end From f8c996e57f05fad9a4a2fe0d4af7f46c037d5087 Mon Sep 17 00:00:00 2001 From: longemen3000 Date: Sun, 2 Jun 2024 23:34:48 -0400 Subject: [PATCH 04/16] enbarrasing typo --- test/Li2.jl | 2 +- test/Li3.jl | 2 +- test/Li4.jl | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/Li2.jl b/test/Li2.jl index 688db0c5..2c22fcfc 100644 --- a/test/Li2.jl +++ b/test/Li2.jl @@ -68,5 +68,5 @@ @test PolyLog.li2(1.0 + 1e300im) ≈ -238585.82620504462 + 1085.06766186232im rtol=eps(Float64) #ForwardDiff Test - @test ForwardDiff.derivative(PolyLog.reli2,float(pi)) == ForwardDiff.reli1(float(pi))/float(pi) + @test ForwardDiff.derivative(PolyLog.reli2,float(pi)) == PolyLog.reli1(float(pi))/float(pi) end diff --git a/test/Li3.jl b/test/Li3.jl index 9dc2d7e7..d923b630 100644 --- a/test/Li3.jl +++ b/test/Li3.jl @@ -62,5 +62,5 @@ @test PolyLog.li3(1.0 + 1e300im) ≈ -5.4936606061973454e7 + 374771.031356405im rtol=eps(Float64) #ForwardDiff Test - @test ForwardDiff.derivative(PolyLog.reli3,float(pi)) == ForwardDiff.reli2(float(pi))/float(pi) + @test ForwardDiff.derivative(PolyLog.reli3,float(pi)) == PolyLog.reli2(float(pi))/float(pi) end diff --git a/test/Li4.jl b/test/Li4.jl index 9458b2a1..7b63e47a 100644 --- a/test/Li4.jl +++ b/test/Li4.jl @@ -54,5 +54,5 @@ @test PolyLog.li4(1.0 + 1e300im) ≈ -9.4872648206269765e9 + 8.62951114411071e7im rtol=eps(Float64) #ForwardDiff Test - @test ForwardDiff.derivative(PolyLog.reli4,float(pi)) == ForwardDiff.reli3(float(pi))/float(pi) + @test ForwardDiff.derivative(PolyLog.reli4,float(pi)) == PolyLog.reli3(float(pi))/float(pi) end From 8390cc0c446086fb7c4acd563e9e4f936daa9169 Mon Sep 17 00:00:00 2001 From: longemen3000 Date: Sun, 2 Jun 2024 23:55:59 -0400 Subject: [PATCH 05/16] move test of FD + reli to ni loop, fix error in derivative --- ext/PolyLogForwardDiffExt.jl | 2 +- test/Li.jl | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ext/PolyLogForwardDiffExt.jl b/ext/PolyLogForwardDiffExt.jl index 32188c5d..0c3869dd 100644 --- a/ext/PolyLogForwardDiffExt.jl +++ b/ext/PolyLogForwardDiffExt.jl @@ -33,7 +33,7 @@ end function PolyLog.reli(n::Integer,d::Dual{T}) where T val = ForwardDiff.value(d) - x = reli(val) + x = reli(n,val) dx = PolyLog.reli(n-1,val)/val return Dual{T}(x, dx*partials(d)) end diff --git a/test/Li.jl b/test/Li.jl index c07eb9d5..96e737e7 100644 --- a/test/Li.jl +++ b/test/Li.jl @@ -93,6 +93,8 @@ end @test PolyLog.li(n, 1//1 + 0//1im) ≈ zeta @test PolyLog.li(n, 1 + 0im) ≈ zeta @test PolyLog.li(n, BigFloat("1.0") + 0im) == PolyLog.zeta(n, BigFloat) + + @test ForwardDiff.derivative(Base.Fix1(PolyLog.reli,n),float(pi)) == PolyLog.reli(n,float(pi))/float(pi) end # value close to boundary between series 1 and 2 in arXiv:2010.09860 @@ -130,5 +132,5 @@ end end #ForwardDiff Test - @test ForwardDiff.derivative(Base.Fix1(PolyLog.reli,6),float(pi)) == PolyLog.reli(5,float(pi))/float(pi) + end From cc3ed0885e83415354f0713163491f5915296c74 Mon Sep 17 00:00:00 2001 From: longemen3000 Date: Mon, 3 Jun 2024 01:10:35 -0400 Subject: [PATCH 06/16] add branch to catch x == 0 case --- ext/PolyLogForwardDiffExt.jl | 12 ++++++++++++ test/Li.jl | 7 +++---- test/Li1.jl | 1 + test/Li2.jl | 1 + test/Li3.jl | 1 + test/Li4.jl | 1 + 6 files changed, 19 insertions(+), 4 deletions(-) diff --git a/ext/PolyLogForwardDiffExt.jl b/ext/PolyLogForwardDiffExt.jl index 0c3869dd..6dfe1dbf 100644 --- a/ext/PolyLogForwardDiffExt.jl +++ b/ext/PolyLogForwardDiffExt.jl @@ -5,6 +5,9 @@ using ForwardDiff using ForwardDiff: Dual, partials function PolyLog.reli4(d::Dual{T}) where T val = ForwardDiff.value(d) + if iszero(val) + return Dual{T}(one(val), partials(d)) + end x = reli4(val) dx = reli3(val)/val return Dual{T}(x, dx * partials(d)) @@ -12,6 +15,9 @@ end function PolyLog.reli3(d::Dual{T}) where T val = ForwardDiff.value(d) + if iszero(val) + return Dual{T}(one(val), partials(d)) + end x = reli3(val) dx = reli2(val)/val return Dual{T}(x, dx * partials(d)) @@ -19,6 +25,9 @@ end function PolyLog.reli2(d::Dual{T}) where T val = ForwardDiff.value(d) + if iszero(val) + return Dual{T}(one(val), partials(d)) + end x = reli2(val) dx = reli1(val)/val return Dual{T}(x, dx * partials(d)) @@ -33,6 +42,9 @@ end function PolyLog.reli(n::Integer,d::Dual{T}) where T val = ForwardDiff.value(d) + if iszero(val) + return Dual{T}(one(val), partials(d)) + end x = reli(n,val) dx = PolyLog.reli(n-1,val)/val return Dual{T}(x, dx*partials(d)) diff --git a/test/Li.jl b/test/Li.jl index 96e737e7..1900f524 100644 --- a/test/Li.jl +++ b/test/Li.jl @@ -94,7 +94,9 @@ end @test PolyLog.li(n, 1 + 0im) ≈ zeta @test PolyLog.li(n, BigFloat("1.0") + 0im) == PolyLog.zeta(n, BigFloat) + #ForwardDiff Test @test ForwardDiff.derivative(Base.Fix1(PolyLog.reli,n),float(pi)) == PolyLog.reli(n,float(pi))/float(pi) + @test ForwardDiff.derivative(Base.Fix1(PolyLog.reli,n),0.0) == 1.0 end # value close to boundary between series 1 and 2 in arXiv:2010.09860 @@ -129,8 +131,5 @@ end @test typeof(PolyLog.reli(n, convert(T, x))) == T end end - end - - #ForwardDiff Test - + end end diff --git a/test/Li1.jl b/test/Li1.jl index cbf1a76d..67c7f1d6 100644 --- a/test/Li1.jl +++ b/test/Li1.jl @@ -62,4 +62,5 @@ #ForwardDiff Test @test ForwardDiff.derivative(PolyLog.reli1,float(pi)) == 1/(1 - pi) + @test ForwardDiff.derivative(PolyLog.reli1,0.0) == 1.0 end diff --git a/test/Li2.jl b/test/Li2.jl index 2c22fcfc..cd0f0319 100644 --- a/test/Li2.jl +++ b/test/Li2.jl @@ -69,4 +69,5 @@ #ForwardDiff Test @test ForwardDiff.derivative(PolyLog.reli2,float(pi)) == PolyLog.reli1(float(pi))/float(pi) + @test ForwardDiff.derivative(PolyLog.reli2,0.0) == 1.0 end diff --git a/test/Li3.jl b/test/Li3.jl index d923b630..f3a1f6e9 100644 --- a/test/Li3.jl +++ b/test/Li3.jl @@ -63,4 +63,5 @@ #ForwardDiff Test @test ForwardDiff.derivative(PolyLog.reli3,float(pi)) == PolyLog.reli2(float(pi))/float(pi) + @test ForwardDiff.derivative(PolyLog.reli3,0.0) == 1.0 end diff --git a/test/Li4.jl b/test/Li4.jl index 7b63e47a..8e3cf680 100644 --- a/test/Li4.jl +++ b/test/Li4.jl @@ -55,4 +55,5 @@ #ForwardDiff Test @test ForwardDiff.derivative(PolyLog.reli4,float(pi)) == PolyLog.reli3(float(pi))/float(pi) + @test ForwardDiff.derivative(PolyLog.reli4,0.0) == 1.0 end From adefae572eab4bf3cb3aafacb64b0cc5a67e3a38 Mon Sep 17 00:00:00 2001 From: longemen3000 Date: Mon, 3 Jun 2024 01:15:08 -0400 Subject: [PATCH 07/16] add ifelse in CRC too --- ext/PolyLogChainRulesExt.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ext/PolyLogChainRulesExt.jl b/ext/PolyLogChainRulesExt.jl index caad9137..dd6d567a 100644 --- a/ext/PolyLogChainRulesExt.jl +++ b/ext/PolyLogChainRulesExt.jl @@ -2,10 +2,10 @@ module PolyLogChainRulesExt using PolyLog using ChainRulesCore -@scalar_rule PolyLog.reli4(x) PolyLog.reli3(x)/x -@scalar_rule PolyLog.reli3(x) PolyLog.reli2(x)/x -@scalar_rule PolyLog.reli2(x) PolyLog.reli1(x)/x +@scalar_rule PolyLog.reli4(x) ifelse(iszero(x), one(x), PolyLog.reli3(x)/x) +@scalar_rule PolyLog.reli3(x) ifelse(iszero(x), one(x), PolyLog.reli2(x)/x) +@scalar_rule PolyLog.reli2(x) ifelse(iszero(x), one(x), PolyLog.reli1(x)/x) @scalar_rule PolyLog.reli1(x) one(x)/(one(x)-x) -@scalar_rule PolyLog.reli(n,x) PolyLog.reli(n-1,x)/x +@scalar_rule PolyLog.reli(n,x) ifelse(iszero(x), one(x), PolyLog.reli(n-1,x)/x) end #module From e455404c805bc9d955d50b5f1b362d9a6b60432f Mon Sep 17 00:00:00 2001 From: longemen3000 Date: Mon, 3 Jun 2024 13:44:34 -0400 Subject: [PATCH 08/16] typo in ForwardDiff reli test --- test/Li.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Li.jl b/test/Li.jl index 1900f524..2abbac71 100644 --- a/test/Li.jl +++ b/test/Li.jl @@ -95,7 +95,7 @@ end @test PolyLog.li(n, BigFloat("1.0") + 0im) == PolyLog.zeta(n, BigFloat) #ForwardDiff Test - @test ForwardDiff.derivative(Base.Fix1(PolyLog.reli,n),float(pi)) == PolyLog.reli(n,float(pi))/float(pi) + @test ForwardDiff.derivative(Base.Fix1(PolyLog.reli,n),float(pi)) == PolyLog.reli(n-1,float(pi))/float(pi) @test ForwardDiff.derivative(Base.Fix1(PolyLog.reli,n),0.0) == 1.0 end From fd6c684874a93a155bccd655b4fdbb155d82d004 Mon Sep 17 00:00:00 2001 From: longemen3000 Date: Mon, 3 Jun 2024 14:03:22 -0400 Subject: [PATCH 09/16] only test derivatives on 1.9 onwards --- test/Li.jl | 6 ++++-- test/Li1.jl | 6 ++++-- test/Li2.jl | 6 ++++-- test/Li3.jl | 6 ++++-- test/Li4.jl | 6 ++++-- test/runtests.jl | 4 +++- 6 files changed, 23 insertions(+), 11 deletions(-) diff --git a/test/Li.jl b/test/Li.jl index 2abbac71..d2329324 100644 --- a/test/Li.jl +++ b/test/Li.jl @@ -95,8 +95,10 @@ end @test PolyLog.li(n, BigFloat("1.0") + 0im) == PolyLog.zeta(n, BigFloat) #ForwardDiff Test - @test ForwardDiff.derivative(Base.Fix1(PolyLog.reli,n),float(pi)) == PolyLog.reli(n-1,float(pi))/float(pi) - @test ForwardDiff.derivative(Base.Fix1(PolyLog.reli,n),0.0) == 1.0 + if isdefined(Base,:get_extension) + @test ForwardDiff.derivative(Base.Fix1(PolyLog.reli,n),float(pi)) == PolyLog.reli(n-1,float(pi))/float(pi) + @test ForwardDiff.derivative(Base.Fix1(PolyLog.reli,n),0.0) == 1.0 + end end # value close to boundary between series 1 and 2 in arXiv:2010.09860 diff --git a/test/Li1.jl b/test/Li1.jl index 67c7f1d6..7cf3b738 100644 --- a/test/Li1.jl +++ b/test/Li1.jl @@ -61,6 +61,8 @@ @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) - @test ForwardDiff.derivative(PolyLog.reli1,0.0) == 1.0 + if isdefined(Base,:get_extension) + @test ForwardDiff.derivative(PolyLog.reli1,float(pi)) == 1/(1 - pi) + @test ForwardDiff.derivative(PolyLog.reli1,0.0) == 1.0 + end end diff --git a/test/Li2.jl b/test/Li2.jl index cd0f0319..c843e392 100644 --- a/test/Li2.jl +++ b/test/Li2.jl @@ -68,6 +68,8 @@ @test PolyLog.li2(1.0 + 1e300im) ≈ -238585.82620504462 + 1085.06766186232im rtol=eps(Float64) #ForwardDiff Test - @test ForwardDiff.derivative(PolyLog.reli2,float(pi)) == PolyLog.reli1(float(pi))/float(pi) - @test ForwardDiff.derivative(PolyLog.reli2,0.0) == 1.0 + if isdefined(Base,:get_extension) + @test ForwardDiff.derivative(PolyLog.reli2,float(pi)) == PolyLog.reli1(float(pi))/float(pi) + @test ForwardDiff.derivative(PolyLog.reli2,0.0) == 1.0 + end end diff --git a/test/Li3.jl b/test/Li3.jl index f3a1f6e9..5a2b65ea 100644 --- a/test/Li3.jl +++ b/test/Li3.jl @@ -62,6 +62,8 @@ @test PolyLog.li3(1.0 + 1e300im) ≈ -5.4936606061973454e7 + 374771.031356405im rtol=eps(Float64) #ForwardDiff Test - @test ForwardDiff.derivative(PolyLog.reli3,float(pi)) == PolyLog.reli2(float(pi))/float(pi) - @test ForwardDiff.derivative(PolyLog.reli3,0.0) == 1.0 + if isdefined(Base, :ForwardDiff) + @test ForwardDiff.derivative(PolyLog.reli3,float(pi)) == PolyLog.reli2(float(pi))/float(pi) + @test ForwardDiff.derivative(PolyLog.reli3,0.0) == 1.0 + end end diff --git a/test/Li4.jl b/test/Li4.jl index 8e3cf680..9afe1390 100644 --- a/test/Li4.jl +++ b/test/Li4.jl @@ -54,6 +54,8 @@ @test PolyLog.li4(1.0 + 1e300im) ≈ -9.4872648206269765e9 + 8.62951114411071e7im rtol=eps(Float64) #ForwardDiff Test - @test ForwardDiff.derivative(PolyLog.reli4,float(pi)) == PolyLog.reli3(float(pi))/float(pi) - @test ForwardDiff.derivative(PolyLog.reli4,0.0) == 1.0 + if isdefined(Base,:get_extension) + @test ForwardDiff.derivative(PolyLog.reli4,float(pi)) == PolyLog.reli3(float(pi))/float(pi) + @test ForwardDiff.derivative(PolyLog.reli4,0.0) == 1.0 + end end diff --git a/test/runtests.jl b/test/runtests.jl index 52a3bac9..00798420 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,6 +1,8 @@ using Test import PolyLog -import ForwardDiff +if isdefined(Base,:get_extension) + import ForwardDiff +end include("TestPrecision.jl") include("DataReader.jl") From 57237f42ce416540904ec63b7ca7dc310ecc981a Mon Sep 17 00:00:00 2001 From: longemen3000 Date: Mon, 3 Jun 2024 14:58:07 -0400 Subject: [PATCH 10/16] support 1.0: add ForwardDiff to extras --- Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Project.toml b/Project.toml index 9cae4038..3e68f55f 100644 --- a/Project.toml +++ b/Project.toml @@ -9,6 +9,7 @@ julia = "1.0" [extras] DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" [weakdeps] ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" From 475646a2b45d50e351e89ebf410cc5061f162362 Mon Sep 17 00:00:00 2001 From: longemen3000 Date: Mon, 3 Jun 2024 22:40:28 -0400 Subject: [PATCH 11/16] typo in conditional --- test/Li3.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Li3.jl b/test/Li3.jl index 5a2b65ea..109b46bb 100644 --- a/test/Li3.jl +++ b/test/Li3.jl @@ -62,7 +62,7 @@ @test PolyLog.li3(1.0 + 1e300im) ≈ -5.4936606061973454e7 + 374771.031356405im rtol=eps(Float64) #ForwardDiff Test - if isdefined(Base, :ForwardDiff) + if isdefined(Base, :get_extension) @test ForwardDiff.derivative(PolyLog.reli3,float(pi)) == PolyLog.reli2(float(pi))/float(pi) @test ForwardDiff.derivative(PolyLog.reli3,0.0) == 1.0 end From 7a22a963a675fbe51b8721f596d89200db5c067f Mon Sep 17 00:00:00 2001 From: longemen3000 Date: Tue, 4 Jun 2024 18:59:09 -0400 Subject: [PATCH 12/16] add ChainRules tests --- Project.toml | 3 ++- ext/PolyLogChainRulesExt.jl | 12 ++++++------ test/Li.jl | 2 ++ test/Li1.jl | 2 ++ test/Li2.jl | 2 ++ test/Li3.jl | 2 ++ test/Li4.jl | 2 ++ test/runtests.jl | 1 + 8 files changed, 19 insertions(+), 7 deletions(-) diff --git a/Project.toml b/Project.toml index 3e68f55f..6e732f2e 100644 --- a/Project.toml +++ b/Project.toml @@ -10,6 +10,7 @@ julia = "1.0" DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" +ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a" [weakdeps] ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" @@ -20,4 +21,4 @@ PolyLogForwardDiffExt = "ForwardDiff" PolyLogChainRulesExt = "ChainRulesCore" [targets] -test = ["Test", "DelimitedFiles", "ForwardDiff"] +test = ["Test", "DelimitedFiles", "ForwardDiff", "ChainRulesTestUtils"] diff --git a/ext/PolyLogChainRulesExt.jl b/ext/PolyLogChainRulesExt.jl index dd6d567a..bb58fecf 100644 --- a/ext/PolyLogChainRulesExt.jl +++ b/ext/PolyLogChainRulesExt.jl @@ -2,10 +2,10 @@ module PolyLogChainRulesExt using PolyLog using ChainRulesCore -@scalar_rule PolyLog.reli4(x) ifelse(iszero(x), one(x), PolyLog.reli3(x)/x) -@scalar_rule PolyLog.reli3(x) ifelse(iszero(x), one(x), PolyLog.reli2(x)/x) -@scalar_rule PolyLog.reli2(x) ifelse(iszero(x), one(x), PolyLog.reli1(x)/x) -@scalar_rule PolyLog.reli1(x) one(x)/(one(x)-x) -@scalar_rule PolyLog.reli(n,x) ifelse(iszero(x), one(x), PolyLog.reli(n-1,x)/x) -end #module +ChainRulesCore.@scalar_rule PolyLog.reli4(x) ifelse(iszero(x), one(x), PolyLog.reli3(x)/x) +ChainRulesCore.@scalar_rule PolyLog.reli3(x) ifelse(iszero(x), one(x), PolyLog.reli2(x)/x) +ChainRulesCore.@scalar_rule PolyLog.reli2(x) ifelse(iszero(x), one(x), PolyLog.reli1(x)/x) +ChainRulesCore.@scalar_rule PolyLog.reli1(x) one(x)/(one(x)-x) +ChainRulesCore.@scalar_rule(PolyLog.reli(n, x),(ChainRulesCore.NoTangent(),ifelse(iszero(x), one(x), PolyLog.reli(n-1,x)/x))) +end #module diff --git a/test/Li.jl b/test/Li.jl index d2329324..7045f8fb 100644 --- a/test/Li.jl +++ b/test/Li.jl @@ -98,6 +98,8 @@ end if isdefined(Base,:get_extension) @test ForwardDiff.derivative(Base.Fix1(PolyLog.reli,n),float(pi)) == PolyLog.reli(n-1,float(pi))/float(pi) @test ForwardDiff.derivative(Base.Fix1(PolyLog.reli,n),0.0) == 1.0 + ChainRulesTestUtils.test_frule(PolyLog.reli, n, 0.0) + ChainRulesTestUtils.test_rrule(PolyLog.reli, n, float(pi)) end end diff --git a/test/Li1.jl b/test/Li1.jl index 7cf3b738..307d07cc 100644 --- a/test/Li1.jl +++ b/test/Li1.jl @@ -64,5 +64,7 @@ if isdefined(Base,:get_extension) @test ForwardDiff.derivative(PolyLog.reli1,float(pi)) == 1/(1 - pi) @test ForwardDiff.derivative(PolyLog.reli1,0.0) == 1.0 + ChainRulesTestUtils.test_frule(PolyLog.reli1, 0.0) + ChainRulesTestUtils.test_rrule(PolyLog.reli1, float(pi)) end end diff --git a/test/Li2.jl b/test/Li2.jl index c843e392..2213df99 100644 --- a/test/Li2.jl +++ b/test/Li2.jl @@ -71,5 +71,7 @@ if isdefined(Base,:get_extension) @test ForwardDiff.derivative(PolyLog.reli2,float(pi)) == PolyLog.reli1(float(pi))/float(pi) @test ForwardDiff.derivative(PolyLog.reli2,0.0) == 1.0 + ChainRulesTestUtils.test_frule(PolyLog.reli2, 0.0) + ChainRulesTestUtils.test_rrule(PolyLog.reli2, float(pi)) end end diff --git a/test/Li3.jl b/test/Li3.jl index 109b46bb..1f815c03 100644 --- a/test/Li3.jl +++ b/test/Li3.jl @@ -65,5 +65,7 @@ if isdefined(Base, :get_extension) @test ForwardDiff.derivative(PolyLog.reli3,float(pi)) == PolyLog.reli2(float(pi))/float(pi) @test ForwardDiff.derivative(PolyLog.reli3,0.0) == 1.0 + ChainRulesTestUtils.test_frule(PolyLog.reli3, 0.0) + ChainRulesTestUtils.test_rrule(PolyLog.reli3, float(pi)) end end diff --git a/test/Li4.jl b/test/Li4.jl index 9afe1390..fb041120 100644 --- a/test/Li4.jl +++ b/test/Li4.jl @@ -57,5 +57,7 @@ if isdefined(Base,:get_extension) @test ForwardDiff.derivative(PolyLog.reli4,float(pi)) == PolyLog.reli3(float(pi))/float(pi) @test ForwardDiff.derivative(PolyLog.reli4,0.0) == 1.0 + ChainRulesTestUtils.test_frule(PolyLog.reli4, 0.0) + ChainRulesTestUtils.test_rrule(PolyLog.reli4, float(pi)) end end diff --git a/test/runtests.jl b/test/runtests.jl index 00798420..98f65563 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,6 +2,7 @@ using Test import PolyLog if isdefined(Base,:get_extension) import ForwardDiff + import ChainRulesTestUtils end include("TestPrecision.jl") From 7e29d6f8370419deec90e06b992f358df57103d9 Mon Sep 17 00:00:00 2001 From: longemen3000 Date: Tue, 4 Jun 2024 18:59:29 -0400 Subject: [PATCH 13/16] whitespace --- ext/PolyLogForwardDiffExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/PolyLogForwardDiffExt.jl b/ext/PolyLogForwardDiffExt.jl index 6dfe1dbf..8831ad0a 100644 --- a/ext/PolyLogForwardDiffExt.jl +++ b/ext/PolyLogForwardDiffExt.jl @@ -50,4 +50,4 @@ function PolyLog.reli(n::Integer,d::Dual{T}) where T return Dual{T}(x, dx*partials(d)) end -end #module \ No newline at end of file +end #module From d5c8d60ea8b6d9b355f01c01f6ac073a4e9b975f Mon Sep 17 00:00:00 2001 From: longemen3000 Date: Wed, 5 Jun 2024 02:50:46 -0400 Subject: [PATCH 14/16] whitespace --- test/Li.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Li.jl b/test/Li.jl index 7045f8fb..2419d41e 100644 --- a/test/Li.jl +++ b/test/Li.jl @@ -135,5 +135,5 @@ end @test typeof(PolyLog.reli(n, convert(T, x))) == T end end - end + end end From b7810dfc072dc35b6b64803faabe7e37a117ae92 Mon Sep 17 00:00:00 2001 From: longemen3000 Date: Fri, 7 Jun 2024 14:42:51 -0400 Subject: [PATCH 15/16] add compat --- Project.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Project.toml b/Project.toml index 6e732f2e..d6f9eefc 100644 --- a/Project.toml +++ b/Project.toml @@ -5,6 +5,8 @@ version = "2.4.2" [compat] julia = "1.0" +ChainRulesCore = "1" +ForwardDiff = "0.11" [extras] DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab" From 59bd8f8a74f97340f2971dd5ca67bfa8f7dfadef Mon Sep 17 00:00:00 2001 From: longemen3000 Date: Fri, 7 Jun 2024 18:05:18 -0400 Subject: [PATCH 16/16] modify ForwardDiff compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index d6f9eefc..3fd42516 100644 --- a/Project.toml +++ b/Project.toml @@ -6,7 +6,7 @@ version = "2.4.2" [compat] julia = "1.0" ChainRulesCore = "1" -ForwardDiff = "0.11" +ForwardDiff = "0.10,0.11" [extras] DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"