>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 x>a and xa and x<=b :
return True
return False
def contains_co(x):
if x>=a and 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_bound>self.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_bound>> I=Interval(1,4,low_open=False,up_open=False)
>>> J=Interval(0,2,low_open=True,up_open=True)
>>> K=Interval(3,5,low_open=False,up_open=True)
>>> L=Interval(6,7,low_open=False,up_open=False)
>>> X=I.fusion_with_list([J,K,L])
>>> for a in X:
... print a
[6 , 7]
]0 , 5[
The result does not depend on the order of the list:
>>> X=I.fusion_with_list([L,K,J])
>>> for a in X:
... print a
[6 , 7]
]0 , 5[
"""
new_list=[]
happend_fusion=False
for I in intervals_list:
a = self.fusion(I)
if len(a)==1:
if happend_fusion == False:
new_guy=a[0]
happend_fusion=True
else :
new_list.append(a[0])
if len(a)==2:
new_list.append(I)
if happend_fusion :
return new_guy.fusion_with_list(new_list)
else :
new_list.append(self)
return tuple(new_list)
def union(self,A):
return constructorContinuouSet(self).union(constructorContinuouSet(A))
def copy(self):
"""
Return a copy of the interval which is a new object.
>>> I=Interval(1,4,low_open=False,up_open=False)
>>> X=I.copy()
>>> I.low_open=T