def test_subs(): from sympy import symbols a, b, c, d, e, f = symbols('a,b,c,d,e,f') mapping = {a: d, d: a, Basic(e): Basic(f)} expr = Basic(a, Basic(b, c), Basic(d, Basic(e))) result = Basic(d, Basic(b, c), Basic(a, Basic(f))) assert subs(mapping)(expr) == result
def patternify(expr, *wilds, **kwargs): """ Create a matching pattern from an expression Example ======= >>> from sympy import symbols, sin, cos, Mul >>> from sympy.unify.usympy import patternify >>> a, b, c, x, y = symbols('a b c x y') >>> # Search for anything of the form sin(foo)**2 + cos(foo)**2 >>> pattern = patternify(sin(x)**2 + cos(x)**2, x) >>> # Search for any two things added to c. Note that here c is not a wild >>> pattern = patternify(a + b + c, a, b) >>> # Search for two things added together, one must be a Mul >>> pattern = patternify(a + b, a, b, types={a: Mul}) """ from sympy.rules.tools import subs types = kwargs.get('types', {}) vars = [CondVariable(wild, mk_matchtype(types[wild])) if wild in types else Variable(wild) for wild in wilds] if any(expr.has(cls) for cls in illegal): raise NotImplementedError("Unification not supported on type %s"%( type(s))) return subs(dict(zip(wilds, vars)))(expr)
def test_subs(): from sympy import symbols a,b,c,d,e,f = symbols('a,b,c,d,e,f') mapping = {a: d, d: a, Basic(e): Basic(f)} expr = Basic(a, Basic(b, c), Basic(d, Basic(e))) result = Basic(d, Basic(b, c), Basic(a, Basic(f))) assert subs(mapping)(expr) == result
def rewrite_rl(expr, assumptions=True): for match in unify(source, expr, {}, variables=variables): if (condition and not condition(*[match.get(var, var) for var in variables])): continue if (assume and not ask(assume.xreplace(match), assumptions)): continue expr2 = subs(match)(target) if isinstance(expr2, Expr): expr2 = rebuild(expr2) yield expr2
def rewrite_rl(expr): for match in unify(p1, expr, {}, variables=variables): expr2 = subs(match)(p2) if isinstance(expr2, Expr): expr2 = rebuild(expr2) yield expr2
def test_subs_empty(): assert subs({})(Basic(1, 2)) == Basic(1, 2)