Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Calculate the complement of intervals
| ResourceFunction["IntervalComplement"][intervalall,interval1,interval2,…] gives the interval representing all the points of intervalall that are not in any of the intervali. | 
Calculate the complement of two intervals:
| In[1]:= | ![ResourceFunction["IntervalComplement"][Interval[{3, 10}], Interval[{0, 5}]]](https://www.wolframcloud.com/obj/resourcesystem/images/f84/f8497275-4db5-4f4f-b292-810a04373213/296d142aba3510e6.png) | 
| Out[1]= |  | 
Intervals can be disjoint:
| In[2]:= | ![ResourceFunction["IntervalComplement"][Interval[{0, 4}, {8, 12}], Interval[{2, 3}, {6, 11}]]](https://www.wolframcloud.com/obj/resourcesystem/images/f84/f8497275-4db5-4f4f-b292-810a04373213/5faa9fe17e92367d.png) | 
| Out[2]= |  | 
"Subtract" two Interval objects from another interval object:
| In[3]:= | ![ResourceFunction["IntervalComplement"][Interval[{0, 10}], Interval[{-2, 5}], Interval[{6, 11}]]](https://www.wolframcloud.com/obj/resourcesystem/images/f84/f8497275-4db5-4f4f-b292-810a04373213/7e4c40f181fa2ce7.png) | 
| Out[3]= |  | 
Subtracting an interval might create a disjoint interval:
| In[4]:= | ![ResourceFunction["IntervalComplement"][Interval[{0, 10}], Interval[{3, 7}]]](https://www.wolframcloud.com/obj/resourcesystem/images/f84/f8497275-4db5-4f4f-b292-810a04373213/03bfad4a09c592e8.png) | 
| Out[4]= |  | 
Unbounded intervals can be used:
| In[5]:= | ![ResourceFunction["IntervalComplement"][Interval[{0, 10}], Interval[{-\[Infinity], 5}]]](https://www.wolframcloud.com/obj/resourcesystem/images/f84/f8497275-4db5-4f4f-b292-810a04373213/5dc0cee2c48c610b.png) | 
| Out[5]= |  | 
| In[6]:= | ![ResourceFunction["IntervalComplement"][Interval[{0, \[Infinity]}], Interval[{-5, 5}]]](https://www.wolframcloud.com/obj/resourcesystem/images/f84/f8497275-4db5-4f4f-b292-810a04373213/222a34d8163920ae.png) | 
| Out[6]= |  | 
| In[7]:= | ![ResourceFunction["IntervalComplement"][
 Interval[{-\[Infinity], \[Infinity]}], Interval[{-5, 5}]]](https://www.wolframcloud.com/obj/resourcesystem/images/f84/f8497275-4db5-4f4f-b292-810a04373213/0a2dd1d843f37362.png) | 
| Out[7]= |  | 
If there is full overlap an empty interval is returned:
| In[8]:= | ![ResourceFunction["IntervalComplement"][Interval[{0, 10}], Interval[{-\[Infinity], \[Infinity]}]]](https://www.wolframcloud.com/obj/resourcesystem/images/f84/f8497275-4db5-4f4f-b292-810a04373213/5b4a3070fcdc0bd6.png) | 
| Out[8]= |  | 
An empty interval stays empty:
| In[9]:= | ![ResourceFunction["IntervalComplement"][Interval[], Interval[{-5, 5}]]](https://www.wolframcloud.com/obj/resourcesystem/images/f84/f8497275-4db5-4f4f-b292-810a04373213/2364649280e46059.png) | 
| Out[9]= |  | 
Complementing with an empty interval has no effect:
| In[10]:= | ![ResourceFunction["IntervalComplement"][Interval[{-5, 5}], Interval[]]](https://www.wolframcloud.com/obj/resourcesystem/images/f84/f8497275-4db5-4f4f-b292-810a04373213/228310a074cdd973.png) | 
| Out[10]= |  | 
Calculate the absolute complement of the interval [-5,5] by taking the relative complement with the full interval:
| In[11]:= | ![a = Interval[{5, -5}];
b = ResourceFunction["IntervalComplement"][
   Interval[{-\[Infinity], \[Infinity]}], a];
Quiet[NumberLinePlot[{a, b}]]](https://www.wolframcloud.com/obj/resourcesystem/images/f84/f8497275-4db5-4f4f-b292-810a04373213/386619580f14d781.png) | 
| Out[13]= |  | 
Compare the complement with the IntervalUnion and the IntervalIntersection of two intervals:
| In[14]:= | ![a = Interval[{0, 10}];
b = Interval[{5, 15}];
NumberLinePlot[{a, b, IntervalUnion[a, b], IntervalIntersection[a, b],
   ResourceFunction["IntervalComplement"][a, b], ResourceFunction["IntervalComplement"][b, a]}, PlotLegends -> {"a", "b", "a\[MediumSpace]\[Union]\[MediumSpace]b", "a\[MediumSpace]\[Intersection]\[MediumSpace]b", "a\[MediumSpace]\\\[MediumSpace]b", "b\[MediumSpace]\\\[MediumSpace]a"}]](https://www.wolframcloud.com/obj/resourcesystem/images/f84/f8497275-4db5-4f4f-b292-810a04373213/2d33f70bd4ce88f3.png) | 
| Out[16]= |  | 
Compare to regular sets:
| In[17]:= | ![a = Range[0, 10];
b = Range[5, 15];
NumberLinePlot[{a, b, Union[a, b], Intersection[a, b], Complement[a, b], Complement[b, a]}, PlotLegends -> {"a", "b", "a\[MediumSpace]\[Union]\[MediumSpace]b", "a\[MediumSpace]\[Intersection]\[MediumSpace]b", "a\[MediumSpace]\\\[MediumSpace]b", "b\[MediumSpace]\\\[MediumSpace]a"}]](https://www.wolframcloud.com/obj/resourcesystem/images/f84/f8497275-4db5-4f4f-b292-810a04373213/36b445d7bcbf51e6.png) | 
| Out[19]= |  | 
Intervals need to be numeric, if the input is symbolic it will stay unevaluated:
| In[20]:= | ![ResourceFunction["IntervalComplement"][Interval[{0, 10}], Interval[{-2, c}]]](https://www.wolframcloud.com/obj/resourcesystem/images/f84/f8497275-4db5-4f4f-b292-810a04373213/13ac8ffdae77890e.png) | 
| Out[20]= |  | 
Visualize the "subtraction" of multiple intervals (red) from a base interval (green):
| In[21]:= | ![all = Interval[{-2, 3}, {7, 8}, {11, 12}, {15, \[Infinity]}];
other = {Interval[{1.6, 3.1}, {3.5, 4}, {6, 7.5}], Interval[{7.5, 7.75}], Interval[{-\[Infinity], -1}], Interval[{0.1, 0.7}], Interval[{11.5, 17.5}]};
comp = ResourceFunction["IntervalComplement"][all, Sequence @@ other];
NumberLinePlot[
 Join[{Style[all, Darker@Green]}, Style[#, Red] & /@ other, {Style[comp, Black]}], PlotLegends -> LineLegend[{Darker@Green, Red, Red, Red, Red, Black}, {"\!\(\*SubscriptBox[\(Interval\), \(all\)]\)", "\!\(\*SubscriptBox[\(Interval\), \(1\)]\)", "\!\(\*SubscriptBox[\(Interval\), \(2\)]\)", "\!\(\*SubscriptBox[\(Interval\), \(3\)]\)", "\!\(\*SubscriptBox[\(Interval\), \(4\)]\)", "Complement"}]]](https://www.wolframcloud.com/obj/resourcesystem/images/f84/f8497275-4db5-4f4f-b292-810a04373213/30789269510ac295.png) | 
| Out[24]= |  | 
This work is licensed under a Creative Commons Attribution 4.0 International License