Skip to content

Commit

Permalink
Make ChainRulesCore a weak dependency (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
devmotion authored Sep 13, 2024
1 parent 10a710f commit a2e58ee
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Roots"
uuid = "f2b01f46-fcfa-551c-844a-d8ac1e96c665"
version = "2.1.8"
version = "2.2.0"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand All @@ -9,12 +9,14 @@ CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[weakdeps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
IntervalRootFinding = "d2bf35a9-74e0-55ec-b149-d360ff49b807"
SymPy = "24249f21-da20-56a4-8eb1-6a02cf4ae2e6"
SymPyPythonCall = "bc8888f7-b21e-4b7c-a06a-5d9c9496438c"

[extensions]
RootsChainRulesCoreExt = "ChainRulesCore"
RootsForwardDiffExt = "ForwardDiff"
RootsIntervalRootFindingExt = "IntervalRootFinding"
RootsSymPyExt = "SymPy"
Expand Down
22 changes: 14 additions & 8 deletions src/chain_rules.jl → ext/RootsChainRulesCoreExt.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module RootsChainRulesCoreExt

using Roots
import ChainRulesCore

# View find_zero as solving `f(x, p) = 0` for `xᵅ(p)`.
# This is implicitly defined. By the implicit function theorem, we have:
# ∇f = 0 => ∂/∂ₓ f(xᵅ, p) ⋅ ∂xᵅ/∂ₚ + ∂/∂ₚf(x\^α, p) ⋅ I = 0
Expand All @@ -15,7 +20,6 @@
# that is fixable.)

# this assumes a function and a parameter `p` passed in
import ChainRulesCore: Tangent, NoTangent, frule, rrule
function ChainRulesCore.frule(
config::ChainRulesCore.RuleConfig{>:ChainRulesCore.HasForwardsMode},
(_, _, _, Δp),
Expand All @@ -42,17 +46,17 @@ ChainRulesCore.frule(
config::ChainRulesCore.RuleConfig{>:ChainRulesCore.HasForwardsMode},
xdots,
::typeof(solve),
ZP::Roots.ZeroProblem,
ZP::ZeroProblem,
M::Roots.AbstractUnivariateZeroMethod,
::Nothing;
kwargs...,
) = frule(config, xdots, solve, ZP, M; kwargs...)
) = ChainRulesCore.frule(config, xdots, solve, ZP, M; kwargs...)

function ChainRulesCore.frule(
config::ChainRulesCore.RuleConfig{>:ChainRulesCore.HasForwardsMode},
(_, Δq, _),
::typeof(solve),
ZP::Roots.ZeroProblem,
ZP::ZeroProblem,
M::Roots.AbstractUnivariateZeroMethod;
kwargs...,
)
Expand All @@ -61,12 +65,12 @@ function ChainRulesCore.frule(
zprob2 = ZeroProblem(|>, ZP.x₀)
nms = fieldnames(typeof(foo))
nt = NamedTuple{nms}(getfield(foo, n) for n in nms)
dfoo = Tangent{typeof(foo)}(; nt...)
dfoo = ChainRulesCore.Tangent{typeof(foo)}(; nt...)

return frule(
return ChainRulesCore.frule(
config,
(NoTangent(), NoTangent(), NoTangent(), dfoo),
Roots.solve,
(ChainRulesCore.NoTangent(), ChainRulesCore.NoTangent(), ChainRulesCore.NoTangent(), dfoo),
solve,
zprob2,
M,
foo,
Expand Down Expand Up @@ -146,3 +150,5 @@ function ChainRulesCore.rrule(

return xᵅ, pullback_solve_ZeroProblem
end

end # module
6 changes: 4 additions & 2 deletions src/Roots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ using Printf
import CommonSolve
import CommonSolve: solve, solve!, init
using Accessors
import ChainRulesCore

export fzero, fzeros, secant_method

Expand Down Expand Up @@ -53,7 +52,6 @@ include("functions.jl")
include("trace.jl")
include("find_zero.jl")
include("hybrid.jl")
include("chain_rules.jl")

include("Bracketing/bracketing.jl")
include("Bracketing/bisection.jl")
Expand Down Expand Up @@ -83,4 +81,8 @@ include("find_zeros.jl")
include("simple.jl")
include("alternative_interfaces.jl")

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

end

2 comments on commit a2e58ee

@jverzani
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/115139

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v2.2.0 -m "<description of version>" a2e58eed840ba51cf5c6789a9f0ea3c2dd2bc6e1
git push origin v2.2.0

Please sign in to comment.