Skip to content

Commit

Permalink
Always return a solution type
Browse files Browse the repository at this point in the history
  • Loading branch information
YingboMa committed Sep 21, 2020
1 parent dc869a9 commit 75bf864
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/scalar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ function DiffEqBase.solve(prob::NonlinearProblem{<:Number}, ::NewtonRaphson, arg
xo = oftype(x, Inf)
for i in 1:maxiters
fx, dfx = value_derivative(f, x)
iszero(fx) && return x
iszero(fx) && return NewtonSolution(x, :Default)
Δx = dfx \ fx
x -= Δx
if isapprox(x, xo, atol=atol, rtol=rtol)
return x
return NewtonSolution(x, :Default)
end
xo = x
end
return oftype(x, NaN)
return NewtonSolution(x, :MaxitersExceeded)
end

function DiffEqBase.solve(prob::NonlinearProblem{<:Number}, ::Bisection, args...; maxiters = 1000, kwargs...)
Expand Down
1 change: 1 addition & 0 deletions src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ end
function DiffEqBase.init(prob::NonlinearProblem{uType, iip}, alg::AbstractBracketingAlgorithm, args...;
alias_u0 = false,
maxiters = 1000,
# bracketing algorithms only solve scalar problems
immutable = (prob.u0 isa StaticArray || prob.u0 isa Number),
kwargs...
) where {uType, iip}
Expand Down
5 changes: 4 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ end
f, u0 = (u,p) -> u .* u .- 2, @SVector[1.0, 1.0]
sf, su0 = (u,p) -> u * u - 2, 1.0
sol = benchmark_immutable(f, u0)
@test sol.retcode === :Default
@test all(sol.u .* sol.u .- 2 .< 1e-9)
sol = benchmark_mutable(f, u0)
@test sol.retcode === :Default
@test all(sol.u .* sol.u .- 2 .< 1e-9)
sol = benchmark_scalar(sf, su0)
@test sol * sol - 2 < 1e-9
@test sol.retcode === :Default
@test sol.u * sol.u - 2 < 1e-9

@test (@ballocated benchmark_immutable($f, $u0)) == 0
@test (@ballocated benchmark_mutable($f, $u0)) < 200
Expand Down

0 comments on commit 75bf864

Please sign in to comment.