Christophe a écrit :
> Aaron S. Meurer a écrit :
>>
>> Interval(3, 4, endpoints='oc'), which would create (3, 4] ('oc'
>> stands for open-closed).
>
> Hello here is a first try fopr working with sets. This code has been
> writing directly as I was thinking of what I neede. I think that it
> could be better. I'll work on unions of intervals : ythis would be
> simply a list of single intervals with additional methods so as to
> simplify a union. For the moment, only the intersection has been made.
> I hope that I didn't forget something.
>
> Every kind of suggestion is welcome.
>
> Christophe.
I've forgotten to add a method to know if something belongs to the
interval. Here are a new file with two others to to do some simple tests.
Christophe.
PS : sorry for my english because I speak it like a french cow... ;-)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sympy" group.
To post to this group, send email to sympy@googlegroups.com
To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sympy?hl=en
-~----------~----~----~----~------~----~------~--~---
#!/usr/bin/env python
#coding=utf-8
import simpleSets
listEx = []
listEx.append("""I = simpleSets.Interval(-4, -2, 'oo')
J = simpleSets.Interval(3, 5, 'oc')""")
listEx.append("""I = simpleSets.Interval(-4, -2, 'oo')
J = simpleSets.Interval(-12, -7, 'oc')""")
listEx.append("""I = simpleSets.Interval(3, -4, 'oo')
J = simpleSets.Interval(3, 3, 'oc')""")
listEx.append("""I = simpleSets.Interval(3, -4, 'oo')
J = simpleSets.Interval(3, 5, 'oc')""")
listEx.append("""I = simpleSets.Interval(3, -4, 'co')
J = simpleSets.Interval(3, 5, 'co')""")
listEx.append("""I = simpleSets.Interval(3, 5, 'co')
J = simpleSets.Interval(-4, 3, 'cc')""")
listEx.append("""I = simpleSets.Interval(-4, 3, 'oo')
J = simpleSets.Interval(-4, 3, 'oc')""")
listEx.append("""I = simpleSets.Interval(-4, 3, 'cc')
J = simpleSets.Interval(-4, 3, 'co')""")
listEx.append("""I = simpleSets.Interval(-4, 3, 'cc')
J = simpleSets.Interval(-4, 5, 'co')""")
listEx.append("""I = simpleSets.Interval(-4, 3, 'oc')
J = simpleSets.Interval(-4, 5, 'co')""")
listEx.append("""I = simpleSets.Interval(-5, 3, 'cc')
J = simpleSets.Interval(-4, 3, 'co')""")
listEx.append("""I = simpleSets.Interval(-5, 3, 'cc')
J = simpleSets.Interval(-4, 3, 'cc')""")
listEx.append("""I = simpleSets.Interval(-5, 3, 'co')
J = simpleSets.Interval(-4, 3, 'cc')""")
listEx.append("""I = simpleSets.Interval(-5, 3, 'oc')
J = simpleSets.Interval(-4, 3, 'cc')""")
listEx.append("""I = simpleSets.Interval(-5, 3, 'oo')
J = simpleSets.Interval(-4, 3, 'cc')""")
###########################
# WRITING OF THE EXAMPLES #
###########################
#for i in range(len(examples)):
for i in range(len(listEx)):
exec(listEx[i])
for line in listEx[i].splitlines():
print line
I_inter_J = I.intersection(J)
print '\tI : ' + str(I)
print '\tJ : ' + str(J)
print '\tI_inter_J : ' + str(I_inter_J)
print '-'*30
#!/usr/bin/env python
#coding=utf-8
# Code writing by BAL Christophe
# Mail : projet...@club.fr
class Interval():
def __init__(self, a, b, endpoints ='cc'):
if endpoints not in ['cc', 'oc', 'co', 'oo']:
raise Exception, 'Unkown endpoints of interval : "' +
str(endpoints) + '"'
self.endpoints = endpoints
if a>b:
self.min,self.max = b,a
self.endpoints_min, self.endpoints_max = endpoints[1], endpoints[0]
else:
self.min = a
self.max = b
self.endpoints_min = endpoints[0]
self.endpoints_max = endpoints[1]
def __str__(self):
if self.min == self.max:
if self.endpoints == 'cc':
return '{' + str(self.min) + '}'
else:
return 'Empty Set'
if self.endpoints_min=='c':
t = '['
else:
t =']'
t+=str(self.min) + ';' + str(self.max)
if self.endpoints_max=='c':
t += ']'
else:
t +='['
return t
def intersection(self, otherInterval):
# The intersection is empty.
if self.min == self.max or otherInterval.min == otherInterval.max:
return Interval(0, 0, 'oo')
maxOfMins = max(self.min, otherInterval.min)
minOfMaxs = min(self.max, otherInterval.max)
if maxOfMins > minOfMaxs:
return Interval(0, 0, 'oo')
if self.min == self.max or otherInterval.min == otherInterval.max:
return Interval(0, 0, 'oo')
# The intersection could be a set with a single element.
if minOfMaxs == maxOfMins:
if self.max == minOfMaxs:
if self.endpoints_max == 'o' or otherInterval.endpoints_min
=='o':
return Interval(0, 0, 'oo')
else:
return Interval(minOfMaxs, minOfMaxs)
else:
if self.endpoints_min == 'o' or otherInterval.endpoints_max
=='o':
return Interval(0, 0, 'oo')
else:
return Interval(minOfMaxs, minOfMaxs)
# Wath is the endpoints of the intersection ?
answerendpoints_min = 'c'
if self.min == maxOfMins :
answerendpoints_min = self.endpoints_min
if otherInterval.min == maxOfMins and answerendpoints_min == 'c':
answerendpoints_min = otherInterval.endpoints_min
answerendpoints_max = 'c'
if self.max == minOfMaxs :
answerendpoints_max = self.endpoints_max
if otherInterval.max == minOfMaxs and answerendpoints_max == 'c':
answerendpoints_max = otherInterval.endpoints_max
return Interval(maxOfMins, minOfMaxs,
answerendpoints_min+answerendpoints_max)
def contains(self, x):
if str(self) == 'Empty Set':
return False
if x == self.min:
return self.endpoints_min =='c'
if x == self.max:
return self.endpoints_max =='c'
return self.min < x < self.max
class UnionIntervals():
def __init__(self, listIntervals):
self.listIntervals = listIntervals
def __str__(self):
if len(self.listIntervals)==1:
return str(self.listIntervals[0])
t = str(self.listIntervals[0])
for i in range(1, len(self.listIntervals)):
t += ' u ' + str(self.listIntervals[i])
return t
#!/usr/bin/env python
#coding=utf-8
import simpleSets
listEx = []
listEx.append("""I = simpleSets.Interval(-4, -4, 'co')
x = -4""")
listEx.append("""I = simpleSets.Interval(-4, -2, 'oo')
x = -4""")
listEx.append("""I = simpleSets.Interval(-4, -2, 'co')
x = -4""")
listEx.append("""I = simpleSets.Interval(-4, 2, 'oc')
x = 2""")
listEx.append("""I = simpleSets.Interval(-4, 2, 'co')
x = 2""")
listEx.append("""I = simpleSets.Interval(-4, 2, 'co')
x = 0""")
###########################
# WRITING OF THE EXAMPLES #
###########################
#for i in range(len(examples)):
for i in range(len(listEx)):
exec(listEx[i])
for line in listEx[i].splitlines():
print line
x_in_I = I.contains(x)
print '\tI : ' + str(I)
print '\tx : ' + str(x)
print '\tx in I : ' + str(x_in_I)
print '-'*30