Ask Your Question
0

How do I find the subsets of given Set?

asked 2012-04-29 06:03:16 +0200

math4tots gravatar image

updated 2015-01-14 14:14:55 +0200

FrédéricC gravatar image

I have code that looks like this:

def Z(m,n):
  return CartesianProduct(IntegerRange(m),IntegerRange(n))

print Subsets(Z(2,2))

However, when I try to run it, it gives me the following scary error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/math4tots/math/<ipython console> in <module>()

/opt/sage-4.8-linux-64bit-ubuntu_10.04.3_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/combinat/combinat.pyc in __str__(self)
   1052             'Partitions of the integer 5'
   1053         """
-> 1054         return self.__repr__()
   1055     
   1056     def _repr_(self):

/opt/sage-4.8-linux-64bit-ubuntu_10.04.3_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/combinat/subset.pyc in __repr__(self)
    152             'Subsets of {1, 2, 3}'
    153         """
--> 154         return "Subsets of %s"%self.s
    155 
    156     def __contains__(self, value):

/opt/sage-4.8-linux-64bit-ubuntu_10.04.3_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/structure/sage_object.so in sage.structure.sage_object.SageObject.__repr__ (sage/structure/sage_object.c:1492)()

/opt/sage-4.8-linux-64bit-ubuntu_10.04.3_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/sets/set.pyc in _repr_(self)
    673             {0, 1}
    674         """
--> 675         s = repr(self.set())
    676         return "{" + s[5:-2] + "}"
    677 

/opt/sage-4.8-linux-64bit-ubuntu_10.04.3_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/sets/set.pyc in set(self)
    717             <class 'sage.sets.set.Set_object_enumerated_with_category'>
    718         """
--> 719         return set(self.object())
    720 
    721     def frozenset(self):

TypeError: unhashable type: 'list'

What did I do wrong? What is the canonical way to handle this situation?

I was expecting an output something along the lines of "Subsets of Cartesian Product of {0..2} {0..2}"

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2012-04-29 06:27:29 +0200

chaesloc2 gravatar image

You don't have a set to begin with. CartesianProduct returns a special object. That object automatically converts to a list of lists when you pass it to Subsets. Then Subsets goes through that list of lists, and gets confused by the lists inside - it cannot make subsets out of them, because lists are not hashable. One way around is to use tuples instead of lists:

def Z(m,n):
    return CartesianProduct(IntegerRange(m),IntegerRange(n))
print Subsets(map(tuple,Z(2,2)))
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-04-29 06:03:16 +0200

Seen: 586 times

Last updated: Apr 29 '12