Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit 88bd472

Browse files
committed
Add tests and doc
1 parent ed4751f commit 88bd472

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

ext/SimpleNonlinearSolveTaylorDiffExt.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ using SciMLBase: isinplace
77

88
import TaylorDiff
99

10-
@inline function __get_higher_order_derivatives(::SimpleHouseholder{N}, prob, f, x) where N
10+
@inline function __get_higher_order_derivatives(::SimpleHouseholder{N}, prob, f, x, fx) where N
1111
vN = Val(N)
1212
l = map(one, x)
13+
t = TaylorDiff.make_seed(x, l, vN)
1314

1415
if isinplace(prob)
15-
fx = f(x)
16-
invf = x -> inv.(f(x))
17-
bundle = TaylorDiff.derivatives(invf, fx, x, l, vN)
16+
bundle = similar(fx, TaylorDiff.TaylorScalar{eltype(fx), N})
17+
f(bundle, t)
18+
map!(TaylorDiff.primal, fx, bundle)
1819
else
19-
t = TaylorDiff.make_seed(x, l, vN)
20-
ft = f(t)
21-
fx = map(TaylorDiff.primal, ft)
22-
bundle = inv.(ft)
20+
bundle = f(t)
21+
fx = map(TaylorDiff.primal, bundle)
2322
end
23+
bundle = inv.(bundle)
2424
num = TaylorDiff.extract_derivative(bundle, N - 1)
2525
den = TaylorDiff.extract_derivative(bundle, N)
2626
return num, den, fx
@@ -40,7 +40,7 @@ function SciMLBase.__solve(prob::ImmutableNonlinearProblem, alg::SimpleHousehold
4040
prob, abstol, reltol, fx, x, termination_condition)
4141

4242
for i in 1:maxiters
43-
num, den, fx = __get_higher_order_derivatives(alg, prob, f, x)
43+
num, den, fx = __get_higher_order_derivatives(alg, prob, f, x, fx)
4444

4545
if i == 1
4646
iszero(fx) && build_solution(prob, alg, x, fx; retcode = ReturnCode.Success)

src/nlsolve/householder.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
"""
22
SimpleHouseholder{order}()
33
4-
A low-overhead implementation of Householder's method. This method is non-allocating on scalar
5-
and static array problems.
4+
A low-overhead implementation of Householder's method to arbitrary order.
5+
This method is non-allocating on scalar and static array problems.
66
7-
Internally, this uses TaylorDiff.jl for the automatic differentiation.
7+
!!! warning
8+
9+
Needs `TaylorDiff.jl` to be explicitly loaded before using this functionality.
10+
Internally, this uses TaylorDiff.jl for automatic differentiation.
811
912
### Type Parameters
1013

test/core/rootfind_tests.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,31 @@ end
9191
end
9292
end
9393

94+
@testitem "SimpleHouseholder" setup=[RootfindingTesting] tags=[:core] begin
95+
using TaylorDiff
96+
@testset "AutoDiff: TaylorDiff.jl" for order in (2, 3, 4)
97+
@testset "[OOP] u0: $(nameof(typeof(u0)))" for u0 in (
98+
[1.0], @SVector[1.0], 1.0)
99+
sol = benchmark_nlsolve_oop(quadratic_f, u0; solver = SimpleHouseholder{order}())
100+
@test SciMLBase.successful_retcode(sol)
101+
@test all(abs.(sol.u .* sol.u .- 2) .< 1e-9)
102+
end
103+
104+
@testset "[IIP] u0: $(nameof(typeof(u0)))" for u0 in ([1.0],)
105+
sol = benchmark_nlsolve_iip(quadratic_f!, u0; solver = SimpleHouseholder{order}())
106+
@test SciMLBase.successful_retcode(sol)
107+
@test all(abs.(sol.u .* sol.u .- 2) .< 1e-9)
108+
end
109+
end
110+
111+
@testset "Termination condition: $(nameof(typeof(termination_condition))) u0: $(nameof(typeof(u0)))" for termination_condition in TERMINATION_CONDITIONS,
112+
u0 in (1.0, [1.0], @SVector[1.0])
113+
114+
probN = NonlinearProblem(quadratic_f, u0, 2.0)
115+
@test all(solve(probN, SimpleHouseholder{2}(); termination_condition).u .≈ sqrt(2.0))
116+
end
117+
end
118+
94119
@testitem "Derivative Free Metods" setup=[RootfindingTesting] tags=[:core] begin
95120
@testset "$(nameof(typeof(alg)))" for alg in [
96121
SimpleBroyden(), SimpleKlement(), SimpleDFSane(),

0 commit comments

Comments
 (0)