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. --~--~---------~--~----~------------~-------~--~----~ 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 # 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) ######################### # SOME EXAMPLES TO TEST # ######################### listEx = [] listEx.append("""I = Interval(-4, -2, 'oo') J = Interval(3, 5, 'oc')""") listEx.append("""I = Interval(-4, -2, 'oo') J = Interval(-12, -7, 'oc')""") listEx.append("""I = Interval(3, -4, 'oo') J = Interval(3, 3, 'oc')""") listEx.append("""I = Interval(3, -4, 'oo') J = Interval(3, 5, 'oc')""") listEx.append("""I = Interval(3, -4, 'co') J = Interval(3, 5, 'co')""") listEx.append("""I = Interval(3, 5, 'co') J = Interval(-4, 3, 'cc')""") listEx.append("""I = Interval(-4, 3, 'oo') J = Interval(-4, 3, 'oc')""") listEx.append("""I = Interval(-4, 3, 'cc') J = Interval(-4, 3, 'co')""") listEx.append("""I = Interval(-4, 3, 'cc') J = Interval(-4, 5, 'co')""") listEx.append("""I = Interval(-4, 3, 'oc') J = Interval(-4, 5, 'co')""") listEx.append("""I = Interval(-5, 3, 'cc') J = Interval(-4, 3, 'co')""") listEx.append("""I = Interval(-5, 3, 'cc') J = Interval(-4, 3, 'cc')""") listEx.append("""I = Interval(-5, 3, 'co') J = Interval(-4, 3, 'cc')""") listEx.append("""I = Interval(-5, 3, 'oc') J = Interval(-4, 3, 'cc')""") listEx.append("""I = Interval(-5, 3, 'oo') J = 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