def allcombinations(A, B, ordered): """ Restructure A and B to have the same number of elements ordered must be either 'commutative' or 'associative' A and B can be rearranged so that the larger of the two lists is reorganized into smaller sublists. >>> from sympy.unify.core import allcombinations >>> for x in allcombinations((1, 2, 3), (5, 6), 'associative'): print(x) (((1,), (2, 3)), ((5,), (6,))) (((1, 2), (3,)), ((5,), (6,))) >>> for x in allcombinations((1, 2, 3), (5, 6), 'commutative'): print(x) (((1,), (2, 3)), ((5,), (6,))) (((1, 2), (3,)), ((5,), (6,))) (((1,), (3, 2)), ((5,), (6,))) (((1, 3), (2,)), ((5,), (6,))) (((2,), (1, 3)), ((5,), (6,))) (((2, 1), (3,)), ((5,), (6,))) (((2,), (3, 1)), ((5,), (6,))) (((2, 3), (1,)), ((5,), (6,))) (((3,), (1, 2)), ((5,), (6,))) (((3, 1), (2,)), ((5,), (6,))) (((3,), (2, 1)), ((5,), (6,))) (((3, 2), (1,)), ((5,), (6,))) """ sm, bg = (A, B) if len(A) < len(B) else (B, A) for part in kbins(range(len(bg)), len(sm), None): if bg == B: yield list((a, ) for a in A), core_partition(B, part) else: yield core_partition(A, part), list((b, ) for b in B)
def allcombinations(A, B, ordered): """ Restructure A and B to have the same number of elements ordered must be either 'commutative' or 'associative' A and B can be rearranged so that the larger of the two lists is reorganized into smaller sublists. >>> from sympy.unify.core import allcombinations >>> for x in allcombinations((1, 2, 3), (5, 6), 'associative'): print(x) (((1,), (2, 3)), ((5,), (6,))) (((1, 2), (3,)), ((5,), (6,))) >>> for x in allcombinations((1, 2, 3), (5, 6), 'commutative'): print(x) (((1,), (2, 3)), ((5,), (6,))) (((1, 2), (3,)), ((5,), (6,))) (((1,), (3, 2)), ((5,), (6,))) (((1, 3), (2,)), ((5,), (6,))) (((2,), (1, 3)), ((5,), (6,))) (((2, 1), (3,)), ((5,), (6,))) (((2,), (3, 1)), ((5,), (6,))) (((2, 3), (1,)), ((5,), (6,))) (((3,), (1, 2)), ((5,), (6,))) (((3, 1), (2,)), ((5,), (6,))) (((3,), (2, 1)), ((5,), (6,))) (((3, 2), (1,)), ((5,), (6,))) """ sm, bg = (A, B) if len(A) < len(B) else (B, A) for part in kbins(range(len(bg)), len(sm), None): if bg == B: yield list((a,) for a in A), core_partition(B, part) else: yield core_partition(A, part), list((b,) for b in B)
def unify_assoccomm(u, v, s, ordering=None): u = walk(u, s) v = walk(v, s) res = unify(u, v, s) if res is not False: yield res if isinstance(u, tuple) and isinstance(v, tuple): uop, u = u[0], u[1:] vop, v = v[0], v[1:] s = unify(uop, vop, s) if s is False: raise StopIteration() op = walk(uop, s) sm, lg = (u, v) if len(u) <= len(v) else (v, u) for part in kbins(range(len(lg)), len(sm), ordering): lg2 = makeops(op, partition(lg, part)) # TODO: we use logpy code within python within logpy # There must be a more elegant way for res in conde((eq_assoccomm, a, b) for a, b in zip(sm, lg2))(s): yield res
def test2(): for orderedval in [None, 0, 1, 10, 11]: print('ordered =', orderedval) for p in kbins(list(range(3)), 2, ordered=orderedval): print(' ', p)
def test1(): for orderedval in [None, 0, 1, 10, 11]: print('ordered =', orderedval) for p in kbins([0, 0, 1], 2, ordered=orderedval): print(' ', p)
def test_kbins(): assert len(list(kbins('1123', 2, ordered=1))) == 24 assert len(list(kbins('1123', 2, ordered=11))) == 36 assert len(list(kbins('1123', 2, ordered=10))) == 10 assert len(list(kbins('1123', 2, ordered=0))) == 5 assert len(list(kbins('1123', 2, ordered=None))) == 3 def test1(): for orderedval in [None, 0, 1, 10, 11]: print('ordered =', orderedval) for p in kbins([0, 0, 1], 2, ordered=orderedval): print(' ', p) assert capture(lambda: test1()) == dedent('''\ ordered = None [[0], [0, 1]] [[0, 0], [1]] ordered = 0 [[0, 0], [1]] [[0, 1], [0]] ordered = 1 [[0], [0, 1]] [[0], [1, 0]] [[1], [0, 0]] ordered = 10 [[0, 0], [1]] [[1], [0, 0]] [[0, 1], [0]] [[0], [0, 1]] ordered = 11 [[0], [0, 1]] [[0, 0], [1]] [[0], [1, 0]] [[0, 1], [0]] [[1], [0, 0]] [[1, 0], [0]]\n''') def test2(): for orderedval in [None, 0, 1, 10, 11]: print('ordered =', orderedval) for p in kbins(list(range(3)), 2, ordered=orderedval): print(' ', p) assert capture(lambda: test2()) == dedent('''\ ordered = None [[0], [1, 2]] [[0, 1], [2]] ordered = 0 [[0, 1], [2]] [[0, 2], [1]] [[0], [1, 2]] ordered = 1 [[0], [1, 2]] [[0], [2, 1]] [[1], [0, 2]] [[1], [2, 0]] [[2], [0, 1]] [[2], [1, 0]] ordered = 10 [[0, 1], [2]] [[2], [0, 1]] [[0, 2], [1]] [[1], [0, 2]] [[0], [1, 2]] [[1, 2], [0]] ordered = 11 [[0], [1, 2]] [[0, 1], [2]] [[0], [2, 1]] [[0, 2], [1]] [[1], [0, 2]] [[1, 0], [2]] [[1], [2, 0]] [[1, 2], [0]] [[2], [0, 1]] [[2, 0], [1]] [[2], [1, 0]] [[2, 1], [0]]\n''')
def test(): for ordered in [None, 0, 1, 10, 11]: print 'ordered =', ordered for p in kbins(range(3), 2, ordered=ordered): print ' ', p
def test_kbins(): assert len(list(kbins('1123', 2, ordered=01))) == 24 assert len(list(kbins('1123', 2, ordered=11))) == 36 assert len(list(kbins('1123', 2, ordered=10))) == 10 assert len(list(kbins('1123', 2, ordered=0))) == 5 assert len(list(kbins('1123', 2, ordered=None))) == 3 def test(): for ordered in [None, 0, 1, 10, 11]: print 'ordered =', ordered for p in kbins([0, 0, 1], 2, ordered=ordered): print ' ', p assert capture(lambda : test()) == dedent('''\ ordered = None [[0], [0, 1]] [[0, 0], [1]] ordered = 0 [[0, 0], [1]] [[0, 1], [0]] ordered = 1 [[0], [0, 1]] [[0], [1, 0]] [[1], [0, 0]] ordered = 10 [[0, 0], [1]] [[1], [0, 0]] [[0, 1], [0]] [[0], [0, 1]] ordered = 11 [[0], [0, 1]] [[0, 0], [1]] [[0], [1, 0]] [[0, 1], [0]] [[1], [0, 0]] [[1, 0], [0]]\n''') def test(): for ordered in [None, 0, 1, 10, 11]: print 'ordered =', ordered for p in kbins(range(3), 2, ordered=ordered): print ' ', p assert capture(lambda : test()) == dedent('''\ ordered = None [[0], [1, 2]] [[0, 1], [2]] ordered = 0 [[0, 1], [2]] [[0, 2], [1]] [[0], [1, 2]] ordered = 1 [[0], [1, 2]] [[0], [2, 1]] [[1], [0, 2]] [[1], [2, 0]] [[2], [0, 1]] [[2], [1, 0]] ordered = 10 [[0, 1], [2]] [[2], [0, 1]] [[0, 2], [1]] [[1], [0, 2]] [[0], [1, 2]] [[1, 2], [0]] ordered = 11 [[0], [1, 2]] [[0, 1], [2]] [[0], [2, 1]] [[0, 2], [1]] [[1], [0, 2]] [[1, 0], [2]] [[1], [2, 0]] [[1, 2], [0]] [[2], [0, 1]] [[2, 0], [1]] [[2], [1, 0]] [[2, 1], [0]]\n''')
def test(): for ordered in [None, 0, 1, 10, 11]: print('ordered =', ordered) for p in kbins(list(range(3)), 2, ordered=ordered): print(' ', p)
def test(): for ordered in [None, 0, 1, 10, 11]: print('ordered =', ordered) for p in kbins([0, 0, 1], 2, ordered=ordered): print(' ', p)
def test(): for ordered in [None, 0, 1, 10, 11]: print("ordered =", ordered) for p in kbins([0, 0, 1], 2, ordered=ordered): print(" ", p)
def test_kbins(): assert len(list(kbins("1123", 2, ordered=1))) == 24 assert len(list(kbins("1123", 2, ordered=11))) == 36 assert len(list(kbins("1123", 2, ordered=10))) == 10 assert len(list(kbins("1123", 2, ordered=0))) == 5 assert len(list(kbins("1123", 2, ordered=None))) == 3 def test(): for ordered in [None, 0, 1, 10, 11]: print("ordered =", ordered) for p in kbins([0, 0, 1], 2, ordered=ordered): print(" ", p) assert capture(lambda: test()) == dedent( """\ ordered = None [[0], [0, 1]] [[0, 0], [1]] ordered = 0 [[0, 0], [1]] [[0, 1], [0]] ordered = 1 [[0], [0, 1]] [[0], [1, 0]] [[1], [0, 0]] ordered = 10 [[0, 0], [1]] [[1], [0, 0]] [[0, 1], [0]] [[0], [0, 1]] ordered = 11 [[0], [0, 1]] [[0, 0], [1]] [[0], [1, 0]] [[0, 1], [0]] [[1], [0, 0]] [[1, 0], [0]]\n""" ) def test(): for ordered in [None, 0, 1, 10, 11]: print("ordered =", ordered) for p in kbins(list(range(3)), 2, ordered=ordered): print(" ", p) assert capture(lambda: test()) == dedent( """\ ordered = None [[0], [1, 2]] [[0, 1], [2]] ordered = 0 [[0, 1], [2]] [[0, 2], [1]] [[0], [1, 2]] ordered = 1 [[0], [1, 2]] [[0], [2, 1]] [[1], [0, 2]] [[1], [2, 0]] [[2], [0, 1]] [[2], [1, 0]] ordered = 10 [[0, 1], [2]] [[2], [0, 1]] [[0, 2], [1]] [[1], [0, 2]] [[0], [1, 2]] [[1, 2], [0]] ordered = 11 [[0], [1, 2]] [[0, 1], [2]] [[0], [2, 1]] [[0, 2], [1]] [[1], [0, 2]] [[1, 0], [2]] [[1], [2, 0]] [[1, 2], [0]] [[2], [0, 1]] [[2, 0], [1]] [[2], [1, 0]] [[2, 1], [0]]\n""" )