On Jul 7, 2009, at 6:21 AM, Priit Laes wrote:
>
> Ühel kenal päeval, T, 2009-07-07 kell 13:28, kirjutas Christophe:
>> Aaron S. Meurer a écrit :
>>>
>>> Interval(3, 4, endpoints='oc'), which would create (3, 4] ('oc'
>>> stands
>>> for open-closed).
>
> How about using '[' for closed and ']' instead of 'o' and 'c'? I find
> '[]' or '][' a bit more intuitive, as it took a bit time to figure out
> that 'oo' means open-open interval, not infinity..
> So: cc = '[]', oc = ']]', oo = '][' and co = '[['.
You're probably right. According to
http://en.wikipedia.org/wiki/Interval_(mathematics)#Notations_for_intervals
, there is a different notation based on whether you are in America or
in Europe. I think we can allow multiple different notations for the
arguments of the function. For example, also allow the use of a
bracket to denote open and a parenthesis to denote closed as is the
standard notation, so open-closed would be '(]', and so on.
As to printing, I think we need to decide. I have never seen
the ]3,4] notation, but I guess that is used. What do you think? I
guess we can include to option to have both, but we still need a
default option.
>
>
>> 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.
>
> Some of the comments below are influenced by "PEP 8 -- Style Guide for
> Python Code" - http://www.python.org/dev/peps/pep-0008/ :)
>
>> #!/usr/bin/env python
>> #coding=utf-8
>>
>>
>> # Code writing by BAL Christophe
>> # Mail : [email protected]
>>
>> 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) + '"'
> please use raise Exception (...)
> ...and note the grammar and typo.
Actually, this should be TypeError, not Exception. Also, the typo he
is referring to is the misspelling of "Unknown".
>>
>> self.endpoints = endpoints
>>
>> if a>b:
> Please use proper spacing here: a > b...
>> self.min,self.max = b,a
> ...and here...
>> 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]
> self.endpoints handling looks broken - they are not reversed here ^^
>>
>> def __str__(self):
>> if self.min == self.max:
>> if self.endpoints == 'cc':
>> return '{' + str(self.min) + '}'
> Should we use [ or { ??
I am unfamiliar with the European notation, as I mentioned above, but
usually '{' is reserved for finite sets, like {1, 2, 3}, or set
builder notation, like {x | 1 < x < 2}.
>> else:
>> return 'Empty Set'
>>
>> if self.endpoints_min=='c':
> ..spacing..
>> t = '['
>> else:
>> t =']'
>>
>> t+=str(self.min) + ';' + str(self.max)
> ...
>> if self.endpoints_max=='c':
> spacing
>> t += ']'
>> else:
>> t +='['
>>
>> return t
>>
>> def intersection(self, otherInterval):
> Please add docstring...
>> # 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 ?
Please change to: # What are the endpoints of the intersection?
>> answerendpoints_min = 'c'
>>
>> if self.min == maxOfMins :
> spacing...
>> 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 :
> spacing..
>> 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)
> spacing...
>
> Also, I would prefer to have lower-case variables:
> minOfMaxs vs min_of_max
Yes, this is the standard naming in Python.
Aaron Meurer
>
> Cheers,
> Priit :)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sympy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/sympy?hl=en
-~----------~----~----~----~------~----~------~--~---