Skip to content

Commit 698754d

Browse files
authored
test: replace NLsolve with NonlinearSolve (#213)
1 parent d123ff1 commit 698754d

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

docs/Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
88
ImplicitDifferentiation = "57b37032-215b-411a-8a7c-41a003a55207"
99
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1010
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
11-
NLsolve = "2774e3e8-f4cf-5e23-947b-6d7e65073b56"
11+
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
1212
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
1313
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1414
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
@@ -23,5 +23,5 @@ FiniteDiff = "2.29.0"
2323
ForwardDiff = "1.3.1"
2424
ImplicitDifferentiation = "0.9.1"
2525
Literate = "2.21.0"
26-
NLsolve = "4.5.1"
26+
NonlinearSolve = "4.17.1"
2727
Zygote = "0.7.10"

examples/1_basic.jl

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Note that some packages from the [SciML](https://sciml.ai/) ecosystem provide a
1212
using ForwardDiff
1313
using ImplicitDifferentiation
1414
using LinearAlgebra
15-
using NLsolve
15+
import NonlinearSolve as NLS
1616
using Optim
1717
using Test #src
1818
using Zygote
@@ -126,22 +126,23 @@ To make verification easy, we solve the following system:
126126
```math
127127
c(x, y) = y \odot y - x = 0
128128
```
129-
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).
129+
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).
130130
=#
131131

132-
function forward_nlsolve(x, method)
133-
F!(storage, y) = (storage .= y .^ 2 .- x)
132+
function forward_nlsolve(x)
133+
F!(storage, y, x) = (storage .= y .^ 2 .- x)
134134
initial_y = similar(x)
135135
initial_y .= 1
136-
result = nlsolve(F!, initial_y; method)
137-
y = result.zero
136+
prob = NLS.NonlinearProblem(F!, initial_y, x; abstol=1e-10, reltol=1e-10)
137+
sol = NLS.solve(prob)
138+
y = sol.u
138139
z = nothing
139140
return y, z
140141
end;
141142

142143
#-
143144

144-
function conditions_nlsolve(x, y, _z, _method)
145+
function conditions_nlsolve(x, y, _z)
145146
c = y .^ 2 .- x
146147
return c
147148
end;
@@ -152,27 +153,27 @@ implicit_nlsolve = ImplicitFunction(forward_nlsolve, conditions_nlsolve)
152153

153154
#-
154155

155-
first(implicit_nlsolve(x, :newton)) .^ 2
156-
@test first(implicit_nlsolve(x, :newton)) .^ 2 x #src
156+
first(implicit_nlsolve(x)) .^ 2
157+
@test first(implicit_nlsolve(x)) .^ 2 x #src
157158

158159
# Forward mode autodiff
159160

160-
ForwardDiff.jacobian(_x -> first(implicit_nlsolve(_x, :newton)), x)
161-
@test ForwardDiff.jacobian(_x -> first(implicit_nlsolve(_x, :newton)), x) J #src
161+
ForwardDiff.jacobian(_x -> first(implicit_nlsolve(_x)), x)
162+
@test ForwardDiff.jacobian(_x -> first(implicit_nlsolve(_x)), x) J #src
162163

163164
#-
164165

165-
ForwardDiff.jacobian(_x -> first(forward_nlsolve(_x, :newton)), x)
166+
ForwardDiff.jacobian(_x -> first(forward_nlsolve(_x)), x)
166167

167168
# Reverse mode autodiff
168169

169-
Zygote.jacobian(_x -> first(implicit_nlsolve(_x, :newton)), x)[1]
170-
@test Zygote.jacobian(_x -> first(implicit_nlsolve(_x, :newton)), x)[1] J #src
170+
Zygote.jacobian(_x -> first(implicit_nlsolve(_x)), x)[1]
171+
@test Zygote.jacobian(_x -> first(implicit_nlsolve(_x)), x)[1] J #src
171172

172173
#-
173174

174175
try
175-
Zygote.jacobian(_x -> first(forward_nlsolve(_x, :newton)), x)[1]
176+
Zygote.jacobian(_x -> first(forward_nlsolve(_x)), x)[1]
176177
catch e
177178
e
178179
end

test/Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ImplicitDifferentiation = "57b37032-215b-411a-8a7c-41a003a55207"
1313
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
1414
KrylovKit = "0b1a1467-8014-51b9-945f-bf0ae24f4b77"
1515
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
16-
NLsolve = "2774e3e8-f4cf-5e23-947b-6d7e65073b56"
16+
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
1717
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
1818
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1919
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
@@ -41,7 +41,7 @@ ImplicitDifferentiation = "0.9.1"
4141
JET = "0.9, 0.10, 0.11"
4242
KrylovKit = "0.10.2"
4343
LinearAlgebra = "1"
44-
NLsolve = "4.5.1"
44+
NonlinearSolve = "4.17.1"
4545
Optim = "1.12.0, 2.0"
4646
Random = "1"
4747
SparseArrays = "1"

0 commit comments

Comments
 (0)