Skip to content

Add open and mixed open/closed intervals #26

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

Merged
merged 19 commits into from
Sep 1, 2018

Conversation

dlfivefifty
Copy link
Member

@dlfivefifty dlfivefifty commented Mar 19, 2018

This PR adds support for general closed/open endpoints.

It also makes the following changes which I believe are necessary:

  1. union(a .. b, nextfloat(b) .. c) now throws an error (for a < b < c): this is consistent with the fact that union(0 .. 1, 2..3) should not be 0 .. 3, and because
a, b, c = 0.0, 1.0, 2.0
x = nextfloat(BigFloat(b))
!(x in a..b) && !(x in nextfloat(b)..c)

Previously x in union(a .. b, nextfloat(b) .. c) returned true, which is inconsistent with the above!
2. convert(UnitRange, 0..1) is now deprecated in favour of UnitRange(0..1), as convert should throw an error (e.g. convert(Int, 1+Im) throws an error).

@dlfivefifty
Copy link
Member Author

@timholy This is ready for review.

@ararslan ararslan requested a review from timholy March 21, 2018 19:08
@dlfivefifty
Copy link
Member Author

@timholy @ararslan Could this be looked at, please? I need this in the in-progress update to ApproxFun .

Copy link
Member

@ararslan ararslan left a comment

Choose a reason for hiding this comment

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

Seems okay to me other than the couple of things I noted.

@@ -4,29 +4,73 @@ module IntervalSets

using Base: @pure
import Base: eltype, convert, show, in, length, isempty, isequal, issubset, ==, hash,
union, intersect, minimum, maximum, extrema, range, ⊇
union, intersect, minimum, maximum, extrema, range, ⊇, mean, median
Copy link
Member

Choose a reason for hiding this comment

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

In Julia 0.7, mean and median are in the Statistics stdlib package. So instead of adding them here, you could add

import Compat.Statistics: mean, median

Just make sure that REQUIRE has an appropriate lower bound for Compat in that case.

end

mean(d::AbstractInterval) = one(eltype(d))/2 * (leftendpoint(d) + rightendpoint(d))
median(d::AbstractInterval) = mean(d)
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about this definition. Typically (not always, granted) the median of some set is an element of the set, but that isn't guaranteed when using a mean. Maybe this is fine, I dunno.

b
end

mean(d::AbstractInterval) = one(eltype(d))/2 * (leftendpoint(d) + rightendpoint(d))
Copy link
Member

Choose a reason for hiding this comment

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

Is the one part needed here? Couldn't you just write this as

(leftendpoint(d) + rightendpoint(d)) / 2

?

…ically for this

• AbstractInfiniteSet -> Domain
• deprecate length in favour of duration
@dlfivefifty
Copy link
Member Author

@ararslan I've addressed your comments, and just removed median as I don't see any use for it. I've also made the following changes which you might have some opinions on:

  1. I renamed AbstractInfiniteSet as Domain, as AbstractSet has a (currently undefined) meaning in Julia, which may implicitly imply that the set is countable. I'd be fine removing this abstract type completely.
  2. Added abstract type TypedEndpointsInterval{L,R,T} to allow custom implementations of intervals such as MyClosedUnitInterval.
  3. Deprecated length in favour of duration, to address Rename length to width/duration #31. This PR already implemented width for arclength.

@ararslan
Copy link
Member

Looks good to me, but I'm not a primary maintainer of this package, so it'd be good to hear from someone else as well before merging.

@dlfivefifty
Copy link
Member Author

We can always chat next week about it...

@dlfivefifty
Copy link
Member Author

Is leftendpoint/rightendpoint too verbose? Would left and right be better?

@ararslan
Copy link
Member

Personally I prefer the clarity of (left|right)endpoint; just left and right seem overly general, IMO.

@dlfivefifty
Copy link
Member Author

@timholy I mentioned this PR at JuliaCon. Do you have time to review it now?

(Still no rush on my part but a revamp of domains in ApproxFun.jl is waiting on this.)

Copy link
Member

@timholy timholy left a comment

Choose a reason for hiding this comment

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

Sorry I forgot about this. It looks fantastic! Tiny comments, neither of which is necessary.

@timholy
Copy link
Member

timholy commented Sep 1, 2018

In other words, merge at will. If you are (justifiably) annoyed by the delay, I can help fix those merge conflicts. Just let me know.

@dlfivefifty
Copy link
Member Author

That's OK, I've resolved the conflicts, though you might have comments on the changes I made:

  1. I removed the general AbstractUnitRange conversion because it seemed to broad, in favour of specific ones:
UnitRange{T}(i::TypedEndpointsInterval{:closed,:closed,I}) where {T<:Integer,I<:Integer} = UnitRange{T}(minimum(i), maximum(i))
UnitRange(i::TypedEndpointsInterval{:closed,:closed,I}) where {I<:Integer} = UnitRange{I}(i)
  1. I added convert(AbstractInterval, x::Number) and variants
  2. I overrode issubset, as the existing definition called the now deprecated length.

@timholy
Copy link
Member

timholy commented Sep 1, 2018 via email

@dlfivefifty dlfivefifty merged commit a0983f1 into JuliaMath:master Sep 1, 2018
@dlfivefifty dlfivefifty deleted the dl/open branch September 1, 2018 19:34
@timholy
Copy link
Member

timholy commented Sep 3, 2018

Having looked again I think your taste in these matters was exquisite. I really like the distinction you made in the comments between conversion and construction, I'm going to be more deliberate about that in the future.

The override for issusbset will become complicated once the backports happen for 0.7, but that's just how it goes.

Comment on lines +194 to +203
# convert numbers to intervals
convert(::Type{AbstractInterval}, x::Number) = x..x
convert(::Type{AbstractInterval{T}}, x::Number) where T =
convert(AbstractInterval{T}, convert(AbstractInterval, x))
convert(::Type{TypedEndpointsInterval{:closed,:closed}}, x::Number) = x..x
convert(::Type{TypedEndpointsInterval{:closed,:closed,T}}, x::Number) where T =
convert(AbstractInterval{T}, convert(AbstractInterval, x))
convert(::Type{ClosedInterval}, x::Number) = x..x
convert(::Type{ClosedInterval{T}}, x::Number) where T =
convert(AbstractInterval{T}, convert(AbstractInterval, x))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need these conversion methods? x-ref:#97

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.

4 participants