JuliaIntervals / IntervalRootFinding.jl

Library for finding the roots of a function using interval arithmetic
https://juliaintervals.github.io/IntervalRootFinding.jl/
Other
127 stars 26 forks source link

find_roots_midpoint should gracefully handle cases with no roots #71

Closed pkofod closed 5 years ago

pkofod commented 6 years ago
julia> using IntervalRootFinding

julia> f(x)=x^0
f (generic function with 1 method)

julia> IntervalRootFinding.find_roots_midpoint(f, -5, 5)
ERROR: BoundsError: attempt to access 0-element Array{IntervalRootFinding.Root{IntervalArithmetic.Interval{Float64}},1} at index [1]
Stacktrace:
 [1] getindex(::Array{IntervalRootFinding.Root{IntervalArithmetic.Interval{Float64}},1}, ::Int64) at ./array.jl:520
 [2] #find_roots_midpoint#61(::Float64, ::Bool, ::Int64, ::Int64, ::Function, ::Function, ::Int64, ::Int64, ::IntervalRootFinding.#newton) at /home/pkm/.julia/v0.6/IntervalRootFinding/src/IntervalRootFinding.jl:107
 [3] find_roots_midpoint(::Function, ::Int64, ::Int64) at /home/pkm/.julia/v0.6/IntervalRootFinding/src/IntervalRootFinding.jl:105

julia> IntervalRootFinding.find_roots(f, -5, 5)
0-element Array{IntervalRootFinding.Root{IntervalArithmetic.Interval{Float64}},1}

It shouldn't throw an error, but return an empty result. Or error with a better error :)

dpsanders commented 6 years ago

Thanks for the report. find_roots_midpoint is part of the old API and will probably disappear. For the moment you can use

rts = roots(f, -5..5)
mid.([rt.interval for rt in rts])   # edited for correct syntax

Later you will be able to do

mid.(interval.(rts))

Nonetheless, the original idea of find_roots_midpoint was to have an API in which the user did not need to know anything at all about intervals. Probably we should strive to maintain such an API available?

pkofod commented 6 years ago

Either way is fine to me!

Later you will be able to do

does that mean "on master" or "yet to be implemented" ? :)

dpsanders commented 6 years ago

I thought it was a PR, but actually it works on master!


julia> using IntervalRootFinding
INFO: Precompiling module IntervalRootFinding.

julia> rts = roots(x->x^2 - 2, -5..5)
mi2-element Array{IntervalRootFinding.Root{IntervalArithmetic.Interval{Float64}},1}:
d. Root([1.41421, 1.41422], :unique)
 Root([-1.41422, -1.41421], :unique)

julia> mid.(interval.(rts))
2-element Array{Float64,1}:
  1.41421
 -1.41421
pkofod commented 6 years ago

Great! Will continue my interval endeavors then!

dpsanders commented 5 years ago

This part of the API is no longer in use.