Source code for sympy.assumptions.refine

[docs]defrefine(expr,assumptions=True):""" Simplify an expression using assumptions. Gives the form of expr that would be obtained if symbols in it were replaced by explicit numerical expressions satisfying the assumptions. Examples ======== >>> from sympy import refine, sqrt, Q >>> from sympy.abc import x >>> refine(sqrt(x**2), Q.real(x)) Abs(x) >>> refine(sqrt(x**2), Q.positive(x)) x """ifnotexpr.is_Atom:args=[refine(arg,assumptions)forarginexpr.args]# TODO: this will probably not work with Integral or Polynomialexpr=expr.func(*args)name=expr.__class__.__name__handler=handlers_dict.get(name,None)ifhandlerisNone:returnexprnew_expr=handler(expr,assumptions)if(new_exprisNone)or(expr==new_expr):returnexprifnotisinstance(new_expr,Expr):returnnew_exprreturnrefine(new_expr,assumptions)