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

WIP: support SpecialFunctions 0.8 and 0.7 #119

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "0.2.1"
[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Expand Down
2 changes: 2 additions & 0 deletions src/ChainRules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const accumulate! = ChainRulesCore.accumulate!
using LinearAlgebra
using LinearAlgebra.BLAS
using Requires
using Pkg: Pkg
using Statistics
using Base.Broadcast: materialize, materialize!, broadcasted, Broadcasted, broadcastable

Expand All @@ -22,6 +23,7 @@ if VERSION < v"1.3.0-DEV.142"
import LinearAlgebra: dot
end

include("glue_utils.jl")
include("helper_functions.jl")

include("rulesets/Base/base.jl")
Expand Down
67 changes: 67 additions & 0 deletions src/glue_utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""
@version_spec_str(str::String)

Returns a `VersionSpec` object which represents the range of compatible verions,
for a given Pkg3 style semver compat specifier.

Example:
```jldoctest
julia> v"2.0.1" ∈ ChainRules.version_spec"1.2"
false

julia> v"1.5.1" ∈ ChainRules.version_spec"1.2"
true

julia> v"2.0.1" ∈ ChainRules.version_spec"1, 2"
true

julia> v"1.0.1" ∈ ChainRules.version_spec"1, 2"
true

julia> v"1.3.0" ∈ ChainRules.version_spec"~1.0, ~1.1"
false

julia> v"1.1.2" ∈ ChainRules.version_spec"~1.0, ~1.1"
true
"""
macro version_spec_str(str::String)
return Pkg.Types.semver_spec(str)
end

"""
pkg_version(_module::Module)

Returns the version of the package that defined a given module.
Does not work on the current module, or on standard libraries
"""
pkg_version(_module::Module)

@static if VERSION ∈ version_spec"~1.0"
function pkg_version(_module::Module)
pkg_id = Base.PkgId(_module)
env = Pkg.Types.Context().env
pkg_info = Pkg.Types.manifest_info(env, pkg_id.uuid)
return VersionNumber(pkg_info["version"])
end
elseif VERSION ∈ version_spec"~1.1"
function pkg_version(_module::Module)
pkg_id = Base.PkgId(_module)
env = Pkg.Types.Context().env
pkg_info = Pkg.Types.manifest_info(env, pkg_id.uuid)
return VersionNumber(pkg_info.version)
end
elseif VERSION ∈ version_spec"~1.2, ~1.3"
function pkg_version(_module::Module)
pkg_id = Base.PkgId(_module)
env = Pkg.Types.Context().env
pkg_info = Pkg.Types.manifest_info(env, pkg_id.uuid)
return pkg_info.version
end
else # tested in 1.4.0-DEV.265
function pkg_version(_module::Module)
pkg_id = Base.PkgId(_module)
ctx = Pkg.Types.Context()
pkg_info = Pkg.Types.manifest_info(ctx, pkg_id.uuid)
return pkg_info.version
end
end
7 changes: 6 additions & 1 deletion src/rulesets/packages/SpecialFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ using ChainRulesCore
using ..SpecialFunctions


@scalar_rule(SpecialFunctions.lgamma(x), SpecialFunctions.digamma(x))
@scalar_rule(SpecialFunctions.erf(x), (2 / sqrt(π)) * exp(-x * x))
@scalar_rule(SpecialFunctions.erfc(x), -(2 / sqrt(π)) * exp(-x * x))
@scalar_rule(SpecialFunctions.erfi(x), (2 / sqrt(π)) * exp(x * x))
Expand All @@ -24,4 +23,10 @@ using ..SpecialFunctions
@scalar_rule(SpecialFunctions.erfcx(x), (2 * x * Ω) - (2 / sqrt(π)))
@scalar_rule(SpecialFunctions.dawson(x), 1 - (2 * x * Ω))

@static if pkg_version(SpecialFunctions) < v"0.8"
@scalar_rule(SpecialFunctions.lgamma(x), SpecialFunctions.digamma(x))
else
@scalar_rule(SpecialFunctions.loggamma(x), SpecialFunctions.digamma(x))
end

end #module
4 changes: 4 additions & 0 deletions test/glue_utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@testset "Package version checking" begin
# This test needs to be updated when we allow a new version of ChainRulesCore
@test ChainRules.pkg_version(ChainRulesCore) ∈ ChainRules.version_spec"0.3"
end
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ include("test_util.jl")

println("Testing ChainRules.jl")
@testset "ChainRules" begin
include("glue_utils.jl")
exit()
include("helper_functions.jl")
@testset "rulesets" begin
@testset "Base" begin
Expand Down