Skip to content

Commit

Permalink
feat: bring in changes from SciML/SimpleNonlinearSolve.jl#158
Browse files Browse the repository at this point in the history
  • Loading branch information
avik-pal committed Oct 5, 2024
1 parent d780662 commit 69e5767
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
10 changes: 9 additions & 1 deletion lib/BracketingNonlinearSolve/src/bisection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ A common bisection method.
end

function CommonSolve.solve(prob::IntervalNonlinearProblem, alg::Bisection,
args...; maxiters = 1000, abstol = nothing, kwargs...)
args...; maxiters = 1000, abstol = nothing, verbose::Bool = true, kwargs...)
@assert !SciMLBase.isinplace(prob) "`Bisection` only supports out-of-place problems."

f = Base.Fix2(prob.f, prob.p)
Expand All @@ -40,6 +40,14 @@ function CommonSolve.solve(prob::IntervalNonlinearProblem, alg::Bisection,
prob, alg, right, fr; retcode = ReturnCode.ExactSolutionRight, left, right)
end

if sign(fl) == sign(fr)
verbose &&
@warn "The interval is not an enclosing interval, opposite signs at the \
boundaries are required."
return SciMLBase.build_solution(
prob, alg, left, fl; retcode = ReturnCode.InitialFailure, left, right)
end

i = 1
while i maxiters
mid = (left + right) / 2
Expand Down
10 changes: 9 additions & 1 deletion lib/BracketingNonlinearSolve/src/brent.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Left non-allocating Brent method.
struct Brent <: AbstractBracketingAlgorithm end

function CommonSolve.solve(prob::IntervalNonlinearProblem, alg::Brent, args...;
maxiters = 1000, abstol = nothing, kwargs...)
maxiters = 1000, abstol = nothing, verbose::Bool = true, kwargs...)
@assert !SciMLBase.isinplace(prob) "`Brent` only supports out-of-place problems."

f = Base.Fix2(prob.f, prob.p)
Expand All @@ -27,6 +27,14 @@ function CommonSolve.solve(prob::IntervalNonlinearProblem, alg::Brent, args...;
prob, alg, right, fr; retcode = ReturnCode.ExactSolutionRight, left, right)
end

if sign(fl) == sign(fr)
verbose &&
@warn "The interval is not an enclosing interval, opposite signs at the \
boundaries are required."
return SciMLBase.build_solution(
prob, alg, left, fl; retcode = ReturnCode.InitialFailure, left, right)
end

if abs(fl) < abs(fr)
left, right = right, left
fl, fr = fr, fl
Expand Down
10 changes: 9 additions & 1 deletion lib/BracketingNonlinearSolve/src/falsi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A non-allocating regula falsi method.
struct Falsi <: AbstractBracketingAlgorithm end

function CommonSolve.solve(prob::IntervalNonlinearProblem, alg::Falsi, args...;
maxiters = 1000, abstol = nothing, kwargs...)
maxiters = 1000, abstol = nothing, verbose::Bool = true, kwargs...)
@assert !SciMLBase.isinplace(prob) "`False` only supports out-of-place problems."

f = Base.Fix2(prob.f, prob.p)
Expand All @@ -27,6 +27,14 @@ function CommonSolve.solve(prob::IntervalNonlinearProblem, alg::Falsi, args...;
prob, alg, right, fr; retcode = ReturnCode.ExactSolutionRight, left, right)
end

if sign(fl) == sign(fr)
verbose &&
@warn "The interval is not an enclosing interval, opposite signs at the \
boundaries are required."
return SciMLBase.build_solution(
prob, alg, left, fl; retcode = ReturnCode.InitialFailure, left, right)
end

i = 1
while i maxiters
if Impl.nextfloat_tdir(left, l, r) == right
Expand Down
10 changes: 9 additions & 1 deletion lib/BracketingNonlinearSolve/src/itp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function ITP(; scaled_k1::Real = 0.2, k2::Real = 2, n0::Int = 10)
end

function CommonSolve.solve(prob::IntervalNonlinearProblem, alg::ITP, args...;
maxiters = 1000, abstol = nothing, kwargs...)
maxiters = 1000, abstol = nothing, verbose::Bool = true, kwargs...)
@assert !SciMLBase.isinplace(prob) "`ITP` only supports out-of-place problems."

f = Base.Fix2(prob.f, prob.p)
Expand All @@ -77,6 +77,14 @@ function CommonSolve.solve(prob::IntervalNonlinearProblem, alg::ITP, args...;
prob, alg, right, fr; retcode = ReturnCode.ExactSolutionRight, left, right)
end

if sign(fl) == sign(fr)
verbose &&
@warn "The interval is not an enclosing interval, opposite signs at the \
boundaries are required."
return SciMLBase.build_solution(
prob, alg, left, fl; retcode = ReturnCode.InitialFailure, left, right)
end

ϵ = abstol
k2 = alg.k2
k1 = alg.scaled_k1 * abs(right - left)^(1 - k2)
Expand Down
10 changes: 9 additions & 1 deletion lib/BracketingNonlinearSolve/src/ridder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A non-allocating ridder method.
struct Ridder <: AbstractBracketingAlgorithm end

function CommonSolve.solve(prob::IntervalNonlinearProblem, alg::Ridder, args...;
maxiters = 1000, abstol = nothing, kwargs...)
maxiters = 1000, abstol = nothing, verbose::Bool = true, kwargs...)
@assert !SciMLBase.isinplace(prob) "`Ridder` only supports out-of-place problems."

f = Base.Fix2(prob.f, prob.p)
Expand All @@ -26,6 +26,14 @@ function CommonSolve.solve(prob::IntervalNonlinearProblem, alg::Ridder, args...;
prob, alg, right, fr; retcode = ReturnCode.ExactSolutionRight, left, right)
end

if sign(fl) == sign(fr)
verbose &&
@warn "The interval is not an enclosing interval, opposite signs at the \
boundaries are required."
return SciMLBase.build_solution(
prob, alg, left, fl; retcode = ReturnCode.InitialFailure, left, right)
end

xo = oftype(left, Inf)
i = 1
while i maxiters
Expand Down

0 comments on commit 69e5767

Please sign in to comment.