From 406d579d900d7b1fef7e7ec156ee6ab34aab3448 Mon Sep 17 00:00:00 2001 From: amrods <11388389+amrods@users.noreply.github.com> Date: Fri, 27 Aug 2021 11:52:55 +1000 Subject: [PATCH 1/7] added rule for gamma(a, x) --- src/rules.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rules.jl b/src/rules.jl index e1a1726..d5fb542 100644 --- a/src/rules.jl +++ b/src/rules.jl @@ -177,6 +177,7 @@ end :NaN, :( (SpecialFunctions.hankelh1($ν - 1, $x) - SpecialFunctions.hankelh1($ν + 1, $x)) / 2 ) @define_diffrule SpecialFunctions.hankelh2(ν, x) = :NaN, :( (SpecialFunctions.hankelh2($ν - 1, $x) - SpecialFunctions.hankelh2($ν + 1, $x)) / 2 ) +@define_diffrule SpecialFunctions.gamma(a, x) = :NaN, :(-exp(-$x) * $x^($a - 1)) @define_diffrule SpecialFunctions.polygamma(m, x) = :NaN, :( SpecialFunctions.polygamma($m + 1, $x) ) @define_diffrule SpecialFunctions.beta(a, b) = From 79ceb6f99fb3e2412d61e828b3063eef0b923e5e Mon Sep 17 00:00:00 2001 From: amrods <11388389+amrods@users.noreply.github.com> Date: Fri, 27 Aug 2021 15:21:49 +1000 Subject: [PATCH 2/7] added a few rules for airy and bessel functions --- src/rules.jl | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/rules.jl b/src/rules.jl index d5fb542..d9b7785 100644 --- a/src/rules.jl +++ b/src/rules.jl @@ -144,6 +144,10 @@ end :( SpecialFunctions.airybiprime($x) ) @define_diffrule SpecialFunctions.airybiprime(x) = :( $x * SpecialFunctions.airybi($x) ) +@define_diffrule SpecialFunctions.airyaix(x) = + :( sqrt($x) * SpecialFunctions.airyaix($x) + SpecialFunctions.airyaiprimex($x) ) +@define_diffrule SpecialFunctions.airyaiprimex(x) = + :( $x * SpecialFunctions.airyaix($x) + sqrt($x) * SpecialFunctions.airyaiprimex($x) ) @define_diffrule SpecialFunctions.besselj0(x) = :( -SpecialFunctions.besselj1($x) ) @define_diffrule SpecialFunctions.besselj1(x) = @@ -157,8 +161,6 @@ end # # eta # zeta -# airyaix -# airyaiprimex # airybix # airybiprimex @@ -173,30 +175,34 @@ end :NaN, :( (SpecialFunctions.bessely($ν - 1, $x) - SpecialFunctions.bessely($ν + 1, $x)) / 2 ) @define_diffrule SpecialFunctions.besselk(ν, x) = :NaN, :( -(SpecialFunctions.besselk($ν - 1, $x) + SpecialFunctions.besselk($ν + 1, $x)) / 2 ) +@define_diffrule SpecialFunctions.besselkx(ν, x) = + :NaN, :( -(SpecialFunctions.besselkx($ν - 1, $x) - 2SpecialFunctions.besselkx($ν, $x) + SpecialFunctions.besselkx($ν + 1, $x)) / 2 ) @define_diffrule SpecialFunctions.hankelh1(ν, x) = :NaN, :( (SpecialFunctions.hankelh1($ν - 1, $x) - SpecialFunctions.hankelh1($ν + 1, $x)) / 2 ) +@define_diffrule SpecialFunctions.hankelh1x(ν, x) = + :NaN, :( (SpecialFunctions.hankelh1x($ν - 1, $x) - 2im*SpecialFunctions.hankelh1x($ν, $x) - SpecialFunctions.hankelh1x($ν + 1, $x)) / 2 ) @define_diffrule SpecialFunctions.hankelh2(ν, x) = :NaN, :( (SpecialFunctions.hankelh2($ν - 1, $x) - SpecialFunctions.hankelh2($ν + 1, $x)) / 2 ) -@define_diffrule SpecialFunctions.gamma(a, x) = :NaN, :(-exp(-$x) * $x^($a - 1)) +@define_diffrule SpecialFunctions.hankelh2x(ν, x) = + :NaN, :( (SpecialFunctions.hankelh2x($ν - 1, $x) + 2im*SpecialFunctions.hankelh2x($ν, $x) - SpecialFunctions.hankelh2x($ν + 1, $x)) / 2 ) +@define_diffrule SpecialFunctions.gamma(a, x) = + :NaN, :(-exp(-$x) * $x^($a - 1)) @define_diffrule SpecialFunctions.polygamma(m, x) = :NaN, :( SpecialFunctions.polygamma($m + 1, $x) ) @define_diffrule SpecialFunctions.beta(a, b) = :( SpecialFunctions.beta($a, $b)*(SpecialFunctions.digamma($a) - SpecialFunctions.digamma($a + $b)) ), :( SpecialFunctions.beta($a, $b)*(SpecialFunctions.digamma($b) - SpecialFunctions.digamma($a + $b)) ) -@define_diffrule SpecialFunctions.logbeta(a, b) = +@define_diffrule SpecialFunctions.logbeta(a, b) = :( SpecialFunctions.digamma($a) - SpecialFunctions.digamma($a + $b) ), :( SpecialFunctions.digamma($b) - SpecialFunctions.digamma($a + $b) ) +@define_diffrule SpecialFunctions.zeta(s, z) = + :NaN, :( -$s * SpecialFunctions.zeta(1 + $s, z) ) # TODO: # -# zeta # besseljx # besselyx # besselix -# besselkx # besselh # besselhx -# hankelh1x -# hankelh2 -# hankelh2x # ternary # #---------# From af7dcfdcac1b2aeeab28d34aa80bbd46f3f9a901 Mon Sep 17 00:00:00 2001 From: amrods <11388389+amrods@users.noreply.github.com> Date: Fri, 27 Aug 2021 15:26:12 +1000 Subject: [PATCH 3/7] fixed missing $ in def of zeta --- src/rules.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rules.jl b/src/rules.jl index d9b7785..4346b32 100644 --- a/src/rules.jl +++ b/src/rules.jl @@ -194,7 +194,7 @@ end @define_diffrule SpecialFunctions.logbeta(a, b) = :( SpecialFunctions.digamma($a) - SpecialFunctions.digamma($a + $b) ), :( SpecialFunctions.digamma($b) - SpecialFunctions.digamma($a + $b) ) @define_diffrule SpecialFunctions.zeta(s, z) = - :NaN, :( -$s * SpecialFunctions.zeta(1 + $s, z) ) + :NaN, :( -$s * SpecialFunctions.zeta(1 + $s, $z) ) # TODO: # From e206f702dfaa4f01beb26a130616c185112573da Mon Sep 17 00:00:00 2001 From: amrods <11388389+amrods@users.noreply.github.com> Date: Mon, 6 Sep 2021 11:08:37 +1000 Subject: [PATCH 4/7] Fixed tests for 2-arg gamma --- test/runtests.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index e3da95f..3c373c4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -18,8 +18,15 @@ end non_numeric_arg_functions = [(:Base, :rem2pi, 2), (:Base, :ifelse, 3)] +recently_defined_functions = [] +if !hasmethod(SpecialFunctions.gamma, Tuple{Number, Number}) + # 2-arg gamma added after SpecialFunctions v0.8.0 + push!(recently_defined_functions, (:SpecialFunctions, :gamma, 2)) +end + for (M, f, arity) in DiffRules.diffrules() (M, f, arity) ∈ non_numeric_arg_functions && continue + (M, f, arity) ∈ recently_defined_functions && continue if arity == 1 @test DiffRules.hasdiffrule(M, f, 1) deriv = DiffRules.diffrule(M, f, :goo) From a5ee29d1268ee39d9eb3deda515536a462fd3965 Mon Sep 17 00:00:00 2001 From: David Widmann Date: Sat, 11 Dec 2021 22:28:59 +0100 Subject: [PATCH 5/7] Update test/runtests.jl --- test/runtests.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 15d5e4b..c27fdd0 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -17,7 +17,6 @@ non_numeric_arg_functions = [(:Base, :rem2pi, 2), (:Base, :ifelse, 3)] for (M, f, arity) in DiffRules.diffrules(; filter_modules=nothing) (M, f, arity) ∈ non_numeric_arg_functions && continue - (M, f, arity) ∈ recently_defined_functions && continue if arity == 1 @test DiffRules.hasdiffrule(M, f, 1) deriv = DiffRules.diffrule(M, f, :goo) From ce09b11180ef82125e2e3c76007d84d8e75caa71 Mon Sep 17 00:00:00 2001 From: David Widmann Date: Sat, 11 Dec 2021 22:41:57 +0100 Subject: [PATCH 6/7] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index fc3dfd5..b4b7fa4 100644 --- a/Project.toml +++ b/Project.toml @@ -11,7 +11,7 @@ SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b" [compat] LogExpFunctions = "0.3.2" NaNMath = "0.3" -SpecialFunctions = "0.10, 1.0, 2" +SpecialFunctions = "1.2, 2" julia = "1.3" [extras] From 813f4e87d5921e86d9c3eefd35eb228cbe2737c9 Mon Sep 17 00:00:00 2001 From: David Widmann Date: Sat, 11 Dec 2021 23:15:29 +0100 Subject: [PATCH 7/7] Make rules consistent with ChainRules definitions in SpecialFunctions --- src/rules.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/rules.jl b/src/rules.jl index 2867f90..4e6b644 100644 --- a/src/rules.jl +++ b/src/rules.jl @@ -140,7 +140,7 @@ _abs_deriv(x) = signbit(x) ? -one(x) : one(x) @define_diffrule SpecialFunctions.airybiprime(x) = :( $x * SpecialFunctions.airybi($x) ) @define_diffrule SpecialFunctions.airyaix(x) = - :( sqrt($x) * SpecialFunctions.airyaix($x) + SpecialFunctions.airyaiprimex($x) ) + :( SpecialFunctions.airyaiprimex($x) + sqrt($x) * SpecialFunctions.airyaix($x) ) @define_diffrule SpecialFunctions.airyaiprimex(x) = :( $x * SpecialFunctions.airyaix($x) + sqrt($x) * SpecialFunctions.airyaiprimex($x) ) @define_diffrule SpecialFunctions.besselj0(x) = @@ -171,15 +171,15 @@ _abs_deriv(x) = signbit(x) ? -one(x) : one(x) @define_diffrule SpecialFunctions.besselk(ν, x) = :NaN, :( -(SpecialFunctions.besselk($ν - 1, $x) + SpecialFunctions.besselk($ν + 1, $x)) / 2 ) @define_diffrule SpecialFunctions.besselkx(ν, x) = - :NaN, :( -(SpecialFunctions.besselkx($ν - 1, $x) - 2SpecialFunctions.besselkx($ν, $x) + SpecialFunctions.besselkx($ν + 1, $x)) / 2 ) + :NaN, :( -(SpecialFunctions.besselkx($ν - 1, $x) + SpecialFunctions.besselkx($ν + 1, $x)) / 2 + SpecialFunctions.besselkx($ν, $x) ) @define_diffrule SpecialFunctions.hankelh1(ν, x) = :NaN, :( (SpecialFunctions.hankelh1($ν - 1, $x) - SpecialFunctions.hankelh1($ν + 1, $x)) / 2 ) @define_diffrule SpecialFunctions.hankelh1x(ν, x) = - :NaN, :( (SpecialFunctions.hankelh1x($ν - 1, $x) - 2im*SpecialFunctions.hankelh1x($ν, $x) - SpecialFunctions.hankelh1x($ν + 1, $x)) / 2 ) + :NaN, :( (SpecialFunctions.hankelh1x($ν - 1, $x) - SpecialFunctions.hankelh1x($ν + 1, $x)) / 2 - im * SpecialFunctions.hankelh1x($ν, $x) ) @define_diffrule SpecialFunctions.hankelh2(ν, x) = :NaN, :( (SpecialFunctions.hankelh2($ν - 1, $x) - SpecialFunctions.hankelh2($ν + 1, $x)) / 2 ) @define_diffrule SpecialFunctions.hankelh2x(ν, x) = - :NaN, :( (SpecialFunctions.hankelh2x($ν - 1, $x) + 2im*SpecialFunctions.hankelh2x($ν, $x) - SpecialFunctions.hankelh2x($ν + 1, $x)) / 2 ) + :NaN, :( (SpecialFunctions.hankelh2x($ν - 1, $x) - SpecialFunctions.hankelh2x($ν + 1, $x)) / 2 + im * SpecialFunctions.hankelh2x($ν, $x) ) @define_diffrule SpecialFunctions.gamma(a, x) = :NaN, :(-exp(-$x) * $x^($a - 1)) @define_diffrule SpecialFunctions.polygamma(m, x) =