What can be contained in a set?

52 views
Skip to first unread message

Samuel Lê

unread,
Aug 4, 2014, 8:27:18 AM8/4/14
to sy...@googlegroups.com
Hi!

I sent a fix to issue #7805 which is about forcing the method _contains of an Interval to return False when the element is a Set (currently it's throwing an exception).
My fix is just a check on the type of other in that method - if it's a set, I return False.

I was thinking, could we add for all the classes that inherit from Set a list of types that can be contained?
For Interval, I would have to add in the __new__ method:

self.containedTypes = [float, int] #and possibly other numerical types.

Then, I could change my _contains method for any class implementing Set in the following way:

def _contains(self, other):

     if type(other) is not Symbol and not any(map(lambda t: isinstance(other, t), self.containedTypes)):
          return False
     #do the actual calculations to see if other is contained, which will depend on the class we are implementing

That way, I automatically ensure that other will be of the types contained by my Set, and I would be able to extend that to more complicated sets (say, a set of sets...).

Let me know what you think!
Sam


Aaron Meurer

unread,
Aug 14, 2014, 7:57:01 PM8/14/14
to sy...@googlegroups.com
I think sets would become less useful if they cannot contain symbolic expressions, e.g., Interval(a, b) instead of Interval(1, 2). So a strict type restriction is not helpful. 

By the way, you should use isinstance, not "type(other) is Symbol" (and also don't use "is" to compare things unless you know there can only ever be exactly one of them).

Aaron Meurer


--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/1f2dabe5-3c42-4948-84e3-b2bee0ce5387%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages