def __iter__(self): if self.is_iterable: from sympy.core.compatibility import product return product(*self.sets) else: raise TypeError("Not all constituent sets are iterable")
def doit(self, **hints): """Expand the density operator into an outer product format. Examples ========= >>> from sympy.physics.quantum.state import Ket >>> from sympy.physics.quantum.density import Density >>> from sympy.physics.quantum.operator import Operator >>> A = Operator('A') >>> d = Density([Ket(0), 0.5], [Ket(1),0.5]) >>> d.doit() 0.5*|0><0| + 0.5*|1><1| """ terms = [] for (state, prob) in self.args: state = state.expand() # needed to break up (a+b)*c if (isinstance(state, Add)): for arg in product(state.args, repeat=2): terms.append(prob * self._generate_outer_prod(arg[0], arg[1])) else: terms.append(prob * self._generate_outer_prod(state, state)) return Add(*terms)
def all_rl(expr): if leaf(expr): yield expr else: myop = op(expr) argss = product(*map(brule, children(expr))) for args in argss: yield new(myop, *args)
def all_rl(expr): if is_leaf(expr): yield expr else: op = type(expr) argss = product(*map(brule, expr.args)) for args in argss: yield new(op, *args)
def top_down_rl(expr): brl = notempty(brule) for newexpr in brl(expr): if is_leaf(newexpr): yield newexpr else: for args in product(*map(top_down_rl, newexpr.args)): yield new(type(newexpr), *args)
def _density(self): proditer = product(*[space._density.iteritems() for space in self.spaces]) d = {} for items in proditer: elems, probs = zip(*items) elem = sumsets(elems) prob = Mul(*probs) d[elem] = d.get(elem, 0) + prob return Dict(d)
def POSform(variables, minterms, dontcares=None): """ 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 """ from sympy.core.symbol import Symbol variables = [ Symbol(v) if not isinstance(v, Symbol) else v for v in variables ] if minterms == []: return False minterms = [list(i) for i in minterms] dontcares = [list(i) for i in (dontcares or [])] for d in dontcares: if d in minterms: raise ValueError('%s in minterms is also in dontcares' % d) maxterms = [] for t in product([0, 1], repeat=len(variables)): t = list(t) if (t not in minterms) and (t not in dontcares): maxterms.append(t) old = None new = maxterms + dontcares while new != old: old = new new = _simplified_pairs(old) essential = _rem_redundancy(new, maxterms) return And(*[_convert_to_varsPOS(x, variables) for x in essential])
def POSform(variables, minterms, dontcares=None): """ 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 """ from sympy.core.symbol import Symbol variables = [Symbol(v) if not isinstance(v, Symbol) else v for v in variables] if minterms == []: return False minterms = [list(i) for i in minterms] dontcares = [list(i) for i in (dontcares or [])] for d in dontcares: if d in minterms: raise ValueError('%s in minterms is also in dontcares' % d) maxterms = [] for t in product([0, 1], repeat=len(variables)): t = list(t) if (t not in minterms) and (t not in dontcares): maxterms.append(t) old = None new = maxterms + dontcares while new != old: old = new new = _simplified_pairs(old) essential = _rem_redundancy(new, maxterms) return And(*[_convert_to_varsPOS(x, variables) for x in essential])
def _density(self): proditer = product( *[space._density.iteritems() for space in self.spaces]) d = {} for items in proditer: elems, probs = zip(*items) elem = sumsets(elems) prob = Mul(*probs) d[elem] = d.get(elem, 0) + prob return Dict(d)
def simplify_logic(expr): """ This function simplifies a boolean function to its simplified version in SOP or POS form. The return type is an Or 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)) """ expr = sympify(expr) if not isinstance(expr, BooleanFunction): return expr variables = list(expr.free_symbols) truthtable = [] for t in product([0, 1], repeat=len(variables)): t = list(t) if expr.subs(zip(variables, t)) == True: truthtable.append(t) if (len(truthtable) >= (2 ** (len(variables) - 1))): return SOPform(variables, truthtable) else: return POSform(variables, truthtable)
def simplify_logic(expr): """ This function simplifies a boolean function to its simplified version in SOP or POS form. The return type is an Or 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)) """ expr = sympify(expr) if not isinstance(expr, BooleanFunction): return expr variables = list(expr.free_symbols) truthtable = [] for t in product([0, 1], repeat=len(variables)): t = list(t) if expr.subs(zip(variables, t)) == True: truthtable.append(t) if (len(truthtable) >= (2**(len(variables) - 1))): return SOPform(variables, truthtable) else: return POSform(variables, truthtable)
def __iter__(self): proditer = product(*self.domains) return (sumsets(items) for items in proditer)