Skip to content
Merged
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
4 changes: 2 additions & 2 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
ImplicitDifferentiation = "57b37032-215b-411a-8a7c-41a003a55207"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
NLsolve = "2774e3e8-f4cf-5e23-947b-6d7e65073b56"
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand All @@ -23,5 +23,5 @@ FiniteDiff = "2.29.0"
ForwardDiff = "1.3.1"
ImplicitDifferentiation = "0.9.1"
Literate = "2.21.0"
NLsolve = "4.5.1"
NonlinearSolve = "4.17.1"
Zygote = "0.7.10"
31 changes: 16 additions & 15 deletions examples/1_basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Note that some packages from the [SciML](https://sciml.ai/) ecosystem provide a
using ForwardDiff
using ImplicitDifferentiation
using LinearAlgebra
using NLsolve
import NonlinearSolve as NLS
using Optim
using Test #src
using Zygote
Expand Down Expand Up @@ -126,22 +126,23 @@ To make verification easy, we solve the following system:
```math
c(x, y) = y \odot y - x = 0
```
In this case, the optimization problem boils down to the componentwise square root function, but we implement it using a black box solver from [NLsolve.jl](https://github.com/JuliaNLSolvers/NLsolve.jl).
In this case, the optimization problem boils down to the componentwise square root function, but we implement it using a black box solver from [NonlinearSolve.jl](https://github.com/SciML/NonlinearSolve.jl).
=#

function forward_nlsolve(x, method)
F!(storage, y) = (storage .= y .^ 2 .- x)
function forward_nlsolve(x)
F!(storage, y, x) = (storage .= y .^ 2 .- x)
initial_y = similar(x)
initial_y .= 1
result = nlsolve(F!, initial_y; method)
y = result.zero
prob = NLS.NonlinearProblem(F!, initial_y, x; abstol=1e-10, reltol=1e-10)
sol = NLS.solve(prob)
y = sol.u
z = nothing
return y, z
end;

#-

function conditions_nlsolve(x, y, _z, _method)
function conditions_nlsolve(x, y, _z)
c = y .^ 2 .- x
return c
end;
Expand All @@ -152,27 +153,27 @@ implicit_nlsolve = ImplicitFunction(forward_nlsolve, conditions_nlsolve)

#-

first(implicit_nlsolve(x, :newton)) .^ 2
@test first(implicit_nlsolve(x, :newton)) .^ 2 ≈ x #src
first(implicit_nlsolve(x)) .^ 2
@test first(implicit_nlsolve(x)) .^ 2 ≈ x #src

# Forward mode autodiff

ForwardDiff.jacobian(_x -> first(implicit_nlsolve(_x, :newton)), x)
@test ForwardDiff.jacobian(_x -> first(implicit_nlsolve(_x, :newton)), x) ≈ J #src
ForwardDiff.jacobian(_x -> first(implicit_nlsolve(_x)), x)
@test ForwardDiff.jacobian(_x -> first(implicit_nlsolve(_x)), x) ≈ J #src

#-

ForwardDiff.jacobian(_x -> first(forward_nlsolve(_x, :newton)), x)
ForwardDiff.jacobian(_x -> first(forward_nlsolve(_x)), x)

# Reverse mode autodiff

Zygote.jacobian(_x -> first(implicit_nlsolve(_x, :newton)), x)[1]
@test Zygote.jacobian(_x -> first(implicit_nlsolve(_x, :newton)), x)[1] ≈ J #src
Zygote.jacobian(_x -> first(implicit_nlsolve(_x)), x)[1]
@test Zygote.jacobian(_x -> first(implicit_nlsolve(_x)), x)[1] ≈ J #src

#-

try
Zygote.jacobian(_x -> first(forward_nlsolve(_x, :newton)), x)[1]
Zygote.jacobian(_x -> first(forward_nlsolve(_x)), x)[1]
catch e
e
end
Expand Down
4 changes: 2 additions & 2 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ImplicitDifferentiation = "57b37032-215b-411a-8a7c-41a003a55207"
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
KrylovKit = "0b1a1467-8014-51b9-945f-bf0ae24f4b77"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
NLsolve = "2774e3e8-f4cf-5e23-947b-6d7e65073b56"
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Expand Down Expand Up @@ -41,7 +41,7 @@ ImplicitDifferentiation = "0.9.1"
JET = "0.9, 0.10, 0.11"
KrylovKit = "0.10.2"
LinearAlgebra = "1"
NLsolve = "4.5.1"
NonlinearSolve = "4.17.1"
Optim = "1.12.0, 2.0"
Random = "1"
SparseArrays = "1"
Expand Down
Loading