def iterate_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_size bits = bin(n)[2:].rjust(self.superset_size, '0') return Subset.subset_from_bitlist(self.superset, bits)
def iterate_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) next_bin_list = list(bin((int(reduce(lambda x, y: x + y, bin_list), 2) + k) % 2**self.superset_size))[2:] next_bin_list = [0] * (self.superset_size - len(next_bin_list)) + \ next_bin_list return Subset.subset_from_bitlist(self.superset, next_bin_list)
def iterate_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) next_bin_list = list( bin((int(reduce(lambda x, y: x + y, bin_list), 2) + k) % 2**self.superset_size))[2:] next_bin_list = [0] * (self.superset_size - len(next_bin_list)) + \ next_bin_list return Subset.subset_from_bitlist(self.superset, next_bin_list)
def current(self): """ Returns the currently referenced Gray code as a bit string. >>> from sympy.combinatorics.graycode import GrayCode >>> GrayCode(3, start='100').current '100' """ rv = self._current or '0' if type(rv) is not str: rv = bin(rv)[2:] return rv.rjust(self.n, '0')
def unrank_binary(self, rank, superset): """ Gets the binary ordered subset of the specified rank. Examples: >>> from sympy.combinatorics.subsets import Subset >>> Subset.unrank_binary(4, ['a','b','c','d']).subset ['b'] """ bin_list = list(bin(rank))[2:] bin_list = [0] * (len(superset) - len(bin_list)) + bin_list return Subset.subset_from_bitlist(superset, bin_list)
def unrank_binary(self, rank, superset): """ Gets the binary ordered subset of the specified rank. Examples ======== >>> from sympy.combinatorics.subsets import Subset >>> Subset.unrank_binary(4, ['a','b','c','d']).subset ['b'] """ bin_list = list(bin(rank))[2:] bin_list = [0] * (len(superset) - len(bin_list)) + bin_list return Subset.subset_from_bitlist(superset, bin_list)
def POSform(variables, minterms, dontcares=[]): """ The POSform function uses simplified_pairs and a redundant-group eliminating algorithm to convert the list of all input combinations that generate '1' (the minterms) into the smallest Product of Sums form. The variables must be given as the first argument. Return a logical And function (i.e., the "product of sums" or "POS" form) that gives the desired outcome. If there are inputs that can be ignored, pass them as a list too. The result will be one of the (perhaps many) functions that satisfy the conditions. Examples ======== >>> from sympy.logic import POSform >>> minterms = [[0, 0, 0, 1], [0, 0, 1, 1], [0, 1, 1, 1], ... [1, 0, 1, 1], [1, 1, 1, 1]] >>> dontcares = [[0, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 1]] >>> POSform(['w','x','y','z'], minterms, dontcares) And(Or(Not(w), y), z) References ========== .. [1] en.wikipedia.org/wiki/Quine-McCluskey_algorithm """ variables = [str(v) for v in variables] from sympy.core.compatibility import bin if minterms == []: return False t = [0] * len(variables) maxterms = [] for x in range(2 ** len(variables)): b = [int(y) for y in bin(x)[2:]] t[-len(b):] = b if (t not in minterms) and (t not in dontcares): maxterms.append(t[:]) l2 = [1] l1 = maxterms + dontcares while (l1 != l2): l1 = _simplified_pairs(l1) l2 = _simplified_pairs(l1) string = _rem_redundancy(l1, maxterms, variables, 2) if string == '': return True return sympify(string)
def current(self): """ Returns the currently referenced Gray code as a bit string. Examples ======== >>> from sympy.combinatorics.graycode import GrayCode >>> GrayCode(3, start='100').current '100' """ rv = self._current or '0' if type(rv) is not str: rv = bin(rv)[2:] return rv.rjust(self.n, '0')
def arr(num, t): """Return a list of ``t`` binary digits of ``num``; low digits rightmost. Examples ======== >>> from sympy.physics.quantum.shor import arr >>> arr(5, 6) [0, 0, 0, 1, 0, 1] >>> arr(5, 2) [0, 1] """ binary_array = [0]*t b = list(bin(abs(num))[2:]) # strip 0b b = b[-t:] for i in range(-1, -min(len(b), t) - 1, -1): binary_array[i] = int(b[i]) return binary_array
def arr(num, t): """Return a list of ``t`` binary digits of ``num``; low digits rightmost. Examples ======== >>> from sympy.physics.quantum.shor import arr >>> arr(5, 6) [0, 0, 0, 1, 0, 1] >>> arr(5, 2) [0, 1] """ binary_array = [0] * t b = list(bin(abs(num))[2:]) # strip 0b b = b[-t:] for i in range(-1, -min(len(b), t) - 1, -1): binary_array[i] = int(b[i]) return binary_array
def unrank_binary(self, rank, superset): """ Gets the binary ordered subset of the specified rank. Examples ======== >>> from sympy.combinatorics.subsets import Subset >>> Subset.unrank_binary(4, ['a','b','c','d']).subset ['b'] See Also ======== iterate_binary, rank_binary """ bits = bin(rank)[2:].rjust(len(superset), '0') return Subset.subset_from_bitlist(superset, bits)
def simplify_logic(expr): """ This function simplifies a boolean function to its simplified version in SOP or POS form. The return type is a Or object or And object in SymPy. The input can be a string or a boolean expression. Examples ======== >>> from sympy.logic import simplify_logic >>> from sympy.abc import x, y, z >>> from sympy import S >>> b = '(~x & ~y & ~z) | ( ~x & ~y & z)' >>> simplify_logic(b) And(Not(x), Not(y)) >>> S(b) Or(And(Not(x), Not(y), Not(z)), And(Not(x), Not(y), z)) >>> simplify_logic(_) And(Not(x), Not(y)) """ from sympy.core.compatibility import bin expr = sympify(expr) variables = list(expr.free_symbols) string_variables = [x.name for x in variables] truthtable = [] t = [0] * len(variables) for x in range(2 ** len(variables)): b = [int(y) for y in bin(x)[2:]] t[-len(b):] = b if expr.subs(zip(variables, [bool(i) for i in t])) is True: truthtable.append(t[:]) if (len(truthtable) >= (2 ** (len(variables) - 1))): return SOPform(string_variables, truthtable) else: return POSform(string_variables, truthtable)