It seems like it would be relatively easy to implement a Sage class
for real intervals that represents finite unions of open, closed, half
open, and unbounded intervals and implements union() and
intersection() methods.
I did it. I attach my piece of code. This is only python, but it is
going to make the job.
Technically I define a class Interval that represent an interval (closed
or open at each extremities). This class has the important
__contrain__() method that tests if a number is contained in the interval.
Then class ContinuousSet that represent finite union and intersections
of intervals. Its main attribute is a list of disjoint intervals. These
intervals represent the set.
For the moment, I have working union() and __contain__() methods; the
delicate part is to express the union of two lists of disjoints
intervals as a new list of disjoint intervals. I'm working on
intersections and I plan to be able to test inclusion.
doctest of the working methods are included in the code.
Up to writing better documentation and finishing the work, do one think
that it can be included in some place in Sage ?
Have a good day
Laurent
--
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org
#! /usr/bin/python
# -*- coding: utf8 -*-
import doctest
def constructorContinuouSet(A):
if type(A)==ContinuousSet:
return A
if type(A)==Interval:
return ContinuousSet([A],[])
def EmptySet():
return ContinuousSet([],[])
class Interval(object):
Represent the real interval between a and b.
By default, the interval is closed : [a,b]
if low_open is false, the interval is lower opened
if up_open is false, the interval is upper opened.
Examples
DEFINITION OF INTERVALS
I=Interval(0,2,low_open=False,up_open=True)
J=Interval(-2,-1)
K=Interval(1,3,low_open=False,up_open=True)
print I
[0 , 2[
print J
[-2 , -1]
print K
[1 , 3[
INTERSECTION
X = I.intersection(K)
print X
[1 , 2[
print 1 in X
True
print 2 in X
False
FUSION
The intervals I and J have no intersection, so the union is disjoint :
Y=I.fusion(J)
for a in Y:
... print a
[0 , 2[
[-2 , -1]
The intervals I and K have intersection, so the union is one interval:
Y=I.fusion(K)
for a in Y:
... print a
...
[0 , 3[
UNION
Asking the union of two intervals returns an object ContinuousSet
import math
I=Interval(1,4,low_open=False,up_open=False)
J=Interval(0,2,low_open=True,up_open=True)
M=Interval(7,8,low_open=True,up_open=False)
print I.union(J)
]0 , 4]
print I.union(M)
]7 , 8] U [1 , 4]
7 in I.union(M)
False
math.pi in I.union(M)
True
def __init__(self,a,b,low_open=False,up_open=False):
self.low_bound=a
self.up_bound=b
self.low_open=low_open
self.up_open=up_open
def contains_oo(x):
if xa and xb :
return True
return False
def contains_oc(x):
if xa and x=b :
return True
return False
def contains_co(x):
if x=a and xb :
return True
return False
def contains_cc(x):
if x=a and x=b :
return True
return False
if self.low_open and self.up_open:
self.contains=contains_oo
elif self.low_open and not self.up_open:
self.contains=contains_oc
elif not self.low_open and self.up_open:
self.contains=contains_co
elif not self.low_open and not self.up_open:
self.contains=contains_cc
def intersection(self,A):
if self.low_bound in A or self.up_bound in A or A.low_bound in self or A.up_bound in self :
low_bound=self.low_bound
up_bound=self.up_bound
low_open=self.low_open
up_open=self.up_open
if A.low_boundself.low_bound:
low_bound=A.low_bound
low_open=A.low_open
if A.low_bound==self.low_bound:
low_open = self.low_bound and A.low_bound
if A.up_boundself.up_bound:
up_bound=A.up_bound
up_open=A.up_open
if A.up_bound==self.up_bound:
up_open = self.up_open and A.up_open
if low_bound == up_bound:
return ContinuousSet([],[low_bound])
else:
return ContinuousSet([Interval(low_bound,up_bound,low_open=low_open,up_open=up_open)],[])
else :
return EmptySet()
def fusion(self,A):
Return the fusion of self and A where A is supposed to be an interval.
If the union of self and A is an interval B, return the tuple (B).
Else, return the tuple (self,A)
a=self.low_bound
b=self.up_bound
c=A.low_bound
d=A.up_bound
if (a in A) or (b in A) or (c in self) or (d in self):
m=min(a,c)
M=max(b,d)
low_open=True
up_open=True
if m==a and self.low_open==False:
low_open=False
if m==c and A.low_open==False:
low_open=False
if M==b and self.up_open==False:
up_open=False
if M==d and A.up_open==False:
up_open=False
return tuple([Interval(m,M,low_open=low_open,up_open=up_open)])
return