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

Sum of ranges in a set-like context #33577

Open
rapus95 opened this issue Oct 16, 2019 · 4 comments
Open

Sum of ranges in a set-like context #33577

rapus95 opened this issue Oct 16, 2019 · 4 comments
Labels
feature Indicates new feature / enhancement requests ranges Everything AbstractRange

Comments

@rapus95
Copy link
Contributor

rapus95 commented Oct 16, 2019

Right now we have

julia> r1=range(-2, 4, step=0.5)
-2.0:0.5:4.0

julia> r2 = range(3, 9, step=0.5)
3.0:0.5:9.0

julia> r1+r2
1.0:1.0:13.0

julia> r1.+r2
1.0:1.0:13.0

But I wonder whether it would be worthwhile to make + the set addition (aka r1+r2 = {a+b | a\in r1, b\in r2})
and thus the step wouldn't change.
Implementation:

julia> function newadd(a::AbstractRange, b::AbstractRange)
    step(a)==step(b) || error("have to have same step")
    return range(first(a)+first(b), last(a)+last(b), step=step(a))
end
newadd (generic function with 1 method)

julia> newadd(r1,r2)
1.0:0.5:13.0 #step ist now 0.5 instead of 1.0
@mbauman
Copy link
Member

mbauman commented Oct 16, 2019

+ means addition. Full stop. This isn't going to change. Even more ranges are arrays. It would be very strange for + to mean one thing for some arrays, but another for other sorts of arrays.

Now, we could potentially try to do something clever with union/ — that's the operation you want. But you'd also have to be careful about non-overlapping ranges.

@rapus95
Copy link
Contributor Author

rapus95 commented Oct 16, 2019

+ means addition. Full stop. This isn't going to change. Even more ranges are arrays. It would be very strange for + to mean one thing for some arrays, but another for other sorts of arrays.

I meanwhile stumbled over AbstractRange<:AbstractVector aswell and from there I must say that I'd hate the inconsistency if addition wouldn't behave vector-like.

Now, we could potentially try to do something clever with union/ — that's the operation you want. But you'd also have to be careful about non-overlapping ranges.

Though for the other point you made I need to disagree. The range in the mathematical sense is just an intersection of a discrete set with an interval. (In case of AbstractUnitRange the discrete set is Z)
Thus the addition of two objects, each defined as an intersection of sets, can indeed be treated as the addition of sets, which in turn behaves different than a union.
The union would not necessarily produce a contiguous set as the intervals can be disjoint and length(r1\union r2)!= length(setadd(r1,r2))=length(r1)+length(r2)-1
Thus as a summary: AbstractRange has multiple abstractions, the one as a vector, which it is currently referring to, and the one as a set, which I was referring to.
As we cannot and do not want to implement context-sensitive addition I'd also stick with the non-breaking definition of +.
Remaining question is now, where to put such set-oriented addition and how to allow for elegant solutions in both contexts.

@rapus95 rapus95 changed the title range .+ range vs range + range (broadcast vs +) Sum of ranges in a set-like context Oct 16, 2019
@mbauman
Copy link
Member

mbauman commented Oct 16, 2019

Oh I see — I was misunderstanding the operation you were after. It looks like what you want is sometimes called the Minkowski sum.

Ranges (and arrays in general) are totally capable set-like objects — they support union, intersect, setdiff, and more.

@tkf
Copy link
Member

tkf commented Oct 16, 2019

FYI, there are discussions about using broadcasting to implement this kind of "product-based" arithmetics: #33282, JuliaMath/IntervalSets.jl#55

@brenhinkeller brenhinkeller added feature Indicates new feature / enhancement requests ranges Everything AbstractRange labels Nov 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Indicates new feature / enhancement requests ranges Everything AbstractRange
Projects
None yet
Development

No branches or pull requests

4 participants