-
Notifications
You must be signed in to change notification settings - Fork 1
/
znlsolve.jl
31 lines (26 loc) · 947 Bytes
/
znlsolve.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""
znlsolve(args...; kwargs...)
It is a Zygote-compatible version of `nlsolve(args...; kwargs...)`.
See:
https://github.com/JuliaNLSolvers/NLsolve.jl/issues/205#issuecomment-524764679
"""
znlsolve(args...; kwargs...) = NLsolve.nlsolve(args...; kwargs...)
@adjoint znlsolve(f, j, x0; kwargs...) =
let result = znlsolve(f, j, x0; kwargs...)
NLsolve.converged(result) || throw(NLsolveNotConvergedError(result))
result, function(vresult)
# This backpropagator returns (- v' (df/dx)⁻¹ (df/dp))'
v = vresult[].zero
x = result.zero
J = j(x)
_, back = forward(f -> f(x), f)
return (back(-(J' \ v))[1], nothing, nothing)
end
end
struct NLsolveNotConvergedError <: Exception
result
end
function Base.showerror(io::IO, err::NLsolveNotConvergedError)
println(io, "NLsolveNotConvergedError")
show(io, "text/plain", err.result)
end