Skip to content

Commit

Permalink
Merge pull request #86 from SciML/simplehally
Browse files Browse the repository at this point in the history
Halley -> SimpleHalley
  • Loading branch information
ChrisRackauckas committed Oct 17, 2023
2 parents 354bb89 + 2107cd7 commit 9737208
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
5 changes: 2 additions & 3 deletions src/SimpleNonlinearSolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ import PrecompileTools
PrecompileTools.@compile_workload begin
for T in (Float32, Float64)
prob_no_brack = NonlinearProblem{false}((u, p) -> u .* u .- p, T(0.1), T(2))
for alg in (SimpleNewtonRaphson, Halley, Broyden, Klement, SimpleTrustRegion,
for alg in (SimpleNewtonRaphson, SimpleHalley, Broyden, Klement, SimpleTrustRegion,
SimpleDFSane)
solve(prob_no_brack, alg(), abstol = T(1e-2))
end
Expand All @@ -88,8 +88,7 @@ PrecompileTools.@compile_workload begin
end
end

# DiffEq styled algorithms
export Bisection, Brent, Broyden, LBroyden, SimpleDFSane, Falsi, Halley, Klement,
export Bisection, Brent, Broyden, LBroyden, SimpleDFSane, Falsi, SimpleHalley, Klement,
Ridder, SimpleNewtonRaphson, SimpleTrustRegion, Alefeld, ITP
export BatchedBroyden, BatchedSimpleNewtonRaphson, BatchedSimpleDFSane

Expand Down
12 changes: 6 additions & 6 deletions src/halley.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""
```julia
Halley(; chunk_size = Val{0}(), autodiff = Val{true}(),
SimpleHalley(; chunk_size = Val{0}(), autodiff = Val{true}(),
diff_type = Val{:forward})
```
A low-overhead implementation of Halley's Method. This method is non-allocating on scalar
A low-overhead implementation of SimpleHalley's Method. This method is non-allocating on scalar
and static array problems.
!!! note
Expand All @@ -28,16 +28,16 @@ and static array problems.
`Val{:forward}` for forward finite differences. For more details on the choices, see the
[FiniteDiff.jl](https://github.com/JuliaDiff/FiniteDiff.jl) documentation.
"""
struct Halley{CS, AD, FDT} <: AbstractNewtonAlgorithm{CS, AD, FDT}
function Halley(; chunk_size = Val{0}(), autodiff = Val{true}(),
struct SimpleHalley{CS, AD, FDT} <: AbstractNewtonAlgorithm{CS, AD, FDT}
function SimpleHalley(; chunk_size = Val{0}(), autodiff = Val{true}(),
diff_type = Val{:forward})
new{SciMLBase._unwrap_val(chunk_size), SciMLBase._unwrap_val(autodiff),
SciMLBase._unwrap_val(diff_type)}()
end
end

function SciMLBase.__solve(prob::NonlinearProblem,
alg::Halley, args...; abstol = nothing,
alg::SimpleHalley, args...; abstol = nothing,
reltol = nothing,
maxiters = 1000, kwargs...)
f = Base.Fix2(prob.f, prob.p)
Expand All @@ -49,7 +49,7 @@ function SciMLBase.__solve(prob::NonlinearProblem,
T = typeof(x)

if SciMLBase.isinplace(prob)
error("Halley currently only supports out-of-place nonlinear problems")
error("SimpleHalley currently only supports out-of-place nonlinear problems")
end

atol = abstol !== nothing ? abstol :
Expand Down
12 changes: 6 additions & 6 deletions test/basictests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ if VERSION >= v"1.7"
@test (@ballocated benchmark_scalar(sf, csu0)) == 0
end

# Halley
# SimpleHalley
function benchmark_scalar(f, u0)
probN = NonlinearProblem{false}(f, u0)
sol = (solve(probN, Halley()))
sol = (solve(probN, SimpleHalley()))
end

function ff(u, p)
Expand Down Expand Up @@ -139,7 +139,7 @@ using ForwardDiff
f, u0 = (u, p) -> u .* u .- p, @SVector[1.0, 1.0]

for alg in (SimpleNewtonRaphson(), LBroyden(), Klement(), SimpleTrustRegion(),
SimpleDFSane(), Halley(), BROYDEN_SOLVERS...)
SimpleDFSane(), SimpleHalley(), BROYDEN_SOLVERS...)
g = function (p)
probN = NonlinearProblem{false}(f, csu0, p)
sol = solve(probN, alg, abstol = 1e-9)
Expand All @@ -162,7 +162,7 @@ end
# Scalar
f, u0 = (u, p) -> u * u - p, 1.0
for alg in (SimpleNewtonRaphson(), Klement(), SimpleTrustRegion(),
SimpleDFSane(), Halley(), BROYDEN_SOLVERS..., LBROYDEN_SOLVERS...)
SimpleDFSane(), SimpleHalley(), BROYDEN_SOLVERS..., LBROYDEN_SOLVERS...)
g = function (p)
probN = NonlinearProblem{false}(f, oftype(p, u0), p)
sol = solve(probN, alg)
Expand Down Expand Up @@ -271,7 +271,7 @@ for alg in [Bisection(), Falsi(), Ridder(), Brent(), ITP()]
end

for alg in (SimpleNewtonRaphson(), Klement(), SimpleTrustRegion(),
SimpleDFSane(), Halley(), BROYDEN_SOLVERS..., LBROYDEN_SOLVERS...)
SimpleDFSane(), SimpleHalley(), BROYDEN_SOLVERS..., LBROYDEN_SOLVERS...)
global g, p
g = function (p)
probN = NonlinearProblem{false}(f, 0.5, p)
Expand All @@ -288,7 +288,7 @@ probN = NonlinearProblem(f, u0)

for alg in (SimpleNewtonRaphson(), SimpleNewtonRaphson(; autodiff = false),
SimpleTrustRegion(),
SimpleTrustRegion(; autodiff = false), Halley(), Halley(; autodiff = false),
SimpleTrustRegion(; autodiff = false), SimpleHalley(), SimpleHalley(; autodiff = false),
Klement(), SimpleDFSane(),
BROYDEN_SOLVERS..., LBROYDEN_SOLVERS...)
sol = solve(probN, alg)
Expand Down

0 comments on commit 9737208

Please sign in to comment.