def test_equivalent_internal_lines_VT2conjT2_ambiguous_order(): # These diagrams invokes _determine_ambiguous() because the # dummies can not be ordered unambiguously by the key alone i, j, k, l, m, n = symbols('i j k l m n', below_fermi=True, cls=Dummy) a, b, c, d, e, f = symbols('a b c d e f', above_fermi=True, cls=Dummy) p1, p2, p3, p4 = symbols('p1 p2 p3 p4', above_fermi=True, cls=Dummy) h1, h2, h3, h4 = symbols('h1 h2 h3 h4', below_fermi=True, cls=Dummy) from sympy.utilities.iterables import variations v = Function('v') t = Function('t') dums = _get_ordered_dummies # v(abcd)t(abij)t(cdij) template = v(p1, p2, p3, p4)*t(p1, p2, i, j)*t(p3, p4, i, j) permutator = variations([a, b, c, d], 4) base = template.subs(zip([p1, p2, p3, p4], next(permutator))) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert dums(base) != dums(expr) assert substitute_dummies(expr) == substitute_dummies(base) template = v(p1, p2, p3, p4)*t(p1, p2, j, i)*t(p3, p4, i, j) permutator = variations([a, b, c, d], 4) base = template.subs(zip([p1, p2, p3, p4], next(permutator))) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert dums(base) != dums(expr) assert substitute_dummies(expr) == substitute_dummies(base)
def test_equivalent_internal_lines_VT2conjT2_ambiguous_order(): # These diagrams invokes _determine_ambiguous() because the # dummies can not be ordered unambiguously by the key alone i, j, k, l, m, n = symbols("i j k l m n", below_fermi=True, cls=Dummy) a, b, c, d, e, f = symbols("a b c d e f", above_fermi=True, cls=Dummy) p1, p2, p3, p4 = symbols("p1 p2 p3 p4", above_fermi=True, cls=Dummy) h1, h2, h3, h4 = symbols("h1 h2 h3 h4", below_fermi=True, cls=Dummy) from sympy.utilities.iterables import variations v = Function("v") t = Function("t") dums = _get_ordered_dummies # v(abcd)t(abij)t(cdij) template = v(p1, p2, p3, p4) * t(p1, p2, i, j) * t(p3, p4, i, j) permutator = variations([a, b, c, d], 4) base = template.subs(zip([p1, p2, p3, p4], next(permutator))) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert dums(base) != dums(expr) assert substitute_dummies(expr) == substitute_dummies(base) template = v(p1, p2, p3, p4) * t(p1, p2, j, i) * t(p3, p4, i, j) permutator = variations([a, b, c, d], 4) base = template.subs(zip([p1, p2, p3, p4], next(permutator))) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert dums(base) != dums(expr) assert substitute_dummies(expr) == substitute_dummies(base)
def test_equivalent_internal_lines_VT2conjT2_ambiguous_order_AT(): # These diagrams invokes _determine_ambiguous() because the # dummies can not be ordered unambiguously by the key alone i, j, k, l, m, n = symbols('i j k l m n', below_fermi=True, cls=Dummy) a, b, c, d, e, f = symbols('a b c d e f', above_fermi=True, cls=Dummy) p1, p2, p3, p4 = symbols('p1 p2 p3 p4', above_fermi=True, cls=Dummy) h1, h2, h3, h4 = symbols('h1 h2 h3 h4', below_fermi=True, cls=Dummy) from sympy.utilities.iterables import variations # atv(abcd)att(abij)att(cdij) template = atv(p1, p2, p3, p4) * att(p1, p2, i, j) * att(p3, p4, i, j) permutator = variations([a, b, c, d], 4) base = template.subs(zip([p1, p2, p3, p4], permutator.next())) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert substitute_dummies(expr) == substitute_dummies(base) template = atv(p1, p2, p3, p4) * att(p1, p2, j, i) * att(p3, p4, i, j) permutator = variations([a, b, c, d], 4) base = template.subs(zip([p1, p2, p3, p4], permutator.next())) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert substitute_dummies(expr) == substitute_dummies(base)
def test_equivalent_internal_lines_VT2conjT2_ambiguous_order_AT(): # These diagrams invokes _determine_ambiguous() because the # dummies can not be ordered unambiguously by the key alone i, j, k, l, m, n = symbols("i j k l m n", below_fermi=True, cls=Dummy) a, b, c, d, e, f = symbols("a b c d e f", above_fermi=True, cls=Dummy) p1, p2, p3, p4 = symbols("p1 p2 p3 p4", above_fermi=True, cls=Dummy) h1, h2, h3, h4 = symbols("h1 h2 h3 h4", below_fermi=True, cls=Dummy) from sympy.utilities.iterables import variations # atv(abcd)att(abij)att(cdij) template = atv(p1, p2, p3, p4) * att(p1, p2, i, j) * att(p3, p4, i, j) permutator = variations([a, b, c, d], 4) base = template.subs(zip([p1, p2, p3, p4], permutator.next())) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert substitute_dummies(expr) == substitute_dummies(base) template = atv(p1, p2, p3, p4) * att(p1, p2, j, i) * att(p3, p4, i, j) permutator = variations([a, b, c, d], 4) base = template.subs(zip([p1, p2, p3, p4], permutator.next())) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert substitute_dummies(expr) == substitute_dummies(base)
def test_equivalent_internal_lines_VT2conjT2(): # this diagram requires special handling in TCE i, j, k, l, m, n = symbols('i j k l m n', below_fermi=True, cls=Dummy) a, b, c, d, e, f = symbols('a b c d e f', above_fermi=True, cls=Dummy) p1, p2, p3, p4 = symbols('p1 p2 p3 p4', above_fermi=True, cls=Dummy) h1, h2, h3, h4 = symbols('h1 h2 h3 h4', below_fermi=True, cls=Dummy) from sympy.utilities.iterables import variations v = Function('v') t = Function('t') dums = _get_ordered_dummies # v(abcd)t(abij)t(ijcd) template = v(p1, p2, p3, p4)*t(p1, p2, i, j)*t(i, j, p3, p4) permutator = variations([a, b, c, d], 4) base = template.subs(zip([p1, p2, p3, p4], next(permutator))) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert dums(base) != dums(expr) assert substitute_dummies(expr) == substitute_dummies(base) template = v(p1, p2, p3, p4)*t(p1, p2, j, i)*t(j, i, p3, p4) permutator = variations([a, b, c, d], 4) base = template.subs(zip([p1, p2, p3, p4], next(permutator))) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert dums(base) != dums(expr) assert substitute_dummies(expr) == substitute_dummies(base) # v(abcd)t(abij)t(jicd) template = v(p1, p2, p3, p4)*t(p1, p2, i, j)*t(j, i, p3, p4) permutator = variations([a, b, c, d], 4) base = template.subs(zip([p1, p2, p3, p4], next(permutator))) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert dums(base) != dums(expr) assert substitute_dummies(expr) == substitute_dummies(base) template = v(p1, p2, p3, p4)*t(p1, p2, j, i)*t(i, j, p3, p4) permutator = variations([a, b, c, d], 4) base = template.subs(zip([p1, p2, p3, p4], next(permutator))) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert dums(base) != dums(expr) assert substitute_dummies(expr) == substitute_dummies(base)
def test_equivalent_internal_lines_VT2conjT2(): # this diagram requires special handling in TCE i, j, k, l, m, n = symbols("i j k l m n", below_fermi=True, cls=Dummy) a, b, c, d, e, f = symbols("a b c d e f", above_fermi=True, cls=Dummy) p1, p2, p3, p4 = symbols("p1 p2 p3 p4", above_fermi=True, cls=Dummy) h1, h2, h3, h4 = symbols("h1 h2 h3 h4", below_fermi=True, cls=Dummy) from sympy.utilities.iterables import variations v = Function("v") t = Function("t") dums = _get_ordered_dummies # v(abcd)t(abij)t(ijcd) template = v(p1, p2, p3, p4) * t(p1, p2, i, j) * t(i, j, p3, p4) permutator = variations([a, b, c, d], 4) base = template.subs(zip([p1, p2, p3, p4], next(permutator))) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert dums(base) != dums(expr) assert substitute_dummies(expr) == substitute_dummies(base) template = v(p1, p2, p3, p4) * t(p1, p2, j, i) * t(j, i, p3, p4) permutator = variations([a, b, c, d], 4) base = template.subs(zip([p1, p2, p3, p4], next(permutator))) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert dums(base) != dums(expr) assert substitute_dummies(expr) == substitute_dummies(base) # v(abcd)t(abij)t(jicd) template = v(p1, p2, p3, p4) * t(p1, p2, i, j) * t(j, i, p3, p4) permutator = variations([a, b, c, d], 4) base = template.subs(zip([p1, p2, p3, p4], next(permutator))) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert dums(base) != dums(expr) assert substitute_dummies(expr) == substitute_dummies(base) template = v(p1, p2, p3, p4) * t(p1, p2, j, i) * t(i, j, p3, p4) permutator = variations([a, b, c, d], 4) base = template.subs(zip([p1, p2, p3, p4], next(permutator))) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert dums(base) != dums(expr) assert substitute_dummies(expr) == substitute_dummies(base)
def test_substitute_dummies_substitution_order(): i, j, k, l = symbols("i j k l", below_fermi=True, cls=Dummy) f = Function("f") from sympy.utilities.iterables import variations for permut in variations([i, j, k, l], 4): assert substitute_dummies(f(*permut) - f(i, j, k, l)) == 0
def find_all_perms_two(target): total = deque() for x in range(len(target)): for item in variations(target, x): total.append(item) return array(total)
def period_find(a, N): """Finds the period of a in modulo N arithmetic This is quantum part of Shor's algorithm.It takes two registers, puts first in superposition of states with Hadamards so: ``|k>|0>`` with k being all possible choices. It then does a controlled mod and a QFT to determine the order of a. """ epsilon = .5 #picks out t's such that maintains accuracy within epsilon t = int(2*math.ceil(log(N, 2))) # make the first half of register be 0's |000...000> start = [0 for x in range(t)] #Put second half into superposition of states so we have |1>x|0> + |2>x|0> + ... |k>x>|0> + ... + |2**n-1>x|0> factor = 1/sqrt(2**t) qubits = 0 for arr in variations(range(2), t, repetition=True): qbitArray = arr + start qubits = qubits + Qubit(*qbitArray) circuit = (factor*qubits).expand() #Controlled second half of register so that we have: # |1>x|a**1 %N> + |2>x|a**2 %N> + ... + |k>x|a**k %N >+ ... + |2**n-1=k>x|a**k % n> circuit = CMod(t, a, N)*circuit #will measure first half of register giving one of the a**k%N's circuit = qapply(circuit) print("controlled Mod'd") for i in range(t): circuit = measure_partial_oneshot(circuit, i) # circuit = measure(i)*circuit # circuit = qapply(circuit) print("measured 1") #Now apply Inverse Quantum Fourier Transform on the second half of the register circuit = qapply(QFT(t, t*2).decompose()*circuit, floatingPoint=True) print("QFT'd") for i in range(t): circuit = measure_partial_oneshot(circuit, i + t) # circuit = measure(i+t)*circuit # circuit = qapply(circuit) print(circuit) if isinstance(circuit, Qubit): register = circuit elif isinstance(circuit, Mul): register = circuit.args[-1] else: register = circuit.args[-1].args[-1] print(register) n = 1 answer = 0 for i in range(len(register)/2): answer += n*register[i + t] n = n << 1 if answer == 0: raise OrderFindingException( "Order finder returned 0. Happens with chance %f" % epsilon) #turn answer into r using continued fractions g = getr(answer, 2**t, N) print(g) return g
def period_find(a, N): """Finds the period of a in modulo N arithmetic This is quantum part of Shor's algorithm.It takes two registers, puts first in superposition of states with Hadamards so: ``|k>|0>`` with k being all possible choices. It then does a controlled mod and a QFT to determine the order of a. """ epsilon = .5 #picks out t's such that maintains accuracy within epsilon t = int(2 * math.ceil(log(N, 2))) # make the first half of register be 0's |000...000> start = [0 for x in range(t)] #Put second half into superposition of states so we have |1>x|0> + |2>x|0> + ... |k>x>|0> + ... + |2**n-1>x|0> factor = 1 / sqrt(2**t) qubits = 0 for arr in variations(range(2), t, repetition=True): qbitArray = arr + start qubits = qubits + Qubit(*qbitArray) circuit = (factor * qubits).expand() #Controlled second half of register so that we have: # |1>x|a**1 %N> + |2>x|a**2 %N> + ... + |k>x|a**k %N >+ ... + |2**n-1=k>x|a**k % n> circuit = CMod(t, a, N) * circuit #will measure first half of register giving one of the a**k%N's circuit = qapply(circuit) print "controlled Mod'd" for i in range(t): circuit = measure_partial_oneshot(circuit, i) # circuit = measure(i)*circuit # circuit = qapply(circuit) print "measured 1" #Now apply Inverse Quantum Fourier Transform on the second half of the register circuit = qapply(QFT(t, t * 2).decompose() * circuit, floatingPoint=True) print "QFT'd" for i in range(t): circuit = measure_partial_oneshot(circuit, i + t) # circuit = measure(i+t)*circuit # circuit = qapply(circuit) print circuit if isinstance(circuit, Qubit): register = circuit elif isinstance(circuit, Mul): register = circuit.args[-1] else: register = circuit.args[-1].args[-1] print register n = 1 answer = 0 for i in range(len(register) / 2): answer += n * register[i + t] n = n << 1 if answer == 0: raise OrderFindingException( "Order finder returned 0. Happens with chance %f" % epsilon) #turn answer into r using continued fractions g = getr(answer, 2**t, N) print g return g
def test_constant_renumber_order_issue_5308(): from sympy.utilities.iterables import variations assert constant_renumber(C1*x + C2*y) == \ constant_renumber(C1*y + C2*x) == \ C1*x + C2*y e = C1 * (C2 + x) * (C3 + y) for a, b, c in variations([C1, C2, C3], 3): assert constant_renumber(a * (b + x) * (c + y)) == e
def test_dummy_order_ambiguous(): aa, bb = symbols('ab', above_fermi=True) i, j, k, l, m = symbols('i j k l m', below_fermi=True, cls=Dummy) a, b, c, d, e = symbols('a b c d e', above_fermi=True, cls=Dummy) p, q = symbols('p q', cls=Dummy) p1, p2, p3, p4 = symbols('p1 p2 p3 p4', above_fermi=True, cls=Dummy) p5, p6, p7, p8 = symbols('p5 p6 p7 p8', above_fermi=True, cls=Dummy) h1, h2, h3, h4 = symbols('h1 h2 h3 h4', below_fermi=True, cls=Dummy) h5, h6, h7, h8 = symbols('h5 h6 h7 h8', below_fermi=True, cls=Dummy) A = Function('A') B = Function('B') dums = _get_ordered_dummies from sympy.utilities.iterables import variations # A*A*A*A*B -- ordering of p5 and p4 is used to figure out the rest template = A(p1, p2) * A(p4, p1) * A(p2, p3) * A(p3, p5) * B(p5, p4) permutator = variations([a, b, c, d, e], 5) base = template.subs(zip([p1, p2, p3, p4, p5], permutator.next())) for permut in permutator: subslist = zip([p1, p2, p3, p4, p5], permut) expr = template.subs(subslist) assert substitute_dummies(expr) == substitute_dummies(base) # A*A*A*A*A -- an arbitrary index is assigned and the rest are figured out template = A(p1, p2) * A(p4, p1) * A(p2, p3) * A(p3, p5) * A(p5, p4) permutator = variations([a, b, c, d, e], 5) base = template.subs(zip([p1, p2, p3, p4, p5], permutator.next())) for permut in permutator: subslist = zip([p1, p2, p3, p4, p5], permut) expr = template.subs(subslist) assert substitute_dummies(expr) == substitute_dummies(base) # A*A*A -- ordering of p5 and p4 is used to figure out the rest template = A(p1, p2, p4, p1) * A(p2, p3, p3, p5) * A(p5, p4) permutator = variations([a, b, c, d, e], 5) base = template.subs(zip([p1, p2, p3, p4, p5], permutator.next())) for permut in permutator: subslist = zip([p1, p2, p3, p4, p5], permut) expr = template.subs(subslist) assert substitute_dummies(expr) == substitute_dummies(base)
def test_dummy_order_ambiguous(): aa, bb = symbols('ab', above_fermi=True) i, j, k, l, m = symbols('i j k l m', below_fermi=True, cls=Dummy) a, b, c, d, e = symbols('a b c d e', above_fermi=True, cls=Dummy) p, q = symbols('p q', cls=Dummy) p1,p2,p3,p4 = symbols('p1 p2 p3 p4',above_fermi=True, cls=Dummy) p5,p6,p7,p8 = symbols('p5 p6 p7 p8',above_fermi=True, cls=Dummy) h1,h2,h3,h4 = symbols('h1 h2 h3 h4',below_fermi=True, cls=Dummy) h5,h6,h7,h8 = symbols('h5 h6 h7 h8',below_fermi=True, cls=Dummy) A = Function('A') B = Function('B') dums = _get_ordered_dummies from sympy.utilities.iterables import variations # A*A*A*A*B -- ordering of p5 and p4 is used to figure out the rest template = A(p1, p2)*A(p4, p1)*A(p2, p3)*A(p3, p5)*B(p5, p4) permutator = variations([a,b,c,d,e], 5) base = template.subs(zip([p1, p2, p3, p4, p5], permutator.next())) for permut in permutator: subslist = zip([p1, p2, p3, p4, p5], permut) expr = template.subs(subslist) assert substitute_dummies(expr) == substitute_dummies(base) # A*A*A*A*A -- an arbitrary index is assigned and the rest are figured out template = A(p1, p2)*A(p4, p1)*A(p2, p3)*A(p3, p5)*A(p5, p4) permutator = variations([a,b,c,d,e], 5) base = template.subs(zip([p1, p2, p3, p4, p5], permutator.next())) for permut in permutator: subslist = zip([p1, p2, p3, p4, p5], permut) expr = template.subs(subslist) assert substitute_dummies(expr) == substitute_dummies(base) # A*A*A -- ordering of p5 and p4 is used to figure out the rest template = A(p1, p2, p4, p1)*A(p2, p3, p3, p5)*A(p5, p4) permutator = variations([a,b,c,d,e], 5) base = template.subs(zip([p1, p2, p3, p4, p5], permutator.next())) for permut in permutator: subslist = zip([p1, p2, p3, p4, p5], permut) expr = template.subs(subslist) assert substitute_dummies(expr) == substitute_dummies(base)
def test_equivalent_internal_lines_VT2conjT2_AT(): # this diagram requires special handling in TCE i,j,k,l,m,n = symbols('i j k l m n',below_fermi=True, cls=Dummy) a,b,c,d,e,f = symbols('a b c d e f',above_fermi=True, cls=Dummy) p1,p2,p3,p4 = symbols('p1 p2 p3 p4',above_fermi=True, cls=Dummy) h1,h2,h3,h4 = symbols('h1 h2 h3 h4',below_fermi=True, cls=Dummy) from sympy.utilities.iterables import variations # atv(abcd)att(abij)att(ijcd) template = atv(p1, p2, p3, p4)*att(p1, p2, i, j)*att(i, j, p3, p4) permutator = variations([a,b,c,d], 4) base = template.subs(zip([p1, p2, p3, p4], permutator.next())) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert substitute_dummies(expr) == substitute_dummies(base) template = atv(p1, p2, p3, p4)*att(p1, p2, j, i)*att(j, i, p3, p4) permutator = variations([a,b,c,d], 4) base = template.subs(zip([p1, p2, p3, p4], permutator.next())) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert substitute_dummies(expr) == substitute_dummies(base) # atv(abcd)att(abij)att(jicd) template = atv(p1, p2, p3, p4)*att(p1, p2, i, j)*att(j, i, p3, p4) permutator = variations([a,b,c,d], 4) base = template.subs(zip([p1, p2, p3, p4], permutator.next())) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert substitute_dummies(expr) == substitute_dummies(base) template = atv(p1, p2, p3, p4)*att(p1, p2, j, i)*att(i, j, p3, p4) permutator = variations([a,b,c,d], 4) base = template.subs(zip([p1, p2, p3, p4], permutator.next())) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert substitute_dummies(expr) == substitute_dummies(base)
def ZIsing(L, k, h): # Calcula la función de partición a temperatura inversa k y campo magnético h Z = 0 M = 0 # Iteramos a través de todas las posibles configuraciones # En cada iteración sumamos al valor anterior de Z, M for s in variations([1, -1], L**2, True): # Recolocamos s como una matriz LxL s = np.reshape(s, (L, L)) Z += peso(s, k, h) M += np.abs(np.sum(s)) * peso(s, k, h) return Z, M / Z / L**2
def symmetric(n): """ Generates the symmetric group of order n, Sn. Examples: >>> from sympy.combinatorics.generators import symmetric >>> list(symmetric(3)) [Permutation([0, 1, 2]), Permutation([0, 2, 1]), Permutation([1, 0, 2]), \ Permutation([1, 2, 0]), Permutation([2, 0, 1]), Permutation([2, 1, 0])] """ for perm in variations(range(n), n): yield Permutation(perm)
def test_equivalent_internal_lines_VT2conjT2_AT(): # this diagram requires special handling in TCE i, j, k, l, m, n = symbols("i j k l m n", below_fermi=True, cls=Dummy) a, b, c, d, e, f = symbols("a b c d e f", above_fermi=True, cls=Dummy) p1, p2, p3, p4 = symbols("p1 p2 p3 p4", above_fermi=True, cls=Dummy) h1, h2, h3, h4 = symbols("h1 h2 h3 h4", below_fermi=True, cls=Dummy) from sympy.utilities.iterables import variations # atv(abcd)att(abij)att(ijcd) template = atv(p1, p2, p3, p4) * att(p1, p2, i, j) * att(i, j, p3, p4) permutator = variations([a, b, c, d], 4) base = template.subs(zip([p1, p2, p3, p4], permutator.next())) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert substitute_dummies(expr) == substitute_dummies(base) template = atv(p1, p2, p3, p4) * att(p1, p2, j, i) * att(j, i, p3, p4) permutator = variations([a, b, c, d], 4) base = template.subs(zip([p1, p2, p3, p4], permutator.next())) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert substitute_dummies(expr) == substitute_dummies(base) # atv(abcd)att(abij)att(jicd) template = atv(p1, p2, p3, p4) * att(p1, p2, i, j) * att(j, i, p3, p4) permutator = variations([a, b, c, d], 4) base = template.subs(zip([p1, p2, p3, p4], permutator.next())) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert substitute_dummies(expr) == substitute_dummies(base) template = atv(p1, p2, p3, p4) * att(p1, p2, j, i) * att(i, j, p3, p4) permutator = variations([a, b, c, d], 4) base = template.subs(zip([p1, p2, p3, p4], permutator.next())) for permut in permutator: subslist = zip([p1, p2, p3, p4], permut) expr = template.subs(subslist) assert substitute_dummies(expr) == substitute_dummies(base)
def symmetric(n): """ Generates the symmetric group of order n, Sn. Examples ======== >>> from sympy.combinatorics.generators import symmetric >>> list(symmetric(3)) [Permutation([0, 1, 2]), Permutation([0, 2, 1]), Permutation([1, 0, 2]), \ Permutation([1, 2, 0]), Permutation([2, 0, 1]), Permutation([2, 1, 0])] """ for perm in variations(range(n), n): yield Permutation(perm)
def symmetric(n): """ Generates the symmetric group of order n, Sn. Examples ======== >>> from sympy.combinatorics.generators import symmetric >>> list(symmetric(3)) [(2), (1 2), (2)(0 1), (0 1 2), (0 2 1), (0 2)] """ for perm in variations(list(range(n)), n): yield Permutation(perm)
def alternating(n): """ Generates the alternating group of order n, An. Examples ======== >>> from sympy.combinatorics.generators import alternating >>> list(alternating(3)) [(2), (0 1 2), (0 2 1)] """ for perm in variations(list(range(n)), n): p = Permutation(perm) if p.is_even: yield p
def symmetric(n): """ Generates the symmetric group of order n, Sn. Examples ======== >>> from sympy.combinatorics.permutations import Permutation >>> Permutation.print_cyclic = True >>> from sympy.combinatorics.generators import symmetric >>> list(symmetric(3)) [(2), (1 2), (2)(0 1), (0 1 2), (0 2 1), (0 2)] """ for perm in variations(list(range(n)), n): yield Permutation(perm)
def possible_matrix_with_delta_l(A_org, delta_l): nG = A_org.shape[0] possible_rows = list(variations([0,1], nG-1, True)) possible_all_A = [] for i in range(nG): A_i = A_org[i] possible_A_i_new = [] for rows in possible_rows: A_i_new = list(rows) A_i_new.insert(i, 0) diff = sum(abs(A_i_new - A_i)) if diff <= delta_l[i]: possible_A_i_new.append(A_i_new) possible_all_A.append(possible_A_i_new) return list(cartes(*possible_all_A))
def symmetric(n): """ Generates the symmetric group of order n, Sn. Examples ======== >>> from sympy.combinatorics.permutations import Permutation >>> Permutation.print_cyclic = True >>> from sympy.combinatorics.generators import symmetric >>> list(symmetric(3)) [Permutation(2), Permutation(1, 2), Permutation(2)(0, 1), Permutation(0, 1, 2), Permutation(0, 2, 1), Permutation(0, 2)] """ for perm in variations(list(range(n)), n): yield Permutation(perm)
def alternating(n): """ Generates the alternating group of order n, An. Examples ======== >>> from sympy.combinatorics.permutations import Permutation >>> Permutation.print_cyclic = True >>> from sympy.combinatorics.generators import alternating >>> list(alternating(3)) [Permutation(2), Permutation(0, 1, 2), Permutation(0, 2, 1)] """ for perm in variations(list(range(n)), n): p = Permutation(perm) if p.is_even: yield p
def alternating(n): """ Generates the alternating group of order n, An. Examples ======== >>> from sympy.combinatorics.permutations import Permutation >>> Permutation.print_cyclic = True >>> from sympy.combinatorics.generators import alternating >>> list(alternating(3)) [Permutation(2), Permutation(0, 1, 2), Permutation(0, 2, 1)] """ for perm in variations(range(n), n): p = Permutation(perm) if p.is_even: yield p
def alternating(n): """ Generates the alternating group of order n, An. Examples: >>> from sympy.combinatorics.generators import alternating >>> list(alternating(4)) [Permutation([0, 1, 2, 3]), Permutation([0, 2, 3, 1]), \ Permutation([0, 3, 1, 2]), Permutation([1, 0, 3, 2]), \ Permutation([1, 2, 0, 3]), Permutation([1, 3, 2, 0]), \ Permutation([2, 0, 1, 3]), Permutation([2, 1, 3, 0]), \ Permutation([2, 3, 0, 1]), Permutation([3, 0, 2, 1]), \ Permutation([3, 1, 0, 2]), Permutation([3, 2, 1, 0])] """ for perm in variations(range(n), n): p = Permutation(perm) if p.is_even: yield p
def calculate_possible_states(self): """Calculates the number of states from an initial configuration Returns: int: number of states """ # All balls of the state balls = [x for x in range(1, self.num_of_colors + 1)] * 4 # All possible tube configurations configurations = [ x for x in variations([0, 1, 2, 3, 4], self.n_tubes, True) if sum(x) == 4 * self.num_of_colors ] balls_permutations = multiset_permutations(balls) return len(self.generate_states(balls_permutations, configurations))
def alternating(n): """ Generates the alternating group of order n, An. Examples ======== >>> from sympy.combinatorics.generators import alternating >>> list(alternating(4)) [Permutation([0, 1, 2, 3]), Permutation([0, 2, 3, 1]), \ Permutation([0, 3, 1, 2]), Permutation([1, 0, 3, 2]), \ Permutation([1, 2, 0, 3]), Permutation([1, 3, 2, 0]), \ Permutation([2, 0, 1, 3]), Permutation([2, 1, 3, 0]), \ Permutation([2, 3, 0, 1]), Permutation([3, 0, 2, 1]), \ Permutation([3, 1, 0, 2]), Permutation([3, 2, 1, 0])] """ for perm in variations(range(n), n): p = Permutation(perm) if p.is_even: yield p
def test_issue2300(): args = [x, y, S(2), S.Half] def ok(a): """Return True if the input args for diff are ok""" if not a: return False if a[0].is_Symbol is False: return False s_at = [i for i in range(len(a)) if a[i].is_Symbol] n_at = [i for i in range(len(a)) if not a[i].is_Symbol] # every symbol is followed by symbol or int # every number is followed by a symbol return (all([a[i+1].is_Symbol or a[i+1].is_Integer for i in s_at if i+1<len(a)]) and all([a[i+1].is_Symbol for i in n_at if i+1<len(a)])) eq = x**10*y**8 for a in subsets(args): for v in variations(a, len(a)): if ok(v): noraise = eq.diff(*v) else: raises(ValueError, 'eq.diff(*v)')
def test_issue2300(): args = [x, y, S(2), S.Half] def ok(a): """Return True if the input args for diff are ok""" if not a: return False if a[0].is_Symbol is False: return False s_at = [i for i in range(len(a)) if a[i].is_Symbol] n_at = [i for i in range(len(a)) if not a[i].is_Symbol] # every symbol is followed by symbol or int # every number is followed by a symbol return (all(a[i+1].is_Symbol or a[i+1].is_Integer for i in s_at if i+1<len(a)) and all(a[i+1].is_Symbol for i in n_at if i+1<len(a))) eq = x**10*y**8 for a in subsets(args): for v in variations(a, len(a)): if ok(v): noraise = eq.diff(*v) else: raises(ValueError, 'eq.diff(*v)')
def test_variations(): assert variations([1,2,3], 2) == [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]] assert variations([1,2,3], 2, True) == [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], \ [3,1], [3,2], [3,3]]
def test_variations(): # permutations l = range(4) assert list(variations(l, 0, repetition=False)) == [[]] assert list(variations(l, 1, repetition=False)) == [[0], [1], [2], [3]] assert list(variations(l, 2, repetition=False)) == [[0, 1], [0, 2], [0, 3], [1, 0], [1, 2], [1, 3], [2, 0], [2, 1], [2, 3], [3, 0], [3, 1], [3, 2]] assert list(variations(l, 3, repetition=False)) == [[0, 1, 2], [0, 1, 3], [0, 2, 1], [0, 2, 3], [0, 3, 1], [0, 3, 2], [1, 0, 2], [1, 0, 3], [1, 2, 0], [1, 2, 3], [1, 3, 0], [1, 3, 2], [2, 0, 1], [2, 0, 3], [2, 1, 0], [2, 1, 3], [2, 3, 0], [2, 3, 1], [3, 0, 1], [3, 0, 2], [3, 1, 0], [3, 1, 2], [3, 2, 0], [3, 2, 1]] assert list(variations(l, 0, repetition=True)) == [[]] assert list(variations(l, 1, repetition=True)) == [[0], [1], [2], [3]] assert list(variations(l, 2, repetition=True)) == [[0, 0], [0, 1], [0, 2], [0, 3], [1, 0], [1, 1], [1, 2], [1, 3], [2, 0], [2, 1], [2, 2], [2, 3], [3, 0], [3, 1], [3, 2], [3, 3]] assert len(list(variations(l, 3, repetition=True))) == 64 assert len(list(variations(l, 4, repetition=True))) == 256 assert list(variations(l[:2], 3, repetition=False)) == [] assert list(variations(l[:2], 3, repetition=True)) == [[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1]]
def test_variations(): # permutations l = list(range(4)) assert list(variations(l, 0, repetition=False)) == [()] assert list(variations(l, 1, repetition=False)) == [(0, ), (1, ), (2, ), (3, )] assert list(variations(l, 2, repetition=False)) == [(0, 1), (0, 2), (0, 3), (1, 0), (1, 2), (1, 3), (2, 0), (2, 1), (2, 3), (3, 0), (3, 1), (3, 2)] assert list(variations(l, 3, repetition=False)) == [(0, 1, 2), (0, 1, 3), (0, 2, 1), (0, 2, 3), (0, 3, 1), (0, 3, 2), (1, 0, 2), (1, 0, 3), (1, 2, 0), (1, 2, 3), (1, 3, 0), (1, 3, 2), (2, 0, 1), (2, 0, 3), (2, 1, 0), (2, 1, 3), (2, 3, 0), (2, 3, 1), (3, 0, 1), (3, 0, 2), (3, 1, 0), (3, 1, 2), (3, 2, 0), (3, 2, 1)] assert list(variations(l, 0, repetition=True)) == [()] assert list(variations(l, 1, repetition=True)) == [(0, ), (1, ), (2, ), (3, )] assert list(variations(l, 2, repetition=True)) == [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3), (3, 0), (3, 1), (3, 2), (3, 3)] assert len(list(variations(l, 3, repetition=True))) == 64 assert len(list(variations(l, 4, repetition=True))) == 256 assert list(variations(l[:2], 3, repetition=False)) == [] assert list(variations(l[:2], 3, repetition=True)) == [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
def factor_nc(expr): """Return the factored form of ``expr`` while handling non-commutative expressions. **examples** >>> from sympy.core.exprtools import factor_nc >>> from sympy import Symbol >>> from sympy.abc import x >>> A = Symbol('A', commutative=False) >>> B = Symbol('B', commutative=False) >>> factor_nc((x**2 + 2*A*x + A**2).expand()) (x + A)**2 >>> factor_nc(((x + A)*(x + B)).expand()) (x + A)*(x + B) """ from sympy.simplify.simplify import _mexpand from sympy.polys import gcd, factor expr = sympify(expr) if not isinstance(expr, Expr) or not expr.args: return expr if not expr.is_Add: return expr.func(*[factor_nc(a) for a in expr.args]) expr, rep, nc_symbols = _mask_nc(expr) if rep: return factor(expr).subs(rep) else: args = [a.args_cnc() for a in Add.make_args(expr)] c = g = l = r = S.One hit = False # find any commutative gcd term for i, a in enumerate(args): if i == 0: c = Mul._from_args(a[0]) elif a[0]: c = gcd(c, Mul._from_args(a[0])) else: c = S.One if c is not S.One: hit = True c, g = c.as_coeff_Mul() if g is not S.One: for i, (cc, _) in enumerate(args): cc = list(Mul.make_args(Mul._from_args(list(cc))/g)) args[i][0] = cc else: for i, (cc, _) in enumerate(args): cc[0] = cc[0]/c args[i][0] = cc # find any noncommutative common prefix for i, a in enumerate(args): if i == 0: n = a[1][:] else: n = common_prefix(n, a[1]) if not n: # is there a power that can be extracted? if not args[0][1]: break b, e = args[0][1][0].as_base_exp() ok = False if e.is_Integer: for t in args: if not t[1]: break bt, et = t[1][0].as_base_exp() if et.is_Integer and bt == b: e = min(e, et) else: break else: ok = hit = True l = b**e il = b**-e for i, a in enumerate(args): args[i][1][0] = il*args[i][1][0] break if not ok: break else: hit = True lenn = len(n) l = Mul(*n) for i, a in enumerate(args): args[i][1] = args[i][1][lenn:] # find any noncommutative common suffix for i, a in enumerate(args): if i == 0: n = a[1][:] else: n = common_suffix(n, a[1]) if not n: # is there a power that can be extracted? if not args[0][1]: break b, e = args[0][1][-1].as_base_exp() ok = False if e.is_Integer: for t in args: if not t[1]: break bt, et = t[1][-1].as_base_exp() if et.is_Integer and bt == b: e = min(e, et) else: break else: ok = hit = True r = b**e il = b**-e for i, a in enumerate(args): args[i][1][-1] = args[i][1][-1]*il break if not ok: break else: hit = True lenn = len(n) r = Mul(*n) for i, a in enumerate(args): args[i][1] = a[1][:len(a[1]) - lenn] if hit: mid = Add(*[Mul(*cc)*Mul(*nc) for cc, nc in args]) else: mid = expr # sort the symbols so the Dummys would appear in the same # order as the original symbols, otherwise you may introduce # a factor of -1, e.g. A**2 - B**2) -- {A:y, B:x} --> y**2 - x**2 # and the former factors into two terms, (A - B)*(A + B) while the # latter factors into 3 terms, (-1)*(x - y)*(x + y) rep1 = [(n, Dummy()) for n in sorted(nc_symbols, key=default_sort_key)] unrep1 = [(v, k) for k, v in rep1] unrep1.reverse() new_mid, r2, _ = _mask_nc(mid.subs(rep1)) new_mid = factor(new_mid) new_mid = new_mid.subs(r2).subs(unrep1) if new_mid.is_Pow: return _keep_coeff(c, g*l*new_mid*r) if new_mid.is_Mul: # XXX TODO there should be a way to inspect what order the terms # must be in and just select the plausible ordering without # checking permutations cfac = [] ncfac = [] for f in new_mid.args: if f.is_commutative: cfac.append(f) else: b, e = f.as_base_exp() assert e.is_Integer ncfac.extend([b]*e) pre_mid = g*Mul(*cfac)*l target = _mexpand(expr/c) for s in variations(ncfac, len(ncfac)): ok = pre_mid*Mul(*s)*r if _mexpand(ok) == target: return _keep_coeff(c, ok) # mid was an Add that didn't factor successfully return _keep_coeff(c, g*l*mid*r)
def test_variations(): # permutations l = range(4) assert list(variations(l, 0, repetition=False)) == [()] assert list(variations(l, 1, repetition=False)) == [(0,), (1,), (2,), (3,)] assert list(variations(l, 2, repetition=False)) == [(0, 1), (0, 2), (0, 3), (1, 0), (1, 2), (1, 3), (2, 0), (2, 1), (2, 3), (3, 0), (3, 1), (3, 2)] assert list(variations(l, 3, repetition=False)) == [(0, 1, 2), (0, 1, 3), (0, 2, 1), (0, 2, 3), (0, 3, 1), (0, 3, 2), (1, 0, 2), (1, 0, 3), (1, 2, 0), (1, 2, 3), (1, 3, 0), (1, 3, 2), (2, 0, 1), (2, 0, 3), (2, 1, 0), (2, 1, 3), (2, 3, 0), (2, 3, 1), (3, 0, 1), (3, 0, 2), (3, 1, 0), (3, 1, 2), (3, 2, 0), (3, 2, 1)] assert list(variations(l, 0, repetition=True)) == [()] assert list(variations(l, 1, repetition=True)) == [(0,), (1,), (2,), (3,)] assert list(variations(l, 2, repetition=True)) == [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3), (3, 0), (3, 1), (3, 2), (3, 3)] assert len(list(variations(l, 3, repetition=True))) == 64 assert len(list(variations(l, 4, repetition=True))) == 256 assert list(variations(l[:2], 3, repetition=False)) == [] assert list(variations(l[:2], 3, repetition=True)) == [ (0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1) ]
def test_variations(): assert variations([1, 2, 3], 2) == [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]] assert variations([1,2,3], 2, True) == [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], \ [3,1], [3,2], [3,3]]