Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1638 - Efficient way to obtain precise Interval bounds in 1D #1976

Merged
merged 2 commits into from
Feb 20, 2020

Conversation

schillic
Copy link
Member

Closes #1638.

The first commit is only related but it makes the convert faster.

julia> @btime convert(Interval, rand(Hyperrectangle, dim=1));  # master
  265.718 ns (7 allocations: 608 bytes)

julia> @btime convert(Interval, rand(Hyperrectangle, dim=1));  # this branch
  225.956 ns (6 allocations: 512 bytes)

@schillic schillic requested a review from mforets February 17, 2020 06:46
@mforets
Copy link
Member

mforets commented Feb 17, 2020

I haven't looked at the PR yet but here is a comment with respect to the benchmark: If the set is hyperrectangular, convert / overapproximate should be very cheap:

julia> using LazySets, BenchmarkTools

julia> x = rand(Hyperrectangle, dim=1);

julia> @btime convert(Interval, $x)
  81.583 ns (3 allocations: 288 bytes)
Interval{Float64,IntervalArithmetic.Interval{Float64}}([0.0447304, 1.20319])

julia> f(x) = Interval(center(x)[1] - radius_hyperrectangle(x, 1), center(x)[1] + radius_hyperrectangle(x)[1])
f (generic function with 1 method)

julia> @btime f($x)
  1.759 ns (0 allocations: 0 bytes)
Interval{Float64,IntervalArithmetic.Interval{Float64}}([0.0447304, 1.20319])

@schillic
Copy link
Member Author

If the set is hyperrectangular, convert / overapproximate should be very cheap

Yes, we already had a fast method for AbstractHyperrectangles.

@mforets
Copy link
Member

mforets commented Feb 17, 2020

True, we have it but it is suboptimal:

function convert(::Type{Interval}, H::AbstractHyperrectangle) # master
    @assert dim(H) == 1 "can only convert a one-dimensional $(typeof(H)) to `Interval`"
    return Interval([low(H); high(H)])
end

function convert3(::Type{Interval}, H::AbstractHyperrectangle)
    @assert dim(H) == 1 "can only convert a one-dimensional $(typeof(H)) to `Interval`"
    r = radius_hyperrectangle(H, 1)
    c = center(H)[1] # TODO: change to center(H, 1) when available (see #.....)
    return Interval(c-r, c+r)
end

julia> using BenchmarkTools; x= rand(Hyperrectangle, dim=1);

julia> @btime convert(Interval, $x) # master
  81.593 ns (3 allocations: 288 bytes)
Interval{Float64,IntervalArithmetic.Interval{Float64}}([-0.222182, 0.716047])

julia> @btime LazySets.convert3(Interval, $x) # new function
  7.583 ns (0 allocations: 0 bytes)
Interval{Float64,IntervalArithmetic.Interval{Float64}}([-0.222182, 0.716047])

julia> @btime LazySets.convert3(Interval, $x) # new function without assertion
  1.780 ns (0 allocations: 0 bytes)
Interval{Float64,IntervalArithmetic.Interval{Float64}}([-0.222182, 0.716047])

I know this discussion is unrelated to the PR, i will make a separate issue..

Copy link
Member

@mforets mforets left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm 🎉

@schillic schillic merged commit e6aaa09 into master Feb 20, 2020
@schillic schillic deleted the schillic/1638 branch February 20, 2020 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Efficient way to obtain precise Interval bounds in 1D
2 participants