Source code for sympy.combinatorics.subsets

[docs]classSubset(Basic):""" Represents a basic subset object. We generate subsets using essentially two techniques, binary enumeration and lexicographic enumeration. The Subset class takes two arguments, the first one describes the initial subset to consider and the second describes the superset. Examples ======== >>> from sympy.combinatorics.subsets import Subset >>> a = Subset(['c','d'], ['a','b','c','d']) >>> a.next_binary().subset ['b'] >>> a.prev_binary().subset ['c'] """_rank_binary=None_rank_lex=None_rank_graycode=None_subset=None_superset=Nonedef__new__(cls,subset,superset):""" Default constructor. It takes the subset and its superset as its parameters. Examples ======== >>> from sympy.combinatorics.subsets import Subset >>> a = Subset(['c','d'], ['a','b','c','d']) >>> a.subset ['c', 'd'] >>> a.superset ['a', 'b', 'c', 'd'] >>> a.size 2 """iflen(subset)>len(superset):raiseValueError('Invalid arguments have been provided. The superset must be larger than the subset.')foreleminsubset:ifelemnotinsuperset:raiseValueError('The superset provided is invalid as it does not contain the element %i'%elem)obj=Basic.__new__(cls)obj._subset=subsetobj._superset=supersetreturnobj

[docs]defiterate_binary(self,k):""" This is a helper function. It iterates over the binary subsets by k steps. This variable can be both positive or negative. Examples ======== >>> from sympy.combinatorics.subsets import Subset >>> a = Subset(['c','d'], ['a','b','c','d']) >>> a.iterate_binary(-2).subset ['d'] >>> a = Subset(['a','b','c'], ['a','b','c','d']) >>> a.iterate_binary(2).subset [] See Also ======== next_binary, prev_binary """bin_list=Subset.bitlist_from_subset(self.subset,self.superset)n=(int(''.join(bin_list),2)+k)%2**self.superset_sizebits=bin(n)[2:].rjust(self.superset_size,'0')returnSubset.subset_from_bitlist(self.superset,bits)