1
+ module RootsChainRulesCoreExt
2
+
3
+ using Roots
4
+ import ChainRulesCore
5
+
1
6
# View find_zero as solving `f(x, p) = 0` for `xᵅ(p)`.
2
7
# This is implicitly defined. By the implicit function theorem, we have:
3
8
# ∇f = 0 => ∂/∂ₓ f(xᵅ, p) ⋅ ∂xᵅ/∂ₚ + ∂/∂ₚf(x\^α, p) ⋅ I = 0
15
20
# that is fixable.)
16
21
17
22
# this assumes a function and a parameter `p` passed in
18
- import ChainRulesCore: Tangent, NoTangent, frule, rrule
19
23
function ChainRulesCore. frule (
20
24
config:: ChainRulesCore.RuleConfig{>:ChainRulesCore.HasForwardsMode} ,
21
25
(_, _, _, Δp),
@@ -42,17 +46,17 @@ ChainRulesCore.frule(
42
46
config:: ChainRulesCore.RuleConfig{>:ChainRulesCore.HasForwardsMode} ,
43
47
xdots,
44
48
:: typeof (solve),
45
- ZP:: Roots. ZeroProblem ,
49
+ ZP:: ZeroProblem ,
46
50
M:: Roots.AbstractUnivariateZeroMethod ,
47
51
:: Nothing ;
48
52
kwargs... ,
49
- ) = frule (config, xdots, solve, ZP, M; kwargs... )
53
+ ) = ChainRulesCore . frule (config, xdots, solve, ZP, M; kwargs... )
50
54
51
55
function ChainRulesCore. frule (
52
56
config:: ChainRulesCore.RuleConfig{>:ChainRulesCore.HasForwardsMode} ,
53
57
(_, Δq, _),
54
58
:: typeof (solve),
55
- ZP:: Roots. ZeroProblem ,
59
+ ZP:: ZeroProblem ,
56
60
M:: Roots.AbstractUnivariateZeroMethod ;
57
61
kwargs... ,
58
62
)
@@ -61,12 +65,12 @@ function ChainRulesCore.frule(
61
65
zprob2 = ZeroProblem (|> , ZP. x₀)
62
66
nms = fieldnames (typeof (foo))
63
67
nt = NamedTuple {nms} (getfield (foo, n) for n in nms)
64
- dfoo = Tangent {typeof(foo)} (; nt... )
68
+ dfoo = ChainRulesCore . Tangent {typeof(foo)} (; nt... )
65
69
66
- return frule (
70
+ return ChainRulesCore . frule (
67
71
config,
68
- (NoTangent (), NoTangent (), NoTangent (), dfoo),
69
- Roots . solve,
72
+ (ChainRulesCore . NoTangent (), ChainRulesCore . NoTangent (), ChainRulesCore . NoTangent (), dfoo),
73
+ solve,
70
74
zprob2,
71
75
M,
72
76
foo,
@@ -146,3 +150,5 @@ function ChainRulesCore.rrule(
146
150
147
151
return xᵅ, pullback_solve_ZeroProblem
148
152
end
153
+
154
+ end # module
0 commit comments