Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Open Intervals

Following up on this question, I was wondering whether Mma can handle open intervals. For example, the union of the intervals, $$1<x<5$$ and $$5<x<8$$

should not include the number 5. This is easy enough to do in one's head, but how can it be done, if at all, computationally?

Interval Complement

Also, is there a way to find the complement of two intervals? IntervalComplement[int1,int2,int3] should contain all the points in int1 that are not in the other intervals.


Edit:

Let's take Mark McClure's data as an example.

int1 = x < -2 || -1 <= x < 1 || x == 3 || 4 < x <= Pi^2;
int2 = -3 <= x < 0 || x > 1;

The intervals are shown below:

intervals

The Interval Complement (drawn above in blue on the x-axis) would seem to be:

x < -3 || 0 <= x < 1
share|improve this question
@belisarius Yes, Andrzej Kozlowski's IntervalComplement does work for closed intervals. Very nice! It seems like his code does not handle open intervals, relying, as it does, on Interval for input. – David Carraher Oct 1 '12 at 2:57
1  
I have now read through to the end. I did not feel that anything was resolved. I did not find compelling the attempted explanation of IntervalComplement's inherent contradiction. Furthermore, the discussions about inclusion or not of endpoints in intervals was biased by the fact that mms does not (apparently) recognize open intervals. Would you like to present your take on the discussion? – David Carraher Oct 1 '12 at 3:18
I had have a little domestic accident and I'm typing single handed and painfully. Perhaps tomorrow,sorry. – belisarius Oct 1 '12 at 5:41
show 3 more comments

2 Answers

up vote 13 down vote accepted

I'd represent the sets using inequalities and/or equalities and then apply Reduce. Here's an example:

set1 = x < -2 || -1 <= x < 1 || x == 3 || 4 < x <= Pi^2;
set2 = -3 <= x < 0 || x > 1;
Reduce[set1 && set2]

enter image description here

Here's the complement of the union of the two intervals.

Reduce[!(set1 || set2)]

(* Out: x==1 *)

We might define an interval complement function as follows:

intervalComplement[bigInt_, moreInts__] := 
  Reduce[bigInt && (! (Or @@ {moreInts}))];

For example:

intervalComplement[-10 < x <= 10, -8 < x <= -6, 
  0 <= x <= 2, x == 3]
(* Out: -10 < x <= -8 || -6 < x < 0 || 2 < x < 3 || 3 < x <= 10 *)
share|improve this answer
You seem to have generated the intersection of the intervals instead of the complement of the intervals. Consider Graphics[{Arrow[{{-2, 1}, {-8, 1}}], Line[{{-1, 1}, {1, 1}}], Point[{3, 1}], Line[{{4, 1}, {Pi^2, 1}}], Line[{{-3, -1}, {0, -1}}], Arrow[{{1, -1}, {12, -1}}]}, Axes -> {True, True}, GridLinesStyle -> Dashed, BaseStyle -> 14, GridLines -> {{-2, -1, 1, 3, 4}, None}, Ticks -> {Range[-8, 10], None}] You gave the points common to set1, set2. – David Carraher Oct 1 '12 at 3:44
Let's see. In any case I probably won't be awarding the accept till tomorrow. It wouldn't surprise me if another came in. There are actually two interrelated questions: one related to open/closed intervals, the other to interval complements. – David Carraher Oct 1 '12 at 4:13
You might want to take a look at the explanation and picture I placed in the question. – David Carraher Oct 1 '12 at 4:31
@DavidCarraher I did. I also checked that my intervalComplement gave your desired answer. – Mark McClure Oct 1 '12 at 4:34
Very nice solution. It handles open intervals! – David Carraher Oct 1 '12 at 16:04

This is my code

{a = x > 1 && x < 5, b = x > 5 && x < 8}
{Reduce[a && b]}  

and

{a = x > 1 && x < 5, b = x >= 5 && x < 8}
{Reduce[a || b]}

Edit

Some examples

{a = x > 0 && x < 3, b = x > -1 && x < 2, c = x > -2 && x < 1} {Reduce[a && (b || c)],
Reduce[(a && b) || (a && c)], Reduce[a || (b && c)], Reduce[(a || b) && (a || c)]} 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.