def test_postorder_traversal(): expr = z + w * (x + y) expected = [z, w, x, y, x + y, w * (x + y), w * (x + y) + z] assert list(postorder_traversal(expr, keys=default_sort_key)) == expected assert list(postorder_traversal(expr, keys=True)) == expected expr = Piecewise((x, x < 1), (x**2, True)) expected = [ x, 1, x, x < 1, ExprCondPair(x, x < 1), true, 2, x, x**2, ExprCondPair(x**2, True), Piecewise((x, x < 1), (x**2, True)) ] assert list(postorder_traversal(expr, keys=default_sort_key)) == expected assert list(postorder_traversal( [expr], keys=default_sort_key)) == expected + [[expr]] assert list( postorder_traversal(Integral(x**2, (x, 0, 1)), keys=default_sort_key)) == [ 2, x, x**2, 0, 1, x, Tuple(x, 0, 1), Integral(x**2, Tuple(x, 0, 1)) ] assert list(postorder_traversal( ('abc', ('d', 'ef')))) == ['abc', 'd', 'ef', ('d', 'ef'), ('abc', ('d', 'ef'))] assert list(ordered(postorder_traversal( x * (y + z)))) == [x, y, z, y + z, x * (y + z)]
def test_postorder_traversal(): expr = z + w*(x + y) expected = [z, w, x, y, x + y, w*(x + y), w*(x + y) + z] assert list(postorder_traversal(expr, keys=default_sort_key)) == expected assert list(postorder_traversal(expr, keys=True)) == expected expr = Piecewise((x, x < 1), (x**2, True)) expected = [ x, 1, x, x < 1, ExprCondPair(x, x < 1), true, 2, x, x**2, ExprCondPair(x**2, True), Piecewise((x, x < 1), (x**2, True)) ] assert list(postorder_traversal(expr, keys=default_sort_key)) == expected assert list(postorder_traversal( [expr], keys=default_sort_key)) == expected + [[expr]] assert list(postorder_traversal(Integral(x**2, (x, 0, 1)), keys=default_sort_key)) == [ 2, x, x**2, 0, 1, x, Tuple(x, 0, 1), Integral(x**2, Tuple(x, 0, 1)) ] assert list(postorder_traversal(('abc', ('d', 'ef')))) == [ 'abc', 'd', 'ef', ('d', 'ef'), ('abc', ('d', 'ef'))] assert list(ordered(postorder_traversal(x*(y + z)))) == [x, y, z, y + z, x*(y + z)]
def _rebuild(expr): if not expr.args: return expr if iterable(expr): new_args = [_rebuild(arg) for arg in expr] return expr.func(*new_args) if expr in subs: return subs[expr] orig_expr = expr if expr in opt_subs: expr = opt_subs[expr] # If enabled, parse Muls and Adds arguments by order to ensure # replacement order independent from hashes if order != 'none': if expr.is_Mul: c, nc = expr.args_cnc() args = list(ordered(c)) + nc elif expr.is_Add: args = list(ordered(expr.args)) else: args = expr.args else: args = expr.args new_args = list(map(_rebuild, args)) if new_args != args: new_expr = expr.func(*new_args) else: new_expr = expr if orig_expr in to_eliminate: try: sym = next(symbols) except StopIteration: raise ValueError("Symbols iterator ran out of symbols.") subs[orig_expr] = sym replacements.append((sym, new_expr)) return sym else: return new_expr
def test_ordered(): assert list(ordered((x, y), hash, default=False)) in [[x, y], [y, x]] assert list(ordered((x, y), hash, default=False)) == \ list(ordered((y, x), hash, default=False)) assert list(ordered((x, y))) == [x, y] seq, keys = [[[1, 2, 1], [0, 3, 1], [1, 1, 3], [2], [1]], (lambda x: len(x), lambda x: sum(x))] assert list(ordered(seq, keys, default=False, warn=False)) == \ [[1], [2], [1, 2, 1], [0, 3, 1], [1, 1, 3]] pytest.raises(ValueError, lambda: list(ordered(seq, keys, default=False, warn=True)))
def _match_common_args(Func, funcs): if order != 'none': funcs = list(ordered(funcs)) else: funcs = sorted(funcs, key=lambda x: len(x.args)) func_args = [set(e.args) for e in funcs] for i in range(len(func_args)): for j in range(i + 1, len(func_args)): com_args = func_args[i].intersection(func_args[j]) if len(com_args) > 1: com_func = Func(*com_args) # for all sets, replace the common symbols by the function # over them, to allow recursive matches diff_i = func_args[i].difference(com_args) func_args[i] = diff_i | {com_func} if diff_i: opt_subs[funcs[i]] = Func(Func(*diff_i), com_func, evaluate=False) diff_j = func_args[j].difference(com_args) func_args[j] = diff_j | {com_func} opt_subs[funcs[j]] = Func(Func(*diff_j), com_func, evaluate=False) for k in range(j + 1, len(func_args)): if not com_args.difference(func_args[k]): diff_k = func_args[k].difference(com_args) func_args[k] = diff_k | {com_func} opt_subs[funcs[k]] = Func(Func(*diff_k), com_func, evaluate=False)