@@ -3,6 +3,7 @@ using StaticArrays
33using BenchmarkTools
44using Test
55
6+ # SimpleNewtonRaphson
67function benchmark_scalar (f, u0)
78 probN = NonlinearProblem {false} (f, u0)
89 sol = (solve (probN, SimpleNewtonRaphson ()))
@@ -23,38 +24,53 @@ sol = benchmark_scalar(sf, csu0)
2324
2425@test (@ballocated benchmark_scalar (sf, csu0)) == 0
2526
27+ # Broyden
28+ function benchmark_scalar (f, u0)
29+ probN = NonlinearProblem {false} (f, u0)
30+ sol = (solve (probN, Broyden ()))
31+ end
32+
33+ sol = benchmark_scalar (sf, csu0)
34+ @test sol. retcode === ReturnCode. Success
35+ @test sol. u * sol. u - 2 < 1e-9
36+ @test (@ballocated benchmark_scalar (sf, csu0)) == 0
37+
2638# AD Tests
2739using ForwardDiff
2840
2941# Immutable
3042f, u0 = (u, p) -> u .* u .- p, @SVector [1.0 , 1.0 ]
3143
32- g = function (p)
33- probN = NonlinearProblem {false} (f, csu0, p)
34- sol = solve (probN, SimpleNewtonRaphson (), tol = 1e-9 )
35- return sol. u[end ]
36- end
44+ for alg in [SimpleNewtonRaphson (), Broyden ()]
45+ g = function (p)
46+ probN = NonlinearProblem {false} (f, csu0, p)
47+ sol = solve (probN, alg, tol = 1e-9 )
48+ return sol. u[end ]
49+ end
3750
38- for p in 1.0 : 0.1 : 100.0
39- @test g (p) ≈ sqrt (p)
40- @test ForwardDiff. derivative (g, p) ≈ 1 / (2 * sqrt (p))
51+ for p in 1.1 : 0.1 : 100.0
52+ @test g (p) ≈ sqrt (p)
53+ @test ForwardDiff. derivative (g, p) ≈ 1 / (2 * sqrt (p))
54+ end
4155end
4256
4357# Scalar
4458f, u0 = (u, p) -> u * u - p, 1.0
59+ for alg in [SimpleNewtonRaphson (), Broyden ()]
4560
46- # SimpleNewtonRaphson
47- g = function (p)
48- probN = NonlinearProblem {false} (f, oftype (p, u0), p)
49- sol = solve (probN, SimpleNewtonRaphson ())
50- return sol. u
51- end
52-
53- @test ForwardDiff. derivative (g, 1.0 ) ≈ 0.5
61+ g = function (p)
62+ probN = NonlinearProblem {false} (f, oftype (p, u0), p)
63+ sol = solve (probN, alg)
64+ return sol. u
65+ end
5466
55- for p in 1.1 : 0.1 : 100.0
56- @test g (p) ≈ sqrt (p)
67+ p = 1.1
5768 @test ForwardDiff. derivative (g, p) ≈ 1 / (2 * sqrt (p))
69+
70+ for p in 1.1 : 0.1 : 100.0
71+ @test g (p) ≈ sqrt (p)
72+ @test ForwardDiff. derivative (g, p) ≈ 1 / (2 * sqrt (p))
73+ end
5874end
5975
6076tspan = (1.0 , 20.0 )
@@ -77,24 +93,26 @@ for alg in [Bisection(), Falsi()]
7793 global g, p
7894 g = function (p)
7995 probN = IntervalNonlinearProblem {false} (f, tspan, p)
80- sol = solve (probN, Bisection ()) # TODO check if " alg" should replace "Bisection()"
96+ sol = solve (probN, alg)
8197 return [sol. left]
8298 end
8399
84100 @test g (p) ≈ [sqrt (p[2 ] / p[1 ])]
85101 @test ForwardDiff. jacobian (g, p) ≈ ForwardDiff. jacobian (t, p)
86102end
87103
88- gnewton = function (p)
89- probN = NonlinearProblem {false} (f, 0.5 , p)
90- sol = solve (probN, SimpleNewtonRaphson ())
91- return [sol. u]
104+ for alg in [SimpleNewtonRaphson (), Broyden ()]
105+ global g, p
106+ g = function (p)
107+ probN = NonlinearProblem {false} (f, 0.5 , p)
108+ sol = solve (probN, alg)
109+ return [sol. u]
110+ end
111+ @test g (p) ≈ [sqrt (p[2 ] / p[1 ])]
112+ @test ForwardDiff. jacobian (g, p) ≈ ForwardDiff. jacobian (t, p)
92113end
93- @test gnewton (p) ≈ [sqrt (p[2 ] / p[1 ])]
94- @test ForwardDiff. jacobian (gnewton, p) ≈ ForwardDiff. jacobian (t, p)
95114
96115# Error Checks
97-
98116f, u0 = (u, p) -> u .* u .- 2.0 , @SVector [1.0 , 1.0 ]
99117probN = NonlinearProblem (f, u0)
100118
@@ -103,6 +121,8 @@ probN = NonlinearProblem(f, u0)
103121@test solve (probN, SimpleNewtonRaphson (; autodiff = false )). u[end ] ≈ sqrt (2.0 )
104122@test solve (probN, SimpleNewtonRaphson (; autodiff = false )). u[end ] ≈ sqrt (2.0 )
105123# TODO check why the 2 lines above are identical
124+ @test solve (probN, Broyden ()). u[end ] ≈ sqrt (2.0 )
125+ @test solve (probN, Broyden (); immutable = false ). u[end ] ≈ sqrt (2.0 )
106126
107127for u0 in [1.0 , [1 , 1.0 ]]
108128 local f, probN, sol
@@ -114,6 +134,7 @@ for u0 in [1.0, [1, 1.0]]
114134 @test solve (probN, SimpleNewtonRaphson ()). u ≈ sol
115135 @test solve (probN, SimpleNewtonRaphson ()). u ≈ sol
116136 @test solve (probN, SimpleNewtonRaphson (; autodiff = false )). u ≈ sol
137+ @test solve (probN, Broyden ()). u ≈ sol
117138end
118139
119140# Bisection Tests
0 commit comments